Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Question on sort() key function

Reply
Thread Tools

Question on sort() key function

 
 
Robert Latest
Guest
Posts: n/a
 
      01-22-2008
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
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      01-22-2008
Robert Latest <(E-Mail Removed)> 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)
 
Reply With Quote
 
 
 
 
Robert Latest
Guest
Posts: n/a
 
      01-22-2008
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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-22-2008
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
 
Reply With Quote
 
Robert Latest
Guest
Posts: n/a
 
      01-22-2008
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
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      01-22-2008
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
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-22-2008
Robert Latest <(E-Mail Removed)> 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() )
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WEP/WPA Key / Network Key...Cant connect ! =?Utf-8?B?SGVsbG8sV2lyZWxlc3MgaXMgQW5ub3lpbmc=?= Wireless Networking 1 09-20-2005 08:30 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Replace Tab Key to Return Key (Enter Key) from Web Forms? M P ASP General 1 08-06-2004 08:32 AM
custom key and hasmap using a ranged key Christian Bongiorno Java 1 06-15-2004 10:03 PM



Advertisments