@@ -826,10 +826,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
826
826
return map(function, count(start))
827
827
828
828
def repeatfunc(func, times=None, *args):
829
- """Repeat calls to func with specified arguments.
830
-
831
- Example: repeatfunc(random.random)
832
- """
829
+ "Repeat calls to func with specified arguments."
833
830
if times is None:
834
831
return starmap(func, repeat(args))
835
832
return starmap(func, repeat(args, times))
@@ -851,10 +848,8 @@ and :term:`generators <generator>` which incur interpreter overhead.
851
848
"Advance the iterator n-steps ahead. If n is None, consume entirely."
852
849
# Use functions that consume iterators at C speed.
853
850
if n is None:
854
- # feed the entire iterator into a zero-length deque
855
851
collections.deque(iterator, maxlen=0)
856
852
else:
857
- # advance to the empty slice starting at position n
858
853
next(islice(iterator, n, n), None)
859
854
860
855
def nth(iterable, n, default=None):
@@ -873,7 +868,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
873
868
874
869
def all_equal(iterable, key=None):
875
870
"Returns True if all the elements are equal to each other."
876
- # all_equal('4٤໔4৪ ', key=int) → True
871
+ # all_equal('4٤௪౪໔ ', key=int) → True
877
872
return len(take(2, groupby(iterable, key))) <= 1
878
873
879
874
def unique_justseen(iterable, key=None):
@@ -903,9 +898,9 @@ and :term:`generators <generator>` which incur interpreter overhead.
903
898
def sliding_window(iterable, n):
904
899
"Collect data into overlapping fixed-length chunks or blocks."
905
900
# sliding_window('ABCDEFG', 4) → ABCD BCDE CDEF DEFG
906
- it = iter(iterable)
907
- window = collections.deque(islice(it , n- 1), maxlen=n)
908
- for x in it :
901
+ iterator = iter(iterable)
902
+ window = collections.deque(islice(iterator , n - 1), maxlen=n)
903
+ for x in iterator :
909
904
window.append(x)
910
905
yield tuple(window)
911
906
@@ -955,8 +950,8 @@ and :term:`generators <generator>` which incur interpreter overhead.
955
950
seq_index = getattr(iterable, 'index', None)
956
951
if seq_index is None:
957
952
# Path for general iterables
958
- it = islice(iterable, start, stop)
959
- for i, element in enumerate(it , start):
953
+ iterator = islice(iterable, start, stop)
954
+ for i, element in enumerate(iterator , start):
960
955
if element is value or element == value:
961
956
yield i
962
957
else:
@@ -971,10 +966,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
971
966
pass
972
967
973
968
def iter_except(func, exception, first=None):
974
- """ Call a function repeatedly until an exception is raised.
975
-
976
- Converts a call-until-exception interface to an iterator interface.
977
- """
969
+ "Convert a call-until-exception interface to an iterator interface."
978
970
# iter_except(d.popitem, KeyError) → non-blocking dictionary iterator
979
971
try:
980
972
if first is not None:
@@ -1074,14 +1066,10 @@ The following recipes have a more mathematical flavor:
1074
1066
# sieve(30) → 2 3 5 7 11 13 17 19 23 29
1075
1067
if n > 2:
1076
1068
yield 2
1077
- start = 3
1078
1069
data = bytearray((0, 1)) * (n // 2)
1079
- limit = math.isqrt(n) + 1
1080
- for p in iter_index(data, 1, start, limit):
1081
- yield from iter_index(data, 1, start, p*p)
1070
+ for p in iter_index(data, 1, start=3, stop=math.isqrt(n) + 1):
1082
1071
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
1083
- start = p*p
1084
- yield from iter_index(data, 1, start)
1072
+ yield from iter_index(data, 1, start=3)
1085
1073
1086
1074
def factor(n):
1087
1075
"Prime factors of n."
@@ -1101,8 +1089,8 @@ The following recipes have a more mathematical flavor:
1101
1089
"Count of natural numbers up to n that are coprime to n."
1102
1090
# https://mathworld.wolfram.com/TotientFunction.html
1103
1091
# totient(12) → 4 because len([1, 5, 7, 11]) == 4
1104
- for p in unique_justseen (factor(n)):
1105
- n -= n // p
1092
+ for prime in set (factor(n)):
1093
+ n -= n // prime
1106
1094
return n
1107
1095
1108
1096
0 commit comments