[ZODB-Dev] BTrees and setdefault

Dieter Maurer dieter at handshake.de
Sun Aug 28 16:40:00 EDT 2005


Michel Pelletier wrote at 2005-8-27 11:45 -0700:
>> > http://mail.zope.org/pipermail/zodb-dev/2004-September/008034.html
>> > http://mail.zope.org/pipermail/zodb-dev/2004-October/008040.html
>> 
>> Dmitry, it's nearly a year since you first suggested this.  Have you missed
>> these methods in practice (i.e., do you have use cases), or it still just a
>> matter of making BTrees formally "more like" newer Python dictionaries?
>
>I've had a use for it, porting existing dictionary code to a BTree.  It
>was reasonably easy to simulate but did make the code slightly less
>readable to the person who originally wrote the code with setdefault.

Now, that "BTrees" are Python new style classes,
you can easily derive your own class from them and give
them a "setdefault" (if you really need it).

>>> from BTrees.IIBTree import IIBTree
>>> class myIIBTree(IIBTree):
...   def setdefault(self, k, value):
...     v = self.get(k, self)
...     if v is self: v = self[k] = value
...     return v
...
>>> i=myIIBTree()
>>> i.setdefault(1,1)
1
>>> i.setdefault(1,2)
1

-- 
Dieter


More information about the ZODB-Dev mailing list