-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiterator.py
More file actions
42 lines (32 loc) · 1.07 KB
/
Copy pathiterator.py
File metadata and controls
42 lines (32 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Iterator that starts again from the beginning after the last item has been reached.
class EndlessIterator:
def __init__(self, collection):
self.step = 1
self.index = 0
self.collection = collection
self.len = len(self.collection) - 1
def __getitem__(self, item):
return self.collection[self.index]
def __next__(self):
return self.next()
def next(self):
el = self.collection[self.index]
if 0 <= self.index + self.step <= self.len:
self.index += self.step
else:
self.index = (self.index + self.step) % self.len + 1
return el
def prev(self):
el = self.collection[self.index]
if 0 <= self.index - self.step <= self.len:
self.index -= self.step
else:
self.index = (self.index - self.step) % self.len + 1
return el
def __iadd__(self, other):
for i in range(0, other):
self.next()
return self
def __add__(self, other):
for i in range(0, other):
self.next()