forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcute_iter_tools.py
More file actions
389 lines (296 loc) · 11.8 KB
/
cute_iter_tools.py
File metadata and controls
389 lines (296 loc) · 11.8 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# Copyright 2009-2014 Ram Rachum.
# This program is distributed under the MIT license.
'''Defines functions for manipulating iterators.'''
# todo: make something like `filter` except it returns first found, or raises
# exception
from __future__ import division
import collections
import itertools
import __builtin__
infinity = float('inf')
def iterate_overlapping_subsequences(iterable, length=2, wrap_around=False,
lazy_tuple=False):
'''
Iterate over overlapping subsequences from the iterable.
Example: if the iterable is [0, 1, 2, 3], then the result would be
`[(0, 1), (1, 2), (2, 3)]`. (Except it would be an iterator and not an
actual list.)
With a length of 3, the result would be an iterator of `[(0, 1, 2), (1,
2, 3)]`.
If `wrap_around=True`, the result would be `[(0, 1, 2), (1,
2, 3), (2, 3, 0), (3, 0, 1)]`.
If `lazy_tuple=True`, returns a `LazyTuple` rather than an iterator.
'''
iterator = _iterate_overlapping_subsequences(
iterable=iterable, length=length, wrap_around=wrap_around
)
if lazy_tuple:
from python_toolbox import nifty_collections # Avoiding circular import.
return nifty_collections.LazyTuple(iterator)
else:
return iterator
def _iterate_overlapping_subsequences(iterable, length, wrap_around):
if length == 1:
for item in iterable:
yield item
assert length >= 2
iterator = iter(iterable)
first_items = get_items(iterator, length)
if len(first_items) < length:
if wrap_around:
raise NotImplementedError(
'`length` is greater than the length of the iterable, and '
'`wrap_around` is set to `True`. Behavior for this is not '
'implemented, because it would require repeating some members '
'more than once.'
)
else:
raise StopIteration
if wrap_around:
first_items_except_last = first_items[:-1]
iterator = itertools.chain(iterator, first_items_except_last)
deque = collections.deque(first_items)
yield first_items
# Allow `first_items` to be garbage-collected:
del first_items
# (Assuming `wrap_around` is `True`, because if it's `False` then all the
# first items except the last will stay saved in
# `first_items_except_last`.)
for current in iterator:
deque.popleft()
deque.append(current)
yield tuple(deque)
def shorten(iterable, n, lazy_tuple=False):
'''
Shorten an iterable to length `n`.
Iterate over the given iterable, but stop after `n` iterations (Or when the
iterable stops iteration by itself.)
`n` may be infinite.
If `lazy_tuple=True`, returns a `LazyTuple` rather than an iterator.
'''
iterator = _shorten(iterable=iterable, n=n)
if lazy_tuple:
from python_toolbox import nifty_collections # Avoiding circular import.
return nifty_collections.LazyTuple(iterator)
else:
return iterator
def _shorten(iterable, n):
if n == infinity:
for thing in iterable:
yield thing
raise StopIteration
assert isinstance(n, int)
if n == 0:
raise StopIteration
for i, thing in enumerate(iterable):
yield thing
if i + 1 == n: # Checking `i + 1` to avoid pulling an extra item.
raise StopIteration
def enumerate(reversible, reverse_index=False, lazy_tuple=False):
'''
Iterate over `(i, item)` pairs, where `i` is the index number of `item`.
This is an extension of the builtin `enumerate`. What it allows is to get a
reverse index, by specifying `reverse_index=True`. This causes `i` to count
down to zero instead of up from zero, so the `i` of the last member will be
zero.
If `lazy_tuple=True`, returns a `LazyTuple` rather than an iterator.
'''
iterator = _enumerate(reversible=reversible, reverse_index=reverse_index)
if lazy_tuple:
from python_toolbox import nifty_collections # Avoiding circular import.
return nifty_collections.LazyTuple(iterator)
else:
return iterator
def _enumerate(reversible, reverse_index):
if reverse_index is False:
return __builtin__.enumerate(reversible)
else:
my_list = list(__builtin__.enumerate(reversed(reversible)))
my_list.reverse()
return my_list
def is_iterable(thing):
'''Return whether an object is iterable.'''
if hasattr(type(thing), '__iter__'):
return True
else:
try:
iter(thing)
except TypeError:
return False
else:
return True
def get_length(iterable):
'''
Get the length of an iterable.
If given an iterator, it will be exhausted.
'''
i = 0
for thing in iterable:
i += 1
return i
def iter_with(iterable, context_manager, lazy_tuple=False):
'''
Iterate on `iterable`, `with`ing the context manager on every `next`.
If `lazy_tuple=True`, returns a `LazyTuple` rather than an iterator.
'''
iterator = _iter_with(iterable=iterable, context_manager=context_manager)
if lazy_tuple:
from python_toolbox import nifty_collections # Avoiding circular import.
return nifty_collections.LazyTuple(iterator)
else:
return iterator
def _iter_with(iterable, context_manager):
iterator = iter(iterable)
while True:
with context_manager:
next_item = next(iterator)
# Recycling `StopIteration` exception. (Assuming the context
# manager doesn't have special treatment for it.)
yield next_item
def get_items(iterable, n, container_type=tuple):
'''
Get the next `n` items from the iterable as a `tuple`.
If there are less than `n` items, no exception will be raised. Whatever
items are there will be returned.
If you pass in a different kind of container than `tuple` as
`container_type`, it'll be used to wrap the results.
'''
return container_type(shorten(iterable, n))
def double_filter(filter_function, iterable, lazy_tuple=False):
'''
Filter an `iterable` into two iterables according to a `filter_function`.
This is similar to the builtin `filter`, except it returns a tuple of two
iterators, the first iterating on items that passed the filter function,
and the second iterating on items that didn't.
Note that this function is not thread-safe. (You may not consume the two
iterators on two separate threads.)
If `lazy_tuple=True`, returns a `LazyTuple` rather than an iterator.
'''
iterator = iter(iterable)
true_deque = collections.deque()
false_deque = collections.deque()
def make_true_iterator():
while True:
try:
yield true_deque.popleft()
except IndexError:
value = next(iterator) # `StopIteration` exception recycled.
if filter_function(value):
yield value
else:
false_deque.append(value)
def make_false_iterator():
while True:
try:
yield false_deque.popleft()
except IndexError:
value = next(iterator) # `StopIteration` exception recycled.
if filter_function(value):
true_deque.append(value)
else:
yield value
iterators = (make_true_iterator(), make_false_iterator())
if lazy_tuple:
from python_toolbox import nifty_collections # Avoiding circular import.
return tuple(map(nifty_collections.LazyTuple, iterators))
else:
return iterators
def get_ratio(filter_function, iterable):
'''Get the ratio of `iterable` items that pass `filter_function`.'''
if isinstance(filter_function, str):
attribute_name = filter_function
filter_function = lambda item: getattr(item, attribute_name, None)
n_total_items = 0
n_passed_items = 0
for item in iterable:
n_total_items += 1
if filter_function(item):
n_passed_items += 1
return n_passed_items / n_total_items
def fill(iterable, fill_value=None, fill_value_maker=None, length=infinity,
sequence_type=None, lazy_tuple=False):
'''
Iterate on `iterable`, and after it's exhaused, yield fill values.
If `fill_value_maker` is given, it's used to create fill values
dynamically. (Useful if your fill value is `[]` and you don't want to use
many copies of the same list.)
If `length` is given, shortens the iterator to that length.
If `sequence_type` is given, instead of returning an iterator, this
function will return a sequence of that type. If `lazy_tuple=True`, uses a
`LazyTuple`. (Can't use both options together.)
'''
# Validating user input:
assert (sequence_type is None) or (lazy_tuple is False)
iterator = _fill(iterable, fill_value=fill_value,
fill_value_maker=fill_value_maker,
length=length)
if lazy_tuple:
from python_toolbox import nifty_collections # Avoiding circular import.
return nifty_collections.LazyTuple(iterator)
elif sequence_type is None:
return iterator
else:
return sequence_type(iterator)
def _fill(iterable, fill_value, fill_value_maker, length):
if fill_value_maker is not None:
assert fill_value is None
else:
fill_value_maker = lambda: fill_value
iterator = iter(iterable)
iterator_exhausted = False
for i in itertools.count():
if i >= length:
raise StopIteration
if iterator_exhausted:
yield fill_value_maker()
else:
try:
yield next(iterator)
except StopIteration:
iterator_exhausted = True
yield fill_value_maker()
def call_until_exception(function, exception, lazy_tuple=False):
'''
Iterate on values returned from `function` until getting `exception`.
If `lazy_tuple=True`, returns a `LazyTuple` rather than an iterator.
'''
iterator = _call_until_exception(function, exception)
if lazy_tuple:
from python_toolbox import nifty_collections # Avoiding circular import.
return nifty_collections.LazyTuple(iterator)
else:
return iterator
def _call_until_exception(function, exception):
from python_toolbox import sequence_tools
exceptions = sequence_tools.to_tuple(exception, item_type=type)
try:
while True:
yield function()
except exceptions:
raise StopIteration
def get_single_if_any(iterable,
exception_on_multiple=Exception('More than one value '
'not allowed.')):
'''
Get the single item of `iterable`, if any.
If `iterable` has one item, return it. If it's empty, get `None`. If it has
more than one item, raise an exception. (Unless
`exception_on_multiple=None`.)
'''
assert isinstance(exception_on_multiple, Exception) or \
exception_on_multiple is None
iterator = iter(iterable)
try:
first_item = next(iterator)
except StopIteration:
return None
else:
if exception_on_multiple:
try:
second_item = next(iterator)
except StopIteration:
return first_item
else:
raise exception_on_multiple
else: # not exception_on_multiple
return first_item