My walk was the walk of a human child, but my heart was a tree.

October 15th, 2002

12:29 pm - Joule again

I spent quite a bit of time yesterday evening optimising joule. The graph and chart versions are now served by the same script. More interestingly, this script uses mod_python instead of CGI, which allows it to run in-process with the webserver, and incidentally also gives it the advantage of automatic bytecode compilation. I'm very impressed with mod_python-- it makes the script run like a scalded cat.

12:44 pm - Python ternary operator

I really want to record a very minor epiphany of last night about Python. For a good while I've wanted something like the C operator ?: (known as the "ternary operator" because it's the only one which considers three things). For the uninitiated, this gives you one result if something's true, and another if it isn't. For example:

(the_user_wants_an_apple?number_of_apples:number_of_oranges)

or, very usefully:

(number==1?"kibbutz":"kibbutzim")

or even, for printing,

(user_is_logged_in?"":"You might want to log in, by the way.")

Well, in Python (as in C), truth is represented by the number one, and falsehood by the number zero. So you can represent ?: by giving a list with two things in (which, traditionally, are number 0 and number 1) and then choosing the member of the list corresponding to the condition. So

['kibbutzim', 'kibbutz'][number==1]

How cool is that?
Powered by LiveJournal.com