Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Question on sort() key function (http://www.velocityreviews.com/forums/t586373-question-on-sort-key-function.html)

Robert Latest 01-22-2008 08:45 AM

Question on sort() key function
 
Hello,

I have this class:

class File:
def __init__(self):
self.name = ''
self.path = ''
self.date = 0
self.mod_date = 0
self.keywords = []
self.url = ''


....and after creating a list of File objects called flist, I'd like to sort
it like thus:

flist.sort(key=File.mod_date.toordinal)

However, Python says:
AttributeError: class File has no attribute 'mod_date'

Well if you ask me, there are many things that may be said about my File
class, but the absence of the attribute 'mod_date' ain't one of them. What
do you think?

And yes, this loop works fine:

for f in flist:
print f.mod_date.isoformat()

(which IMO proves that all mod_date members are properly initialized as
datetime objects).

robert

Paul Rubin 01-22-2008 08:50 AM

Re: Question on sort() key function
 
Robert Latest <boblatest@yahoo.com> writes:
> flist.sort(key=File.mod_date.toordinal)
>
> However, Python says:
> AttributeError: class File has no attribute 'mod_date'


The attribute is on instances of File, not on the class itself. See
if this works:

flist.sort(key=lambda f: f.mod_date.toordinal)

Robert Latest 01-22-2008 09:12 AM

Re: Question on sort() key function
 
Paul Rubin wrote:
> The attribute is on instances of File, not on the class itself. See
> if this works:
>
> flist.sort(key=lambda f: f.mod_date.toordinal)


It doesn't throw an error any more, but neither does it sort the list. This,
however, works:

----------------------
def by_date(f1, f2):
return f1.mod_date.toordinal() - f2.mod_date.toordinal()

flist.sort(by_date)
----------------------

So I'm sticking with it, although I sort of liked the key approach.

robert

Peter Otten 01-22-2008 09:30 AM

Re: Question on sort() key function
 
Robert Latest wrote:

> Paul Rubin wrote:
>> The attribute is on instances of File, not on the class itself. See
>> if this works:
>>
>> flist.sort(key=lambda f: f.mod_date.toordinal)

>
> It doesn't throw an error any more, but neither does it sort the list. This,
> however, works:
>
> ----------------------
> def by_date(f1, f2):
> return f1.mod_date.toordinal() - f2.mod_date.toordinal()
>
> flist.sort(by_date)
> ----------------------
>
> So I'm sticking with it, although I sort of liked the key approach.
>
> robert


This should work then:

def date_key(f):
return f.mod_date.toordinal()
flist.sort(key=date_key)

This can also be written as

flist.sort(key=lambda f: f.mod_date.toordinal())

Peter

Robert Latest 01-22-2008 09:56 AM

Re: Question on sort() key function
 
Peter Otten wrote:
> Robert Latest wrote:
>
>> Paul Rubin wrote:
>>> The attribute is on instances of File, not on the class itself. See
>>> if this works:
>>>
>>> flist.sort(key=lambda f: f.mod_date.toordinal)

>>
>> It doesn't throw an error any more, but neither does it sort the list. This,
>> however, works:
>>
>> ----------------------
>> def by_date(f1, f2):
>> return f1.mod_date.toordinal() - f2.mod_date.toordinal()
>>
>> flist.sort(by_date)
>> ----------------------
>>
>> So I'm sticking with it, although I sort of liked the key approach.
>>
>> robert

>
> This should work then:
>
> def date_key(f):
> return f.mod_date.toordinal()
> flist.sort(key=date_key)
>
> This can also be written as
>
> flist.sort(key=lambda f: f.mod_date.toordinal())


Well, that's almost Paul's (non-working) suggestion above, but it works
because of the parentheses after toordinal. Beats me how both versions can
be valid, anyway.

To me it's all greek. I grew up with C function pointers, and they
always work.

robert

Marc 'BlackJack' Rintsch 01-22-2008 10:04 AM

Re: Question on sort() key function
 
On Tue, 22 Jan 2008 09:56:55 +0000, Robert Latest wrote:

> Peter Otten wrote:
>> Robert Latest wrote:
>>
>> This should work then:
>>
>> def date_key(f):
>> return f.mod_date.toordinal()
>> flist.sort(key=date_key)
>>
>> This can also be written as
>>
>> flist.sort(key=lambda f: f.mod_date.toordinal())

>
> Well, that's almost Paul's (non-working) suggestion above, but it works
> because of the parentheses after toordinal. Beats me how both versions can
> be valid, anyway.
>
> To me it's all greek. I grew up with C function pointers, and they
> always work.
>
> robert


Suppose `func` is a C function pointer, then

foo = func;

and

foo = func();

have different meanings. It's just the same in Python. First is the
function itself, second *calls* the function.

Ciao,
Marc 'BlackJack' Rintsch

Paul Rubin 01-22-2008 01:24 PM

Re: Question on sort() key function
 
Robert Latest <boblatest@yahoo.com> writes:
> > flist.sort(key=lambda f: f.mod_date.toordinal)

>
> It doesn't throw an error any more, but neither does it sort the list. This,
> however, works:


Oh, I didn't realize that toordinal was a callable, given your earlier
sample. You want:

flist.sort(key=lambda f: f.mod_date.toordinal() )


All times are GMT. The time now is 10:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.