28
28
__version__ = "0.0.0+auto.0"
29
29
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Itertools.git"
30
30
31
+ try :
32
+ from typing import (
33
+ Any ,
34
+ Callable ,
35
+ Iterable ,
36
+ Iterator ,
37
+ List ,
38
+ Optional ,
39
+ Sequence ,
40
+ Tuple ,
41
+ TypeVar ,
42
+ Union ,
43
+ )
44
+ from typing_extensions import TypeAlias
45
+
46
+ _T = TypeVar ("_T" )
47
+ _Fill = TypeVar ("_Fill" )
48
+ _OptionalFill : TypeAlias = Optional [_Fill ]
49
+ _N : TypeAlias = Union [int , float , complex ]
50
+ _Predicate : TypeAlias = Callable [[_T ], object ]
51
+
52
+ except ImportError :
53
+ pass
54
+
31
55
32
56
def accumulate (iterable , func = lambda x , y : x + y ):
33
57
"""Make an iterator that returns accumulated sums, or accumulated
@@ -52,7 +76,7 @@ def accumulate(iterable, func=lambda x, y: x + y):
52
76
yield acc
53
77
54
78
55
- def chain (* iterables ) :
79
+ def chain (* iterables : Iterable [ _T ]) -> Iterator [ _T ] :
56
80
"""Make an iterator that returns elements from the first iterable until it
57
81
is exhausted, then proceeds to the next iterable, until all of the iterables
58
82
are exhausted. Used for treating consecutive sequences as a single sequence.
@@ -65,7 +89,7 @@ def chain(*iterables):
65
89
yield from i
66
90
67
91
68
- def chain_from_iterable (iterables ) :
92
+ def chain_from_iterable (iterables : Iterable [ Iterable [ _T ]]) -> Iterator [ _T ] :
69
93
"""Alternate constructor for chain(). Gets chained inputs from a
70
94
single iterable argument that is evaluated lazily.
71
95
@@ -78,7 +102,7 @@ def chain_from_iterable(iterables):
78
102
yield element
79
103
80
104
81
- def combinations (iterable , r ) :
105
+ def combinations (iterable : Iterable [ _T ] , r : int ) -> Iterator [ Tuple [ _T , ...]] :
82
106
"""Return r length subsequences of elements from the input iterable.
83
107
Combinations are emitted in lexicographic sort order. So, if the input
84
108
iterable is sorted, the combination tuples will be produced in sorted order.
@@ -113,7 +137,9 @@ def combinations(iterable, r):
113
137
yield tuple (pool [i ] for i in indices )
114
138
115
139
116
- def combinations_with_replacement (iterable , r ):
140
+ def combinations_with_replacement (
141
+ iterable : Iterable [_T ], r : int
142
+ ) -> Iterator [Tuple [_T , ...]]:
117
143
"""Return r length subsequences of elements from the input iterable allowing
118
144
individual elements to be repeated more than once.
119
145
@@ -147,7 +173,7 @@ def combinations_with_replacement(iterable, r):
147
173
yield tuple (pool [i ] for i in indices )
148
174
149
175
150
- def compress (data , selectors ) :
176
+ def compress (data : Iterable [ _T ] , selectors : Iterable [ Any ]) -> Iterable [ _T ] :
151
177
"""Make an iterator that filters elements from data returning only those
152
178
that have a corresponding element in selectors that evaluates to True.
153
179
Stops when either the data or selectors iterables has been exhausted.
@@ -160,7 +186,7 @@ def compress(data, selectors):
160
186
return (d for d , s in zip (data , selectors ) if s )
161
187
162
188
163
- def count (start = 0 , step = 1 ) :
189
+ def count (start : _N = 0 , step : _N = 1 ) -> Iterator [ _N ] :
164
190
"""Make an iterator that returns evenly spaced values starting with number
165
191
start. Often used as an argument to map() to generate consecutive data
166
192
points. Also, used with zip() to add sequence numbers.
@@ -196,7 +222,7 @@ def cycle(p):
196
222
yield from p
197
223
198
224
199
- def dropwhile (predicate , iterable ) :
225
+ def dropwhile (predicate : _Predicate [ _T ] , iterable : Iterable [ _T ]) -> Iterator [ _T ] :
200
226
"""Make an iterator that drops elements from the iterable as long as the
201
227
predicate is true; afterwards, returns every element. Note, the iterator
202
228
does not produce any output until the predicate first becomes false, so it
@@ -216,7 +242,7 @@ def dropwhile(predicate, iterable):
216
242
yield x
217
243
218
244
219
- def filterfalse (predicate , iterable ) :
245
+ def filterfalse (predicate : _Predicate [ _T ] , iterable : Iterable [ _T ]) -> Iterator [ _T ] :
220
246
"""Make an iterator that filters elements from iterable returning only those
221
247
for which the predicate is False. If predicate is None, return the items
222
248
that are false.
@@ -332,7 +358,9 @@ def islice(p, start, stop=(), step=1):
332
358
return
333
359
334
360
335
- def permutations (iterable , r = None ):
361
+ def permutations (
362
+ iterable : Iterable [_T ], r : Optional [int ] = None
363
+ ) -> Iterator [Tuple [_T , ...]]:
336
364
"""Return successive r length permutations of elements in the iterable.
337
365
338
366
If r is not specified or is None, then r defaults to the length of the
@@ -375,7 +403,7 @@ def permutations(iterable, r=None):
375
403
return
376
404
377
405
378
- def product (* args , r = 1 ) :
406
+ def product (* args : Iterable [ _T ] , r : int = 1 ) -> Iterator [ Tuple [ _T , ...]] :
379
407
"""Cartesian product of input iterables.
380
408
381
409
Roughly equivalent to nested for-loops in a generator expression. For
@@ -399,14 +427,14 @@ def product(*args, r=1):
399
427
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
400
428
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
401
429
pools = [tuple (pool ) for pool in args ] * r
402
- result = [[]]
430
+ result : List [ List [ _T ]] = [[]]
403
431
for pool in pools :
404
432
result = [x + [y ] for x in result for y in pool ]
405
433
for prod in result :
406
434
yield tuple (prod )
407
435
408
436
409
- def repeat (el , n = None ):
437
+ def repeat (el : _T , n : Optional [ int ] = None ) -> Iterator [ _T ] :
410
438
"""Make an iterator that returns object over and over again. Runs
411
439
indefinitely unless the times argument is specified. Used as argument to
412
440
map() for invariant parameters to the called function. Also used with zip()
@@ -424,7 +452,9 @@ def repeat(el, n=None):
424
452
yield el
425
453
426
454
427
- def starmap (function , iterable ):
455
+ def starmap (
456
+ function : Callable [..., _T ], iterable : Iterable [Iterable [Any ]]
457
+ ) -> Iterator [_T ]:
428
458
"""Make an iterator that computes the function using arguments obtained from
429
459
the iterable. Used instead of map() when argument parameters are already
430
460
grouped in tuples from a single iterable (the data has been “pre-zipped”).
@@ -439,7 +469,7 @@ def starmap(function, iterable):
439
469
yield function (* args )
440
470
441
471
442
- def takewhile (predicate , iterable ) :
472
+ def takewhile (predicate : _Predicate [ _T ] , iterable : Iterable [ _T ]) -> Iterator [ _T ] :
443
473
"""Make an iterator that returns elements from the iterable as long
444
474
as the predicate is true.
445
475
@@ -455,7 +485,7 @@ def takewhile(predicate, iterable):
455
485
break
456
486
457
487
458
- def tee (iterable , n = 2 ) :
488
+ def tee (iterable : Iterable [ _T ] , n : int = 2 ) -> Sequence [ Iterator [ _T ]] :
459
489
"""Return n independent iterators from a single iterable.
460
490
461
491
:param iterable: the iterator from which to make iterators.
@@ -465,7 +495,9 @@ def tee(iterable, n=2):
465
495
return [iter (iterable ) for _ in range (n )]
466
496
467
497
468
- def zip_longest (* args , fillvalue = None ):
498
+ def zip_longest (
499
+ * args : Iterable [_T ], fillvalue : _OptionalFill = None
500
+ ) -> Iterator [Tuple [Union [_T , _OptionalFill ], ...]]:
469
501
"""Make an iterator that aggregates elements from each of the
470
502
iterables. If the iterables are of uneven length, missing values are
471
503
filled-in with fillvalue. Iteration continues until the longest
@@ -475,7 +507,7 @@ def zip_longest(*args, fillvalue=None):
475
507
:param fillvalue: value to fill in those missing from shorter iterables
476
508
"""
477
509
# zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
478
- iterators = [iter (it ) for it in args ]
510
+ iterators : List [ Iterator [ Union [ _T , _OptionalFill ]]] = [iter (it ) for it in args ]
479
511
num_active = len (iterators )
480
512
if not num_active :
481
513
return
0 commit comments