Skip to content

Commit 7736044

Browse files
committed
fixed __getitem__(slice) for viewer._LazyDimLabels and added doctest
also made __iter__ lazy (which is the whole point of the class)
1 parent bbe2c45 commit 7736044

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

larray/viewer.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,15 +1678,44 @@ def __len__(self):
16781678

16791679

16801680
class _LazyDimLabels(object):
1681+
"""
1682+
Examples
1683+
--------
1684+
>>> p = Product([['a', 'b', 'c'], [1, 2]])
1685+
>>> list(p)
1686+
[('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)]
1687+
>>> l0 = _LazyDimLabels(p, 0)
1688+
>>> l1 = _LazyDimLabels(p, 1)
1689+
>>> for i in range(len(p)):
1690+
... print(l0[i], l1[i])
1691+
a 1
1692+
a 2
1693+
b 1
1694+
b 2
1695+
c 1
1696+
c 2
1697+
>>> l0[1:4]
1698+
['a', 'b', 'b']
1699+
>>> l1[1:4]
1700+
[2, 1, 2]
1701+
>>> list(l0)
1702+
['a', 'a', 'b', 'b', 'c', 'c']
1703+
>>> list(l1)
1704+
[1, 2, 1, 2, 1, 2]
1705+
"""
16811706
def __init__(self, prod, i):
16821707
self.prod = prod
16831708
self.i = i
16841709

16851710
def __iter__(self):
1686-
return iter(self.prod[:][self.i])
1711+
return iter(self.prod[i][self.i] for i in range(len(self.prod)))
16871712

16881713
def __getitem__(self, key):
1689-
return self.prod[key][self.i]
1714+
key_prod = self.prod[key]
1715+
if isinstance(key, slice):
1716+
return [p[self.i] for p in key_prod]
1717+
else:
1718+
return key_prod[self.i]
16901719

16911720
def __len__(self):
16921721
return len(self.prod)

0 commit comments

Comments
 (0)