@@ -818,10 +818,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
818
818
return map(function, count(start))
819
819
820
820
def repeatfunc(func, times=None, *args):
821
- """Repeat calls to func with specified arguments.
822
-
823
- Example: repeatfunc(random.random)
824
- """
821
+ "Repeat calls to func with specified arguments."
825
822
if times is None:
826
823
return starmap(func, repeat(args))
827
824
return starmap(func, repeat(args, times))
@@ -843,10 +840,8 @@ and :term:`generators <generator>` which incur interpreter overhead.
843
840
"Advance the iterator n-steps ahead. If n is None, consume entirely."
844
841
# Use functions that consume iterators at C speed.
845
842
if n is None:
846
- # feed the entire iterator into a zero-length deque
847
843
collections.deque(iterator, maxlen=0)
848
844
else:
849
- # advance to the empty slice starting at position n
850
845
next(islice(iterator, n, n), None)
851
846
852
847
def nth(iterable, n, default=None):
@@ -865,7 +860,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
865
860
866
861
def all_equal(iterable, key=None):
867
862
"Returns True if all the elements are equal to each other."
868
- # all_equal('4٤໔4৪ ', key=int) → True
863
+ # all_equal('4٤௪౪໔ ', key=int) → True
869
864
return len(take(2, groupby(iterable, key))) <= 1
870
865
871
866
def unique_justseen(iterable, key=None):
@@ -895,9 +890,9 @@ and :term:`generators <generator>` which incur interpreter overhead.
895
890
def sliding_window(iterable, n):
896
891
"Collect data into overlapping fixed-length chunks or blocks."
897
892
# sliding_window('ABCDEFG', 4) → ABCD BCDE CDEF DEFG
898
- it = iter(iterable)
899
- window = collections.deque(islice(it , n- 1), maxlen=n)
900
- for x in it :
893
+ iterator = iter(iterable)
894
+ window = collections.deque(islice(iterator , n - 1), maxlen=n)
895
+ for x in iterator :
901
896
window.append(x)
902
897
yield tuple(window)
903
898
@@ -947,8 +942,8 @@ and :term:`generators <generator>` which incur interpreter overhead.
947
942
seq_index = getattr(iterable, 'index', None)
948
943
if seq_index is None:
949
944
# Path for general iterables
950
- it = islice(iterable, start, stop)
951
- for i, element in enumerate(it , start):
945
+ iterator = islice(iterable, start, stop)
946
+ for i, element in enumerate(iterator , start):
952
947
if element is value or element == value:
953
948
yield i
954
949
else:
@@ -963,10 +958,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
963
958
pass
964
959
965
960
def iter_except(func, exception, first=None):
966
- """ Call a function repeatedly until an exception is raised.
967
-
968
- Converts a call-until-exception interface to an iterator interface.
969
- """
961
+ "Convert a call-until-exception interface to an iterator interface."
970
962
# iter_except(d.popitem, KeyError) → non-blocking dictionary iterator
971
963
try:
972
964
if first is not None:
@@ -1066,14 +1058,10 @@ The following recipes have a more mathematical flavor:
1066
1058
# sieve(30) → 2 3 5 7 11 13 17 19 23 29
1067
1059
if n > 2:
1068
1060
yield 2
1069
- start = 3
1070
1061
data = bytearray((0, 1)) * (n // 2)
1071
- limit = math.isqrt(n) + 1
1072
- for p in iter_index(data, 1, start, limit):
1073
- yield from iter_index(data, 1, start, p*p)
1062
+ for p in iter_index(data, 1, start=3, stop=math.isqrt(n) + 1):
1074
1063
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
1075
- start = p*p
1076
- yield from iter_index(data, 1, start)
1064
+ yield from iter_index(data, 1, start=3)
1077
1065
1078
1066
def factor(n):
1079
1067
"Prime factors of n."
@@ -1093,8 +1081,8 @@ The following recipes have a more mathematical flavor:
1093
1081
"Count of natural numbers up to n that are coprime to n."
1094
1082
# https://mathworld.wolfram.com/TotientFunction.html
1095
1083
# totient(12) → 4 because len([1, 5, 7, 11]) == 4
1096
- for p in unique_justseen (factor(n)):
1097
- n -= n // p
1084
+ for prime in set (factor(n)):
1085
+ n -= n // prime
1098
1086
return n
1099
1087
1100
1088
0 commit comments