PRE-PEP: new Path class; sorting and __cmp__

Christoph Becker-Freyseng webmaster at
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):
		return path1._cmp_filesize(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 
	except XXX:
	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[0])):
			# like filesize but
			pathCmpFunc= getattr(path1, '_cmp_'+what)
			return pathCmpFunc(path2)
			# Catch exceptions ...
		return cmp_x
	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?

Christoph Becker-Freyseng

More information about the Python-list mailing list