Re: [ZODB-Dev] How can I append Persistent mechanism to OrderedDict class ?

2011-09-16 Thread Stéphane Klein
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=113734view=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 ?

Regards,
Stephane

-- 
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:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zodb-dev


Re: [ZODB-Dev] How can I append Persistent mechanism to OrderedDict class ?

2011-09-16 Thread Stéphane Klein
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=113734view=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, 
dict_setitem=dict.__setitem__):
 res =  self.__data.__setitem__(key, value, PREV=0, NEXT=1, 
dict_setitem=dict.__setitem__)
 self._p_changed = 1
 return res

 def __delitem__(self, key, PREV=0, NEXT=1, 
dict_delitem=dict.__delitem__):
 res = self.__data.__delitem__(key, PREV=0, NEXT=1, 
dict_delitem=dict.__delitem__)
 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()

 @classmethod
 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:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
https://mail.zope.org/mailman/listinfo/zodb-dev