Filename type (Was: Re: finding file size)

Gerrit Holl gerrit at nl.linux.org
Sat Jan 3 08:09:48 EST 2004


Martin v. Loewis wrote:
> Gerrit Holl wrote:
> >Any comments?
> 
> It should be possible to implement that type without modifying
> Python proper.

It should indeed. But it isn't what I had in mind, and it's not exactly
the same as a filename type in the language: for example, the name
attribute of a file will still be a string, just as the contents of
os.listdir, glob.glob, etc. (it seems glob follows listdir).

> It might make a good recipe for the cookbook.

If the type would be created without changing python proper, the type
would probably just call os.path.foo for the filename.foo method. It
would be the other way around if the type would become part of the
language: os.path would only be there for backward compatibility, like
string. But in order for os.listdir (and probably more functions) to
return Path objects rather than strings, a C implementation would be
preferable (necessary?). On the other hand, would this type ever be
added, a python implementation would of course be a must.

> Any volunteers?

I may have a look at it.
When thinking about it, a lot more issues than raised in my first post
need to be resolved, like what to do when the intializer is empty...
curdir? root? 

I guess there would a base class with all os-independent stuff, or stuff
that can be coded independently, e.g:
class Path(str):
    def split(self):
        return self.rsplit(self.sep, 1)
    def splitext(self):
        return self.rsplit(self.extsep, 1)
    def basename(self):
        return self.split()[1]
    def dirname(self):
        return self.split()[0]
    def getsize(self):
        return os.stat(self).st_size
    def getmtime(self):
        return os.stat(self).st_mtime
    def getatime(self):
        return os.stat(self).st_atime
    def getctime(self):
        return os.stat(self).st_ctime

where the subclasses define, sep, extsep, etc.

yours,
Gerrit.

-- 
168. If a man wish to put his son out of his house, and declare before
the judge: "I want to put my son out," then the judge shall examine into
his reasons. If the son be guilty of no great fault, for which he can be
rightfully put out, the father shall not put him out.
          -- 1780 BC, Hammurabi, Code of Law
-- 
Asperger's Syndrome - a personal approach:
	http://people.nl.linux.org/~gerrit/english/




More information about the Python-list mailing list