Code

Computer programming unplugged

I just found this via reddit.com and I think it is brilliant! Computer Programming Unplugged

Ruby's not ready

I am not a Ruby fan. I really like Python. I have never really been able to accurately explain why I don't like Ruby. Well I just found a lengthy post that breaks down the differences in Ruby and Python via reddit: http://glyphobet.net/blog/essay/228. The author does a great job of breaking down the Ruby-isms I don't like and I learned about some I didn't know about.

Posting from the command line

Posting from the command line is cool. I found a wordpress library for python:

http://code.google.com/p/wordpress-library/ and wrote a little script to do the rest.

The Nature of Lisp

I just read a great article on Lisp called the Nature of Lisp as someone who has been trying to figure out why Lisp is so cool to so many people this helped a lot. I suggest you read it.

Python web programming

I starting playing with web.py today to see how easy it would be to make a simple web app.

I followed the tutorial and everything was going well until it all stopped working. Took me about 20 minutes to figure out was wrong. Can you see it?


urls = (

'/', 'view'
'/add','add'
)

No? Look again:


urls = (

'/', 'view',
'/add','add'
)

That's right I was missing a comma. Did the program complain? No. What was the output on the web browser: "not found".


I wasn't impressed. I hate when stuff like that happens. I will still play with web.py because it is so simple but I will be much more careful in the future.


I play a lot of computer games. Most of those games require a serial number. I am going to us web.py to make a small web app to store all of my serial numbers. Seems like a good test and more complicated than the tutorials "todo list".

Perl labels

I learned about Perl labels and thought I would pass it on. Basically a label names a block of code descriptively. They are usually uppercase. You can use next, redo and last within a labeled block. On the surface this doesn't seem that useful until you start talking about loops within loops.

Take the following bit of code with out labels:

while ( $somecondition ) {
# do some work
for my $iterator (1..10) {
# do some more work
}
}

Let's say you want to use
next in the for loop to control the while loop. There isn't way to do that here. You would have to come up with some other logic to handle that. Using labels its very easy:

WHILE:
while ( $somecondition ) {
# do some work
FOR:
for my $iterator (1..10) {
# do some more work
next WHILE;
}
}
Of course the above is a meaningless example but you get the idea.

Python debugging and emacs

I am normally a vim user but am looking at emacs again. One of the features that emacs has that I love is how it does interactive debugging.
First thing to do is to have the python debugger in your path and called 'pdb'. On my Ubuntu system that would be:

cd ~/bin
ln -s /usr/lib/python2.4/pdb.py pdb

Now edit your favorite python script with emacs and start the debugger:

M-x pdb
pdb

emacs screenshot

Whatever happened to selling computer games?

I went down to Electronics Boutique, er EBX, er EB Games today looking for some computer games - you remember those right?

Well I was shocked when I walked in and saw every single wall covered with console games. I remember back when at least two walls were devoted to PC games.


Near the back of the store on a four sided vertical shelf was the PC games. Terrible selection and only the newest games out right now.


What has happened? I wish I knew. I can only guess the console games have better profit margins.


There was a Best Buy across the street and decided to check it out against my gut feeling to run in terror (I had a really bad experience there last year and swore never to return). I cautiously approached the PC game section and it was filled with lots of games - new ones, old ones, classics, etc...


A sad day indeed with Best Buy has a better selection of PC games than EB Games. I now have no reason to ever go there again since I have a Game Crazy across the street from where I live.

Python == Perl when it comes to regex's

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.