[orm-devel] dbclass - __getattr__

Michael Watkins orm-devel@mailman.tux4web.de
Wed, 27 Aug 2003 02:00:39 +0000


Greetings ORM folks. A minor patch request to allow ORM objects to be
pickled. Note, I have not considered the implications of this at all,
but thought I would put this out there to spark a discussion.

Why pickle? I have a web Session object which has a user attribute which
is an object containing other objects and collections of objects. 
(Person, Subscriptions, Groups, etc). It would be handy to save this
Session object in the db to restore state between web requests without
re-querying the database.

I suppose the biggest issue is that when unpickled, both the database
connection, cursor and associated objects may have changed radically in
the interim. The database may have been stopped. Connections broken.
Etc. And how would the objects know this without requerying...

So, the change is not required, in the end, to address the dead end I
was walking down.

But, this very quick process of trying something and deciding it was
foolish did raise one thing - because __getattr__ has been overridden,
some standard python library code (like pickle) may fail because it
expects the presence or absence of an attribute (and thus tests for
AttributeError) rather than a key.

Hope everyone is having a good summer...
Mike



in dbclass, change
    def __getattr__(self, name):
        """
        The __getattr__ method is overwriten to call each column's get
        method when an attribute is requested. Attributes that start
        with a _ are ignored.
        """
        if name[0] == "_": self.__dict__[name]

to:

    def __getattr__(self, name):
        """
        The __getattr__ method is overwriten to call each column's get
        method when an attribute is requested. Attributes that start
        with a _ are ignored.
        """

        if name[0] == "_": 
            try:
                return self.__dict__[name]
            except KeyError:
                # remapped the error so that getattr (was failing on
__getstate__) works in pickle and other standard Python library
functions
                raise AttributeError