[Zope-dev] Zope/Python Object Persistence

Chris McDonough chrism@digicool.com
Mon, 01 May 2000 23:05:14 -0400


Chimezie is right... one clarification:  you don't necessarily need to
make the counter an attribute of the object the getNextCount() method is
implemented on, but it needs to be an attribute of some object that
sublclasses Persistence.Persistent (any standard Zope object).

Note also that you will undoubtedly be bitten at some point during the
writing of code in Zope by the somewhat unobvious fact that you need to
treat mutable Python objects that are attributes of persistent objects
as immutable.  So instead of (for example):

self.a[0] = b

You'd need to do something like:

tmp = self.a
tmp[0] = b
self.a = tmp

Doing this triggers the persistence machinery.  A different, short-hand
way to do this is:

self.a[0] = b
self.a._p_changed=1


Chimezie Thomas-Ogbuji wrote:
> 
> If you make the dictionary an attribute of the zope object the
> getNextCount() method is called on, it should be pickled automatically
> by ZODB. You'll need this logic in the beggining of getNextCount:
> 
> if (attribute exists):
>         increment count attribute
> else:
>         set attribute to default value (0) and increment
> 
> This solution assumes you want a per-object persistent counter. If what
> you want is a class-wide counter (common to all instances) then I'm not
> sure how you'd do this besides:
> 1) using ZPickle product (might be overkill)
> 2) using Persistent List product (might be more appropriate)
> 
> > Adam Pawliuk wrote:
> >
> > Hi,
> >
> > Is there a way that I can maintain persistent python objects in
> > memory through the Zope Server?
> >
> > As a simple example if I create a counter in a python module and
> > access it through an external method, the module seems to get reloaded
> > every so often, so the counter will get reset. Can I avoid this?
> > Basically I just want to keep a persistent hashtable/Dictionary.
> >
> > MODULE:
> > *************************************************************
> > __persist = {};
> > __persist['count'] = 0;
> >
> > def getNextCount():
> >    __persist['count'] = int( __persist['count'] )+1
> >    return __persist['count']
> > ************************************************************
> > External method in DTML:
> >
> > <dtml-var "getNextCount()">
> >
> >
> > thanks in advance
> >
> 
> _______________________________________________
> Zope-Dev maillist  -  Zope-Dev@zope.org
> http://lists.zope.org/mailman/listinfo/zope-dev
> **  No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope )

-- 
Chris McDonough
Digital Creations
Publishers of Zope - http://www.zope.org