My name is Paul Bissex, and e-scribe.com is my consulting business. I build web applications using as much open source software as possible. From September to June I teach web design and other important non-photographic professional skills to photographers. In the '90s I wrote technology commentary and reviews for magazines, newspapers, and web publications, including Wired, Salon.com, FamilyPC, the late lamented Web Review, and the Chicago Tribune. Feel free to email me.
I'm co-authoring a book, "Python Web Development with Django", with Jeff Forcier and Wesley Chun. It will be published by Prentice Hall in July 2008, but is available for pre-ordering on Amazon now.
This site is built on a fresh trunk checkout of Django, running on Python 2.5.1, served by Apache and mod_python. The database is SQLite. The operating system is FreeBSD, on a VPS hosted at Johncompanies.com. Comment-spam protection by Akismet. Vintage topo imagery from the Maptech archive.
Akismet, del.icio.us, Django, dpaste.com, Emacs, FreeBSD, Freenode, jQuery, LaunchBar, MacPorts, Markdown, Mercurial, OS X, Postfix, Python, SQLite, Subversion, TextMate, Trac, Ubuntu Linux, wmii
Copyright 2008
by Paul Bissex
and E-Scribe New Media
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)
Excelllent catch, Matt, thanks! Fixed.
And yes, I should have mentioned that this is Python 2.5+ only, due to the ternary logic.
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.
Great! Now add internationalization for Russian, Swahili, and Chinese.
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')
Comments use Markdown syntax. Your comment will not appear until approved, which may take a few hours or more. Spammers will be torpedoed.
The iPhone keyboard doesn't suck
Python one-liner of the day
7 comments
How not to advocate via Google Code
2 comments
99 problems
3 comments
The original Lego Star Wars
2 comments
Toolbot.com source code available on request
bruce
Neat Python hack: infix operators
5 days ago
David Reynolds
The original Lego Star Wars
13 days ago
Jason Davies
Python one-liner of the day
15 days ago
Paddy3118
Let's play a game: BASIC vs. Ruby vs. Python vs. PHP
15 days ago
kbob
Python one-liner of the day
17 days ago
Dan
Let's play a game: BASIC vs. Ruby vs. Python vs. PHP
17 days ago
Leo Petr
Python one-liner of the day
17 days ago
At least 21925 pieces of comment spam killed since January 12th. Thanks are mostly due to Akismet.
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)