[Zope3-dev] performance and BTrees

Tim Peters tim@zope.com
Thu, 1 May 2003 11:00:32 -0400


[Marcus J. Ertl]
> ...
> Is there a way to tell OOBTree, I'm only interested in a slice of it's
> .items()?

Oh yes.  Note that BTrees aren't Python dicts:  BTrees are stored in sorted
order, automatically (this example happens to be under ZODB3):

>>> from BTrees.OOBTree import *
>>> t = OOBTree([('e', 5), ('c', 3), ('b', 2), ('d', 4), ('a', 1)])

Note that the items are automatically sorted by key:

>>> list(t.items())
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]

.items() (and .keys() and .values()) also have optional min and max
arguments:

>>> list(t.items('b'))  # all items with keys >= 'b'
[('b', 2), ('c', 3), ('d', 4), ('e', 5)]

>>> list(t.items('a', 'c'))  # all items with keys >= 'a' and <= 'c'
[('a', 1), ('b', 2), ('c', 3)]
>>>

These are very efficient.

In ZODB4, these methods also have optional boolean excludemin and excludemax
arguments, for when you don't want to include the min and/or max in the
result.