Re: Trouble sorting a list of objects by attributes
I've found myself stumped when trying to organize this list of objects. The objects in question are timesheets which i'd like to sort by four attributes: class TimeSheet: department = string engagement = string date = datetime.date stare_hour = datetime.time My ultimate goal is to have a list of this timesheet objects which are first sorted by departments, then within each department block of the list, have it organized by projects. Within each project block i finally want them sorted chronologically by date and time. Python natural sort for tuples is doing the right thing for you, for example: from operator import attrgetter from random import randint class Point: def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return (%s, %s) % (self.x, self.y) points = [Point(randint(1, 10), randint(1, 10)) for i in range(10)] print points points.sort(key=attrgetter(x, y)) print points Gave me: [(2, 3), (1, 4), (4, 4), (6, 6), (2, 10), (3, 5), (7, 1), (3, 6), (4, 1), (9, 6)] [(1, 4), (2, 3), (2, 10), (3, 5), (3, 6), (4, 1), (4, 4), (6, 6), (7, 1), (9, 6)] Points first sorted by 'x' and then by 'y'. HTH, -- Miki miki.teb...@gmail.com http://pythonwise.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
Robocop: then within each department block of the list, have it organized by projects. I don't know what does it means. timesheets.sort(key=operator.attrgetter('string')) Try something like: timesheets.sort(key=attrgetter(department, engagement, date, stare_hour)) My brain might explode if i continue. Relax. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: Robocop: then within each department block of the list, have it organized by projects. I don't know what does it means. timesheets.sort(key=operator.attrgetter('string')) Try something like: timesheets.sort(key=attrgetter(department, engagement, date, stare_hour)) My brain might explode if i continue. Relax. Bye, bearophile Projects was meant to be engagements. All these suggestions are great, let me see what i can make happen right now. Thanks all! -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: Robocop: then within each department block of the list, have it organized by projects. I don't know what does it means. timesheets.sort(key=operator.attrgetter('string')) Try something like: timesheets.sort(key=attrgetter(department, engagement, date, stare_hour)) My brain might explode if i continue. Relax. Bye, bearophile UH OH GUYS! line 110, in sorter timesheets.sort(key=attrgetter(department, engagement, date,start)) TypeError: attrgetter expected 1 arguments, got 4 -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 2:17 pm, Robocop btha...@physics.ucsd.edu wrote: On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: Robocop: then within each department block of the list, have it organized by projects. I don't know what does it means. timesheets.sort(key=operator.attrgetter('string')) Try something like: timesheets.sort(key=attrgetter(department, engagement, date, stare_hour)) My brain might explode if i continue. Relax. Bye, bearophile UH OH GUYS! line 110, in sorter timesheets.sort(key=attrgetter(department, engagement, date,start)) TypeError: attrgetter expected 1 arguments, got 4 I think there may have been a misunderstanding. I was already using attrgetter, my problem is that it doesn't appear to be sorting by the argument i give it. How does sort work with strings? How about with datetime.time or datetime.date? So far i can get it sorting strictly by the datetime objects, but i need all of this sorting done within the constraints imposed by doing sorts via department and engagements. Any ideas? -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 2:20 pm, Robocop btha...@physics.ucsd.edu wrote: On Feb 6, 2:17 pm, Robocop btha...@physics.ucsd.edu wrote: On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: Robocop: then within each department block of the list, have it organized by projects. I don't know what does it means. timesheets.sort(key=operator.attrgetter('string')) Try something like: timesheets.sort(key=attrgetter(department, engagement, date, stare_hour)) My brain might explode if i continue. Relax. Bye, bearophile UH OH GUYS! line 110, in sorter timesheets.sort(key=attrgetter(department, engagement, date,start)) TypeError: attrgetter expected 1 arguments, got 4 I think there may have been a misunderstanding. I was already using attrgetter, my problem is that it doesn't appear to be sorting by the argument i give it. How does sort work with strings? How about with datetime.time or datetime.date? So far i can get it sorting strictly by the datetime objects, but i need all of this sorting done within the constraints imposed by doing sorts via department and engagements. Any ideas? I'm stuck with python 2.4 right now:( -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
Robocop wrote: UH OH GUYS! line 110, in sorter timesheets.sort(key=attrgetter(department, engagement, date,start)) TypeError: attrgetter expected 1 arguments, got 4 Um... what version of Python are you running? Alway specify. (Too many people do not). In 3.0 from operator import attrgetter f=attrgetter(department, engagement,date,start) runs fine as per the doc. operator.attrgetter(attr[, args...]) Return a callable object that fetches attr from its operand. If more than one attribute is requested, returns a tuple of attributes. After, f = attrgetter('name'), the call f(b) returns b.name. After, f = attrgetter('name', 'date'), the call f(b) returns (b.name, b.date). -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 2:34 pm, Robocop btha...@physics.ucsd.edu wrote: On Feb 6, 2:20 pm, Robocop btha...@physics.ucsd.edu wrote: On Feb 6, 2:17 pm, Robocop btha...@physics.ucsd.edu wrote: On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: Robocop: then within each department block of the list, have it organized by projects. I don't know what does it means. timesheets.sort(key=operator.attrgetter('string')) Try something like: timesheets.sort(key=attrgetter(department, engagement, date, stare_hour)) My brain might explode if i continue. Relax. Bye, bearophile UH OH GUYS! line 110, in sorter timesheets.sort(key=attrgetter(department, engagement, date,start)) TypeError: attrgetter expected 1 arguments, got 4 I think there may have been a misunderstanding. I was already using attrgetter, my problem is that it doesn't appear to be sorting by the argument i give it. How does sort work with strings? How about with datetime.time or datetime.date? So far i can get it sorting strictly by the datetime objects, but i need all of this sorting done within the constraints imposed by doing sorts via department and engagements. Any ideas? I'm stuck with python 2.4 right now:( I've got a python 2.4 fix though! timesheets.sort(key= lambda i:(i.department,i.engagement,i.start)) Thanks for the help and time! -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
I think there may have been a misunderstanding. I was already using attrgetter, my problem is that it doesn't appear to be sorting by the argument i give it. How does sort work with strings? How about with datetime.time or datetime.date? You were using the attrgetter, but it looks like you weren't doing it correctly: timesheets.sort(key=operator.attrgetter('string')) You don't specify a type, be it string or date or anything: you specify the /name/ of the attribute to get. It'll handle sorting by any data type that has an order to it without you having to worry about it at all. Strings, ints, dates, times. It should be: timesheets.sort(key=operator.attrgetter(department)) If you want to sort by the value of the department attribute, which happens to be a string. If you want to sort by the date attribute, which happens to be a datetime.date, you do: timesheets.sort(key=operator.attrgetter(date)) Where date is not a datatype a la datetime.date, but the name of an attribute on your TimeSheet instances. --S -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 2:41 pm, Stephen Hansen apt.shan...@gmail.com wrote: I think there may have been a misunderstanding. I was already using attrgetter, my problem is that it doesn't appear to be sorting by the argument i give it. How does sort work with strings? How about with datetime.time or datetime.date? You were using the attrgetter, but it looks like you weren't doing it correctly: timesheets.sort(key=operator.attrgetter('string')) You don't specify a type, be it string or date or anything: you specify the /name/ of the attribute to get. It'll handle sorting by any data type that has an order to it without you having to worry about it at all. Strings, ints, dates, times. It should be: timesheets.sort(key=operator.attrgetter(department)) If you want to sort by the value of the department attribute, which happens to be a string. If you want to sort by the date attribute, which happens to be a datetime.date, you do: timesheets.sort(key=operator.attrgetter(date)) Where date is not a datatype a la datetime.date, but the name of an attribute on your TimeSheet instances. --S Yeah this i totally understand. In my code i was using attrgetter correctly, in the above answer i used string to represent the datatype of the attribute, not what i actually put in there. More my laziness than anything else. The problem boiled down attrgetter taking multiple arguments in 2.5, but only one in 2.4. Thanks for the input though. -- http://mail.python.org/mailman/listinfo/python-list