faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 3 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

UserFile module

Jul 5th, 2000 09:59
Nathan Wallace, Hans Nowak, Snippet 70, Neelakantan Krishnaswami


"""
Packages: files
"""
"""
Since I've complained about it, I figured that I had an obligation 
to try providing a workaround: here's a small wrapper class around 
file objects, written in Python. Now you can subclass file objects
more or less with impunity. 
The "more-or-less" stems from the fact that the UserFile currently 
uses the getattr/setattr methods to allow transparent access to the 
underlying file object's attributes. This may cause bizarre errors 
if the user tries to write their own getattr or setattr methods in 
subclasses. Ideally, I'd like someone to figure out a clever way to 
avoid the call altogether, but I'd settle for discovering that it's
impossible. :)
I don't have any easily-accessible web space right now, so I'm 
posting it to the newsgroup -- apologies if it is too long.
Neel
-*-*-*- UserFile.py -*-*-*-
"""
# Name:         UserFile.py
# Purpose:      To provide a class wrapper around file objects.
# License info: X-ish. IOW, go wild, as long as you don't sue me.
#
# 15-Feb-1999   Created. (NK)
#
# TODO:
#
#  UserFile currently uses the getattr/setattr methods to allow
#  transparent access to the underlying file object's attributes. This may
#  cause bizarre errors if the user tries to write their own getattr or
#  setattr methods in subclasses. Ideally, I'd like someone to figure out
#  a clever way to avoid the call altogether, but I'd settle for
#  discovering that it's impossible. :)
#
#  Also, this code is a quick hack, so testing has been quite cursory.
#  Some comprehensive testing would be good; I will try to work up a test
#  suite this weekend. 
class UserFile:
    __file_attributes = ('closed','mode','name','softspace')
    def __init__(self, *args):
        self.fd = apply(open, args)
    def close(self):
        return self.fd.close()
    def flush(self):
        return self.fd.flush()
    def isatty(self):
        return self.fd.isatty()
    def fileno(self):
        return self.fd.fileno()
    def read(self, *args):
        return apply(self.fd.read, args)
    def readline(self, *args):
        return apply(self.fd.readline, args)
    def readlines(self, *args):
        return apply(self.fd.readlines, args)
    def seek(self, *args):
        return apply(self.fd.seek, args)
    def tell(self):
        return self.fd.tell()
    def write(self, str):
        return self.fd.write(str)
    def writelines(self, list):
        return self.fd.writelines(list)
    def __repr__(self): 
        return repr(self.fd)
    def __getattr__(self, name):
        if name in self.__file_attributes:
            return getattr(self.fd, name)
        else:
            return self.__dict__[name]
    def __setattr__(self, name, value):
        if name in self.__file_attributes:
            setattr(self.fd, name, value)
        else:
            self.__dict__[name] = value
    def __cmp__(self, file):
        """I'm not sure what the correct behavior is, and therefore 
        this implementation is just a guess."""
        if type(file) == type(self.fd):
            return cmp(self.fd, file)
        else:
            return cmp(self.fd, file.fd)