Skip to content

Commit 9ef5749

Browse files
committed
reslicing
1 parent 8bb2a7f commit 9ef5749

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

timetest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def test_to_int(self):
201201
def test_repr(self):
202202
self.assertEqual("Week", repr(Week))
203203
self.assertEqual("Week[102123:105341:]", repr(Week[102123:105341:]))
204+
self.assertEqual("Week[102123:105341:1]", repr(Week[102123:105341:][1:1:1]))
204205

205206
def test_hierarchy(self):
206207
"""

unit_of_time/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def __getitem__(self, key):
4242
idc = range(len(self))[key]
4343
if isinstance(idc, int):
4444
return self._from_index(idc)
45+
elif isinstance(self, SlicedProxy) and isinstance(key, slice):
46+
return SlicedProxy(self.parent, self.sliced_range(key))
4547
else:
4648
return SlicedProxy(self, key)
4749

@@ -53,13 +55,18 @@ def __init__(self, parent, _slice: slice):
5355

5456
def __eq__(self, other):
5557
if isinstance(other, SlicedProxy):
58+
# by using the range object, we check if the indexes are the same
5659
return self.parent == other.parent and self.range_object == other.range_object
5760
return super().__eq__(other)
5861

5962
@property
6063
def range_object(self):
6164
return range(len(self.parent))[self._slice]
6265

66+
def sliced_range(self, _slice: slice):
67+
new_range = self.range_object[_slice]
68+
return slice(new_range.start, new_range.stop, new_range.step)
69+
6370
def __iter__(self):
6471
for idx in self.range_object:
6572
yield self.parent[idx]

0 commit comments

Comments
 (0)