[ZODB-Dev] Adopting ZODB
Chris S
chrisspen at gmail.com
Thu Jul 13 12:55:16 EDT 2006
On 7/13/06, Christian Theune <ct at gocept.com> wrote:
> Chris S wrote:
> > I'm trying to adopt some code to use ZODB as its persistence level.
> > I've read the "Writing a Persistent Class" article
> > (http://www.zope.org/Wikis/ZODB/FrontPage/guide/node3.html#SECTION000350000000000000000)
> >
> > but how do you persist a class that already inherits another class?
> > Does ZODB work with multiple inheritence?
>
> Yes.
>
> > Also, how should I persist
> > objects in libraries beyond my control? For example, I'd like to
> > persist instances of pyrobot.brain.conx.Network
> > (http://pyrorobotics.org/?page=Building_20Neural_20Networks_20using_20Conx).
> >
> > Is there any ability in ZODB to retroactively wrap objects in a
> > persistence mechanism instead of having to rewrite an entire library
> > to use the Persistent class?
>
> You can always persist (almost) any object, even if it does not subclass
> from Persistent. However, any changes to the object will not be detected
> automatically and you would have to either a) reassign the object to the
> ZODB or b) mark it as changed using _p_changed()
>
> Christian
I don't think this is the case. Consider my simple example below. None
of my classes inherit Persistent, and even though I set _p_changed =
1, nothing's persisted.
from UserDict import UserDict as dict
from ZODB import FileStorage, DB
import transaction
class User(object):
def __init__(self, name):
self.name = name
self.children = dict()
storage = FileStorage.FileStorage('test.db')
db = DB(storage)
conn = db.open()
root = conn.root()
import random
parentName = 'bob'
parent = root.get(parentName, User(parentName))
print 'before:',parent.children.keys()
for n in xrange(10):
childName = random.randint(1,100)
parent.children[childName] = User(childName)
parent._p_changed = 1
parent.children._p_changed = 1
print 'after:',parent.children.keys()
transaction.commit()
conn.close()
db.close()
storage.close()
More information about the ZODB-Dev
mailing list