RE: [ZODB-Dev] Dynamic Wrapper?
[Chris Spencer] > Instead of requiring all persistable objects to inherit a special class, > wouldn't it be possible to dynamically wrap a class's __setattr__ and/or > __setitem__ methods to determine when an object's been modified? I'm not clear on what you're trying to accomplish, but any persistence system needs to do a lot more than just note when an attribute has changed. For example, it also needs ways to avoid loading the transitive closure of all persistent state reachable from a persistent object P when P is loaded, and to materialize reachable state as needed as attributes of P get referenced; it needs ways to manage caches of persistent objects; it needs a way to associate unique identifiers (object ids; oids) with persistent objects; it needs to associate objects with the connections from which they were loaded; etc. .. [snipped implementation sketch, because I don't understand its goal] .. > I understand this has some drawbacks. Namely, it will only work for > new-style classes, but for a large code base this might be easier than > manually writing _p_changed = 1 everywhere. If that's the goal, you may be alone in caring about it ;-): manual fiddling of _p_changed is rarely needed outside the _implementation_ of persistence. If you find yourself doing it a lot, there may be a flaw in your design, or in your mental model of how persistence in ZODB actually works. ___ For more information about ZODB, see the ZODB Wiki: http://www.zope.org/Wikis/ZODB/ ZODB-Dev mailing list - ZODB-Dev@zope.org http://mail.zope.org/mailman/listinfo/zodb-dev
Re: [ZODB-Dev] Dynamic Wrapper?
Chris Spencer wrote: I understand this has some drawbacks. Namely, it will only work for new-style classes, but for a large code base this might be easier than manually writing _p_changed = 1 everywhere. The number of times you end up actually having to write this is pretty minimal ;-) Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk ___ For more information about ZODB, see the ZODB Wiki: http://www.zope.org/Wikis/ZODB/ ZODB-Dev mailing list - ZODB-Dev@zope.org http://mail.zope.org/mailman/listinfo/zodb-dev
[ZODB-Dev] Dynamic Wrapper?
Instead of requiring all persistable objects to inherit a special class, wouldn't it be possible to dynamically wrap a class's __setattr__ and/or __setitem__ methods to determine when an object's been modified? Something like: class Monitor(object): class Proxy(object): def __init__(self, obj): self.obj = obj def __hash__(self): return id(self.obj) def __cmp__(self, p): return cmp(id(self.obj),id(p.obj)) def __init__(self): self.modified = set() def add(self, obj): def newSetter(func): def registerChanges(theirself, *args, **kwargs): self.modified.add(self.Proxy(theirself)) return func(theirself, *args, **kwargs) return registerChanges if hasattr(obj, '__setattr__'): obj.__class__.__setattr__ = newSetter(obj.__class__.__setattr__) if hasattr(obj, '__setitem__'): obj.__class__.__setitem__ = newSetter(obj.__class__.__setitem__) m = Monitor() from UserDict import UserDict d = UserDict() m.add(d) d[123] = 'abc' print m.modified >>> set([<__main__.Proxy object at 0xb7f28acc>]) I understand this has some drawbacks. Namely, it will only work for new-style classes, but for a large code base this might be easier than manually writing _p_changed = 1 everywhere. Chris ___ For more information about ZODB, see the ZODB Wiki: http://www.zope.org/Wikis/ZODB/ ZODB-Dev mailing list - ZODB-Dev@zope.org http://mail.zope.org/mailman/listinfo/zodb-dev