E-Scribe : a programmer’s blog

About Me

PBX I'm Paul Bissex. I build web applications using open source software, especially Django. Started my career doing graphic design for newspapers and magazines in the '90s. Then wrote tech commentary and reviews for Wired, Salon, Chicago Tribune, and others you never heard of. Then I built operations software at a photography school. Then I helped big media serve 40 million pages a day. Then I worked on a translation services API doing millions of dollars of business. Now I'm building the core platform of a global startup accelerator. Feel free to email me.

Book

I co-wrote "Python Web Development with Django". It was the first book to cover the long-awaited Django 1.0. Published by Addison-Wesley and still in print!

Colophon

Built using Django, served with gunicorn and nginx. The database is SQLite. Hosted on a FreeBSD VPS at Johncompanies.com. Comment-spam protection by Akismet.

Pile o'Tags

Stuff I Use

bitbucket, Django, Emacs, FreeBSD, Git, jQuery, LaunchBar, Markdown, Mercurial, OS X, Python, Review Board, S3, SQLite, Sublime Text, Ubuntu Linux

Spam Report

At least 236509 pieces of comment spam killed since 2008, mostly via Akismet.

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!

for t in "1st 2nd 3rd 4th 11th 12th 13th 21st 22nd 23rd 111th 112th 113th".split(): assert(ord_text(int(t[:-2])) == t)

Thursday, April 24th, 2008
+
9 comments

Comment from Matt Tarbit , later that day

I think you might need to add 111th, 112th and 113th to the string in your test suite.

(Can't be certain though because I get a SyntaxError when trying to run your one-liner in python 2.3.4)

Comment from Paul , later that day

Excelllent catch, Matt, thanks! Fixed.

And yes, I should have mentioned that this is Python 2.5+ only, due to the ternary logic.

Comment from Reggie Drake , later that day

Unfortunately, your one-liner is so desirous to be a one-liner that on my resolution it reaches way out of the central column of your layout and past the 'Atom feed' link.

Comment from Leo Petr , later that day

Great! Now add internationalization for Russian, Swahili, and Chinese.

Comment from Paul , later that day

I knew that was coming.

I'll need a couple more lines.

Comment from kbob , later that day

Here's a version that eliminates the repeated "th" AND runs in both Python 2.3 and 2.5.

ord_text = lambda n: str(n) + {1: 'st', 2: 'nd', 3: 'rd'}.get(n % (10 < n % 100 < 14 or 10), 'th')

Comment from Jason Davies , 2 days later

Nice.

Comment from Banibrata Dutta , 6 months later

Hi,

Excellent blogroll/site there. Infact I was happy to find this site running on Django. However, the CSS (I believe, since I'm not too good with web-technologies), may need some mending, because using Google Chrome (or even IE), in a 800px wide window the code-snippets run outside the central pane boundary, and onto the right pane, rendering the text undreadable. Hope that you fix it.

cheers, Banibrata

Comment from Paul , 6 months later

Banibrata, thanks for the note. Keep in mind that the lines above are extra-wide by design -- they're supposed to look too long for their own good. Though a style makeover of the site is inevitable, this post won't be driving it!

Comments are closed for this post. But I welcome questions/comments via email or Twitter.