[ZODB-Dev] Is any function called when an object is loaded from the database?

Marius Gedminas marius at gedmin.as
Tue Jun 19 18:50:55 UTC 2012


On Tue, Jun 19, 2012 at 01:54:21PM -0400, Claudiu Saftoiu wrote:
> Ideally I could do something like this:
> 
>     class Foo(Persistent):
>         def __init__(self, a):
>             self.a = a
>             self.b_cache = PersistentDict()
> 
>         def __just_loaded__(self):
>             if not hasattr(self, 'b_cache'): self.b_cache = PersistentDict()
> 
>         def calc_it(self, b):
> 
>             if b in self.b_cache: return self.b_cache[b]
>             res = expensive_function(a, b)
>             self.b_cache[b] = res
>             return res
> 
> That is, a function called whenever the object is loaded, that does
> all the necessary backwards-compatibility work right there. It
> separates the backwards-compat code cleanly, and also only updates the
> objects as-needed... though still a minor performance hit as it does
> the check each time the object is loaded.
> 
> Is there a way to do that last option?

Yes, you can override __setstate__:

    def __setstate__(self, state):
        super(Foo).__setstate__(self, state)
        if not hasattr(self, 'b_cache'):
            self.b_cache = PersistentDict()

This assumes you haven't been defining other pickling protocol
functions like __reduce__, and so cPickle -- which is the foundation
of ZODB -- uses the usual __getstate__/__setstate__ pair.

> What's the best practice for this sort of thing, in general?

It depends. :-)

If the new attribute is immutable, you can set it as a class attribute.

If it's easy to locate all Foo objects in your DB, you may want to write
a generation script (see http://pypi.python.org/pypi/zope.generations)
and do the migration once, instead of paying the cost of checking for
missing attributes on every single object load.

It's also best to avoid write-on-read semantics, because those tend to
cause database growth and increase the chances of getting ConflictErrors.

Marius Gedminas
-- 
An expert is a person who has made all the mistakes that can be made in a very
narrow field.
        -- Niels Bohr
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://mail.zope.org/pipermail/zodb-dev/attachments/20120619/8f10105a/attachment.sig>


More information about the ZODB-Dev mailing list