sequence.sort from python
Hi All! I am trying to use the sequence.sort function to sort the results from an ldap search (using the LDAPDelegate module from LDAPUserFolder to search ldap). This is in a custom python product, not in the zodb. Since every attribute value returned is a tuple, I can't exactly use sort's standard cmp function (at least it doesn't seem to work for me). I thought I'd try to use a custom comparison function, so I wrote my own to compare the first item in each attribute's tuple. I then called sort: sequence.sort(ldap_results,(('givenName',res_cmp),)) (res_cmp is the custom function) I get an attribute error saying 'None' object has no attribute 'res_cmp'. The traceback (included below) says that the error occurred on line 178 of SortEx.py. This line tried to get the actual function using the current namespace as passed into the sort function. I didn't pass one in, and the default is None. Is there anyway around this? Is there a way to create my own namespace and pass it in? Would it be easiest to delegate the sort to a zodb script? Thanks for the help, Andy
Andrew Altepeter wrote at 2003-10-2 14:30 -0500:
I am trying to use the sequence.sort function to sort the results from an ldap search (using the LDAPDelegate module from LDAPUserFolder to search ldap). This is in a custom python product, not in the zodb.
Since every attribute value returned is a tuple, I can't exactly use sort's standard cmp function (at least it doesn't seem to work for me). I thought I'd try to use a custom comparison function, so I wrote my own to compare the first item in each attribute's tuple.
"sequence.sort" is made to sort a sequence of objects (instances) according to some attribute values. It is not for sorting tuples. Python's "list.sort" may be what you are looking for. It accepts a custom comparison function (x,y --> -1|0|1) as optional parameter. When your comparison function justs compares the first element of a tuple, you can drop it as tuples are by default sorted lexicographically. Dieter
participants (2)
-
Andrew Altepeter -
Dieter Maurer