sort in python script dynamically?
Dear all, I have python script that does for i in self.objectItems('Folder') I want to sort by value of i[0] before "i" is taken to FOR. So it is possible to do something like for i in self.objectItems('Folder').sort(i[0]) ??? I want to sort by id value, which is in thos case value of i[0] Under Zope DTML it is very easy: <dtml-in "objectItems('Folder')" sort=id> But under python? Many thanks, J. Lukesh
Python: somelist.sort(cmpmethod) Zope: sequence.sort() Both methods are well documented. -aj --On Donnerstag, 8. Januar 2004 16:56 Uhr +0100 Jaroslav Lukesh <lukesh@seznam.cz> wrote:
Dear all,
I have python script that does
for i in self.objectItems('Folder')
I want to sort by value of i[0] before "i" is taken to FOR. So it is possible to do something like
for i in self.objectItems('Folder').sort(i[0])
???
I want to sort by id value, which is in thos case value of i[0]
Under Zope DTML it is very easy:
<dtml-in "objectItems('Folder')" sort=id>
But under python?
Many thanks,
J. Lukesh
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Andreas Jung wrote:
Python: somelist.sort(cmpmethod) Zope: sequence.sort()
Both methods are well documented.
Hi Andreas, I am not big pythonist, just little beginner and I was think that object language was able to do for i in self.objectItems('Folder').sort() But it does not. So I was try ids = self.objectIds('Folder') ids = ids.sort() for i in ids : TypeError: object of type 'string' is not callable But I was get following tip: a=context.objectItems() a.sort(lambda x,y: cmp(x[0],y[0])) for i in a: And that works fine. But I absolutelly does not understand to that. And my ideas about object programming was get one big hole. Thanks to all which helps me. Best Regards, JL.
I have python script that does
for i in self.objectItems('Folder')
I want to sort by value of i[0] before "i" is taken to FOR. So it is possible to do something like
for i in self.objectItems('Folder').sort(i[0])
Under Zope DTML it is very easy:
<dtml-in "objectItems('Folder')" sort=id>
But under python?
--On Freitag, 9. Januar 2004 8:46 Uhr +0100 Jaroslav Lukesh <lukesh@seznam.cz> wrote:
Andreas Jung wrote:
Python: somelist.sort(cmpmethod) Zope: sequence.sort()
Both methods are well documented.
Hi Andreas,
I am not big pythonist, just little beginner and I was think that object language was able to do
for i in self.objectItems('Folder').sort()
But it does not. So I was try
ids = self.objectIds('Folder') ids = ids.sort() for i in ids :
See Python documentation: sort() sorts *in-place* -aj
Andreas Jung wrote:
--On Freitag, 9. Januar 2004 8:46 Uhr +0100 Jaroslav Lukesh <lukesh@seznam.cz> wrote:
Andreas Jung wrote:
Python: somelist.sort(cmpmethod) Zope: sequence.sort()
Both methods are well documented.
Hi Andreas,
I am not big pythonist, just little beginner and I was think that object language was able to do
for i in self.objectItems('Folder').sort()
But it does not. So I was try
ids = self.objectIds('Folder') ids = ids.sort() for i in ids :
See Python documentation: sort() sorts *in-place*
Thanks for explaining, so this without any lambda function works too: ids = self.objectIds('Folder') ids.sort() for i in ids : Regards, JL.
-----Original Message----- From: zope-bounces@zope.org [mailto:zope-bounces@zope.org]On Behalf Of Jaroslav Lukesh Sent: Friday, January 09, 2004 4:47 AM To: Andreas Jung; zope@zope.org Subject: Re: [Zope] sort in python script dynamically?
Hi Andreas,
I am not big pythonist, just little beginner and I was think that object language was able to do
for i in self.objectItems('Folder').sort()
But it does not. So I was try
ids = self.objectIds('Folder') ids = ids.sort() for i in ids :
TypeError: object of type 'string' is not callable
But I was get following tip:
a=context.objectItems() a.sort(lambda x,y: cmp(x[0],y[0])) for i in a:
And that works fine. But I absolutelly does not understand to that. And my ideas about object programming was get one big hole.
Look how object oriented is it. This function does not return the list that has been sorted, instead it only sorts the list, why this? Because the list is a mutable object, it dont create another list from the sorted original one, so you dont have the list A unsorted and list B sorted, only sorted list A. I hope i could explain why this behavior. Best Regards -- Mauricio Souza Lima Web Developer VARIG Brasil - GGTI mauricio.lima@varig.com
Thanks to all which helps me.
Best Regards,
JL.
Hi, Many thanks to all for exlpain. Regards, JL.
Let me get back to a related issue regarding sorting, if you permit. As we learned in previous mails, it is simple to sort a list by any of its fields if they can be indexed like this: def sort_by_index(self, list, i): list.sort(lambda a, b: cmp(a[i], b[i])) list = [[x, y], [y, z]] sort_by_index(list) But how can one sort by field names (= attributes of an object) as parameters??? The following certainly does not work! def sort_by_field(self, list, f): list.sort(lambda a, b: cmp(a.f, b.f)) x = myObject(i, j, k) y = myObject(l, m, n) list = [x, y] sort_by_field(list) How can an attribute name (a string) be translated in the actual attribute? thanks for your help André
On Tue, Jan 13, 2004 at 09:19:01AM +0100, Andre Meyer wrote:
How can an attribute name (a string) be translated in the actual attribute?
getattr(object, 'attribute_name') -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's CHERRY-POPPING PELICAN MEGA! (random hero from isometric.spaceninja.com)
On Tuesday 13 January 2004 12:19 am, Andre Meyer wrote:
def sort_by_field(self, list, f): list.sort(lambda a, b: cmp(a.f, b.f))
x = myObject(i, j, k) y = myObject(l, m, n) list = [x, y] sort_by_field(list)
How can an attribute name (a string) be translated in the actual attribute?
getattr(a, f) Though using the 'decorate-sort-undecorate' idiom will likely perform much better, especially if you are sorting a large number of objects. An example is available here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52234 in this case it would be: def sort_by_field(list, f): decorated = [(getattr(obj, f), obj) for obj in list] decorated.sort() return [a[1] for a in decorated] If you prefer to sort in place that's just a trivial change (list[:] = ${the return statement}), as is adding secondary characteristics to sort on, or insuring sort stability (as in the link above). Also, it's not clear why you are making your sort function a member of a particular class (judging by the 'self' parameter), it probably makes more sense as a general utility function. Alec Mitchell
Thanks a lot for your help. What a nice and responsive community ;-) best wishes Andre
On Tue, Jan 13, 2004 at 12:47:37AM -0800, Alec Mitchell wrote:
in this case it would be:
def sort_by_field(list, f): decorated = [(getattr(obj, f), obj) for obj in list] decorated.sort() return [a[1] for a in decorated]
That's fine, but dont't get in the habit of using "list" as a variable name. There is a python built-in with the same name! Hiding builtins is likely to be cause bugs sooner or later. -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's BASTARD-HUGGER! (random hero from isometric.spaceninja.com)
On Thursday 08 January 2004 09:56 am, Jaroslav Lukesh wrote:
I want to sort by value of i[0] before "i" is taken to FOR. So it is possible to do something like
for i in self.objectItems('Folder').sort(i[0])
The reason this doesn't work as written is because the .sort() method works in-place and does not return a value. Also, sort needs a function that does the comparison. You want: values = self.objectItems('Folder') values.sort(lambda a,b: cmp(a[0],b[0]) for i in values: # do stuff pass Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
On Thu, Jan 08, 2004 at 04:11:08PM -0600, Terry Hancock wrote:
On Thursday 08 January 2004 09:56 am, Jaroslav Lukesh wrote:
I want to sort by value of i[0] before "i" is taken to FOR. So it is possible to do something like
for i in self.objectItems('Folder').sort(i[0])
The reason this doesn't work as written is because the .sort() method works in-place and does not return a value. Also, sort needs a function that does the comparison. You want:
values = self.objectItems('Folder') values.sort(lambda a,b: cmp(a[0],b[0]) for i in values: # do stuff pass
sort() is lexicographic by default, which means it already sorts on the first element just fine :-) No comparison function needed in this case. -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's THE PANTSY NUT! (random hero from isometric.spaceninja.com)
Paul Winkler wrote:
for i in self.objectItems('Folder').sort(i[0])
values = self.objectItems('Folder') values.sort(lambda a,b: cmp(a[0],b[0]) for i in values: # do stuff pass
This works fine.
sort() is lexicographic by default, which means it already sorts on the first element just fine :-) No comparison function needed in this case.
I was try before: ids = self.objectIds('Folder') ids = ids.sort() for i in ids : TypeError: object of type 'string' is not callable Regards, JL.
On Fri, Jan 09, 2004 at 09:33:42AM +0100, Jaroslav Lukesh wrote:
I was try before:
ids = self.objectIds('Folder') ids = ids.sort() for i in ids :
TypeError: object of type 'string' is not callable
Regards, JL.
two points: 1) "self" has meaning in Product code and in External Methods, but not Script (Python). Which are you writing? (I doubt that's the problem because if it was, it wouldn't give a TypeError, it should give a NameError) 2) Your example looks fine except that it's incomplete. What's after "for i in ids:"? If you try to call i(), you will get that error :-) foo.objectIds() returns a list of strings. foo.objectValues() returns a lit of zope objects. foo.objectItems() returns a list of (id, object) pairs. -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's WALRUS HYDRO WEASLE-CAKE ! (random hero from isometric.spaceninja.com)
participants (8)
-
Alec Mitchell -
Andre Meyer -
Andre Meyer -
Andreas Jung -
Jaroslav Lukesh -
Mauricio Souza Lima -
Paul Winkler -
Terry Hancock