forked from idank/explainshell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
145 lines (134 loc) · 3.72 KB
/
util.py
File metadata and controls
145 lines (134 loc) · 3.72 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import itertools
from operator import itemgetter
def consecutive(l, fn):
'''yield consecutive items from l that fn returns True for them
>>> even = lambda x: x % 2 == 0
>>> list(consecutive([], even))
[]
>>> list(consecutive([1], even))
[[1]]
>>> list(consecutive([1, 2], even))
[[1], [2]]
>>> list(consecutive([2, 4], even))
[[2, 4]]
>>> list(consecutive([1, 2, 4], even))
[[1], [2, 4]]
>>> list(consecutive([1, 2, 4, 5, 7, 8, 10], even))
[[1], [2, 4], [5], [7], [8, 10]]
'''
it = iter(l)
ll = []
try:
while True:
x = next(it)
if fn(x):
ll.append(x)
else:
if ll:
yield ll
ll = []
yield [x]
except StopIteration:
if ll:
yield ll
def groupcontinuous(l, key=None):
'''
>>> list(groupcontinuous([1, 2, 4, 5, 7, 8, 10]))
[[1, 2], [4, 5], [7, 8], [10]]
>>> list(groupcontinuous(range(5)))
[[0, 1, 2, 3, 4]]
'''
if key is None:
key = lambda x: x
for k, g in itertools.groupby(enumerate(l), lambda i_x: i_x[0]-key(i_x[1])):
yield list(map(itemgetter(1), g))
def toposorted(graph, parents):
"""
Returns vertices of a DAG in topological order.
Arguments:
graph -- vetices of a graph to be toposorted
parents -- function (vertex) -> vertices to preceed
given vertex in output
"""
result = []
used = set()
def use(v, top):
if id(v) in used:
return
for parent in parents(v):
if parent is top:
raise ValueError('graph is cyclical', graph)
use(parent, v)
used.add(id(v))
result.append(v)
for v in graph:
use(v, v)
return result
def pairwise(iterable):
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
class peekable(object):
'''
>>> it = peekable(iter('abc'))
>>> it.index, it.peek(), it.index, it.peek(), it.next(), it.index, it.peek(), it.next(), it.next(), it.index
(0, 'a', 0, 'a', 'a', 1, 'b', 'b', 'c', 3)
>>> it.peek()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
>>> it.peek()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
>>> it.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
'''
def __init__(self, it):
self.it = it
self._peeked = False
self._peekvalue = None
self._idx = 0
def __iter__(self):
return self
def __next__(self):
if self._peeked:
self._peeked = False
self._idx += 1
return self._peekvalue
n = next(self.it)
self._idx += 1
return n
def hasnext(self):
try:
self.peek()
return True
except StopIteration:
return False
def peek(self):
if self._peeked:
return self._peekvalue
else:
self._peekvalue = next(self.it)
self._peeked = True
return self._peekvalue
@property
def index(self):
'''return the index of the next item returned by next()'''
return self._idx
def namesection(path):
assert '.gz' not in path
name, section = path.rsplit('.', 1)
return name, section
class propertycache(object):
def __init__(self, func):
self.func = func
self.name = func.__name__
def __get__(self, obj, type=None):
result = self.func(obj)
self.cachevalue(obj, result)
return result
def cachevalue(self, obj, value):
setattr(obj, self.name, value)