Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
| 3 | from itertools import * |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 4 | import sys |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 5 | import operator |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 6 | import random |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 7 | |
| 8 | def onearg(x): |
| 9 | 'Test function of one argument' |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 10 | return 2*x |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 11 | |
| 12 | def errfunc(*args): |
| 13 | 'Test function that raises an error' |
| 14 | raise ValueError |
| 15 | |
| 16 | def gen3(): |
| 17 | 'Non-restartable source sequence' |
| 18 | for i in (0, 1, 2): |
| 19 | yield i |
| 20 | |
| 21 | def isEven(x): |
| 22 | 'Test predicate' |
| 23 | return x%2==0 |
| 24 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 25 | def isOdd(x): |
| 26 | 'Test predicate' |
| 27 | return x%2==1 |
| 28 | |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 29 | class StopNow: |
| 30 | 'Class emulating an empty iterable.' |
| 31 | def __iter__(self): |
| 32 | return self |
| 33 | def next(self): |
| 34 | raise StopIteration |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 35 | |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 36 | def take(n, seq): |
| 37 | 'Convenience function for partially consuming a long of infinite iterable' |
| 38 | return list(islice(seq, n)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 39 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 40 | class TestBasicOps(unittest.TestCase): |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 41 | def test_chain(self): |
| 42 | self.assertEqual(list(chain('abc', 'def')), list('abcdef')) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 43 | self.assertEqual(list(chain('abc')), list('abc')) |
| 44 | self.assertEqual(list(chain('')), []) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 45 | self.assertEqual(take(4, chain('abc', 'def')), list('abcd')) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 46 | self.assertRaises(TypeError, chain, 2, 3) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 47 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 48 | def test_count(self): |
| 49 | self.assertEqual(zip('abc',count()), [('a', 0), ('b', 1), ('c', 2)]) |
| 50 | self.assertEqual(zip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)]) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 51 | self.assertEqual(take(2, zip('abc',count(3))), [('a', 3), ('b', 4)]) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 52 | self.assertRaises(TypeError, count, 2, 3) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 53 | self.assertRaises(TypeError, count, 'a') |
| 54 | c = count(sys.maxint-2) # verify that rollover doesn't crash |
| 55 | c.next(); c.next(); c.next(); c.next(); c.next() |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 56 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 57 | def test_cycle(self): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 58 | self.assertEqual(take(10, cycle('abc')), list('abcabcabca')) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 59 | self.assertEqual(list(cycle('')), []) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 60 | self.assertRaises(TypeError, cycle) |
| 61 | self.assertRaises(TypeError, cycle, 5) |
| 62 | self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0]) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 63 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 64 | def test_groupby(self): |
| 65 | # Check whether it accepts arguments correctly |
| 66 | self.assertEqual([], list(groupby([]))) |
| 67 | self.assertEqual([], list(groupby([], key=id))) |
| 68 | self.assertRaises(TypeError, list, groupby('abc', [])) |
| 69 | self.assertRaises(TypeError, groupby, None) |
| 70 | |
| 71 | # Check normal input |
| 72 | s = [(0, 10, 20), (0, 11,21), (0,12,21), (1,13,21), (1,14,22), |
| 73 | (2,15,22), (3,16,23), (3,17,23)] |
| 74 | dup = [] |
| 75 | for k, g in groupby(s, lambda r:r[0]): |
| 76 | for elem in g: |
| 77 | self.assertEqual(k, elem[0]) |
| 78 | dup.append(elem) |
| 79 | self.assertEqual(s, dup) |
| 80 | |
| 81 | # Check nested case |
| 82 | dup = [] |
| 83 | for k, g in groupby(s, lambda r:r[0]): |
| 84 | for ik, ig in groupby(g, lambda r:r[2]): |
| 85 | for elem in ig: |
| 86 | self.assertEqual(k, elem[0]) |
| 87 | self.assertEqual(ik, elem[2]) |
| 88 | dup.append(elem) |
| 89 | self.assertEqual(s, dup) |
| 90 | |
| 91 | # Check case where inner iterator is not used |
| 92 | keys = [k for k, g in groupby(s, lambda r:r[0])] |
| 93 | expectedkeys = set([r[0] for r in s]) |
| 94 | self.assertEqual(set(keys), expectedkeys) |
| 95 | self.assertEqual(len(keys), len(expectedkeys)) |
| 96 | |
| 97 | # Exercise pipes and filters style |
| 98 | s = 'abracadabra' |
| 99 | # sort s | uniq |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 100 | r = [k for k, g in groupby(sorted(s))] |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 101 | self.assertEqual(r, ['a', 'b', 'c', 'd', 'r']) |
| 102 | # sort s | uniq -d |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 103 | r = [k for k, g in groupby(sorted(s)) if list(islice(g,1,2))] |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 104 | self.assertEqual(r, ['a', 'b', 'r']) |
| 105 | # sort s | uniq -c |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 106 | r = [(len(list(g)), k) for k, g in groupby(sorted(s))] |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 107 | self.assertEqual(r, [(5, 'a'), (2, 'b'), (1, 'c'), (1, 'd'), (2, 'r')]) |
| 108 | # sort s | uniq -c | sort -rn | head -3 |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 109 | r = sorted([(len(list(g)) , k) for k, g in groupby(sorted(s))], reverse=True)[:3] |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 110 | self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')]) |
| 111 | |
| 112 | # iter.next failure |
| 113 | class ExpectedError(Exception): |
| 114 | pass |
| 115 | def delayed_raise(n=0): |
| 116 | for i in range(n): |
| 117 | yield 'yo' |
| 118 | raise ExpectedError |
| 119 | def gulp(iterable, keyp=None, func=list): |
| 120 | return [func(g) for k, g in groupby(iterable, keyp)] |
| 121 | |
| 122 | # iter.next failure on outer object |
| 123 | self.assertRaises(ExpectedError, gulp, delayed_raise(0)) |
| 124 | # iter.next failure on inner object |
| 125 | self.assertRaises(ExpectedError, gulp, delayed_raise(1)) |
| 126 | |
| 127 | # __cmp__ failure |
| 128 | class DummyCmp: |
| 129 | def __cmp__(self, dst): |
| 130 | raise ExpectedError |
| 131 | s = [DummyCmp(), DummyCmp(), None] |
| 132 | |
| 133 | # __cmp__ failure on outer object |
| 134 | self.assertRaises(ExpectedError, gulp, s, func=id) |
| 135 | # __cmp__ failure on inner object |
| 136 | self.assertRaises(ExpectedError, gulp, s) |
| 137 | |
| 138 | # keyfunc failure |
| 139 | def keyfunc(obj): |
| 140 | if keyfunc.skip > 0: |
| 141 | keyfunc.skip -= 1 |
| 142 | return obj |
| 143 | else: |
| 144 | raise ExpectedError |
| 145 | |
| 146 | # keyfunc failure on outer object |
| 147 | keyfunc.skip = 0 |
| 148 | self.assertRaises(ExpectedError, gulp, [None], keyfunc) |
| 149 | keyfunc.skip = 1 |
| 150 | self.assertRaises(ExpectedError, gulp, [None, None], keyfunc) |
| 151 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 152 | def test_ifilter(self): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 153 | self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4]) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 154 | self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2]) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 155 | self.assertEqual(take(4, ifilter(isEven, count())), [0,2,4,6]) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 156 | self.assertRaises(TypeError, ifilter) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 157 | self.assertRaises(TypeError, ifilter, lambda x:x) |
| 158 | self.assertRaises(TypeError, ifilter, lambda x:x, range(6), 7) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 159 | self.assertRaises(TypeError, ifilter, isEven, 3) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 160 | self.assertRaises(TypeError, ifilter(range(6), range(6)).next) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 161 | |
| 162 | def test_ifilterfalse(self): |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 163 | self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5]) |
| 164 | self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0]) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 165 | self.assertEqual(take(4, ifilterfalse(isEven, count())), [1,3,5,7]) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 166 | self.assertRaises(TypeError, ifilterfalse) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 167 | self.assertRaises(TypeError, ifilterfalse, lambda x:x) |
| 168 | self.assertRaises(TypeError, ifilterfalse, lambda x:x, range(6), 7) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 169 | self.assertRaises(TypeError, ifilterfalse, isEven, 3) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 170 | self.assertRaises(TypeError, ifilterfalse(range(6), range(6)).next) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 171 | |
| 172 | def test_izip(self): |
| 173 | ans = [(x,y) for x, y in izip('abc',count())] |
| 174 | self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 175 | self.assertEqual(list(izip('abc', range(6))), zip('abc', range(6))) |
| 176 | self.assertEqual(list(izip('abcdef', range(3))), zip('abcdef', range(3))) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 177 | self.assertEqual(take(3,izip('abcdef', count())), zip('abcdef', range(3))) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 178 | self.assertEqual(list(izip('abcdef')), zip('abcdef')) |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 179 | self.assertEqual(list(izip()), zip()) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 180 | self.assertRaises(TypeError, izip, 3) |
| 181 | self.assertRaises(TypeError, izip, range(3), 3) |
| 182 | # Check tuple re-use (implementation detail) |
| 183 | self.assertEqual([tuple(list(pair)) for pair in izip('abc', 'def')], |
| 184 | zip('abc', 'def')) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 185 | self.assertEqual([pair for pair in izip('abc', 'def')], |
| 186 | zip('abc', 'def')) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 187 | ids = map(id, izip('abc', 'def')) |
| 188 | self.assertEqual(min(ids), max(ids)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 189 | ids = map(id, list(izip('abc', 'def'))) |
| 190 | self.assertEqual(len(dict.fromkeys(ids)), len(ids)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 191 | |
| 192 | def test_repeat(self): |
| 193 | self.assertEqual(zip(xrange(3),repeat('a')), |
| 194 | [(0, 'a'), (1, 'a'), (2, 'a')]) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 195 | self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a']) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 196 | self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a']) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 197 | self.assertEqual(list(repeat('a', 0)), []) |
| 198 | self.assertEqual(list(repeat('a', -3)), []) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 199 | self.assertRaises(TypeError, repeat) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 200 | self.assertRaises(TypeError, repeat, None, 3, 4) |
| 201 | self.assertRaises(TypeError, repeat, None, 'a') |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 202 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 203 | def test_imap(self): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 204 | self.assertEqual(list(imap(operator.pow, range(3), range(1,7))), |
| 205 | [0**1, 1**2, 2**3]) |
| 206 | self.assertEqual(list(imap(None, 'abc', range(5))), |
| 207 | [('a',0),('b',1),('c',2)]) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 208 | self.assertEqual(list(imap(None, 'abc', count())), |
| 209 | [('a',0),('b',1),('c',2)]) |
| 210 | self.assertEqual(take(2,imap(None, 'abc', count())), |
| 211 | [('a',0),('b',1)]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 212 | self.assertEqual(list(imap(operator.pow, [])), []) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 213 | self.assertRaises(TypeError, imap) |
| 214 | self.assertRaises(TypeError, imap, operator.neg) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 215 | self.assertRaises(TypeError, imap(10, range(5)).next) |
| 216 | self.assertRaises(ValueError, imap(errfunc, [4], [5]).next) |
| 217 | self.assertRaises(TypeError, imap(onearg, [4], [5]).next) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 218 | |
| 219 | def test_starmap(self): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 220 | self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))), |
| 221 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 222 | self.assertEqual(take(3, starmap(operator.pow, izip(count(), count(1)))), |
| 223 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 224 | self.assertEqual(list(starmap(operator.pow, [])), []) |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 225 | self.assertRaises(TypeError, list, starmap(operator.pow, [[4,5]])) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 226 | self.assertRaises(TypeError, starmap) |
| 227 | self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra') |
| 228 | self.assertRaises(TypeError, starmap(10, [(4,5)]).next) |
| 229 | self.assertRaises(ValueError, starmap(errfunc, [(4,5)]).next) |
| 230 | self.assertRaises(TypeError, starmap(onearg, [(4,5)]).next) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 231 | |
| 232 | def test_islice(self): |
| 233 | for args in [ # islice(args) should agree with range(args) |
| 234 | (10, 20, 3), |
| 235 | (10, 3, 20), |
| 236 | (10, 20), |
| 237 | (10, 3), |
| 238 | (20,) |
| 239 | ]: |
| 240 | self.assertEqual(list(islice(xrange(100), *args)), range(*args)) |
| 241 | |
| 242 | for args, tgtargs in [ # Stop when seqn is exhausted |
| 243 | ((10, 110, 3), ((10, 100, 3))), |
| 244 | ((10, 110), ((10, 100))), |
| 245 | ((110,), (100,)) |
| 246 | ]: |
| 247 | self.assertEqual(list(islice(xrange(100), *args)), range(*tgtargs)) |
| 248 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 249 | # Test stop=None |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 250 | self.assertEqual(list(islice(xrange(10), None)), range(10)) |
| 251 | self.assertEqual(list(islice(xrange(10), 2, None)), range(2, 10)) |
| 252 | self.assertEqual(list(islice(xrange(10), 1, None, 2)), range(1, 10, 2)) |
| 253 | |
| 254 | # Test invalid arguments |
Raymond Hettinger | 341deb7 | 2003-05-02 19:44:20 +0000 | [diff] [blame] | 255 | self.assertRaises(TypeError, islice, xrange(10)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 256 | self.assertRaises(TypeError, islice, xrange(10), 1, 2, 3, 4) |
| 257 | self.assertRaises(ValueError, islice, xrange(10), -5, 10, 1) |
| 258 | self.assertRaises(ValueError, islice, xrange(10), 1, -5, -1) |
| 259 | self.assertRaises(ValueError, islice, xrange(10), 1, 10, -1) |
| 260 | self.assertRaises(ValueError, islice, xrange(10), 1, 10, 0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 261 | self.assertRaises(ValueError, islice, xrange(10), 'a') |
| 262 | self.assertRaises(ValueError, islice, xrange(10), 'a', 1) |
| 263 | self.assertRaises(ValueError, islice, xrange(10), 1, 'a') |
| 264 | self.assertRaises(ValueError, islice, xrange(10), 'a', 1, 1) |
| 265 | self.assertRaises(ValueError, islice, xrange(10), 1, 'a', 1) |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 266 | self.assertEqual(len(list(islice(count(), 1, 10, sys.maxint))), 1) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 267 | |
| 268 | def test_takewhile(self): |
| 269 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 270 | underten = lambda x: x<10 |
| 271 | self.assertEqual(list(takewhile(underten, data)), [1, 3, 5]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 272 | self.assertEqual(list(takewhile(underten, [])), []) |
| 273 | self.assertRaises(TypeError, takewhile) |
| 274 | self.assertRaises(TypeError, takewhile, operator.pow) |
| 275 | self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra') |
| 276 | self.assertRaises(TypeError, takewhile(10, [(4,5)]).next) |
| 277 | self.assertRaises(ValueError, takewhile(errfunc, [(4,5)]).next) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 278 | |
| 279 | def test_dropwhile(self): |
| 280 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 281 | underten = lambda x: x<10 |
| 282 | self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 283 | self.assertEqual(list(dropwhile(underten, [])), []) |
| 284 | self.assertRaises(TypeError, dropwhile) |
| 285 | self.assertRaises(TypeError, dropwhile, operator.pow) |
| 286 | self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra') |
| 287 | self.assertRaises(TypeError, dropwhile(10, [(4,5)]).next) |
| 288 | self.assertRaises(ValueError, dropwhile(errfunc, [(4,5)]).next) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 289 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 290 | def test_tee(self): |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 291 | n = 200 |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 292 | def irange(n): |
| 293 | for i in xrange(n): |
| 294 | yield i |
| 295 | |
| 296 | a, b = tee([]) # test empty iterator |
| 297 | self.assertEqual(list(a), []) |
| 298 | self.assertEqual(list(b), []) |
| 299 | |
| 300 | a, b = tee(irange(n)) # test 100% interleaved |
| 301 | self.assertEqual(zip(a,b), zip(range(n),range(n))) |
| 302 | |
| 303 | a, b = tee(irange(n)) # test 0% interleaved |
| 304 | self.assertEqual(list(a), range(n)) |
| 305 | self.assertEqual(list(b), range(n)) |
| 306 | |
| 307 | a, b = tee(irange(n)) # test dealloc of leading iterator |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 308 | for i in xrange(100): |
| 309 | self.assertEqual(a.next(), i) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 310 | del a |
| 311 | self.assertEqual(list(b), range(n)) |
| 312 | |
| 313 | a, b = tee(irange(n)) # test dealloc of trailing iterator |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 314 | for i in xrange(100): |
| 315 | self.assertEqual(a.next(), i) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 316 | del b |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 317 | self.assertEqual(list(a), range(100, n)) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 318 | |
| 319 | for j in xrange(5): # test randomly interleaved |
| 320 | order = [0]*n + [1]*n |
| 321 | random.shuffle(order) |
| 322 | lists = ([], []) |
| 323 | its = tee(irange(n)) |
| 324 | for i in order: |
| 325 | value = its[i].next() |
| 326 | lists[i].append(value) |
| 327 | self.assertEqual(lists[0], range(n)) |
| 328 | self.assertEqual(lists[1], range(n)) |
| 329 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 330 | # test argument format checking |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 331 | self.assertRaises(TypeError, tee) |
| 332 | self.assertRaises(TypeError, tee, 3) |
| 333 | self.assertRaises(TypeError, tee, [1,2], 'x') |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 334 | self.assertRaises(TypeError, tee, [1,2], 3, 'x') |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 335 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 336 | # tee object should be instantiable |
| 337 | a, b = tee('abc') |
| 338 | c = type(a)('def') |
| 339 | self.assertEqual(list(c), list('def')) |
Raymond Hettinger | f0c5aec | 2003-10-26 14:25:56 +0000 | [diff] [blame] | 340 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 341 | # test long-lagged and multi-way split |
| 342 | a, b, c = tee(xrange(2000), 3) |
| 343 | for i in xrange(100): |
| 344 | self.assertEqual(a.next(), i) |
| 345 | self.assertEqual(list(b), range(2000)) |
| 346 | self.assertEqual([c.next(), c.next()], range(2)) |
| 347 | self.assertEqual(list(a), range(100,2000)) |
| 348 | self.assertEqual(list(c), range(2,2000)) |
| 349 | |
| 350 | # tee pass-through to copyable iterator |
| 351 | a, b = tee('abc') |
| 352 | c, d = tee(a) |
| 353 | self.assert_(a is c) |
| 354 | |
Raymond Hettinger | f0c5aec | 2003-10-26 14:25:56 +0000 | [diff] [blame] | 355 | |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 356 | def test_StopIteration(self): |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 357 | self.assertRaises(StopIteration, izip().next) |
| 358 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 359 | for f in (chain, cycle, izip, groupby): |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 360 | self.assertRaises(StopIteration, f([]).next) |
| 361 | self.assertRaises(StopIteration, f(StopNow()).next) |
| 362 | |
| 363 | self.assertRaises(StopIteration, islice([], None).next) |
| 364 | self.assertRaises(StopIteration, islice(StopNow(), None).next) |
| 365 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 366 | p, q = tee([]) |
| 367 | self.assertRaises(StopIteration, p.next) |
| 368 | self.assertRaises(StopIteration, q.next) |
| 369 | p, q = tee(StopNow()) |
| 370 | self.assertRaises(StopIteration, p.next) |
| 371 | self.assertRaises(StopIteration, q.next) |
| 372 | |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 373 | self.assertRaises(StopIteration, repeat(None, 0).next) |
| 374 | |
| 375 | for f in (ifilter, ifilterfalse, imap, takewhile, dropwhile, starmap): |
| 376 | self.assertRaises(StopIteration, f(lambda x:x, []).next) |
| 377 | self.assertRaises(StopIteration, f(lambda x:x, StopNow()).next) |
| 378 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 379 | class TestGC(unittest.TestCase): |
| 380 | |
| 381 | def makecycle(self, iterator, container): |
| 382 | container.append(iterator) |
| 383 | iterator.next() |
| 384 | del container, iterator |
| 385 | |
| 386 | def test_chain(self): |
| 387 | a = [] |
| 388 | self.makecycle(chain(a), a) |
| 389 | |
| 390 | def test_cycle(self): |
| 391 | a = [] |
| 392 | self.makecycle(cycle([a]*2), a) |
| 393 | |
| 394 | def test_ifilter(self): |
| 395 | a = [] |
| 396 | self.makecycle(ifilter(lambda x:True, [a]*2), a) |
| 397 | |
| 398 | def test_ifilterfalse(self): |
| 399 | a = [] |
| 400 | self.makecycle(ifilterfalse(lambda x:False, a), a) |
| 401 | |
| 402 | def test_izip(self): |
| 403 | a = [] |
| 404 | self.makecycle(izip([a]*2, [a]*3), a) |
| 405 | |
| 406 | def test_imap(self): |
| 407 | a = [] |
| 408 | self.makecycle(imap(lambda x:x, [a]*2), a) |
| 409 | |
| 410 | def test_islice(self): |
| 411 | a = [] |
| 412 | self.makecycle(islice([a]*2, None), a) |
| 413 | |
| 414 | def test_starmap(self): |
| 415 | a = [] |
| 416 | self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a) |
| 417 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 418 | def R(seqn): |
| 419 | 'Regular generator' |
| 420 | for i in seqn: |
| 421 | yield i |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 422 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 423 | class G: |
| 424 | 'Sequence using __getitem__' |
| 425 | def __init__(self, seqn): |
| 426 | self.seqn = seqn |
| 427 | def __getitem__(self, i): |
| 428 | return self.seqn[i] |
| 429 | |
| 430 | class I: |
| 431 | 'Sequence using iterator protocol' |
| 432 | def __init__(self, seqn): |
| 433 | self.seqn = seqn |
| 434 | self.i = 0 |
| 435 | def __iter__(self): |
| 436 | return self |
| 437 | def next(self): |
| 438 | if self.i >= len(self.seqn): raise StopIteration |
| 439 | v = self.seqn[self.i] |
| 440 | self.i += 1 |
| 441 | return v |
| 442 | |
| 443 | class Ig: |
| 444 | 'Sequence using iterator protocol defined with a generator' |
| 445 | def __init__(self, seqn): |
| 446 | self.seqn = seqn |
| 447 | self.i = 0 |
| 448 | def __iter__(self): |
| 449 | for val in self.seqn: |
| 450 | yield val |
| 451 | |
| 452 | class X: |
| 453 | 'Missing __getitem__ and __iter__' |
| 454 | def __init__(self, seqn): |
| 455 | self.seqn = seqn |
| 456 | self.i = 0 |
| 457 | def next(self): |
| 458 | if self.i >= len(self.seqn): raise StopIteration |
| 459 | v = self.seqn[self.i] |
| 460 | self.i += 1 |
| 461 | return v |
| 462 | |
| 463 | class N: |
| 464 | 'Iterator missing next()' |
| 465 | def __init__(self, seqn): |
| 466 | self.seqn = seqn |
| 467 | self.i = 0 |
| 468 | def __iter__(self): |
| 469 | return self |
| 470 | |
| 471 | class E: |
| 472 | 'Test propagation of exceptions' |
| 473 | def __init__(self, seqn): |
| 474 | self.seqn = seqn |
| 475 | self.i = 0 |
| 476 | def __iter__(self): |
| 477 | return self |
| 478 | def next(self): |
| 479 | 3/0 |
| 480 | |
| 481 | class S: |
| 482 | 'Test immediate stop' |
| 483 | def __init__(self, seqn): |
| 484 | pass |
| 485 | def __iter__(self): |
| 486 | return self |
| 487 | def next(self): |
| 488 | raise StopIteration |
| 489 | |
| 490 | def L(seqn): |
| 491 | 'Test multiple tiers of iterators' |
| 492 | return chain(imap(lambda x:x, R(Ig(G(seqn))))) |
| 493 | |
| 494 | |
| 495 | class TestVariousIteratorArgs(unittest.TestCase): |
| 496 | |
| 497 | def test_chain(self): |
| 498 | for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 499 | for g in (G, I, Ig, S, L, R): |
| 500 | self.assertEqual(list(chain(g(s))), list(g(s))) |
| 501 | self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s))) |
| 502 | self.assertRaises(TypeError, chain, X(s)) |
| 503 | self.assertRaises(TypeError, list, chain(N(s))) |
| 504 | self.assertRaises(ZeroDivisionError, list, chain(E(s))) |
| 505 | |
| 506 | def test_cycle(self): |
| 507 | for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 508 | for g in (G, I, Ig, S, L, R): |
| 509 | tgtlen = len(s) * 3 |
| 510 | expected = list(g(s))*3 |
| 511 | actual = list(islice(cycle(g(s)), tgtlen)) |
| 512 | self.assertEqual(actual, expected) |
| 513 | self.assertRaises(TypeError, cycle, X(s)) |
| 514 | self.assertRaises(TypeError, list, cycle(N(s))) |
| 515 | self.assertRaises(ZeroDivisionError, list, cycle(E(s))) |
| 516 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 517 | def test_groupby(self): |
| 518 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 519 | for g in (G, I, Ig, S, L, R): |
| 520 | self.assertEqual([k for k, sb in groupby(g(s))], list(g(s))) |
| 521 | self.assertRaises(TypeError, groupby, X(s)) |
| 522 | self.assertRaises(TypeError, list, groupby(N(s))) |
| 523 | self.assertRaises(ZeroDivisionError, list, groupby(E(s))) |
| 524 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 525 | def test_ifilter(self): |
| 526 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 527 | for g in (G, I, Ig, S, L, R): |
| 528 | self.assertEqual(list(ifilter(isEven, g(s))), filter(isEven, g(s))) |
| 529 | self.assertRaises(TypeError, ifilter, isEven, X(s)) |
| 530 | self.assertRaises(TypeError, list, ifilter(isEven, N(s))) |
| 531 | self.assertRaises(ZeroDivisionError, list, ifilter(isEven, E(s))) |
| 532 | |
| 533 | def test_ifilterfalse(self): |
| 534 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 535 | for g in (G, I, Ig, S, L, R): |
| 536 | self.assertEqual(list(ifilterfalse(isEven, g(s))), filter(isOdd, g(s))) |
| 537 | self.assertRaises(TypeError, ifilterfalse, isEven, X(s)) |
| 538 | self.assertRaises(TypeError, list, ifilterfalse(isEven, N(s))) |
| 539 | self.assertRaises(ZeroDivisionError, list, ifilterfalse(isEven, E(s))) |
| 540 | |
| 541 | def test_izip(self): |
| 542 | for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 543 | for g in (G, I, Ig, S, L, R): |
| 544 | self.assertEqual(list(izip(g(s))), zip(g(s))) |
| 545 | self.assertEqual(list(izip(g(s), g(s))), zip(g(s), g(s))) |
| 546 | self.assertRaises(TypeError, izip, X(s)) |
| 547 | self.assertRaises(TypeError, list, izip(N(s))) |
| 548 | self.assertRaises(ZeroDivisionError, list, izip(E(s))) |
| 549 | |
| 550 | def test_imap(self): |
| 551 | for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)): |
| 552 | for g in (G, I, Ig, S, L, R): |
| 553 | self.assertEqual(list(imap(onearg, g(s))), map(onearg, g(s))) |
| 554 | self.assertEqual(list(imap(operator.pow, g(s), g(s))), map(operator.pow, g(s), g(s))) |
| 555 | self.assertRaises(TypeError, imap, onearg, X(s)) |
| 556 | self.assertRaises(TypeError, list, imap(onearg, N(s))) |
| 557 | self.assertRaises(ZeroDivisionError, list, imap(onearg, E(s))) |
| 558 | |
| 559 | def test_islice(self): |
| 560 | for s in ("12345", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 561 | for g in (G, I, Ig, S, L, R): |
| 562 | self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2]) |
| 563 | self.assertRaises(TypeError, islice, X(s), 10) |
| 564 | self.assertRaises(TypeError, list, islice(N(s), 10)) |
| 565 | self.assertRaises(ZeroDivisionError, list, islice(E(s), 10)) |
| 566 | |
| 567 | def test_starmap(self): |
| 568 | for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)): |
| 569 | for g in (G, I, Ig, S, L, R): |
| 570 | ss = zip(s, s) |
| 571 | self.assertEqual(list(starmap(operator.pow, g(ss))), map(operator.pow, g(s), g(s))) |
| 572 | self.assertRaises(TypeError, starmap, operator.pow, X(ss)) |
| 573 | self.assertRaises(TypeError, list, starmap(operator.pow, N(ss))) |
| 574 | self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss))) |
| 575 | |
| 576 | def test_takewhile(self): |
| 577 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 578 | for g in (G, I, Ig, S, L, R): |
| 579 | tgt = [] |
| 580 | for elem in g(s): |
| 581 | if not isEven(elem): break |
| 582 | tgt.append(elem) |
| 583 | self.assertEqual(list(takewhile(isEven, g(s))), tgt) |
| 584 | self.assertRaises(TypeError, takewhile, isEven, X(s)) |
| 585 | self.assertRaises(TypeError, list, takewhile(isEven, N(s))) |
| 586 | self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s))) |
| 587 | |
| 588 | def test_dropwhile(self): |
| 589 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 590 | for g in (G, I, Ig, S, L, R): |
| 591 | tgt = [] |
| 592 | for elem in g(s): |
| 593 | if not tgt and isOdd(elem): continue |
| 594 | tgt.append(elem) |
| 595 | self.assertEqual(list(dropwhile(isOdd, g(s))), tgt) |
| 596 | self.assertRaises(TypeError, dropwhile, isOdd, X(s)) |
| 597 | self.assertRaises(TypeError, list, dropwhile(isOdd, N(s))) |
| 598 | self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s))) |
| 599 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 600 | def test_tee(self): |
| 601 | for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 602 | for g in (G, I, Ig, S, L, R): |
| 603 | it1, it2 = tee(g(s)) |
| 604 | self.assertEqual(list(it1), list(g(s))) |
| 605 | self.assertEqual(list(it2), list(g(s))) |
| 606 | self.assertRaises(TypeError, tee, X(s)) |
| 607 | self.assertRaises(TypeError, list, tee(N(s))[0]) |
| 608 | self.assertRaises(ZeroDivisionError, list, tee(E(s))[0]) |
| 609 | |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame^] | 610 | class LengthTransparency(unittest.TestCase): |
| 611 | |
| 612 | def test_repeat(self): |
| 613 | self.assertEqual(len(repeat(None, 50)), 50) |
| 614 | self.assertRaises(TypeError, len, repeat(None)) |
| 615 | |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 616 | class RegressionTests(unittest.TestCase): |
| 617 | |
| 618 | def test_sf_793826(self): |
| 619 | # Fix Armin Rigo's successful efforts to wreak havoc |
| 620 | |
| 621 | def mutatingtuple(tuple1, f, tuple2): |
| 622 | # this builds a tuple t which is a copy of tuple1, |
| 623 | # then calls f(t), then mutates t to be equal to tuple2 |
| 624 | # (needs len(tuple1) == len(tuple2)). |
| 625 | def g(value, first=[1]): |
| 626 | if first: |
| 627 | del first[:] |
| 628 | f(z.next()) |
| 629 | return value |
| 630 | items = list(tuple2) |
| 631 | items[1:1] = list(tuple1) |
| 632 | gen = imap(g, items) |
| 633 | z = izip(*[gen]*len(tuple1)) |
| 634 | z.next() |
| 635 | |
| 636 | def f(t): |
| 637 | global T |
| 638 | T = t |
| 639 | first[:] = list(T) |
| 640 | |
| 641 | first = [] |
| 642 | mutatingtuple((1,2,3), f, (4,5,6)) |
| 643 | second = list(T) |
| 644 | self.assertEqual(first, second) |
| 645 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 646 | |
| 647 | libreftest = """ Doctest for examples in the library reference: libitertools.tex |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 648 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 649 | |
| 650 | >>> amounts = [120.15, 764.05, 823.14] |
| 651 | >>> for checknum, amount in izip(count(1200), amounts): |
| 652 | ... print 'Check %d is for $%.2f' % (checknum, amount) |
| 653 | ... |
| 654 | Check 1200 is for $120.15 |
| 655 | Check 1201 is for $764.05 |
| 656 | Check 1202 is for $823.14 |
| 657 | |
| 658 | >>> import operator |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 659 | >>> for cube in imap(operator.pow, xrange(1,4), repeat(3)): |
| 660 | ... print cube |
| 661 | ... |
| 662 | 1 |
| 663 | 8 |
| 664 | 27 |
| 665 | |
| 666 | >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele'] |
Raymond Hettinger | 3567a87 | 2003-06-28 05:44:36 +0000 | [diff] [blame] | 667 | >>> for name in islice(reportlines, 3, None, 2): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 668 | ... print name.title() |
| 669 | ... |
| 670 | Alex |
| 671 | Laura |
| 672 | Martin |
| 673 | Walter |
| 674 | Samuele |
| 675 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 676 | >>> from operator import itemgetter |
| 677 | >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 678 | >>> di = sorted(d.iteritems(), key=itemgetter(1)) |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 679 | >>> for k, g in groupby(di, itemgetter(1)): |
| 680 | ... print k, map(itemgetter(0), g) |
| 681 | ... |
| 682 | 1 ['a', 'c', 'e'] |
| 683 | 2 ['b', 'd', 'f'] |
| 684 | 3 ['g'] |
| 685 | |
Raymond Hettinger | 734fb57 | 2004-01-20 20:04:40 +0000 | [diff] [blame] | 686 | # Find runs of consecutive numbers using groupby. The key to the solution |
| 687 | # is differencing with a range so that consecutive numbers all appear in |
| 688 | # same group. |
| 689 | >>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28] |
| 690 | >>> for k, g in groupby(enumerate(data), lambda (i,x):i-x): |
| 691 | ... print map(operator.itemgetter(1), g) |
| 692 | ... |
| 693 | [1] |
| 694 | [4, 5, 6] |
| 695 | [10] |
| 696 | [15, 16, 17, 18] |
| 697 | [22] |
| 698 | [25, 26, 27, 28] |
| 699 | |
Raymond Hettinger | a098b33 | 2003-09-08 23:58:40 +0000 | [diff] [blame] | 700 | >>> def take(n, seq): |
| 701 | ... return list(islice(seq, n)) |
| 702 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 703 | >>> def enumerate(iterable): |
| 704 | ... return izip(count(), iterable) |
| 705 | |
| 706 | >>> def tabulate(function): |
| 707 | ... "Return function(0), function(1), ..." |
| 708 | ... return imap(function, count()) |
| 709 | |
| 710 | >>> def iteritems(mapping): |
| 711 | ... return izip(mapping.iterkeys(), mapping.itervalues()) |
| 712 | |
| 713 | >>> def nth(iterable, n): |
| 714 | ... "Returns the nth item" |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 715 | ... return list(islice(iterable, n, n+1)) |
| 716 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 717 | >>> def all(seq, pred=bool): |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 718 | ... "Returns True if pred(x) is True for every element in the iterable" |
Raymond Hettinger | 77fe69b | 2003-08-08 04:33:19 +0000 | [diff] [blame] | 719 | ... return False not in imap(pred, seq) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 720 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 721 | >>> def any(seq, pred=bool): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 722 | ... "Returns True if pred(x) is True for at least one element in the iterable" |
Raymond Hettinger | 77fe69b | 2003-08-08 04:33:19 +0000 | [diff] [blame] | 723 | ... return True in imap(pred, seq) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 724 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 725 | >>> def no(seq, pred=bool): |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 726 | ... "Returns True if pred(x) is False for every element in the iterable" |
Raymond Hettinger | 77fe69b | 2003-08-08 04:33:19 +0000 | [diff] [blame] | 727 | ... return True not in imap(pred, seq) |
| 728 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 729 | >>> def quantify(seq, pred=bool): |
Raymond Hettinger | 77fe69b | 2003-08-08 04:33:19 +0000 | [diff] [blame] | 730 | ... "Count how many times the predicate is True in the sequence" |
| 731 | ... return sum(imap(pred, seq)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 732 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 733 | >>> def padnone(seq): |
| 734 | ... "Returns the sequence elements and then returns None indefinitely" |
| 735 | ... return chain(seq, repeat(None)) |
| 736 | |
| 737 | >>> def ncycles(seq, n): |
| 738 | ... "Returns the sequence elements n times" |
| 739 | ... return chain(*repeat(seq, n)) |
| 740 | |
| 741 | >>> def dotproduct(vec1, vec2): |
| 742 | ... return sum(imap(operator.mul, vec1, vec2)) |
| 743 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 744 | >>> def flatten(listOfLists): |
| 745 | ... return list(chain(*listOfLists)) |
| 746 | |
| 747 | >>> def repeatfunc(func, times=None, *args): |
| 748 | ... "Repeat calls to func with specified arguments." |
| 749 | ... " Example: repeatfunc(random.random)" |
| 750 | ... if times is None: |
| 751 | ... return starmap(func, repeat(args)) |
| 752 | ... else: |
| 753 | ... return starmap(func, repeat(args, times)) |
| 754 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 755 | >>> def pairwise(iterable): |
| 756 | ... "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
| 757 | ... a, b = tee(iterable) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 758 | ... try: |
| 759 | ... b.next() |
| 760 | ... except StopIteration: |
| 761 | ... pass |
| 762 | ... return izip(a, b) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 763 | |
| 764 | This is not part of the examples but it tests to make sure the definitions |
| 765 | perform as purported. |
| 766 | |
Raymond Hettinger | a098b33 | 2003-09-08 23:58:40 +0000 | [diff] [blame] | 767 | >>> take(10, count()) |
| 768 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 769 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 770 | >>> list(enumerate('abc')) |
| 771 | [(0, 'a'), (1, 'b'), (2, 'c')] |
| 772 | |
| 773 | >>> list(islice(tabulate(lambda x: 2*x), 4)) |
| 774 | [0, 2, 4, 6] |
| 775 | |
| 776 | >>> nth('abcde', 3) |
| 777 | ['d'] |
| 778 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 779 | >>> all([2, 4, 6, 8], lambda x: x%2==0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 780 | True |
| 781 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 782 | >>> all([2, 3, 6, 8], lambda x: x%2==0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 783 | False |
| 784 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 785 | >>> any([2, 4, 6, 8], lambda x: x%2==0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 786 | True |
| 787 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 788 | >>> any([1, 3, 5, 9], lambda x: x%2==0,) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 789 | False |
| 790 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 791 | >>> no([1, 3, 5, 9], lambda x: x%2==0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 792 | True |
| 793 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 794 | >>> no([1, 2, 5, 9], lambda x: x%2==0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 795 | False |
| 796 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 797 | >>> quantify(xrange(99), lambda x: x%2==0) |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 798 | 50 |
| 799 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 800 | >>> a = [[1, 2, 3], [4, 5, 6]] |
| 801 | >>> flatten(a) |
| 802 | [1, 2, 3, 4, 5, 6] |
| 803 | |
| 804 | >>> list(repeatfunc(pow, 5, 2, 3)) |
| 805 | [8, 8, 8, 8, 8] |
| 806 | |
| 807 | >>> import random |
| 808 | >>> take(5, imap(int, repeatfunc(random.random))) |
| 809 | [0, 0, 0, 0, 0] |
| 810 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 811 | >>> list(pairwise('abcd')) |
| 812 | [('a', 'b'), ('b', 'c'), ('c', 'd')] |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 813 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 814 | >>> list(pairwise([])) |
| 815 | [] |
| 816 | |
| 817 | >>> list(pairwise('a')) |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 818 | [] |
| 819 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 820 | >>> list(islice(padnone('abc'), 0, 6)) |
| 821 | ['a', 'b', 'c', None, None, None] |
| 822 | |
| 823 | >>> list(ncycles('abc', 3)) |
| 824 | ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] |
| 825 | |
| 826 | >>> dotproduct([1,2,3], [4,5,6]) |
| 827 | 32 |
| 828 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 829 | """ |
| 830 | |
| 831 | __test__ = {'libreftest' : libreftest} |
| 832 | |
| 833 | def test_main(verbose=None): |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 834 | test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC, |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame^] | 835 | RegressionTests, LengthTransparency) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 836 | test_support.run_unittest(*test_classes) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 837 | |
| 838 | # verify reference counting |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 839 | if verbose and hasattr(sys, "gettotalrefcount"): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 840 | import gc |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 841 | counts = [None] * 5 |
| 842 | for i in xrange(len(counts)): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 843 | test_support.run_unittest(*test_classes) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 844 | gc.collect() |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 845 | counts[i] = sys.gettotalrefcount() |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 846 | print counts |
| 847 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 848 | # doctest the examples in the library reference |
Raymond Hettinger | 929f06c | 2003-05-16 23:16:36 +0000 | [diff] [blame] | 849 | test_support.run_doctest(sys.modules[__name__], verbose) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 850 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 851 | if __name__ == "__main__": |
| 852 | test_main(verbose=True) |