[ZCM] [ZC] 1873/ 4 Comment "PersistentMapping can't be used to
update/construct a BTree"
Collector: Zope Bugs, Features,
and Patches ...
zope-coders-admin at zope.org
Mon Aug 22 10:45:16 EDT 2005
Issue #1873 Update (Comment) "PersistentMapping can't be used to update/construct a BTree"
Status Pending, Database/bug medium
To followup, visit:
http://www.zope.org/Collectors/Zope/1873
==============================================================
= Comment - Entry #4 by tim_one on Aug 22, 2005 10:45 am
More discussion starting here:
http://mail.zope.org/pipermail/zodb-dev/2005-August/009126.html
________________________________________
= Edit - Entry #3 by tim_one on Aug 22, 2005 10:43 am
Changes: submitter email, edited transcript, revised title, new comment
Changed Title; note that PersistentMapping not playing nicely with the iteration prototol is also part of this.
________________________________________
= Comment - Entry #2 by tim_one on Aug 22, 2005 10:09 am
This is so messy it may not get changed. A BTree constructor or update() takes a sequence as argument (in which case it must be a sequence of 2-tuples), or an object with an .items() method. Changing any of that would have a major chance of being backward-incompatible.
PersistentMapping "looks like a sequence", so that's the path that's taken. _Any_ way of using a PersistentMapping as a sequence is likely to blow up the same way. Examples:
>>> from ZODB.PersistentMapping import PersistentMapping
>>> pm = PersistentMapping({1: 2})
>>> for x in pm:
... print x
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\python23\lib\UserDict.py", line 19, in __getitem__
def __getitem__(self, key): return self.data[key]
KeyError: 0
>>> list(pm)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\python23\lib\UserDict.py", line 19, in __getitem__
def __getitem__(self, key): return self.data[key]
KeyError: 0
>>> i = iter(pm)
>>> i.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\python23\lib\UserDict.py", line 19, in __getitem__
def __getitem__(self, key): return self.data[key]
KeyError: 0
The same thing happens if you try to use a PersistentMapping as an argument to a BTree update method too.
Until (if ever) this changes, the way to get what you want is to invoke pm.items(). Like
>>> from BTrees.OOBTree import OOBTree
>>> b = OOBTree()
>>> b.update(pm.items()) # no problem
>>> list(b.items())
[(1, 2)]
________________________________________
= Request - Entry #1 by suvit on Aug 22, 2005 5:49 am
>>> import ZODB
>>> from ZODB.PersistentMapping import PersistentMapping
>>> from BTrees.OOBTree import OOBTree
works
>>> d1 = OOBTree()
>>> d1[1] = 1
>>> pm = PersistentMapping({2:2})
>>> d = {1:1}
>>> bt = OOBTree({3:3})
>>> bt.update(d)
>>> pm.update(bt)
>>> dict(bt)
{1: 1, 3: 3}
But not works
>>> OOBTree(pm)
Traceback (most recent call last):
File "<pyshell#12>", line 1, in ?
OOBTree(pm)
File "C:\Python23\Lib\UserDict.py", line 14, in __getitem__
def __getitem__(self, key): return self.data[key]
KeyError: 0
>>> bt.update(pm)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in ?
bt.update(pm)
File "C:\Python23\Lib\UserDict.py", line 14, in __getitem__
def __getitem__(self, key): return self.data[key]
KeyError: 0
>>>
==============================================================
More information about the Zope-Collector-Monitor
mailing list