Skip to content

Commit 7da5737

Browse files
[3.12] Minor improvements to the itertools recipes (GH-118563) (#118565)
1 parent 4a18a9b commit 7da5737

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

Doc/library/itertools.rst

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -818,10 +818,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
818818
return map(function, count(start))
819819

820820
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."
825822
if times is None:
826823
return starmap(func, repeat(args))
827824
return starmap(func, repeat(args, times))
@@ -843,10 +840,8 @@ and :term:`generators <generator>` which incur interpreter overhead.
843840
"Advance the iterator n-steps ahead. If n is None, consume entirely."
844841
# Use functions that consume iterators at C speed.
845842
if n is None:
846-
# feed the entire iterator into a zero-length deque
847843
collections.deque(iterator, maxlen=0)
848844
else:
849-
# advance to the empty slice starting at position n
850845
next(islice(iterator, n, n), None)
851846

852847
def nth(iterable, n, default=None):
@@ -865,7 +860,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
865860

866861
def all_equal(iterable, key=None):
867862
"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
869864
return len(take(2, groupby(iterable, key))) <= 1
870865

871866
def unique_justseen(iterable, key=None):
@@ -895,9 +890,9 @@ and :term:`generators <generator>` which incur interpreter overhead.
895890
def sliding_window(iterable, n):
896891
"Collect data into overlapping fixed-length chunks or blocks."
897892
# 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:
901896
window.append(x)
902897
yield tuple(window)
903898

@@ -947,8 +942,8 @@ and :term:`generators <generator>` which incur interpreter overhead.
947942
seq_index = getattr(iterable, 'index', None)
948943
if seq_index is None:
949944
# 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):
952947
if element is value or element == value:
953948
yield i
954949
else:
@@ -963,10 +958,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
963958
pass
964959

965960
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."
970962
# iter_except(d.popitem, KeyError) → non-blocking dictionary iterator
971963
try:
972964
if first is not None:
@@ -1066,14 +1058,10 @@ The following recipes have a more mathematical flavor:
10661058
# sieve(30) → 2 3 5 7 11 13 17 19 23 29
10671059
if n > 2:
10681060
yield 2
1069-
start = 3
10701061
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):
10741063
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)
10771065

10781066
def factor(n):
10791067
"Prime factors of n."
@@ -1093,8 +1081,8 @@ The following recipes have a more mathematical flavor:
10931081
"Count of natural numbers up to n that are coprime to n."
10941082
# https://mathworld.wolfram.com/TotientFunction.html
10951083
# 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
10981086
return n
10991087

11001088

0 commit comments

Comments
 (0)