sort by last then by first
adalke at mindspring.com
Tue Jan 28 17:26:58 CET 2003
Padraig at Linux.ie wrote:
> Andrew Dalke wrote:
>> 1) requires dispatch based on type (string vs. function)
Doing so is not Pythonic.
What should be done in the following?
>>> class prefix(str):
... def __call__(self, x, y):
... if ":" not in x: x = self + ":" + x
... if ":" not in y: y = self + ":" + y
... return cmp(x, y)
>>> p = prefix("http")
>>> values = ["python.org", "slashdot.org", "http:salon.com"]
It's a callable string. Does that mean it should be translated
as one of your sort strings or as a sort function?
(bad example, but I'm not going to spend the time coming up
with a better case of where one might not know if something
is a string or a callable.)
>> 2) would be just about the only function where the argument
>> parameters are serialized into a string. (Are there others
>> in the standard library?)
> well I was just suggested that so that the sort docs would
> suffice and also more users would know the syntax straight off.
> You could of course do
I have never used that syntax and did not know that it existed.
> Yes you're probably right that gnu sort has too much baggage.
> How about passing a tuple like: ("1(r)","0(n)")
> This would first reverse sort on second "column" and
> then forward sort on first column treating strings as numeric
Python objects are strongly typed so there it no need to say
"treating strings as numeric". The values should be numbers in
the first place. If you want an option to do so, you need to
allow that float and int types have different ranges, eg,
>>> s = "12345" * 1000
>>> float(s) == int(s)
__main__:1: RuntimeWarning: tp_compare didn't return -1 or -2 for exception
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OverflowError: long int too large to convert to float
(Hmmm, strange RuntimeWarning, submitted as bug.)
This means you'll want to pass in a conversion function name.
And if you start doing that, well, why not just use Python code?
>> 4) More generic to have a parse function which turns such a
>> string into a comparison function, so you have
>> names.sort(GnuSortOpt("--key=1,1 --key=0,0"))
> Hmm, maybe.
>> 5) Doesn't handle the more common case of sorting by
>> attribute name.
> ("attr2(r)", "attr1(n)")
Why not just write this as Python code?
from sortfuncs import *
f = attr("name1").reverse() + attr("name2").as(int) + \
x = [ .... ]
and for optimization you can also implement
which would do the fast decorate, sort, undecorate.
Answer: because it isn't all that useful.
dalke at dalkescientific.com
More information about the Python-list