March 9, 2006

Python == Perl when it comes to regex's

Filed under: Code

A friend of mine is learning python and commented how he thought it was a better programming language but perl’s regex’s were better and so was better suited to scripting. I had to set him straight so I wrote a simple script in perl and python to parse telephone numbers.

Sample data:

(206) 329-1173
206.329.1173
206-329-1173
206 329-1173
329-1173
3291173

Python:

#!/usr/bin/env python

import sys,re
phonenumber = re.compile("\(?(\d{0,3})\)?[\.\-\s]?(\d{3})[\.\-\s]?(\d{4})")
for line in sys.stdin:
   matches = re.match(phonenumber, line)
   areacode = matches.group(1)
   prefix = matches.group(2)
   extension = matches.group(3)
   if areacode == "":
       print "Prefix = " + prefix + " Extension = " + extension
   else:
       print "Area code = " + areacode + " Prefix = " + prefix
       print " Extension = " + extension

Perl:

#!/usr/bin/env perl

use warnings;
use strict;

while (<>) {
my ($areacode, $prefix, $extension) =
    ( $_ =~ /\(?(\d{0,3})\)?[\.\-\s]?(\d{3})[\.\-\s]?(\d{4})/ );
   if ($areacode eq "") {
       print "Prefix = $prefix Extension = $extension\n";
   }
   else {
       print "Area code = $areacode Prefix = $prefix "
       print "Extension = $extension\n";
   }
}

Both scripts produce the same output:

Area code = 206 Prefix = 329 Extension = 1173
Area code = 206 Prefix = 329 Extension = 1173
Area code = 206 Prefix = 329 Extension = 1173
Area code = 206 Prefix = 329 Extension = 1173
Prefix = 329 Extension = 1173
Prefix = 329 Extension = 1173

Both scripts uses the exact same regex. Python is just as powerful as Perl for regular expressions.

March 8, 2006

Where are all the emacs books?

Filed under: Code

So I decided to give emacs another try. I say another because in 2004 I used it for about 3 months before deciding to go back to vim. So I went to Barnes & Noble at the University Village shopping center tonight and they didn’t have a single book! When I got home I went to Amazon.com (I work there by the way) and searched for ‘emacs’. Three titles came up that were relatively new and in print. Three. The greatest editor ever and there are only three books? Eclipse hasn’t been out that long and there are entire bookshelves dedicated to it so what is the deal? I don’t know but it might be related to lisp. Lisp is supposedly the greatest programming language available but good luck finding more than one or two books on the topic and those will probably be academic texts.

Maybe the emperor doesn’t have any clothes on. Maybe I am reading all these blogs from Paul Graham and Steve Yegge and am convincing myself that the whole world is using emacs and lisp except me.

I will am still going to give emacs another go but I won’t expect it to solve all my problems this time around and maybe I will enjoy it all the more.

March 7, 2006

New Python website is sexy

Filed under: Code

Well it looks like python.org has finally entered the realms of web 2.0. Nice colors, css templates, and new round jelly looking logo. Is this a reaction to Guido’s recent post regarding better marketing or had they planned this for quite some time? (my guess is its been in the works for a while).

Currently Python is my language of choice but after reading all of Steve Yegge’s old blog entries I am second guessing myself and thinking I shouldn’t drop Ruby totally.

Powered by WordPress