Skip to content

Commit

Permalink
More generous whitespacing in doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger committed Oct 8, 2024
1 parent 7f164b7 commit 922a7bf
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1189,15 +1189,19 @@ The following recipes have a more mathematical flavor:
>>> list(it)
['d', 'e', 'f']


>>> list(prepend(1, [2, 3, 4]))
[1, 2, 3, 4]


>>> list(enumerate('abc'))
[(0, 'a'), (1, 'b'), (2, 'c')]


>>> list(islice(tabulate(lambda x: 2*x), 4))
[0, 2, 4, 6]


>>> list(tail(3, 'ABCDEFG'))
['E', 'F', 'G']
>>> # Verify the input is consumed greedily
Expand All @@ -1206,6 +1210,7 @@ The following recipes have a more mathematical flavor:
>>> list(input_iterator)
[]


>>> it = iter(range(10))
>>> consume(it, 3)
>>> # Verify the input is consumed lazily
Expand All @@ -1216,6 +1221,7 @@ The following recipes have a more mathematical flavor:
>>> next(it, 'Done')
'Done'


>>> nth('abcde', 3)
'd'
>>> nth('abcde', 9) is None
Expand All @@ -1227,6 +1233,7 @@ The following recipes have a more mathematical flavor:
>>> list(it)
['d', 'e']


>>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')]
[True, True, True, False, False]
>>> [all_equal(s, key=str.casefold) for s in ('', 'A', 'AaAa', 'AAAB', 'AAABA')]
Expand All @@ -1240,25 +1247,26 @@ The following recipes have a more mathematical flavor:
>>> ''.join(it)
'bbccc'


>>> quantify(range(99), lambda x: x%2==0)
50

>>> quantify([True, False, False, True, True])
3

>>> quantify(range(12), predicate=lambda x: x%2==1)
6


>>> a = [[1, 2, 3], [4, 5, 6]]
>>> list(flatten(a))
[1, 2, 3, 4, 5, 6]


>>> list(repeatfunc(pow, 5, 2, 3))
[8, 8, 8, 8, 8]

>>> take(5, map(int, repeatfunc(random.random)))
[0, 0, 0, 0, 0]


>>> list(ncycles('abc', 3))
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
>>> # Verify greedy consumption of input iterator
Expand All @@ -1267,9 +1275,11 @@ The following recipes have a more mathematical flavor:
>>> list(input_iterator)
[]


>>> sum_of_squares([10, 20, 30])
1400


>>> list(reshape([(0, 1), (2, 3), (4, 5)], 3))
[(0, 1, 2), (3, 4, 5)]
>>> M = [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)]
Expand All @@ -1290,6 +1300,7 @@ The following recipes have a more mathematical flavor:
>>> list(reshape(M, 12))
[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)]


>>> list(transpose([(1, 2, 3), (11, 22, 33)]))
[(1, 11), (2, 22), (3, 33)]
>>> # Verify that the inputs are consumed lazily
Expand All @@ -1301,11 +1312,13 @@ The following recipes have a more mathematical flavor:
>>> list(zip(input1, input2))
[(2, 22), (3, 33)]


>>> list(matmul([(7, 5), (3, 5)], [[2, 5], [7, 9]]))
[(49, 80), (41, 60)]
>>> list(matmul([[2, 5], [7, 9], [3, 4]], [[7, 11, 5, 4, 9], [3, 5, 2, 6, 3]]))
[(29, 47, 20, 38, 33), (76, 122, 53, 82, 90), (33, 53, 23, 36, 39)]


>>> list(convolve([1, -1, -20], [1, -3])) == [1, -4, -17, 60]
True
>>> data = [20, 40, 24, 32, 20, 28, 16]
Expand All @@ -1328,6 +1341,7 @@ The following recipes have a more mathematical flavor:
>>> list(signal_iterator)
[30, 40, 50]


>>> from fractions import Fraction
>>> from decimal import Decimal
>>> polynomial_eval([1, -4, -17, 60], x=5)
Expand Down Expand Up @@ -1359,16 +1373,19 @@ The following recipes have a more mathematical flavor:
>>> polynomial_eval([11, 2], 7) == 11 * 7 + 2
True


>>> polynomial_from_roots([5, -4, 3])
[1, -4, -17, 60]
>>> factored = lambda x: (x - 5) * (x + 4) * (x - 3)
>>> expanded = lambda x: x**3 -4*x**2 -17*x + 60
>>> all(factored(x) == expanded(x) for x in range(-10, 11))
True


>>> polynomial_derivative([1, -4, -17, 60])
[3, -8, -17]


>>> list(iter_index('AABCADEAF', 'A'))
[0, 1, 4, 7]
>>> list(iter_index('AABCADEAF', 'B'))
Expand Down Expand Up @@ -1426,12 +1443,14 @@ The following recipes have a more mathematical flavor:
>>> ''.join(input_iterator)
'DEAF'


>>> # Verify that the target value can be a sequence.
>>> seq = [[10, 20], [30, 40], 30, 40, [30, 40], 50]
>>> target = [30, 40]
>>> list(iter_index(seq, target))
[1, 4]


>>> # Verify faithfulness to type specific index() method behaviors.
>>> # For example, bytes and str perform continuous-subsequence searches
>>> # that do not match the general behavior specified
Expand Down Expand Up @@ -1461,6 +1480,7 @@ The following recipes have a more mathematical flavor:
>>> set(sieve(10_000)).isdisjoint(carmichael)
True


>>> list(factor(99)) # Code example 1
[3, 3, 11]
>>> list(factor(1_000_000_000_000_007)) # Code example 2
Expand Down Expand Up @@ -1506,6 +1526,7 @@ The following recipes have a more mathematical flavor:
>>> all(list(factor(n)) == sorted(factor(n)) for n in range(2_000))
True


>>> totient(0) # https://www.wolframalpha.com/input?i=totient+0
0
>>> first_totients = [1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16, 6,
Expand All @@ -1525,9 +1546,11 @@ The following recipes have a more mathematical flavor:
>>> totient(6 ** 20) == 1 * 2**19 * 2 * 3**19 # repeated primes
True


>>> list(flatten([('a', 'b'), (), ('c', 'd', 'e'), ('f',), ('g', 'h', 'i')]))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']


>>> random.seed(85753098575309)
>>> list(repeatfunc(random.random, 3))
[0.16370491282496968, 0.45889608687313455, 0.3747076837820118]
Expand All @@ -1536,9 +1559,11 @@ The following recipes have a more mathematical flavor:
>>> list(repeatfunc(pow, 3, 2, 5))
[32, 32, 32]


>>> list(grouper('abcdefg', 3, fillvalue='x'))
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]


>>> it = grouper('abcdefg', 3, incomplete='strict')
>>> next(it)
('a', 'b', 'c')
Expand All @@ -1552,6 +1577,7 @@ The following recipes have a more mathematical flavor:
>>> list(grouper('abcdefg', n=3, incomplete='ignore'))
[('a', 'b', 'c'), ('d', 'e', 'f')]


>>> list(sliding_window('ABCDEFG', 1))
[('A',), ('B',), ('C',), ('D',), ('E',), ('F',), ('G',)]
>>> list(sliding_window('ABCDEFG', 2))
Expand Down Expand Up @@ -1581,6 +1607,7 @@ The following recipes have a more mathematical flavor:
...
'zero or negative n not supported'


>>> list(roundrobin('abc', 'd', 'ef'))
['a', 'd', 'e', 'b', 'f', 'c']
>>> ranges = [range(5, 1000), range(4, 3000), range(0), range(3, 2000), range(2, 5000), range(1, 3500)]
Expand All @@ -1594,18 +1621,19 @@ The following recipes have a more mathematical flavor:
>>> ''.join(chain(*input_iterators))
'dijkopqr'


>>> list(subslices('ABCD'))
['A', 'AB', 'ABC', 'ABCD', 'B', 'BC', 'BCD', 'C', 'CD', 'D']


>>> list(powerset([1,2,3]))
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]

>>> all(len(list(powerset(range(n)))) == 2**n for n in range(18))
True

>>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len)
True


>>> list(unique_everseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D']
>>> list(unique_everseen('ABBCcAD', str.casefold))
Expand All @@ -1620,6 +1648,7 @@ The following recipes have a more mathematical flavor:
>>> ''.join(input_iterator)
'AAABBBCCDAABBB'


>>> list(unique_justseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D', 'A', 'B']
>>> list(unique_justseen('ABBCcAD', str.casefold))
Expand All @@ -1634,13 +1663,15 @@ The following recipes have a more mathematical flavor:
>>> ''.join(input_iterator)
'AAABBBCCDAABBB'


>>> list(unique([[1, 2], [3, 4], [1, 2]]))
[[1, 2], [3, 4]]
>>> list(unique('ABBcCAD', str.casefold))
['A', 'B', 'c', 'D']
>>> list(unique('ABBcCAD', str.casefold, reverse=True))
['D', 'c', 'B', 'A']


>>> d = dict(a=1, b=2, c=3)
>>> it = iter_except(d.popitem, KeyError)
>>> d['d'] = 4
Expand All @@ -1658,6 +1689,7 @@ The following recipes have a more mathematical flavor:
>>> next(it, 'empty')
'empty'


>>> first_true('ABC0DEF1', '9', str.isdigit)
'0'
>>> # Verify that inputs are consumed lazily
Expand Down Expand Up @@ -1755,39 +1787,45 @@ The following recipes have a more mathematical flavor:
>>> dotproduct([1,2,3], [4,5,6])
32


>>> sumprod([1,2,3], [4,5,6])
32


>>> list(islice(pad_none('abc'), 0, 6))
['a', 'b', 'c', None, None, None]


>>> list(triplewise('ABCDEFG'))
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]


>>> population = 'ABCDEFGH'
>>> for r in range(len(population) + 1):
... seq = list(combinations(population, r))
... for i in range(len(seq)):
... assert nth_combination(population, r, i) == seq[i]
... for i in range(-len(seq), 0):
... assert nth_combination(population, r, i) == seq[i]

...
>>> iterable = 'abcde'
>>> r = 3
>>> combos = list(combinations(iterable, r))
>>> all(nth_combination(iterable, r, i) == comb for i, comb in enumerate(combos))
True


>>> it = iter('ABCdEfGhI')
>>> all_upper, remainder = before_and_after(str.isupper, it)
>>> ''.join(all_upper)
'ABC'
>>> ''.join(remainder)
'dEfGhI'


>>> def is_odd(x):
... return x % 2 == 1

...
>>> evens, odds = partition(is_odd, range(10))
>>> list(evens)
[0, 2, 4, 6, 8]
Expand Down

0 comments on commit 922a7bf

Please sign in to comment.