-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
33 lines (25 loc) · 826 Bytes
/
utils.py
File metadata and controls
33 lines (25 loc) · 826 Bytes
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
class groupby:
def __init__(self, predicate, iterable):
self.predicate = predicate
self.it = iter(iterable)
self.curr_val = next(self.it, None)
def __iter__(self):
return self
def __next__(self):
if self.curr_val:
return self._grouper()
else:
raise StopIteration
def _grouper(self):
while self.curr_val:
yield self.curr_val
try:
next_val = next(self.it, None)
if not next_val or not self.predicate(self.curr_val, next_val):
self.curr_val = next_val
break
self.curr_val = next_val
except StopIteration:
return
def slice_len(offset, length):
return slice(offset, offset + length)