Computer programming unplugged
I just found this via reddit.com and I think it is brilliant! Computer Programming Unplugged
I just found this via reddit.com and I think it is brilliant! Computer Programming Unplugged
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.
I just picked up Secrets of The Rock Star Programmers: Riding the IT Crest. Wow what a horrible title. Good thing the book is really good. Ed Burns interviews well known folks in the field of programming. He asks them a range of questions from “How do you setup a new machine to work on?” to “How do you keep the work/life balance?”. Someone once said that to be successful you need to study the habits of successful people, so this is my attempt at that I guess.
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.
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.
I haven’t written anything in the last week because I have been slammed at work. I moved to a new team and I am doing more development work. This is good because eventually I want to be a full time programmer. Right now I am working in the high level languages like Perl and Python but I do have C training and I think I would like to do more lower level stuff.
I also am getting my first taste of what it means to be a programmer. Being a system administrator is fun because you can program when you want to - if I ever got bored of a script I was working on I could do something else. Its another story when your primary role is to write that script. Getting bored? Too bad finish it.
My first project is a Perl script about 2,000 lines long with lots of mySQL interactions. This also marks the first time I have worked with databases in any meaningful way. Took me about three or four days to get to the point where I was comfortable writing SQL queries. The Perl DBI layer makes a lot of this easy.
I am also now writing code that other people depend on which is a lot different from the system administrator scripts I usually write. Now my code needs to do the right thing under a variety of circumstances. I am making liberal use of Perl’s eval statement so I can have some sort of deterministic behavior.
Recently I read a Damien Katz blog entry called Error codes or Exceptions? Why is Reliable Software so Hard? which really spoke to me on my current plight. I am doing a combination of his three error handling styles - including the travel back in time one (I have rollback queue that gets poplulated with the actions to undo a step, in the case of an error I just run through the queue and have gone back in time:))
Writing a Perl script this big has really made me appreciate Perl more in its flexability but has also made me yearn for Python more for its cleaner syntax and excellent exception handling.
At this point I am on the fence whether I want to be a full time programmer but this experience is a great way for me to get my toe in the water without jumping in.
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?
No? Look again:
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”.
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.
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

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.
Powered by WordPress