[ZODB-Dev] RE: B-Tree API - again!

Gfeller Martin Martin.Gfeller@comit.ch
Fri, 6 Dec 2002 17:10:02 +0100


Thanks, Steve and Christian,

this is of course what I've tried initially, and so I wrote in my
original posting (1 Dec,
http://lists.zope.org/pipermail/zodb-dev/2002-December/003890.html):

> [...] but I can neither inherit from OOBTree (del fails, the
__delitem__ method isn't inherited) nor easily delegate to it (the
special methods such as __delitem__ are not exposed).

So using Steve's MyOOBTree class on Python 2.1.3:

>>> bt =3D MyOOBTree()
>>> bt['a'] =3D 1
>>> del bt['a']
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
AttributeError: __delitem__

The alternative is delegation, but this would involve indirection
through a Persistent object, which I think is clumsy.

Best regards, Martin


-----Original Message-----
From: Steve Alexander [mailto:steve@cat-box.net]=20
Sent: Friday, 06 Dec 2002 17:52
To: Gfeller Martin
Cc: zodb-dev@zope.org; Anderegg Hansjoerg; Jeremy Hylton
Subject: Re: [ZODB-Dev] RE: B-Tree API - again!


Gfeller Martin wrote:
> Sorry to be re-iterate my question, but all the interesting answers
> do not address our concern, namely to have a compatible interface
> between B-Tree and PersistentMapping/dictionary.
>=20
> Our aim is to replace PersistentMappings by OOBTrees in a large body
> of existing code, so we would profit from a do-nothing sort() method
> on the OOBTreeItems object returned by keys().

I suggest you write your own subclass of OOBTree that returns an=20
OOBTreeItems, like this (untested):

class MyOOBTree(OOBTree):

     def keys(self, min=3DNone, max=3DNone):
         return MyOOBTreeItems(OOBTree.keys(min, max))

class MyOOBTreeItems(object):

     __slots__ =3D ['__items']

     def __init__(self, items):
         self.__items =3D items

     def __getitem__(self, index):
         return self.__items[index]

     def __getslice__(self, start, end):
         return self.__items[start:end]

     def __len__(self):
         return len(self.__items)

     def __nonzero__(self):
         return self.__items.__nonzero__

     def sort(self):
         # XXX log deprecation warning
         pass


--
Steve Alexander