commit 6337fdd331240345a0870418a0b4465bdb88c070
parent 9040a6c6ce1fe7be2ababba96b8ed26e00cd4a76
Author: Chris Bracken <chris@bracken.jp>
Date: Mon, 9 Mar 2020 22:10:25 -0700
Fix indexing in CircularList
Fixes an edge-condition wherein rtk[:0] returned the whole list rather
than an empty slice. This causes random kanji to be returned from
`get_review_kanji()` on day 0.
Also tries to implement reasonable wrapping semantics when a slice wraps
around the end of the list.
Diffstat:
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/.local/lib/python3/kotd/__init__.py b/.local/lib/python3/kotd/__init__.py
@@ -13,11 +13,13 @@ class CircularList:
def __getitem__(self, key):
assert isinstance(key, int) or isinstance(key, slice)
if isinstance(key, slice):
- key = slice(
- key.start % len(self.src) if key.start else None,
- key.stop % len(self.src) if key.stop else None,
- key.step)
- return self.src[key]
+ start = key.start % len(self.src) if key.start is not None else 0
+ stop = key.stop if key.stop is not None else len(self.src)
+ if stop > 0:
+ stop = (stop - 1) % len(self.src) + 1
+ if start <= stop:
+ return self.src[slice(start, stop, key.step)]
+ return self.src[stop + 1:len(self.src)] + self.src[0:start]
return self.src[key % len(self.src)]
rtk = CircularList("""