[Zope-dev] Announce: Persistent list class

Andrew Wilcox circle@gwi.net
Mon, 14 Feb 2000 10:53:48 -0500


Here's a small but useful utility class that implements persistent lists.
It recognizes when it has been modified, and triggers the ZODB persistence
mechanism.

I also threw in a PersistentDict class that implements persistent
dictionaries.  This is very similar to the PersistentMapping that comes
with Zope, but is a little more straightforward.  It doesn't restrict keys
to be strings, and doesn't hide keys that begin with an underline.

Code is available at
http://www.zope.org/Members/AndrewWilcox/PersistentListDict

As an example, here's a class that uses a regular Python list.  Because
the ZODB doesn't know when the list has been modified automatically,
the programmer needs to trigger persistence by setting one of the
object's attributes.

    class Myclass (Persistent):
        def __init__(self):
            self.l = [1, 2, 3, 4, 5, 6]

        def act(self):
            l = self.l
            l[2:4] = [33, 37, 39, 41, 44]
            self.l = l


By using a PersistentList, the ZODB will know when the list has been changed.

    class Myclass (Persistent):
        def __init__(self):
            self.l = PersistentList([1, 2, 3, 4, 5, 6])

        def act(self):
            self.l[2:4] = [33, 37, 39, 41, 44]


I am experiencing a bug in Zope 2.1.4 where the comparision method,
__cmp__, is not getting called consistently in Persistent subclasses.
Don't know what that it about.  If you need to compare persistent lists,
use list() to get you a regular list and compare on that.