Semi-persistent objects, volatile attributes
Is it possible to have persistent objects with attributes that are only persistent in memory? Effectively, an object that is always cached, for volatile attributes to be preserved during the Zope process' execution. Is there a way for an object to load itself permanently into the cache, say? I figure I could store the attributes in the product module as part of a dictionary keyed by the object instance, but this feels very dirty. Speaking of volatile attributes, is there an idiom for handling these? Since they're not stored, and __init__() is only ever called when the object is initially created (not persistently loaded), it seems you have to verify their existence, something like: def my_method(self): self._v_foo = (hasattr(self, '_v_foo') and self._v_foo) or '<default>' ...ad tedium if you have lots of methods. Does the persistence engine support an "onload" mechanism which you can override to know when your instance has been loaded and needs to initialize itself? Pointers to appropriate documentation would be nice. I've been rummaging through the ZODB UML stuff and while it's enlightening, the documentation is also quite fragmented. -- Alexander Staubo http://www.mop.no/~alex/ "Give me an underground laboratory, half a dozen atom smashers and a beautiful girl in a diaphanous veil waiting to be turned into a chimpanzee, and I care not who writes the nation's laws." --S. J. Perelman
Alexander Staubo wrote:
Is it possible to have persistent objects with attributes that are only persistent in memory?
Yes, but it takes some work and the mechanism need to be formalized. Would you want to specify this on a per-class or per-instance basis?
Effectively, an object that is always cached, for volatile attributes to be preserved during the Zope process' execution. Is there a way for an object to load itself permanently into the cache, say?
ditto
I figure I could store the attributes in the product module as part of a dictionary keyed by the object instance, but this feels very dirty.
Right.
Speaking of volatile attributes, is there an idiom for handling these? Since they're not stored, and __init__() is only ever called when the object is initially created (not persistently loaded), it seems you have to verify their existence, something like:
def my_method(self): self._v_foo = (hasattr(self, '_v_foo') and self._v_foo) or '<default>'
This is one way.
...ad tedium if you have lots of methods.
Yup.
Does the persistence engine support an "onload" mechanism which you can override to know when your instance has been loaded and needs to initialize itself?
Yup, it's called __setstate__. __setstate__ is called when the object state is loaded. You can overload __setstate__ and initialize any volitile variables when you load your state: class C: def __setstate__(self, state): C.inheritedAttribute('__setstate__')(self, state) self._v_spam=..... Jim -- Jim Fulton mailto:jim@digicool.com Python Powered! Technical Director (888) 344-4332 http://www.python.org Digital Creations http://www.digicool.com http://www.zope.org Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email address may not be added to any commercial mail list with out my permission. Violation of my privacy with advertising or SPAM will result in a suit for a MINIMUM of $500 damages/incident, $1500 for repeats.
participants (2)
-
Alexander Staubo -
Jim Fulton