-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do slices really must have step size of 1? #90
Comments
In the C extension, slices are implemented by just returning a new Items object, just like a plain call to >>> a.keys()
<OOBTreeItems at 0x11084fc40>
>>> a.keys()[1:2]
<OOBTreeItems at 0x10fcc2ee0> That object uses the same underlying bucket storage as the tree, and it doesn't have any place to store the extra state needed to track a different step size, so that's the first problem. More serious, though, is that BTreeItems support indexed subscripting of themselves: >>> bt = OOBTree()
>>> bt['a'] = 1
>>> bt.keys()[0]
'a' They implement iteration using that subscripting support through the old iteration protocol. So the object would have to be changed to understand how to compute "real" indexes based on a different step size, which sounds like it could get ugly. I'll note that the Python implementation just punts the whole problem over to list for any slice: >>> from BTrees.OOBTree import OOBTreePy
>>> a = OOBTreePy({1:"one", 2:"two", 3:"three"})
>>> a.keys()
<BTrees._base._TreeItems object at 0x1026fb858>
>>> a.keys()[1:2]
[2] That loads the whole tree into memory, though, which is not efficient. But it works (for small cases...) and probably wouldn't be that hard to adopt in C if we wanted to. (Either that or the Python implementation should enforce the same restriction.) |
As I found,
OOBTreeItems
cannot be sliced backwards or with a step greater than1
, seemingly due to this line.Is there any plans to develop this feature? Does it require a lot of work?
For the moment, I use workarounds like
reversed(a.keys()[1:2])
to simulate a-1
step, but I suppose it would be more efficient if that was coded in C?The text was updated successfully, but these errors were encountered: