Skip to content

Commit ee60550

Browse files
authored
Move doctests to the main docs. Eliminate duplication. Improve coverage. (GH-30869)
1 parent 96bf84d commit ee60550

File tree

2 files changed

+238
-393
lines changed

2 files changed

+238
-393
lines changed

Doc/library/itertools.rst

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,3 +1004,241 @@ which incur interpreter overhead.
10041004
c, n = c*(n-r)//n, n-1
10051005
result.append(pool[-1-n])
10061006
return tuple(result)
1007+
1008+
.. doctest::
1009+
:hide:
1010+
1011+
These examples no longer appear in the docs but are guaranteed
1012+
to keep working.
1013+
1014+
>>> amounts = [120.15, 764.05, 823.14]
1015+
>>> for checknum, amount in zip(count(1200), amounts):
1016+
... print('Check %d is for $%.2f' % (checknum, amount))
1017+
...
1018+
Check 1200 is for $120.15
1019+
Check 1201 is for $764.05
1020+
Check 1202 is for $823.14
1021+
1022+
>>> import operator
1023+
>>> for cube in map(operator.pow, range(1,4), repeat(3)):
1024+
... print(cube)
1025+
...
1026+
1
1027+
8
1028+
27
1029+
1030+
>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
1031+
>>> for name in islice(reportlines, 3, None, 2):
1032+
... print(name.title())
1033+
...
1034+
Alex
1035+
Laura
1036+
Martin
1037+
Walter
1038+
Samuele
1039+
1040+
>>> from operator import itemgetter
1041+
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
1042+
>>> di = sorted(sorted(d.items()), key=itemgetter(1))
1043+
>>> for k, g in groupby(di, itemgetter(1)):
1044+
... print(k, list(map(itemgetter(0), g)))
1045+
...
1046+
1 ['a', 'c', 'e']
1047+
2 ['b', 'd', 'f']
1048+
3 ['g']
1049+
1050+
# Find runs of consecutive numbers using groupby. The key to the solution
1051+
# is differencing with a range so that consecutive numbers all appear in
1052+
# same group.
1053+
>>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
1054+
>>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]):
1055+
... print(list(map(operator.itemgetter(1), g)))
1056+
...
1057+
[1]
1058+
[4, 5, 6]
1059+
[10]
1060+
[15, 16, 17, 18]
1061+
[22]
1062+
[25, 26, 27, 28]
1063+
1064+
Now, we test all of the itertool recipes
1065+
1066+
>>> import operator
1067+
>>> import collections
1068+
1069+
>>> take(10, count())
1070+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1071+
1072+
>>> list(prepend(1, [2, 3, 4]))
1073+
[1, 2, 3, 4]
1074+
1075+
>>> list(enumerate('abc'))
1076+
[(0, 'a'), (1, 'b'), (2, 'c')]
1077+
1078+
>>> list(islice(tabulate(lambda x: 2*x), 4))
1079+
[0, 2, 4, 6]
1080+
1081+
>>> list(tail(3, 'ABCDEFG'))
1082+
['E', 'F', 'G']
1083+
1084+
>>> it = iter(range(10))
1085+
>>> consume(it, 3)
1086+
>>> next(it)
1087+
3
1088+
>>> consume(it)
1089+
>>> next(it, 'Done')
1090+
'Done'
1091+
1092+
>>> nth('abcde', 3)
1093+
'd'
1094+
1095+
>>> nth('abcde', 9) is None
1096+
True
1097+
1098+
>>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')]
1099+
[True, True, True, False, False]
1100+
1101+
>>> quantify(range(99), lambda x: x%2==0)
1102+
50
1103+
1104+
>>> quantify([True, False, False, True, True])
1105+
3
1106+
1107+
>>> quantify(range(12), pred=lambda x: x%2==1)
1108+
6
1109+
1110+
>>> a = [[1, 2, 3], [4, 5, 6]]
1111+
>>> list(flatten(a))
1112+
[1, 2, 3, 4, 5, 6]
1113+
1114+
>>> list(repeatfunc(pow, 5, 2, 3))
1115+
[8, 8, 8, 8, 8]
1116+
1117+
>>> import random
1118+
>>> take(5, map(int, repeatfunc(random.random)))
1119+
[0, 0, 0, 0, 0]
1120+
1121+
>>> list(islice(pad_none('abc'), 0, 6))
1122+
['a', 'b', 'c', None, None, None]
1123+
1124+
>>> list(ncycles('abc', 3))
1125+
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
1126+
1127+
>>> dotproduct([1,2,3], [4,5,6])
1128+
32
1129+
1130+
>>> data = [20, 40, 24, 32, 20, 28, 16]
1131+
>>> list(convolve(data, [0.25, 0.25, 0.25, 0.25]))
1132+
[5.0, 15.0, 21.0, 29.0, 29.0, 26.0, 24.0, 16.0, 11.0, 4.0]
1133+
>>> list(convolve(data, [1, -1]))
1134+
[20, 20, -16, 8, -12, 8, -12, -16]
1135+
>>> list(convolve(data, [1, -2, 1]))
1136+
[20, 0, -36, 24, -20, 20, -20, -4, 16]
1137+
1138+
>>> list(flatten([('a', 'b'), (), ('c', 'd', 'e'), ('f',), ('g', 'h', 'i')]))
1139+
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
1140+
1141+
>>> import random
1142+
>>> random.seed(85753098575309)
1143+
>>> list(repeatfunc(random.random, 3))
1144+
[0.16370491282496968, 0.45889608687313455, 0.3747076837820118]
1145+
>>> list(repeatfunc(chr, 3, 65))
1146+
['A', 'A', 'A']
1147+
>>> list(repeatfunc(pow, 3, 2, 5))
1148+
[32, 32, 32]
1149+
1150+
>>> list(grouper('abcdefg', 3, fillvalue='x'))
1151+
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
1152+
1153+
>>> it = grouper('abcdefg', 3, incomplete='strict')
1154+
>>> next(it)
1155+
('a', 'b', 'c')
1156+
>>> next(it)
1157+
('d', 'e', 'f')
1158+
>>> next(it)
1159+
Traceback (most recent call last):
1160+
...
1161+
ValueError: zip() argument 2 is shorter than argument 1
1162+
1163+
>>> list(grouper('abcdefg', n=3, incomplete='ignore'))
1164+
[('a', 'b', 'c'), ('d', 'e', 'f')]
1165+
1166+
>>> list(triplewise('ABCDEFG'))
1167+
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
1168+
1169+
>>> list(sliding_window('ABCDEFG', 4))
1170+
[('A', 'B', 'C', 'D'), ('B', 'C', 'D', 'E'), ('C', 'D', 'E', 'F'), ('D', 'E', 'F', 'G')]
1171+
1172+
>>> list(roundrobin('abc', 'd', 'ef'))
1173+
['a', 'd', 'e', 'b', 'f', 'c']
1174+
1175+
>>> def is_odd(x):
1176+
... return x % 2 == 1
1177+
1178+
>>> evens, odds = partition(is_odd, range(10))
1179+
>>> list(evens)
1180+
[0, 2, 4, 6, 8]
1181+
>>> list(odds)
1182+
[1, 3, 5, 7, 9]
1183+
1184+
>>> it = iter('ABCdEfGhI')
1185+
>>> all_upper, remainder = before_and_after(str.isupper, it)
1186+
>>> ''.join(all_upper)
1187+
'ABC'
1188+
>>> ''.join(remainder)
1189+
'dEfGhI'
1190+
1191+
>>> list(powerset([1,2,3]))
1192+
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
1193+
1194+
>>> all(len(list(powerset(range(n)))) == 2**n for n in range(18))
1195+
True
1196+
1197+
>>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len)
1198+
True
1199+
1200+
>>> list(unique_everseen('AAAABBBCCDAABBB'))
1201+
['A', 'B', 'C', 'D']
1202+
1203+
>>> list(unique_everseen('ABBCcAD', str.lower))
1204+
['A', 'B', 'C', 'D']
1205+
1206+
>>> list(unique_justseen('AAAABBBCCDAABBB'))
1207+
['A', 'B', 'C', 'D', 'A', 'B']
1208+
1209+
>>> list(unique_justseen('ABBCcAD', str.lower))
1210+
['A', 'B', 'C', 'A', 'D']
1211+
1212+
>>> d = dict(a=1, b=2, c=3)
1213+
>>> it = iter_except(d.popitem, KeyError)
1214+
>>> d['d'] = 4
1215+
>>> next(it)
1216+
('d', 4)
1217+
>>> next(it)
1218+
('c', 3)
1219+
>>> next(it)
1220+
('b', 2)
1221+
>>> d['e'] = 5
1222+
>>> next(it)
1223+
('e', 5)
1224+
>>> next(it)
1225+
('a', 1)
1226+
>>> next(it, 'empty')
1227+
'empty'
1228+
1229+
>>> first_true('ABC0DEF1', '9', str.isdigit)
1230+
'0'
1231+
1232+
>>> population = 'ABCDEFGH'
1233+
>>> for r in range(len(population) + 1):
1234+
... seq = list(combinations(population, r))
1235+
... for i in range(len(seq)):
1236+
... assert nth_combination(population, r, i) == seq[i]
1237+
... for i in range(-len(seq), 0):
1238+
... assert nth_combination(population, r, i) == seq[i]
1239+
1240+
>>> iterable = 'abcde'
1241+
>>> r = 3
1242+
>>> combos = list(combinations(iterable, r))
1243+
>>> all(nth_combination(iterable, r, i) == comb for i, comb in enumerate(combos))
1244+
True

0 commit comments

Comments
 (0)