Showing posts from May, 2009

Vim as a Python IDE

This is a pretty good post: VIM as Python IDE. Also see my NetBeans post.

Python: Look What the Stork Dragged In

I have this Python program that I haven't run in a couple years. I wanted to see if I could make it run faster. Last time, it took 22 hours. Sure enough, using some advanced optimization techniques (and a swig of castor oil), my wife and I were able to run the program in a mere 6.5 hours! Here it is:#!/usr/bin/env python

"""Help Gina-Marie time her contractions."""

import time


last_start = None
while True:
print "Press enter when the contraction starts.",
start = time.time()
if last_start:
print "It's been %s minutes %s seconds since last contraction." \
% divmod(int(start - last_start), SECS_PER_MIN)
last_start = start
print "Press enter when the contraction stops.",
stop = time.time()
print "Contraction lasted %s seconds." % int(stop - start)

Python: lambda to the Rescue

As every Lisp programmer knows, sometimes a lambda (i.e. an anonymous, on-the-fly function) provides just the right amount of indirection you need to solve a problem elegantly. Using a lambda in Python, you can shove an expression into a function, and then call it a little later when the time is right.

I was coding some Python, and I kept ending up with code that looked like:try:
count = d['a']['b']['c']
except KeyError:
count = 0Translating that little idiom into a function is hard. Every single use of [] might result in a KeyError. I could use .get() on each, but that's painful. Similarly, managing a hierarchy of defaultdicts is a bit painful too; the values in my dicts are a mix of ints and other nested dicts.

The question is, how do you put the try / except part into a function, and use d['a']['b']['c'] as an argument? After all, if you call f(d['a']['b']['c']), and d['a']['b']['…

Oz: Translating Python into Oz

I've been working my way through Concepts, Techniques, and Models of Computer Programming. In chapter 3, one of the exercises was to transpose a matrix.

I immediately felt completely overwhelmed. I couldn't even remember what it meant to transpose a matrix, and I definitely hadn't seen anything matrix-related in the book. I went to Wikipedia and figured out that transposing a matrix just means swapping the rows and the columns. That seemed more possible, but I still didn't think I had the necessary tools to code it in Oz yet.

I decided to try solving the problem in Python. Python is sometimes called "executable pseudocode", and sure enough, the solution came to me pretty easily:m = [[1, 2, 3],
[4, 5, 6]]

def transpose(m):
transposed = []
if len(m) >= 1:
len_cols = len(m[0])
for col in xrange(len_cols):
transposed_row = []
for row in m:

Programming Languages: A Brief, Incomplete, and Mostly Wrong History of Programming Languages