December 19, 2007

Web Browsing With Python

Drew Stephens @ 8:52 pm — Tags: ,

Python provides a stateful web browsing module called mechanize, named after Perl’s mature and featureful WWW::Mechanize. Though it isn’t as powerful as the Perl version, mechanize provides an easy-to-use framework for browsing web pages including interacting with forms and accessing SSL content. The documentation for mechanize on the web is sparse, but viewing the source file (/usr/lib/python2.5/site-packages/mechanize/_mechanize.py on my Ubuntu machine) provides some needed insight. Here’s a quick overview of the operation of mechanize:

#!/usr/bin/python
import re
from mechanize import Browser
br = Browser()
 
# Ignore robots.txt
br.set_handle_robots( False )
# Google demands a user-agent that isn't a robot
br.addheaders = [('User-agent', 'Firefox')]
 
# Retrieve the Google home page, saving the response
br.open( "http://google.com" )
 
# Select the search box and search for 'foo'
br.select_form( 'f' )
br.form[ 'q' ] = 'foo'
 
# Get the search results
br.submit()
 
# Find the link to foofighters.com; why did we run a search?
resp = None
for link in br.links():
    siteMatch = re.compile( 'www.foofighters.com' ).search( link.url )
    if siteMatch:
        resp = br.follow_link( link )
        break
 
# Print the site
content = resp.get_data()
print content

That’s a pretty straightforward and simple usage. The get_data() method gives you the HTML content of the pages, which I often find suitable to run a .split('\n') and then do some regex on line by line.

December 11, 2007

Random Futurama

Drew Stephens @ 10:36 pm — Tags: ,

I like Futurama. All of it. Though I occasionally have a hankering for a specific episode, I often just want /any/ episode, so I wrote a quick Perl script to play one at random. I have a directory filled with all of Futurama, so this is an easy proposition:

#!/usr/bin/perl
use strict;
use warnings;
 
my @files = split /\n/, `ls *avi`;
 
my $num = rand(scalar(@files));
 
system "mplayer "$files[$num]"";

December 7, 2007

Custom Weather Radar Images

Drew Stephens @ 8:19 am — Tags: ,

NWS base reflectivity for LWX If you know the slightest bit about weather and the geography of the area which you are observing, you can, in the short term, forecast more accurately than what a weather website provides. I often want to be able to bring a radar image up quickly and clicking around websites takes time. Sure I could bookmark the weather page directly, but this was an interesting exercise and will prove useful when I get an iPhone.

The National Weather Service doesn’t have a simple image of the weather on their radar page, rather, it is an image that is dynamically generated from a set of standard overlays using Javascript. This allows them to more efficiently distribute radar images, since the terrain and maps that are the background never change. The organization they use is well documented, making it easy to find the overlays for you local radar. In my case, I wanted the topographical map, counties and highways in the image. I found the URL for each of these components using the aforementioned documentation and grabbed the appropriate images with wget.

wget http://radar.weather.gov/ridge/Legend/N0R/LWX_N0R_Legend_0.gif\
http://radar.weather.gov/ridge/Overlays/County/Short/LWX_County_Short.gif\
http://radar.weather.gov/ridge/Overlays/Highways/Short/LWX_Highways_Short.gif\
http://radar.weather.gov/ridge/Overlays/Topo/Short/LWX_Topo_Short.jpg\
http://radar.weather.gov/ridge/RadarImg/N0R/LWX_N0R_0.gif

The only layer that will change will be the last one, the actual radar data, so in my script that is the only one that will be wgetd on subsequent runs. To build the image, I used the composite program that is part of the ImageMagick package:

composite -compose atop LWX_N0R_0.gif LWX_Topo_Short.jpg base_reflectivity.jpg
composite -compose atop LWX_Highways_Short.gif base_reflectivity.jpg base_reflectivity.jpg
composite -compose atop LWX_County_Short.gif base_reflectivity.jpg base_reflectivity.jpg
composite -compose atop LWX_N0R_Legend_0.gif base_reflectivity.jpg base_reflectivity.jpg

I want the counties, highways and legend to show up on top of the weather data which itself is pasted atop the topographical map. To achieve this, I first toss the radar data onto the topographical image creating a new image, base_reflectivity.jpg. Then, I add each of the other layers to the base_reflectivity.jpg image in sequence.

December 4, 2007

Connecting to Oracle Without a Password on Windows

Drew Stephens @ 10:56 am — Tags: ,

If you have forgotten, were never given or otherwise don’t have the password to an Oracle database, never fear, there is a method to accessing the database. From the local machine you must be a user in the group “ora_dba”. Run “sqlplus” (the command line version) with the option “/nolog”, which tells SQL*Plus not to login. At the “SQL>” prompt, type “connect / as sysdba” which ought to log you in. At that point, you can change the password for any account (sys would be a good one to change, since apparently you don’t know it) using the command alter user <username> identified by "<password>";. Make sure to commit; after doing that.

December 2, 2007

Disconnecting: Corrupted MAC on input.

Drew Stephens @ 9:25 pm — Tags: , ,

I woke up to find the entitled error in the rsnapshot logs on one of my machines, which occurred as it was trying to remotely backup, via SSH, another. A quick search and reading through a number of threads revealed this message stating that this problem was encountered with a Linksys router involved. Lo and behold, my home network was just switched to a Linksys BEFSR41 when the problem began.

I think the router may also be the culprit for the occasional “error establishing encrypted connection code -12192″ that Firefox has been giving me since the switch.

December 1, 2007

Perl, Python and Ruby

Drew Stephens @ 10:31 am — Tags: ,

After reading this article comparing the new Ruby 1.9 to it’s older version and Python, I thought it would be interesting to see how my preferred language, Perl, compares. First, because my hardware is certainly different from the author of that post, I re-ran the Python and Ruby 1.9 tests:

Python:
real    0m43.415s
user    0m41.455s
sys     0m0.132s

Ruby: real 0m35.379s user 0m33.722s sys 0m0.112s

I threw together some quick Perl:

sub fib {
    my $n = shift;
 
    if ( $n == 0 or $n == 1 ) {
        return $n;
    } else {
        return fib($n - 1) + fib($n - 2);
    }
}
 
for (0..35) {
    print "n=$_ => " . fib($_) . "\n";
}

and ran it:

Perl:
real    1m15.329s
user    1m9.188s
sys     0m0.364s

That’s impressive; Perl sucks at this recursive implementation of computing Fibonacci numbers. Since I had the makings of a little suite of benchmarks, I implemented this test in C and Java:

C code:

#include <stdio.h>
 
int fib(int n) {
    if ( (n == 0) || (n == 1) )
        return n;
    else
        return (fib(n-1) + fib(n-2));
} //fib
 
int main() {
    int x;
    for (x = 0; x < 36; x++) {
        printf( "n = %d => %d\n", x, fib(x) );
    } //for
 
    return 0;
}

C timing:

real    0m0.605s
user    0m0.564s
sys     0m0.000s

Java code:

class Fib {
    static int fib(int n) {
        if ( (n == 0) || (n == 1) )
            return n;
        else
            return (fib(n-1) + fib(n-2));
    } //fib
 
    public static void main(String args[]) {
        for (int x = 0; x < 36; x++) {
            System.out.println( "n = " + x + " => " + fib(x) );
        } //for
    } //main
} //Fib

Java timing:

real    0m0.455s
user    0m0.412s
sys     0m0.012s

It’s very interesting to see the huge speed disparities in these languages, but remember, this was a single, very limited test; don’t take these benchmarks as having any significant meaning. Really, don’t take them to mean much of anything, unless all you do is write lots of simple recursive code.

Powered by WordPress