[Zope-CMF] Persistent dictionaries
Dieter Maurer
dieter@handshake.de
Fri, 24 Aug 2001 23:46:53 +0200 (CEST)
Thomas Olsen writes:
> I'm working on a CMF Product which is based on PortalFolder and
> PortalContent. This Folderish object contains some items which inherits from
> OFS.SimpleItem.Item, Persistence.Persistent and Aquisition.Implicit.
> These items have (had) some properties which are speciel to their class. In
> order to get around the problem with adding new attributes and having
> instantiated objects which doesn't have the attributes I decided to keep the
> attributes in a dictionary. So in my base class I did:
>
> ---snip--------------------------------------------
> class AbstractElement(Persistent, Item, Implicit):
> """ Abstract base class for Element objects. """
> _attributes = PersistentMapping()
>
> def __getattr__(self, name):
> if self._attributes.has_key(name):
> return self._attributes[name]
> else:
> raise AttributeError, name
> ---snip--------------------------------------------
>
> and my derived classes look something like this:
>
> ---snip--------------------------------------------
> class TextElement(AbstractElement):
> """
> text element.
> """
> meta_type = 'Text Element'
>
> def __init__(self, id):
> self.id = id
> self._attributes = {'value':'','html':'','alignment':''}
>From here on "self._attributes" is no longer a PersistentMapping
but a normal Python dictionary.
Internal modifications do not trigger the persistence machinery.
They would, if "_attributes" were still a PersistentMapping....
Dieter