[Zope] Re: sorry to bother you again

Chris McDonough chrism@digicool.com
Fri, 8 Dec 2000 09:10:50 -0500


----- Original Message -----
From: "Matt" <matt.bion@eudoramail.com>
To: "Chris McDonough" <chrism@digicool.com>
Sent: Friday, December 08, 2000 4:46 AM
Subject: sorry to bother you again

No problem... I'd like to keep it on the mail list, so other folks can get
the benefit.

> Chris,
> I found the following in the mailing lists, it was one of your replies to
a
> persistence question, I think it could finally clarify some things for me.
>
> Now if I understand what you have been saying in the following and before
then
> if I used self.a[0] = b then this will only be committed if I had called
the
> function that calls this within my product.  If I called the function
outside
> of this, say using a dtmlMethod or an external method, then it would not
> persist and instead I should really use
>
>                 tmp = self.a
>                 tmp[0] = b
>                 self.a = tmp

The main question to ask yourself when trying to persist things is "is the
__setattr__ of a persistent object getting called"?  In the above example,
the answer is yes.

> I feel I am missing something.  The boringplus product I made uses
self.a[0] =
> b type referencing, but on a dictionary, and works well.  Where would my
> product fail?

It would not show your mutations of the 'self.a' dictionary:

  - after the server is stopped and restarted

    -or-

  - after the object referenced by 'self' was flushed from the RAM
    cache during normal operations



>
> thanks for your help
> regards
> Matt
>
>
>                       Message 29479 of 46695
>
>                                                    [ Reply ]
>                                                          [ Forward ]
>                                                                    [ View
> Source ]
>
>
>
>
>                 From: Chris McDonough  <chrism@d...>
>                 Date: Tue May 2, 2000 3:05am
>                 Subject: Re: [Zope-dev] Zope/Python Object Persistence
>
>
>                 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
>                 > >
>                 >
>
>