PRE-PEP: new Path class; sorting and __cmp__
webmaster at beyond-thoughts.com
Fri Jan 9 19:41:32 CET 2004
As I pointed out path.__cmp__ should not be used for e.g. comparing
But features like sorting on filesizes are very useful.
I'm not sure if Gerrit Holl already meant this in his conclusion on
"Comparing files" in the PEP.
I'll outline it a bit ...
I propose a callable singleton class which only instance we assign to
sort_on (defined in the path-module).
It will have methods like: filesize, extension, filename, etc.
They will all be defined like:
def filesize(self, path1, path2):
except XXX: # catch Exceptions that are raised because path1 doesn't
know how to compare with path2 (for different path-subclasses)
return (-1) * path2._cmp_filesize(path1) # is this the best way to do
raise YYY # "path1 and path2 can't be compared on filesize; class1 and
class2 are not compatible"
def __call__(self, *args):
if len(args) == 0:
return self.filesize # example!
elif len(args) == 1: # allow comparing uncommon things for subclasses
of path e.g. ServerName/IPs for FTPPath ...
def cmp_x(path1, path2, what=str(args)):
# like filesize but
pathCmpFunc= getattr(path1, '_cmp_'+what)
# Catch exceptions ...
elif len(args) == 2: # default comparison
return self.filesize(path1, path2) # example!
raise "Won't work ... FIXME"
Then we can have things like:
l= [path1, path2, path3]
I like this :-)
What do You think?
More information about the Python-list