This post originated from an RSS feed registered with Python Buzz
by maxim khesin.
Original Post: Acronymizer in Python (and why does sort have to suck?)
Feed Title: python and the web
Feed URL: http://feeds.feedburner.com/PythonAndTheWeb
Feed Description: blog dedicated to python and the networks we live in
When I am not wearing a cape and protecting the Universe from super-villans I mostly code a Windoze GUI for backoffice processing at a (albeit a very cool) financial company.
A lot of our GUI is just a specific view of the database. We have some functionality where the user has to type in a column name. As DATABASE_COLUMN_NAMES_ARE_WAY_TOO_LONG_AND_UGLY I hardcoded 2-letter acronyms for some of them.
Today the question came up whether this approach will scale to tables with a lot of columns.
I immediately thought that 22^2 is quite enough possibilities, but then you have to take into account that acronyms have to actually sound a bit like the original, at the very least be composed of two letters of the original in ascending order.
I figured that writing a script to do this would be faster than theorizing, so here is the result: http://fluidobjects.com/static/acronyms.py. The algorithm is pretty self-explainatory and the script makes (may I say) nice use of generators.
Which brings me to another point: the interface for list.sort appears to be broken, at least the documentation:
The obvious question is why specifically -1 and 1, rather than any negative or positive number?
The very reason for the tri-state API (rather than using a boolean for strictly-less-then, which is sufficient for sorting, as comp's return) is to make this kind of thing simple (at least in C, where I think the original interface came from):
stuct Person{int age;} int age_comp(Person* p1, Person* p2){return p1->age - p2->age;} Person people[] = {...}; qsort(people, age_comp);
Requiring the values of comp to be stricly -1 or 1 defeats the whole point.
In reality -1/1 does not seem to be required altogether and any negative or positive number works fine: