Archive for August, 2009

To Risk Life or Job

From my[confined]space

Code

A comment says:

On the other hand, if you make your code confusing as hell, you will have much better job security.


I have been assuming some maniac serial killer would be maintaining it while doing my recent (private, but to be public) project, but still find my own codes could be confusing.

Anyway, I am glad that this did not happen until it reached about 70 files and 8,000 lines – the threshold used to be 2 files and 300 lines. The project now has about 110 files and 14,000 lines.

Tags: ,

__cmp__ should be reintroduced to Python

A lot of people think this decision to drop __cmp__ from Python sucks. (1 2)

The cmp argument is also dropped from list.sort and sorted. Some people explain that some programmers used to use cmp when it is more convenient and efficient to use key. By dropping cmp, poor programmers are forced to use key. On the other hand, almost all comparators found in practice are actually based on some kind of key function.

Well, theoretically this probably is true: something.sort(key=locale.strxfrm) is better than something.sort(cmp=locale.strcoll).

BUT, what can I do if I only have a third-party comparator?

I have a Python script where I use vercmp, provided in the source of Gentoo Portage, to sort a list of Gentoo packages. No corresponding verkey is provided. So now, in Python 3, I have to either write a verkey myself, or use the following ugly and inefficient workaround:

class VerKey:
    def __init__(self,str):
        self.str = str
    def __lt__(self,other):
        return vercmp(self.str, other.str) < 0

versions.sort (key = VerKey)

Tags: