The Artima Developer Community
Sponsored Link

Python Buzz Forum
Acronymizer in Python (and why does sort have to suck?)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
maxim khesin

Posts: 251
Nickname: xamdam
Registered: Mar, 2005

Maxim Khesin is developer for Liquidnet. I like C++, python, attend design patterns study group/NYC.
Acronymizer in Python (and why does sort have to suck?) Posted: Nov 9, 2005 9:43 PM
Reply to this message Reply

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
Latest Python Buzz Posts
Latest Python Buzz Posts by maxim khesin
Latest Posts From python and the web

Advertisement
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:

>>> help(list.sort)
Help on method_descriptor:

sort(...)
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1

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:

>>> l = [8, 3, 1, 5, 4, 7, 2, 6]
>>> l.sort(lambda v1, v2:v1-v2)
>>> l
[1, 2, 3, 4, 5, 6, 7, 8]

Is this a documentation bug???

Read: Acronymizer in Python (and why does sort have to suck?)

Topic: Wordpress and PHP5? Previous Topic   Next Topic Topic: The blog has moved

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use