I have a slightly strange problem with sorting in a python script. Basically, sorting by title and meta_type both work as expected, but by id produces totally random results that I cannot fathom (I really can't see any pattern at all). Here is the code snippet ------------------------ def sort_id(ob1, ob2): return cmp(ob1.id, ob2.id) def sort_title(ob1, ob2): return cmp(ob1.title, ob2.title) def sort_meta(ob1, ob2): return cmp(ob1.meta_type, ob2.meta_type) if context.REQUEST.has_key('sort'): obVals = context.objectValues(all_types) obVals.sort(sort_id) if context.REQUEST['sort'] == 'title': obVals.sort(sort_title) elif context.REQUEST['sort'] == 'object_type': obVals.sort(sort_meta) -------------------------- I have also tried: getattr(ob1, 'id') - which gives the same wierd results ob1.id() - which gives a TypeError of 'call of non-function (type string)' I copied this approach from Chris McD's post here (http://zope.nipltd.com/public/lists/zope-archive.nsf/ByKey/19E1DED84C7E413F). Can anyone tell me why the id sort goes so crazy? Thanks tim tim at sitefusion dot co dot uk
Tim Hicks writes:
I have a slightly strange problem with sorting in a python script. Basically, sorting by title and meta_type both work as expected, but by id produces totally random results that I cannot fathom (I really can't see any pattern at all). You know, that "id" is often a method and not an attribute. If it is, your comparisons will produce funny results.
Use "getID()" instead. Dieter
----- Original Message ----- From: "Dieter Maurer" <dieter@handshake.de> To: "Tim Hicks" <tim@sitefusion.co.uk> Cc: <zope@zope.org> Sent: Friday, July 27, 2001 8:21 PM Subject: Re: [Zope] Python script sort by id
Tim Hicks writes:
I have a slightly strange problem with sorting in a python script. Basically, sorting by title and meta_type both work as expected, but by id produces totally random results that I cannot fathom (I really can't see any pattern at all). You know, that "id" is often a method and not an attribute. If it is, your comparisons will produce funny results.
Use "getID()" instead.
def sort_id(ob1, ob2): return cmp(ob1.getID(), ob2.getID()) Thanks Dieter, but with this, I get... """ Error Type: AttributeError Error Value: getID Traceback (innermost last): File /mnt2/local/Zope-2.3.2-src/lib/python/ZPublisher/Publish.py, line 223, in publish_module File /mnt2/local/Zope-2.3.2-src/lib/python/ZPublisher/Publish.py, line 187, in publish File /mnt2/local/Zope-2.3.2-src/lib/python/Zope/__init__.py, line 221, in zpublisher_exception_hook (Object: Traversable) File /mnt2/local/Zope-2.3.2-src/lib/python/ZPublisher/Publish.py, line 171, in publish File /mnt2/local/Zope-2.3.2-src/lib/python/ZPublisher/mapply.py, line 160, in mapply (Object: index_html) File /mnt2/local/Zope-2.3.2-src/lib/python/ZPublisher/Publish.py, line 112, in call_object (Object: index_html) File /mnt2/local/Zope-2.3.2-src/lib/python/Shared/DC/Scripts/Bindings.py, line 324, in __call__ (Object: index_html) File /mnt2/local/Zope-2.3.2-src/lib/python/Shared/DC/Scripts/Bindings.py, line 354, in _bindAndExec (Object: index_html) File /mnt2/local/Zope-2.3.2-src/lib/python/Products/PythonScripts/PythonScript.py , line 336, in _exec (Object: index_html) (Info: ({'script': <PythonScript instance at 99beb0>, 'context': <Folder instance at 99c478>, 'container': <Folder instance at 832438>, 'traverse_subpath': []}, (), {}, None)) File Script (Python), line 121, in index_html (Object: string) File <string>, line 40, in sort_id (Object: Traversable) File /mnt2/local/Zope-2.3.2-src/lib/python/Products/PythonScripts/Guarded.py, line 273, in __getattr__ File /mnt2/local/Zope-2.3.2-src/lib/python/Products/PythonScripts/Guarded.py, line 143, in __careful_getattr__ (Object: Traversable) AttributeError: (see above) """ It seems that the cmp function needs to get hold of attributes, not methods. Even if I specifically call a method on ob1 or ob2, getattr seems to be called in there somewhere. Any more clues? tim
Tim Hicks wrote:
def sort_id(ob1, ob2): return cmp(ob1.getID(), ob2.getID())
It seems that the cmp function needs to get hold of attributes, not methods. Even if I specifically call a method on ob1 or ob2, getattr seems to be called in there somewhere.
Any more clues?
try getId() instead of getID() Maarten.
----- Original Message ----- From: "Maarten Slaets" <maarten.slaets@neolabs.be> To: "Tim Hicks" <tim@sitefusion.co.uk> Cc: <zope@zope.org> Sent: Sunday, July 29, 2001 7:29 PM Subject: Re: [Zope] Python script sort by id
Tim Hicks wrote:
def sort_id(ob1, ob2): return cmp(ob1.getID(), ob2.getID())
It seems that the cmp function needs to get hold of attributes, not methods. Even if I specifically call a method on ob1 or ob2, getattr seems to be called in there somewhere.
Any more clues?
try getId() instead of getID()
Maarten, that's cracked it :-)). Thanks very much indeed. tim
participants (3)
-
Dieter Maurer -
Maarten Slaets -
Tim Hicks