-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathiters.py
251 lines (205 loc) · 8.33 KB
/
iters.py
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
from sys import version_info
from collections import deque, Iterable
from operator import add, itemgetter, attrgetter, not_
from functools import partial
from itertools import (islice,
chain,
starmap,
repeat,
tee,
cycle,
takewhile,
dropwhile,
combinations)
from .op import flip
from .func import F
from .uniform import *
def take(limit, base):
return islice(base, limit)
def drop(limit, base):
return islice(base, limit, None)
def takelast(n, iterable):
"Return iterator to produce last n items from origin"
return iter(deque(iterable, maxlen=n))
def droplast(n, iterable):
"Return iterator to produce items from origin except last n"
t1, t2 = tee(iterable)
return map(itemgetter(0), zip(t1, islice(t2, n, None)))
def consume(iterator, n=None):
"""Advance the iterator n-steps ahead. If n is none, consume entirely.
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
# Use functions that consume iterators at C speed.
if n is None:
# feed the entire iterator into a zero-length deque
deque(iterator, maxlen=0)
else:
# advance to the empty slice starting at position n
next(islice(iterator, n, n), None)
def nth(iterable, n, default=None):
"""Returns the nth item or a default value
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
return next(islice(iterable, n, None), default)
def first_true(iterable, default=False, pred=None):
"""Returns the first true value in the iterable.
If no true value is found, returns *default*
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
return next(filter(pred, iterable), default)
# widely-spreaded shortcuts to get first item, all but first item,
# second item, and first item of first item from iterator respectively
head = first = partial(flip(nth), 0)
tail = rest = partial(drop, 1)
second = F(rest) >> first
ffirst = F(first) >> first
# shortcut to remove all falsey items from iterable
compact = partial(filter, None)
# filterfalse under alias 'reject'
reject = filterfalse
# shortcuts to 1. return True if f(x) is logical true for every x in
# iterable (False otherwise), and 2. return the first logical true
# value of f(x) for any x in iterable (None otherwise) respectively
every = F(partial(map)) >> all
some = F(partial(map)) >> compact >> first
def iterate(f, x):
"""Return an iterator yielding x, f(x), f(f(x)) etc.
"""
while True:
yield x
x = f(x)
def padnone(iterable):
"""Returns the sequence elements and then returns None indefinitely.
Useful for emulating the behavior of the built-in map() function.
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
return chain(iterable, repeat(None))
def ncycles(iterable, n):
"""Returns the sequence elements n times
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
return chain.from_iterable(repeat(tuple(iterable), n))
def repeatfunc(func, times=None, *args):
"""Repeat calls to func with specified arguments.
Example: repeatfunc(random.random)
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
if times is None:
return starmap(func, repeat(args))
return starmap(func, repeat(args, times))
def grouper(n, iterable, fillvalue=None):
"""Collect data into fixed-length chunks or blocks, so
grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
def group_by(keyfunc, iterable):
"""Returns a dict of the elements from given iterable keyed by result
of keyfunc on each element. The value at each key will be a list of
the corresponding elements, in the order they appeared in the iterable.
"""
grouped = {}
for item in iterable:
grouped.setdefault(keyfunc(item), []).append(item)
return grouped
def roundrobin(*iterables):
"""roundrobin('ABC', 'D', 'EF') --> A D E B F C
Recipe originally credited to George Sakkis.
Reimplemented to work both in Python 2+ and 3+.
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
pending = len(iterables)
next_attr = "next" if version_info[0] == 2 else "__next__"
nexts = cycle(map(attrgetter(next_attr), map(iter, iterables)))
while pending:
try:
for n in nexts:
yield n()
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))
def partition(pred, iterable):
"""Use a predicate to partition entries into false entries and true entries
partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
t1, t2 = tee(iterable)
return filterfalse(pred, t1), filter(pred, t2)
def splitat(t, iterable):
"""Split iterable into two iterators after given number of iterations
splitat(2, range(5)) --> 0 1 and 2 3 4
"""
t1, t2 = tee(iterable)
return islice(t1, t), islice(t2, t, None)
def splitby(pred, iterable):
"""Split iterable into two iterators at first false predicate
splitby(is_even, range(5)) --> 0 and 1 2 3 4
"""
t1, t2 = tee(iterable)
return takewhile(pred, t1), dropwhile(pred, t2)
def powerset(iterable):
"""powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
def pairwise(iterable):
"""pairwise(s) -> (s0,s1), (s1,s2), (s2, s3), ...
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
a, b = tee(iterable)
next(b, None)
return zip(a, b)
def iter_except(func, exception, first_=None):
""" Call a function repeatedly until an exception is raised.
Converts a call-until-exception interface to an iterator interface.
Like __builtin__.iter(func, sentinel) but uses an exception instead
of a sentinel to end the loop.
Examples:
iter_except(functools.partial(heappop, h), IndexError) # priority queue iterator
iter_except(d.popitem, KeyError) # non-blocking dict iterator
iter_except(d.popleft, IndexError) # non-blocking deque iterator
iter_except(q.get_nowait, Queue.Empty) # loop over a producer Queue
iter_except(s.pop, KeyError) # non-blocking set iterator
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
try:
if first_ is not None:
yield first_() # For database APIs needing an initial cast to db.first()
while 1:
yield func()
except exception:
pass
def flatten(items):
"""Flatten any level of nested iterables (not including strings, bytes or
bytearrays).
Reimplemented to work with all nested levels (not only one).
http://docs.python.org/3.4/library/itertools.html#itertools-recipes
"""
for item in items:
is_iterable = isinstance(item, Iterable)
is_string_or_bytes = isinstance(item, (str, bytes, bytearray))
if is_iterable and not is_string_or_bytes:
for i in flatten(item):
yield i
else:
yield item
if version_info[0] == 3 and version_info[1] >= 3:
from itertools import accumulate
else:
def accumulate(iterable, func=add):
"""Make an iterator that returns accumulated sums.
Elements may be any addable type including Decimal or Fraction.
If the optional func argument is supplied, it should be a
function of two arguments and it will be used instead of addition.
Origin implementation:
http://docs.python.org/dev/library/itertools.html#itertools.accumulate
Backported to work with all python versions (< 3.3)
"""
it = iter(iterable)
total = next(it)
yield total
for element in it:
total = func(total, element)
yield total