Skip to content

Commit c8b8104

Browse files
committed
applied codeformat
1 parent 8698e33 commit c8b8104

File tree

2 files changed

+232
-88
lines changed

2 files changed

+232
-88
lines changed

python-stdlib/itertools/itertools.py

+61-27
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ def accumulate(iterable, func=lambda x, y: x + y, initial=None):
2121
total = func(total, element)
2222
yield total
2323

24+
2425
# chain('abcd',[],range(5))) --> 'a' 'b' 'c' 'd' 0 1 2 3 4
2526
class chain:
2627
def __init__(self, *iterables):
2728
self.iterables = list(iterables)
2829
self.it = iter([])
30+
2931
def __iter__(self):
3032
return self
33+
3134
def __next__(self):
3235
while True:
3336
try:
@@ -38,12 +41,14 @@ def __next__(self):
3841
continue
3942
except IndexError:
4043
raise StopIteration
44+
4145
# chain.from_iterable(['ABC', 'DEF']) --> 'A' 'B' 'C' 'D' 'E' 'F'
4246
@staticmethod
43-
def from_iterable(iterables):
47+
def from_iterable(iterables):
4448
for it in iterables:
4549
yield from it
4650

51+
4752
# combinations('ABCD', 2) --> ('A','B') ('A','C') ('A','D') ('B','C') ('B','D') ('C','D')
4853
def combinations(iterable, r):
4954
pool = tuple(iterable)
@@ -65,6 +70,7 @@ def combinations(iterable, r):
6570
indices[j] = indices[j - 1] + 1
6671
yield tuple(pool[i] for i in indices)
6772

73+
6874
# combinations_with_replacement('ABC', 2) --> ('A','A') ('A','B') ('A','C') ('B','B') ('B','C') ('C','C')
6975
def combinations_with_replacement(iterable, r):
7076
pool = tuple(iterable)
@@ -84,21 +90,24 @@ def combinations_with_replacement(iterable, r):
8490
indices[index:] = [indices[index] + 1] * (r - index)
8591
yield tuple(pool[i] for i in indices)
8692

93+
8794
# compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
8895
def compress(data, selectors):
8996
return (d for d, s in zip(data, selectors) if s)
9097

98+
9199
# count(4, 3) --> 4 7 10 13 16 19 ....
92100
def count(start=0, step=1):
93101
while True:
94102
yield start
95103
start += step
96104

105+
97106
# cycle('abc') --> a b c a b c a b c a ....
98107
def cycle(iterable):
99108
try:
100109
len(iterable)
101-
except TypeError: # len() not defined: Assume p is a finite iterable: We cache the elements.
110+
except TypeError: # len() not defined: Assume p is a finite iterable: We cache the elements.
102111
cache = []
103112
for i in iterable:
104113
yield i
@@ -107,6 +116,7 @@ def cycle(iterable):
107116
while iterable:
108117
yield from iterable
109118

119+
110120
# # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
111121
def dropwhile(predicate, iterable):
112122
it = iter(iterable)
@@ -117,6 +127,7 @@ def dropwhile(predicate, iterable):
117127
for x in it:
118128
yield x
119129

130+
120131
# filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
121132
def filterfalse(predicate, iterable):
122133
if predicate is None:
@@ -125,38 +136,48 @@ def filterfalse(predicate, iterable):
125136
if not predicate(x):
126137
yield x
127138

139+
128140
# groupby('aaaabbbccdaa'))) --> ('a', gen1) ('b', gen2) ('c', gen3) ('d', gen4) ('a', gen5)
129141
# where gen1 --> a a a a, gen2 --> b b b, gen3 --> c c, gen4 --> d, gen5 --> a a
130142
def groupby(iterable, key=None):
131143
it = iter(iterable)
132144
keyf = key if key is not None else lambda x: x
145+
133146
def ggen(ktgt):
134147
nonlocal cur, kcur
135148
while kcur == ktgt:
136149
yield cur
137150
try:
138-
cur = next(it); kcur = keyf(cur)
151+
cur = next(it)
152+
kcur = keyf(cur)
139153
except StopIteration:
140-
break
141-
kcur = kold = object() # need an object that never can be a returned from key function
154+
break
155+
156+
kcur = kold = object() # need an object that never can be a returned from key function
142157
while True:
143-
while kcur == kold: # not all iterables with the same (old) key were used up by ggen, so use them up here
158+
while (
159+
kcur == kold
160+
): # not all iterables with the same (old) key were used up by ggen, so use them up here
144161
try:
145-
cur = next(it); kcur = keyf(cur)
162+
cur = next(it)
163+
kcur = keyf(cur)
146164
except StopIteration:
147165
return
148166
kold = kcur
149167
yield (kcur, ggen(kcur))
150168

169+
151170
# islice('abcdefghij', 2, None, 3)) --> c f i
152171
# islice(range(10), 2, 6, 2)) --> 2 4
153172
def islice(iterable, *sargs):
154173
if len(sargs) < 1 or len(sargs) > 3:
155-
raise TypeError('islice expected at least 2, at most 4 arguments, got {:d}'.format(len(sargs)+1))
174+
raise TypeError(
175+
"islice expected at least 2, at most 4 arguments, got {:d}".format(len(sargs) + 1)
176+
)
156177
step = 1 if len(sargs) < 3 else sargs[2]
157178
step = 1 if step is None else step
158179
if step <= 0:
159-
raise ValueError('step for islice() must be a positive integer or None')
180+
raise ValueError("step for islice() must be a positive integer or None")
160181
start = 0 if len(sargs) < 2 else sargs[0]
161182
stop = sargs[0] if len(sargs) == 1 else sargs[1]
162183
it = iter(iterable)
@@ -173,18 +194,20 @@ def islice(iterable, *sargs):
173194
except StopIteration:
174195
return
175196

197+
176198
# pairwise(range(5)) --> (0,1) (1,2) (2,3) (3,4)
177199
# pairwise('abcdefg') --> ('a','b') ('b','c') ('c','d') ('d','e') ('e','f') ('f','g')
178200
def pairwise(iterable):
179-
it = iter(iterable)
201+
it = iter(iterable)
180202
try:
181203
l = next(it)
182204
while True:
183205
c = next(it)
184206
yield l, c
185207
l = c
186208
except StopIteration:
187-
return
209+
return
210+
188211

189212
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
190213
# permutations(range(3)) --> 012 021 102 120 201 210
@@ -211,6 +234,7 @@ def permutations(iterable, r=None):
211234
else:
212235
return
213236

237+
214238
# product('ABCD', 'xy') --> ('A','x') ('A','y') ('B','x') ('B','y') ('C','x') ('C','y') ('D','x') ('D','y')
215239
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111 # but in tuples, of course
216240
def product(*args, repeat=1):
@@ -221,6 +245,7 @@ def product(*args, repeat=1):
221245
for prod in result:
222246
yield tuple(prod)
223247

248+
224249
# repeat(10, 3) --> 10 10 10
225250
def repeat(obj, times=None):
226251
if times is None:
@@ -230,11 +255,13 @@ def repeat(obj, times=None):
230255
for _ in range(times):
231256
yield obj
232257

258+
233259
# starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
234260
def starmap(function, iterable):
235261
for args in iterable:
236262
yield function(*args)
237263

264+
238265
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
239266
def takewhile(predicate, iterable):
240267
for x in iterable:
@@ -243,27 +270,33 @@ def takewhile(predicate, iterable):
243270
else:
244271
break
245272

273+
246274
# tee(range(2,10), 3) --> (it1, it2, it3) all parallel generators, but dependent on original generator (e.g. range(2,10))
247275
# --> (min(it1), max(it2), sum(it3)) --> (2, 9, 44)
248276
def tee(iterable, n=2):
249-
if iter(iterable) is not iter(iterable): # save buffer for special cases that iterable is range, tuple, list ...
277+
if iter(iterable) is not iter(
278+
iterable
279+
): # save buffer for special cases that iterable is range, tuple, list ...
250280
return [iter(iterable) for _ in range(n)] # that have independent iterators
251281
it = iter(iterable)
252282
if n < 1:
253283
return ()
254284
elif n == 1:
255285
return (it,)
256-
buf = [] # Buffer, contains stored values from itr
257-
ibuf = [0]*n # Indices of the individual generators, could be array('H', [0]*n)
258-
def gen(k): # but we have no 0 in ibuf in MP
259-
nonlocal buf, ibuf # These are bound to the generators as closures
286+
buf = [] # Buffer, contains stored values from itr
287+
ibuf = [0] * n # Indices of the individual generators, could be array('H', [0]*n)
288+
289+
def gen(k): # but we have no 0 in ibuf in MP
290+
nonlocal buf, ibuf # These are bound to the generators as closures
260291
while True:
261-
if ibuf[k] < len(buf): # We get an object stored in the buffer.
292+
if ibuf[k] < len(buf): # We get an object stored in the buffer.
262293
r = buf[ibuf[k]]
263294
ibuf[k] += 1
264-
if ibuf[k] == 1: # If we got the first object in the buffer,
265-
if 0 not in ibuf: # then check if other generators do not wait anymore on it
266-
buf.pop(0) # so it may be popped left. Afterwards decrease all indices by 1.
295+
if ibuf[k] == 1: # If we got the first object in the buffer,
296+
if 0 not in ibuf: # then check if other generators do not wait anymore on it
297+
buf.pop(
298+
0
299+
) # so it may be popped left. Afterwards decrease all indices by 1.
267300
for i in range(n):
268301
ibuf[i] -= 1
269302
elif ibuf[k] == len(buf):
@@ -273,8 +306,10 @@ def gen(k): # but we have no 0 in ibuf in MP
273306
ibuf[k] += 1
274307
except StopIteration:
275308
return
276-
yield r # The returned generators are not thread-safe. For that the access to the
277-
return tuple(gen(i) for i in range(n)) # shared buf and ibuf should be protected by locks.
309+
yield r # The returned generators are not thread-safe. For that the access to the
310+
311+
return tuple(gen(i) for i in range(n)) # shared buf and ibuf should be protected by locks.
312+
278313

279314
# zip_longest('ABCD', 'xy', fillvalue='-') --> ('A','x') ('B','y') ('C','-') ('D','-')
280315
def zip_longest(*args, fillvalue=None):
@@ -299,19 +334,18 @@ def zip_longest(*args, fillvalue=None):
299334

300335
# # Full analog of CPython builtin iter with 2 arguments
301336
# def iter(*args):
302-
#
337+
#
303338
# if len(args) == 1:
304339
# return builtins.iter(args[0])
305-
#
340+
#
306341
# class _iter:
307-
#
342+
#
308343
# def __init__(self, args):
309344
# self.f, self.sentinel = args
310345
# def __next__(self):
311346
# v = self.f()
312347
# if v == self.sentinel:
313348
# raise StopIteration
314349
# return v
315-
#
350+
#
316351
# return _iter(args)
317-

0 commit comments

Comments
 (0)