Box
Box copied to clipboard
Add support for accessing nested items in BoxList using numpy-style tuple indexing.
Allow BoxList to use numpy-style tuple indexing, similar to l[0,1,2,3]
, which is equivalent to l[0][1][2][3]
.
@cdgriffith Could you approve the workflow review, please?
This would need a new feature flag if added. As tuples are supported dictionary characters themselves.
Consider:
from box import Box
bx = Box({0: {1: {2: {3: 3}}}, (0, 1, 2, 3): 4})
print(bx[0,1,2,3])
# 4
I have implemented this feature only in the BoxList
class, whereas Box
is a different class and will still output normally. Using the following code in the unit tests is not a problem:
bx = Box({0: {1: {2: {3: 3}}}, (0, 1, 2, 3): 4})
assert bx[0, 1, 2, 3] == 4
But I will add some error messages to make it more user-friendly.
My code only works for list
and its subclasses. The first layer must be BoxList
, and dict
and its subclasses cannot appear in the middle.
@cdgriffith Do you think there are any other problems with the code?
I don't know how to program with PyPy, and I can't provide an equivalent version of it.
Sorry for delay, I like the addition!
The pypy things is failing even on master as of now, need to sort that out so no something with this code.