[Zope3-dev] Do not serialize one attribute

Jeff Shell eucci.group at gmail.com
Thu Dec 14 18:45:11 EST 2006


On 12/14/06, Jeff Shell <eucci.group at gmail.com> wrote:
> On 12/14/06, Florian Lindner <mailinglists at xgm.de> wrote:
>
> > Hello, I have a object, derived from Persistent when contain one
> > attribute which is not serializable and that's not a problem for me,
> > but Zope complains about that.  Can I mark this attribute somehow as
> > "not to serialize" and make Zope call a member function when it has
> > unserialized this object so I can reinstantiate this attribute?
>
> What do you need to do to re-instatiate the attribute? As another
> reply mentioned, you can use the Python property descriptor in
> combination with an `_v_` attribute. The ZODB will not serialize an
> attribute whose name starts with _v_ (v means "volatile").

Is this the IJabberClient object you were asking about a couple of
weeks ago? I just saw that thread today. Anyways, the _v_ option is
probably what you want if that is the problem.

    class JabberClient(Persistent):
        ... (other code) ...

        @property
        def client(self):
            if not hasattr(self, '_v_client'):
                self._v_client = xmpp.Client(...)
            return self._v_client


You could try using this descriptor as well (something like this might
already exist. If not, perhaps it should? This is a simple
implementation, but it should work)

    class volatile(object):
        """ A descriptor that reads and writes to a volatile attribute """
        def __init__(self, name):
            self._volatilename = '_v_%s' % name

        def __get__(self, klass, inst):
            if inst is None:
                # return this descriptor if accessed on the class
                return self

            return getattr(inst, self._volatilename, None)

        def __set__(self, inst, value):
            """ Set the value on the instance in a volatile attribute """
            setattr(inst, self._volatilename, value)

        def __del__(self, inst):
            """ Clear the volatile attribute """
            delattr(inst, self._volatilename)

And here's how it would be used.

    class JabberClient(Persistent):
        ... (other code) ...

        client = volatile('client')


-- 
Jeff Shell


More information about the Zope3-dev mailing list