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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
"""
Collection of functionality to make using iterators transparently easier
"""
__all__ = ("partition", "expandable_chain", "caching_iter", "iter_sort")
import itertools
from collections import deque
def partition(iterable, predicate=bool):
"""Partition an iterable into two iterables based on a given filter.
Taking care that the predicate is called only once for each element.
Args:
iterable: target iterable to split into two
predicate: filtering function used to split the iterable
Returns:
A tuple of iterators, the first containing items that don't match the
filter and the second the matched items.
"""
a, b = itertools.tee((predicate(x), x) for x in iterable)
return ((x for pred, x in a if not pred),
(x for pred, x in b if pred))
class expandable_chain:
"""
chained iterables, with the ability to add new iterables to the chain
as long as the instance hasn't raised StopIteration already. This is
fairly useful for implementing queues of things that must be processed.
>>> from snakeoil.iterables import expandable_chain
>>> l = range(5)
>>> i = expandable_chain(l)
>>> print(i.next())
0
>>> print(i.next())
1
>>> i.appendleft(range(5, 7))
>>> print(i.next())
5
>>> print(i.next())
6
>>> print(i.next())
2
"""
__slot__ = ("iterables", "__weakref__")
def __init__(self, *iterables):
"""
accepts N iterables, must have at least one specified
"""
self.iterables = deque()
self.extend(iterables)
def __iter__(self):
return self
def __next__(self):
if self.iterables is not None:
while self.iterables:
try:
return next(self.iterables[0])
except StopIteration:
self.iterables.popleft()
self.iterables = None
raise StopIteration()
def append(self, iterable):
"""append an iterable to the chain to be consumed"""
if self.iterables is None:
raise StopIteration()
self.iterables.append(iter(iterable))
def appendleft(self, iterable):
"""prepend an iterable to the chain to be consumed"""
if self.iterables is None:
raise StopIteration()
self.iterables.appendleft(iter(iterable))
def extend(self, iterables):
"""extend multiple iterables to the chain to be consumed"""
if self.iterables is None:
raise StopIteration()
self.iterables.extend(iter(x) for x in iterables)
def extendleft(self, iterables):
"""prepend multiple iterables to the chain to be consumed"""
if self.iterables is None:
raise StopIteration()
self.iterables.extendleft(iter(x) for x in iterables)
class caching_iter:
"""
On demand consumes from an iterable so as to appear like a tuple
>>> from snakeoil.iterables import caching_iter
>>> i = iter(range(5))
>>> ci = caching_iter(i)
>>> print(ci[0])
0
>>> print(ci[2])
2
>>> print(i.next())
3
"""
__slots__ = ("iterable", "__weakref__", "cached_list", "sorter")
def __init__(self, iterable, sorter=None):
self.sorter = sorter
self.iterable = iter(iterable)
self.cached_list = []
def __setitem__(self, key, val):
raise TypeError("unmodifiable")
def __getitem__(self, index):
existing_len = len(self.cached_list)
if self.iterable is not None and self.sorter:
self.cached_list.extend(self.iterable)
self.cached_list = tuple(self.sorter(self.cached_list))
self.iterable = self.sorter = None
existing_len = len(self.cached_list)
if index < 0:
if self.iterable is not None:
self.cached_list = tuple(self.cached_list + list(self.iterable))
self.iterable = None
existing_len = len(self.cached_list)
index = existing_len + index
if index < 0:
raise IndexError("list index out of range")
elif index >= existing_len - 1:
if self.iterable is not None:
i = itertools.islice(self.iterable, 0, index - (existing_len - 1))
self.cached_list.extend(i)
if len(self.cached_list) -1 != index:
# consumed, baby.
self.iterable = None
self.cached_list = tuple(self.cached_list)
raise IndexError("list index out of range")
return self.cached_list[index]
def _flatten(self):
if self.iterable is not None:
if self.sorter:
self.cached_list.extend(self.iterable)
self.cached_list = tuple(self.sorter(self.cached_list))
self.sorter = None
else:
self.cached_list = tuple(self.cached_list + list(self.iterable))
self.iterable = None
def __lt__(self, other):
self._flatten()
for x, y in itertools.zip_longest(self.cached_list, other):
if x != y:
return x < y
return False
def __gt__(self, other):
self._flatten()
for x, y in itertools.zip_longest(self.cached_list, other):
if x != y:
return x > y
return False
def __le__(self, other):
return self.__lt__(other) or self.__eq__(other)
def __ge__(self, other):
return not self.__lt__(other)
def __eq__(self, other):
self._flatten()
return self.cached_list == other
def __ne__(self, other):
return not self.__eq__(other)
def __bool__(self):
if self.cached_list:
return True
if self.iterable:
for x in self.iterable:
self.cached_list.append(x)
return True
# if we've made it here... then nothing more in the iterable.
self.iterable = self.sorter = None
self.cached_list = ()
return False
def __len__(self):
if self.iterable is not None:
self.cached_list.extend(self.iterable)
if self.sorter:
self.cached_list = tuple(self.sorter(self.cached_list))
self.sorter = None
else:
self.cached_list = tuple(self.cached_list)
self.iterable = None
return len(self.cached_list)
def __iter__(self):
if (self.sorter is not None and
self.iterable is not None):
if self.cached_list:
self.cached_list.extend(self.iterable)
self.cached_list = tuple(self.sorter(self.cached_list))
else:
self.cached_list = tuple(self.sorter(self.iterable))
self.iterable = self.sorter = None
for x in self.cached_list:
yield x
if self.iterable is not None:
for x in self.iterable:
self.cached_list.append(x)
yield x
else:
return
self.iterable = None
self.cached_list = tuple(self.cached_list)
def __hash__(self):
if self.iterable is not None:
self.cached_list.extend(self.iterable)
self.cached_list = tuple(self.cached_list)
self.iterable = None
return hash(self.cached_list)
def __str__(self):
return "iterable(%s), cached: %s" % (
self.iterable, str(self.cached_list))
def iter_sort(sorter, *iterables):
"""Merge a number of sorted iterables into a single sorted iterable.
:type sorter: callable.
:param sorter: function, passed a list of [element, iterable].
:param iterables: iterables to consume from. It's **required**
that each iterable to consume from is presorted already within
that specific iterable.
:return: yields items one by one in combined sorted order
For example:
>>> from snakeoil.iterables import iter_sort
>>> iter1 = range(0, 5, 2)
>>> iter2 = range(1, 6, 2)
>>> # note that these lists will be consumed as they go,
>>> # sorted is just being used to compare the individual items
>>> sorted_iter = iter_sort(sorted, iter1, iter2)
>>> print(list(sorted_iter))
[0, 1, 2, 3, 4, 5]
"""
l = []
for x in iterables:
try:
x = iter(x)
l.append([next(x), x])
except StopIteration:
pass
if len(l) == 1:
yield l[0][0]
for x in l[0][1]:
yield x
return
l = sorter(l)
while l:
yield l[0][0]
for y in l[0][1]:
l[0][0] = y
break
else:
del l[0]
if len(l) == 1:
yield l[0][0]
for x in l[0][1]:
yield x
break
continue
l = sorter(l)
|