wishlist: more general file objects, and some doc improvements

Evan Simpson evan at tokenexchange.com
Wed May 5 13:16:45 EDT 1999


Here's a quick&dirty implementation; It just fetches all attribute access
other than 'open' and 'name' from os.path and curries the result so that it
will get the name as the first argument when called.

class Pathname:
    class _Curried:
        def __init__(self, pn, f):
            self._pn = pn
            self._f = f
        def __call__(self, *args, **kargs):
            return apply(self._f, (self._pn.name,) + args, kargs)
    def __init__(self, name):
        self.name = name
        import os
        self._path = os.path
    def open(self, mode):
        return open(self.name, mode)
    def __getattr__(self, attr):
        return self._Curried(self, getattr(self._path, attr))


>>> pn = Pathname('/home/me')
>>> pn.exists()
1
>>> pn.isfile()
0
>>> pn.name = pn.join('myfile')
>>> pn.name
'/home/me/myfile'
>>> pn.isfile()
1
>>> pn.open('r').read()
'Hi!'
>>> pn.getsize()
3

would-you-care-for-some-dessert-with-that-curry?-ly yr's
Evan






More information about the Python-list mailing list