[Zope] Using custom comparasion function in sequence.sort

Hong Yaun hongyuan at homemaster.cn
Mon Feb 14 00:11:40 EST 2005


I'd like to use a custom comparasion function in a PythonScript to sort 
certain objects, using the sequence.sort utility function.

In the doc-string for sort in sequence/SortEx.py, there is a line about 
custom function:

 - "xxx" -- a user-defined comparison function

So I defined a comparasion function in the PythonScript and passed its 
name to sort. However, when the script is called, I got the error:

    * Module DocumentTemplate.sequence.SortEx, line 66, in sort
    * Module DocumentTemplate.sequence.SortEx, line 175, in
      make_sortfunctions

AttributeError: 'NoneType' object has no attribute 'getitem'

A closer look at make_sortfunctions shows that it tries to find the 
custom comparasion in the name space '_', which is default to None in 
sort(). So it seems I need only supply the correct name space as the 
third, undocumented parameter to sequence.sort for it to find my 
comparasion function.

I tried to use globals() but this seems to be unavailable in a 
PythonScript. So the question is, how to pass the correct name space 
containing my comparasion function to the sort() function?

My PythonScript code segment looks like below:

results = ...

def my_cmp(x, y):
   return cmp(x,y)

sort_on = (('property', 'my_cmp', 'asc'),)
return sequence.sort(results, sort_on)

Thanks for advices.

Best Regards,
Hong Yuan


More information about the Zope mailing list