Le 16/09/2011 10:07, Stéphane Klein a écrit :
> Le 30/01/2011 12:12, Vincent Pelletier a écrit :
>> Le dimanche 30 janvier 2011 09:52:32, Stéphane Klein a écrit :
>>> How can I append Persistent mechanism to this OrderedDict class ?
>> You should probably read the code for PersistentMapping[1] and adapt it to
>> OrderedDict's API.
>> [1]
>> http://svn.zope.org/ZODB/trunk/src/persistent/mapping.py?rev=113734&view=markup
> Hi,
> I'll try that :
>       from collections import OrderedDict
>       from persistent import Persistent
>       class PersistentOrderedDict(OrderedDict, Persistent):
>           pass
> but I've this error :
>       TypeError: Error when calling the metaclass bases
>           multiple bases have instance lay-out conflict
> Then, I can't do like "persistent.mapping.PersistentMapping"
>       class PersistentMapping(UserDict.IterableUserDict,
> persistent.Persistent)"
> I need to create
> PersistentOrderedDict that need to be a "proxy" class over OrderedDict,
> and a child on Persistent ?

I've just created this class, what do you about ?

from collections import OrderedDict
from persistent import Persistent

class PersistentOrderedDict(Persistent):
     def __init__(self, *args, **kwds):
         self.__data = OrderedDict(*args, **kwds)

     def __setitem__(self, key, value, PREV=0, NEXT=1, 
         res =  self.__data.__setitem__(key, value, PREV=0, NEXT=1, 
         self._p_changed = 1
         return res

     def __delitem__(self, key, PREV=0, NEXT=1, 
         res = self.__data.__delitem__(key, PREV=0, NEXT=1, 
         self._p_changed = 1
         return res

     def __iter__(self, NEXT=1, KEY=2):
         return self.__data.__iter__(NEXT=1, KEY=2)

     def __reversed__(self, PREV=0, KEY=2):
         return self.__data.__reversed__(PREV=0, KEY=2)

     def __reduce__(self):
         return self.__data.__reduce__()

     def clear(self):
         return self.__data.clear()

     def setdefault(self, key, default=None):
         if key in self.__data:
             return self.__data[key]

         self.__data[key] = default
         self._p_changed = 1
         return default

     def update(self, other):
         res = self.__data.update(other)
         self._p_changed = 1
         return res

     def pop(self, key, default=None):
         res = self.__data.pop(key, default)
         self._p_changed = 1
         return res

     def keys(self):
         return self.__data.keys()

     def values(self):
         return self.__data.values()

     def items(self):
         return self.__data.items()

     def iterkeys(self):
         return self.__data.iterkeys()

     def itervalues(self):
         return self.__data.itervalues()

     def iteritems(self):
         return self.__data.iteritems()

     def __ne__(self, other):
         return self.__ne__(other)

     def viewkeys(self):
         return self.__data.viewkeys()

     def viewvalues(self):
         return self.__data.viewvalues()

     def viewitems(self):
         return self.__data.viewitems()

     def popitem(self, last=True):
         return self.__data.popitem(last=last)

     def __repr__(self):
         return self.__data.__repr__()

     def copy(self):
         return self.__data.copy()

     def fromkeys(cls, iterable, value=None):
         return OrderedDict.fromkeys(iterable, value)

     def __eq__(self, other):
         return self.__data.__eq__(other)

Stéphane Klein <steph...@harobed.org>
blog: http://stephane-klein.info
Twitter: http://twitter.com/klein_stephane
pro: http://www.is-webdesign.com

For more information about ZODB, see the ZODB Wiki:

ZODB-Dev mailing list  -  ZODB-Dev@zope.org

Reply via email to