Python one-liner of the day

This is a function that takes an integer and returns its ordinal representation, e.g. “1st” for 1 and so on.

It’s not the most readable thing, but once I saw the pieces falling into place I couldn’t help myself. Repetition of the “th” literal is the only thing that bugs me. Oh well.

ord_text = lambda n: "%d%s" % (n, "th" if 10 < n % 100 < 14 else {1:"st", 2:"nd", 3:"rd"}.get(n % 10, "th"))

Comes with a one-line test suite!

History lesson

This has been going around – give people a peek at what commands you run most often. I ran this on my server, where I spend most of my shell time:

> history|awk '{a[$2]++} END{for(i in a){printf "%5d\t%s\n",a[i],i}}'|sort -rn|head
  103   hg
   81   cd
   67   ll
   29   ./manage.py
   23   ab
   21   re-ap
   17   hgup
   14   svn
   13   cat
   12   ls

Notes:

  • Mercurial has pushed my use of Subversion way down.
  • I can’t remember what I was benchmarking with ab, but I’m sure it’s faster now!
  • re-ap is my alias for restarting Apache (re-po restarts Postfix, re-my restarts MySQL, etc.).
  • hgup is a simple shell script that updates the live instance of my site by fetching from the Mercurial repository in the staging instance. It would make a neat Django custom management command, but not one tied to a particular app.

How not to advocate via Google Code

People sure are excited about the Google App Engine. Especially people who have some other favorite language besides Python. A significant number of the issue tracker items are of the form “Please add support for $MY_LANGUAGE”, where $MY_LANGUAGE might be VB.NET, C#, PHP, Java, Groovy, Ruby, Perl, etc. ad nauseam.

I’m not going to comment on the language-wars aspect.

But if you want your language supported (this goes for any issue in the tracker in fact), the thing to do is not to go to one of those issue pages and add a comment that consists of “+1”. (“DUGG!!” is also not recommended.) That sends an email to everyone who has “starred” the issue. An email that consists of “+1”. With your name on it.

99 problems

There is a classic set of programming exercises called “Ninety-Nine Prolog Problems”. Though somewhat tailored to logic programming, they form an interesting set of exercises for other languages. I’ve seen adaptations of varying completeness for Haskell, Lisp, Perl 6, and Python.

I was reminded of this all by a recent blog post by some bloke called Dave who was using the problems as a way to become more familiar with Python. He used Python’s unittest module to test his solutions.

The original Lego Star Wars

The original Lego Star Wars

I attempted to make a Super-8 animated-Lego version of Star Wars when I was 14 – in 1982 or so. I had made several other animated movies, Lego-powered and otherwise, but this was my most ambitious project. Over several weeks of painstaking stop-motion animation, I got as far as the escape-from-the-Death-Star scene. In real time this was about four minutes of footage (yes, it was a multi-reel production), but as anyone who has done traditional animation can tell you, that’s a lot of work.

Toolbot.com source code available on request

I’m doing a small experiment in open source distribution.

I have a site, toolbot.com, which formerly was a collection of miscellaneous PHP scripts that I had assembled over the years for specific tasks – package tracking, dummy text generation, link shortening, etc. Those tools are now offline. The original cause of their disappearance was a MySQL failure, but that really just provided an opportunity for me to make a break with that pile of old code.

Mercurial 1.0 released

Very cool news – Mercurial 1.0 has been released. See the announcement and the site for details. There are no earth-shaking individual changes in this release, just a long list of improvements that have been made since 0.9.5.

One of the new features is easy_install friendliness, so assuming you’ve got that installed, getting the new version of Mercurial is as easy as:

$ sudo easy_install http://www.selenic.com/mercurial/release/mercurial-1.0.tar.gz

If you want to be kind to the server, since it’s getting pounded today, you can do this instead in order to make your request via the Coral CDN: