2003-01-31 20:10:11 -04:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
|
|
|
from itertools import *
|
2004-10-17 13:40:14 -03:00
|
|
|
from weakref import proxy
|
2003-02-07 01:32:58 -04:00
|
|
|
import sys
|
2003-05-03 02:59:48 -03:00
|
|
|
import operator
|
2003-10-24 05:45:23 -03:00
|
|
|
import random
|
2007-05-03 17:09:56 -03:00
|
|
|
maxsize = test_support.MAX_Py_ssize_t
|
|
|
|
minsize = -maxsize-1
|
2003-05-03 02:59:48 -03:00
|
|
|
|
|
|
|
def onearg(x):
|
|
|
|
'Test function of one argument'
|
2003-05-29 04:18:57 -03:00
|
|
|
return 2*x
|
2003-05-03 02:59:48 -03:00
|
|
|
|
|
|
|
def errfunc(*args):
|
|
|
|
'Test function that raises an error'
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
def gen3():
|
|
|
|
'Non-restartable source sequence'
|
|
|
|
for i in (0, 1, 2):
|
|
|
|
yield i
|
|
|
|
|
|
|
|
def isEven(x):
|
|
|
|
'Test predicate'
|
|
|
|
return x%2==0
|
|
|
|
|
2003-05-29 04:18:57 -03:00
|
|
|
def isOdd(x):
|
|
|
|
'Test predicate'
|
|
|
|
return x%2==1
|
|
|
|
|
2003-05-03 02:59:48 -03:00
|
|
|
class StopNow:
|
|
|
|
'Class emulating an empty iterable.'
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
def next(self):
|
|
|
|
raise StopIteration
|
2003-01-31 20:10:11 -04:00
|
|
|
|
2003-06-29 17:36:23 -03:00
|
|
|
def take(n, seq):
|
|
|
|
'Convenience function for partially consuming a long of infinite iterable'
|
|
|
|
return list(islice(seq, n))
|
2003-05-29 04:18:57 -03:00
|
|
|
|
2008-03-04 00:17:08 -04:00
|
|
|
def prod(iterable):
|
|
|
|
return reduce(operator.mul, iterable, 1)
|
|
|
|
|
2008-02-26 19:40:50 -04:00
|
|
|
def fact(n):
|
|
|
|
'Factorial'
|
2008-03-04 00:17:08 -04:00
|
|
|
return prod(range(1, n+1))
|
|
|
|
|
2003-01-31 20:10:11 -04:00
|
|
|
class TestBasicOps(unittest.TestCase):
|
2003-02-23 00:40:07 -04:00
|
|
|
def test_chain(self):
|
2008-03-06 16:52:01 -04:00
|
|
|
|
|
|
|
def chain2(*iterables):
|
|
|
|
'Pure python version in the docs'
|
|
|
|
for it in iterables:
|
|
|
|
for element in it:
|
|
|
|
yield element
|
|
|
|
|
|
|
|
for c in (chain, chain2):
|
|
|
|
self.assertEqual(list(c('abc', 'def')), list('abcdef'))
|
|
|
|
self.assertEqual(list(c('abc')), list('abc'))
|
|
|
|
self.assertEqual(list(c('')), [])
|
|
|
|
self.assertEqual(take(4, c('abc', 'def')), list('abcd'))
|
|
|
|
self.assertRaises(TypeError, list,c(2, 3))
|
2003-02-23 00:40:07 -04:00
|
|
|
|
2008-02-28 18:46:41 -04:00
|
|
|
def test_chain_from_iterable(self):
|
|
|
|
self.assertEqual(list(chain.from_iterable(['abc', 'def'])), list('abcdef'))
|
|
|
|
self.assertEqual(list(chain.from_iterable(['abc'])), list('abc'))
|
|
|
|
self.assertEqual(list(chain.from_iterable([''])), [])
|
|
|
|
self.assertEqual(take(4, chain.from_iterable(['abc', 'def'])), list('abcd'))
|
|
|
|
self.assertRaises(TypeError, list, chain.from_iterable([2, 3]))
|
|
|
|
|
2008-02-26 19:40:50 -04:00
|
|
|
def test_combinations(self):
|
2009-01-08 02:39:04 -04:00
|
|
|
self.assertRaises(TypeError, combinations, 'abc') # missing r argument
|
2008-02-26 19:40:50 -04:00
|
|
|
self.assertRaises(TypeError, combinations, 'abc', 2, 1) # too many arguments
|
2008-03-04 00:17:08 -04:00
|
|
|
self.assertRaises(TypeError, combinations, None) # pool is not iterable
|
2008-02-26 19:40:50 -04:00
|
|
|
self.assertRaises(ValueError, combinations, 'abc', -2) # r is negative
|
2009-01-08 02:39:04 -04:00
|
|
|
self.assertEqual(list(combinations('abc', 32)), []) # r > n
|
2008-02-26 19:40:50 -04:00
|
|
|
self.assertEqual(list(combinations(range(4), 3)),
|
|
|
|
[(0,1,2), (0,1,3), (0,2,3), (1,2,3)])
|
2008-03-04 00:17:08 -04:00
|
|
|
|
|
|
|
def combinations1(iterable, r):
|
|
|
|
'Pure python version shown in the docs'
|
|
|
|
pool = tuple(iterable)
|
|
|
|
n = len(pool)
|
2009-01-08 02:39:04 -04:00
|
|
|
if r > n:
|
|
|
|
return
|
2008-03-04 00:17:08 -04:00
|
|
|
indices = range(r)
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
while 1:
|
|
|
|
for i in reversed(range(r)):
|
|
|
|
if indices[i] != i + n - r:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
indices[i] += 1
|
|
|
|
for j in range(i+1, r):
|
|
|
|
indices[j] = indices[j-1] + 1
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
|
|
def combinations2(iterable, r):
|
|
|
|
'Pure python version shown in the docs'
|
|
|
|
pool = tuple(iterable)
|
|
|
|
n = len(pool)
|
|
|
|
for indices in permutations(range(n), r):
|
|
|
|
if sorted(indices) == list(indices):
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
2009-01-27 05:33:06 -04:00
|
|
|
def combinations3(iterable, r):
|
|
|
|
'Pure python version from cwr()'
|
|
|
|
pool = tuple(iterable)
|
|
|
|
n = len(pool)
|
|
|
|
for indices in combinations_with_replacement(range(n), r):
|
|
|
|
if len(set(indices)) == r:
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
2008-03-04 00:17:08 -04:00
|
|
|
for n in range(7):
|
2008-02-26 19:40:50 -04:00
|
|
|
values = [5*x-12 for x in range(n)]
|
2009-01-08 02:39:04 -04:00
|
|
|
for r in range(n+2):
|
2008-02-26 19:40:50 -04:00
|
|
|
result = list(combinations(values, r))
|
2009-01-08 02:39:04 -04:00
|
|
|
self.assertEqual(len(result), 0 if r>n else fact(n) / fact(r) / fact(n-r)) # right number of combs
|
2008-02-26 19:40:50 -04:00
|
|
|
self.assertEqual(len(result), len(set(result))) # no repeats
|
|
|
|
self.assertEqual(result, sorted(result)) # lexicographic order
|
|
|
|
for c in result:
|
|
|
|
self.assertEqual(len(c), r) # r-length combinations
|
|
|
|
self.assertEqual(len(set(c)), r) # no duplicate elements
|
|
|
|
self.assertEqual(list(c), sorted(c)) # keep original ordering
|
|
|
|
self.assert_(all(e in values for e in c)) # elements taken from input iterable
|
2008-03-05 16:59:58 -04:00
|
|
|
self.assertEqual(list(c),
|
|
|
|
[e for e in values if e in c]) # comb is a subsequence of the input iterable
|
2008-03-04 00:17:08 -04:00
|
|
|
self.assertEqual(result, list(combinations1(values, r))) # matches first pure python version
|
2009-01-08 02:39:04 -04:00
|
|
|
self.assertEqual(result, list(combinations2(values, r))) # matches second pure python version
|
2009-01-27 05:33:06 -04:00
|
|
|
self.assertEqual(result, list(combinations3(values, r))) # matches second pure python version
|
2008-03-04 00:17:08 -04:00
|
|
|
|
|
|
|
# Test implementation detail: tuple re-use
|
|
|
|
self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1)
|
|
|
|
self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1)
|
|
|
|
|
2009-01-26 22:58:49 -04:00
|
|
|
def test_combinations_with_replacement(self):
|
|
|
|
cwr = combinations_with_replacement
|
|
|
|
self.assertRaises(TypeError, cwr, 'abc') # missing r argument
|
|
|
|
self.assertRaises(TypeError, cwr, 'abc', 2, 1) # too many arguments
|
|
|
|
self.assertRaises(TypeError, cwr, None) # pool is not iterable
|
|
|
|
self.assertRaises(ValueError, cwr, 'abc', -2) # r is negative
|
|
|
|
self.assertEqual(list(cwr('ABC', 2)),
|
|
|
|
[('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
|
|
|
|
|
|
|
|
def cwr1(iterable, r):
|
|
|
|
'Pure python version shown in the docs'
|
|
|
|
# number items returned: (n+r-1)! / r! / (n-1)! when n>0
|
|
|
|
pool = tuple(iterable)
|
|
|
|
n = len(pool)
|
|
|
|
if not n and r:
|
|
|
|
return
|
|
|
|
indices = [0] * r
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
while 1:
|
|
|
|
for i in reversed(range(r)):
|
|
|
|
if indices[i] != n - 1:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
indices[i:] = [indices[i] + 1] * (r - i)
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
|
|
def cwr2(iterable, r):
|
|
|
|
'Pure python version shown in the docs'
|
|
|
|
pool = tuple(iterable)
|
|
|
|
n = len(pool)
|
|
|
|
for indices in product(range(n), repeat=r):
|
|
|
|
if sorted(indices) == list(indices):
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
|
|
def numcombs(n, r):
|
|
|
|
if not n:
|
|
|
|
return 0 if r else 1
|
|
|
|
return fact(n+r-1) / fact(r)/ fact(n-1)
|
|
|
|
|
|
|
|
for n in range(7):
|
|
|
|
values = [5*x-12 for x in range(n)]
|
|
|
|
for r in range(n+2):
|
|
|
|
result = list(cwr(values, r))
|
|
|
|
|
|
|
|
self.assertEqual(len(result), numcombs(n, r)) # right number of combs
|
|
|
|
self.assertEqual(len(result), len(set(result))) # no repeats
|
|
|
|
self.assertEqual(result, sorted(result)) # lexicographic order
|
|
|
|
|
|
|
|
regular_combs = list(combinations(values, r)) # compare to combs without replacement
|
|
|
|
if n == 0 or r <= 1:
|
|
|
|
self.assertEquals(result, regular_combs) # cases that should be identical
|
|
|
|
else:
|
|
|
|
self.assert_(set(result) >= set(regular_combs)) # rest should be supersets of regular combs
|
|
|
|
|
|
|
|
for c in result:
|
|
|
|
self.assertEqual(len(c), r) # r-length combinations
|
|
|
|
noruns = [k for k,v in groupby(c)] # combo without consecutive repeats
|
|
|
|
self.assertEqual(len(noruns), len(set(noruns))) # no repeats other than consecutive
|
|
|
|
self.assertEqual(list(c), sorted(c)) # keep original ordering
|
|
|
|
self.assert_(all(e in values for e in c)) # elements taken from input iterable
|
|
|
|
self.assertEqual(noruns,
|
|
|
|
[e for e in values if e in c]) # comb is a subsequence of the input iterable
|
|
|
|
self.assertEqual(result, list(cwr1(values, r))) # matches first pure python version
|
|
|
|
self.assertEqual(result, list(cwr2(values, r))) # matches second pure python version
|
|
|
|
|
|
|
|
# Test implementation detail: tuple re-use
|
|
|
|
self.assertEqual(len(set(map(id, cwr('abcde', 3)))), 1)
|
|
|
|
self.assertNotEqual(len(set(map(id, list(cwr('abcde', 3))))), 1)
|
|
|
|
|
2008-03-04 00:17:08 -04:00
|
|
|
def test_permutations(self):
|
|
|
|
self.assertRaises(TypeError, permutations) # too few arguments
|
|
|
|
self.assertRaises(TypeError, permutations, 'abc', 2, 1) # too many arguments
|
2008-03-05 16:59:58 -04:00
|
|
|
self.assertRaises(TypeError, permutations, None) # pool is not iterable
|
|
|
|
self.assertRaises(ValueError, permutations, 'abc', -2) # r is negative
|
2009-01-08 02:39:04 -04:00
|
|
|
self.assertEqual(list(permutations('abc', 32)), []) # r > n
|
2008-03-05 16:59:58 -04:00
|
|
|
self.assertRaises(TypeError, permutations, 'abc', 's') # r is not an int or None
|
2008-03-04 00:17:08 -04:00
|
|
|
self.assertEqual(list(permutations(range(3), 2)),
|
|
|
|
[(0,1), (0,2), (1,0), (1,2), (2,0), (2,1)])
|
|
|
|
|
|
|
|
def permutations1(iterable, r=None):
|
|
|
|
'Pure python version shown in the docs'
|
|
|
|
pool = tuple(iterable)
|
|
|
|
n = len(pool)
|
|
|
|
r = n if r is None else r
|
2009-01-08 02:39:04 -04:00
|
|
|
if r > n:
|
|
|
|
return
|
2008-03-04 00:17:08 -04:00
|
|
|
indices = range(n)
|
2008-03-22 21:55:46 -03:00
|
|
|
cycles = range(n, n-r, -1)
|
2008-03-04 00:17:08 -04:00
|
|
|
yield tuple(pool[i] for i in indices[:r])
|
|
|
|
while n:
|
|
|
|
for i in reversed(range(r)):
|
|
|
|
cycles[i] -= 1
|
|
|
|
if cycles[i] == 0:
|
|
|
|
indices[i:] = indices[i+1:] + indices[i:i+1]
|
|
|
|
cycles[i] = n - i
|
|
|
|
else:
|
|
|
|
j = cycles[i]
|
|
|
|
indices[i], indices[-j] = indices[-j], indices[i]
|
|
|
|
yield tuple(pool[i] for i in indices[:r])
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
def permutations2(iterable, r=None):
|
|
|
|
'Pure python version shown in the docs'
|
|
|
|
pool = tuple(iterable)
|
|
|
|
n = len(pool)
|
|
|
|
r = n if r is None else r
|
|
|
|
for indices in product(range(n), repeat=r):
|
|
|
|
if len(set(indices)) == r:
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
|
|
for n in range(7):
|
|
|
|
values = [5*x-12 for x in range(n)]
|
2009-01-08 02:39:04 -04:00
|
|
|
for r in range(n+2):
|
2008-03-04 00:17:08 -04:00
|
|
|
result = list(permutations(values, r))
|
2009-01-08 02:39:04 -04:00
|
|
|
self.assertEqual(len(result), 0 if r>n else fact(n) / fact(n-r)) # right number of perms
|
2008-03-04 00:17:08 -04:00
|
|
|
self.assertEqual(len(result), len(set(result))) # no repeats
|
|
|
|
self.assertEqual(result, sorted(result)) # lexicographic order
|
|
|
|
for p in result:
|
|
|
|
self.assertEqual(len(p), r) # r-length permutations
|
|
|
|
self.assertEqual(len(set(p)), r) # no duplicate elements
|
|
|
|
self.assert_(all(e in values for e in p)) # elements taken from input iterable
|
|
|
|
self.assertEqual(result, list(permutations1(values, r))) # matches first pure python version
|
2009-01-08 02:39:04 -04:00
|
|
|
self.assertEqual(result, list(permutations2(values, r))) # matches second pure python version
|
2008-03-04 00:17:08 -04:00
|
|
|
if r == n:
|
|
|
|
self.assertEqual(result, list(permutations(values, None))) # test r as None
|
|
|
|
self.assertEqual(result, list(permutations(values))) # test default r
|
|
|
|
|
|
|
|
# Test implementation detail: tuple re-use
|
2008-03-05 16:59:58 -04:00
|
|
|
self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1)
|
2008-03-04 00:17:08 -04:00
|
|
|
self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1)
|
2008-02-26 19:40:50 -04:00
|
|
|
|
2009-01-27 05:33:06 -04:00
|
|
|
def test_combinatorics(self):
|
|
|
|
# Test relationships between product(), permutations(),
|
|
|
|
# combinations() and combinations_with_replacement().
|
|
|
|
|
2009-01-27 06:36:14 -04:00
|
|
|
for n in range(6):
|
|
|
|
s = 'ABCDEFG'[:n]
|
|
|
|
for r in range(8):
|
|
|
|
prod = list(product(s, repeat=r))
|
|
|
|
cwr = list(combinations_with_replacement(s, r))
|
|
|
|
perm = list(permutations(s, r))
|
|
|
|
comb = list(combinations(s, r))
|
|
|
|
|
|
|
|
# Check size
|
|
|
|
self.assertEquals(len(prod), n**r)
|
|
|
|
self.assertEquals(len(cwr), (fact(n+r-1) / fact(r)/ fact(n-1)) if n else (not r))
|
|
|
|
self.assertEquals(len(perm), 0 if r>n else fact(n) / fact(n-r))
|
|
|
|
self.assertEquals(len(comb), 0 if r>n else fact(n) / fact(r) / fact(n-r))
|
|
|
|
|
|
|
|
# Check lexicographic order without repeated tuples
|
|
|
|
self.assertEquals(prod, sorted(set(prod)))
|
|
|
|
self.assertEquals(cwr, sorted(set(cwr)))
|
|
|
|
self.assertEquals(perm, sorted(set(perm)))
|
|
|
|
self.assertEquals(comb, sorted(set(comb)))
|
|
|
|
|
|
|
|
# Check interrelationships
|
|
|
|
self.assertEquals(cwr, [t for t in prod if sorted(t)==list(t)]) # cwr: prods which are sorted
|
|
|
|
self.assertEquals(perm, [t for t in prod if len(set(t))==r]) # perm: prods with no dups
|
|
|
|
self.assertEqual(comb, [t for t in perm if sorted(t)==list(t)]) # comb: perms that are sorted
|
|
|
|
self.assertEqual(comb, [t for t in cwr if len(set(t))==r]) # comb: cwrs without dups
|
|
|
|
self.assertEqual(comb, filter(set(cwr).__contains__, perm)) # comb: perm that is a cwr
|
|
|
|
self.assertEqual(comb, filter(set(perm).__contains__, cwr)) # comb: cwr that is a perm
|
|
|
|
self.assertEqual(comb, sorted(set(cwr) & set(perm))) # comb: both a cwr and a perm
|
2009-01-27 05:33:06 -04:00
|
|
|
|
2009-01-25 17:04:14 -04:00
|
|
|
def test_compress(self):
|
|
|
|
self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))
|
|
|
|
self.assertEqual(list(compress('ABCDEF', [0,0,0,0,0,0])), list(''))
|
|
|
|
self.assertEqual(list(compress('ABCDEF', [1,1,1,1,1,1])), list('ABCDEF'))
|
|
|
|
self.assertEqual(list(compress('ABCDEF', [1,0,1])), list('AC'))
|
|
|
|
self.assertEqual(list(compress('ABC', [0,1,1,1,1,1])), list('BC'))
|
|
|
|
n = 10000
|
|
|
|
data = chain.from_iterable(repeat(range(6), n))
|
|
|
|
selectors = chain.from_iterable(repeat((0, 1)))
|
|
|
|
self.assertEqual(list(compress(data, selectors)), [1,3,5] * n)
|
|
|
|
self.assertRaises(TypeError, compress, None, range(6)) # 1st arg not iterable
|
|
|
|
self.assertRaises(TypeError, compress, range(6), None) # 2nd arg not iterable
|
|
|
|
self.assertRaises(TypeError, compress, range(6)) # too few args
|
|
|
|
self.assertRaises(TypeError, compress, range(6), None) # too many args
|
|
|
|
|
2003-01-31 20:10:11 -04:00
|
|
|
def test_count(self):
|
|
|
|
self.assertEqual(zip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
|
|
|
|
self.assertEqual(zip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])
|
2003-06-29 17:36:23 -03:00
|
|
|
self.assertEqual(take(2, zip('abc',count(3))), [('a', 3), ('b', 4)])
|
2007-10-03 21:20:27 -03:00
|
|
|
self.assertEqual(take(2, zip('abc',count(-1))), [('a', -1), ('b', 0)])
|
|
|
|
self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)])
|
2003-01-31 20:10:11 -04:00
|
|
|
self.assertRaises(TypeError, count, 2, 3)
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertRaises(TypeError, count, 'a')
|
2007-10-03 21:20:27 -03:00
|
|
|
self.assertEqual(list(islice(count(maxsize-5), 10)), range(maxsize-5, maxsize+5))
|
|
|
|
self.assertEqual(list(islice(count(-maxsize-5), 10)), range(-maxsize-5, -maxsize+5))
|
2004-09-28 01:45:28 -03:00
|
|
|
c = count(3)
|
|
|
|
self.assertEqual(repr(c), 'count(3)')
|
|
|
|
c.next()
|
|
|
|
self.assertEqual(repr(c), 'count(4)')
|
2006-09-21 14:50:26 -03:00
|
|
|
c = count(-9)
|
|
|
|
self.assertEqual(repr(c), 'count(-9)')
|
|
|
|
c.next()
|
|
|
|
self.assertEqual(c.next(), -8)
|
2007-10-03 21:20:27 -03:00
|
|
|
for i in (-sys.maxint-5, -sys.maxint+5 ,-10, -1, 0, 10, sys.maxint-5, sys.maxint+5):
|
2007-10-12 14:53:11 -03:00
|
|
|
# Test repr (ignoring the L in longs)
|
|
|
|
r1 = repr(count(i)).replace('L', '')
|
|
|
|
r2 = 'count(%r)'.__mod__(i).replace('L', '')
|
|
|
|
self.assertEqual(r1, r2)
|
2003-01-31 20:10:11 -04:00
|
|
|
|
2003-02-23 00:40:07 -04:00
|
|
|
def test_cycle(self):
|
2003-06-29 17:36:23 -03:00
|
|
|
self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
|
2003-02-23 00:40:07 -04:00
|
|
|
self.assertEqual(list(cycle('')), [])
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertRaises(TypeError, cycle)
|
|
|
|
self.assertRaises(TypeError, cycle, 5)
|
|
|
|
self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0])
|
2003-02-23 00:40:07 -04:00
|
|
|
|
2003-12-06 12:23:06 -04:00
|
|
|
def test_groupby(self):
|
|
|
|
# Check whether it accepts arguments correctly
|
|
|
|
self.assertEqual([], list(groupby([])))
|
|
|
|
self.assertEqual([], list(groupby([], key=id)))
|
|
|
|
self.assertRaises(TypeError, list, groupby('abc', []))
|
|
|
|
self.assertRaises(TypeError, groupby, None)
|
2004-09-28 01:45:28 -03:00
|
|
|
self.assertRaises(TypeError, groupby, 'abc', lambda x:x, 10)
|
2003-12-06 12:23:06 -04:00
|
|
|
|
|
|
|
# Check normal input
|
|
|
|
s = [(0, 10, 20), (0, 11,21), (0,12,21), (1,13,21), (1,14,22),
|
|
|
|
(2,15,22), (3,16,23), (3,17,23)]
|
|
|
|
dup = []
|
|
|
|
for k, g in groupby(s, lambda r:r[0]):
|
|
|
|
for elem in g:
|
|
|
|
self.assertEqual(k, elem[0])
|
|
|
|
dup.append(elem)
|
|
|
|
self.assertEqual(s, dup)
|
|
|
|
|
|
|
|
# Check nested case
|
|
|
|
dup = []
|
|
|
|
for k, g in groupby(s, lambda r:r[0]):
|
|
|
|
for ik, ig in groupby(g, lambda r:r[2]):
|
|
|
|
for elem in ig:
|
|
|
|
self.assertEqual(k, elem[0])
|
|
|
|
self.assertEqual(ik, elem[2])
|
|
|
|
dup.append(elem)
|
|
|
|
self.assertEqual(s, dup)
|
|
|
|
|
|
|
|
# Check case where inner iterator is not used
|
|
|
|
keys = [k for k, g in groupby(s, lambda r:r[0])]
|
|
|
|
expectedkeys = set([r[0] for r in s])
|
|
|
|
self.assertEqual(set(keys), expectedkeys)
|
|
|
|
self.assertEqual(len(keys), len(expectedkeys))
|
|
|
|
|
|
|
|
# Exercise pipes and filters style
|
|
|
|
s = 'abracadabra'
|
|
|
|
# sort s | uniq
|
2003-12-17 16:43:33 -04:00
|
|
|
r = [k for k, g in groupby(sorted(s))]
|
2003-12-06 12:23:06 -04:00
|
|
|
self.assertEqual(r, ['a', 'b', 'c', 'd', 'r'])
|
|
|
|
# sort s | uniq -d
|
2003-12-17 16:43:33 -04:00
|
|
|
r = [k for k, g in groupby(sorted(s)) if list(islice(g,1,2))]
|
2003-12-06 12:23:06 -04:00
|
|
|
self.assertEqual(r, ['a', 'b', 'r'])
|
|
|
|
# sort s | uniq -c
|
2003-12-17 16:43:33 -04:00
|
|
|
r = [(len(list(g)), k) for k, g in groupby(sorted(s))]
|
2003-12-06 12:23:06 -04:00
|
|
|
self.assertEqual(r, [(5, 'a'), (2, 'b'), (1, 'c'), (1, 'd'), (2, 'r')])
|
|
|
|
# sort s | uniq -c | sort -rn | head -3
|
2003-12-17 16:43:33 -04:00
|
|
|
r = sorted([(len(list(g)) , k) for k, g in groupby(sorted(s))], reverse=True)[:3]
|
2003-12-06 12:23:06 -04:00
|
|
|
self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')])
|
|
|
|
|
|
|
|
# iter.next failure
|
|
|
|
class ExpectedError(Exception):
|
|
|
|
pass
|
|
|
|
def delayed_raise(n=0):
|
|
|
|
for i in range(n):
|
|
|
|
yield 'yo'
|
|
|
|
raise ExpectedError
|
|
|
|
def gulp(iterable, keyp=None, func=list):
|
|
|
|
return [func(g) for k, g in groupby(iterable, keyp)]
|
|
|
|
|
|
|
|
# iter.next failure on outer object
|
|
|
|
self.assertRaises(ExpectedError, gulp, delayed_raise(0))
|
|
|
|
# iter.next failure on inner object
|
|
|
|
self.assertRaises(ExpectedError, gulp, delayed_raise(1))
|
|
|
|
|
|
|
|
# __cmp__ failure
|
|
|
|
class DummyCmp:
|
|
|
|
def __cmp__(self, dst):
|
|
|
|
raise ExpectedError
|
|
|
|
s = [DummyCmp(), DummyCmp(), None]
|
|
|
|
|
|
|
|
# __cmp__ failure on outer object
|
|
|
|
self.assertRaises(ExpectedError, gulp, s, func=id)
|
|
|
|
# __cmp__ failure on inner object
|
|
|
|
self.assertRaises(ExpectedError, gulp, s)
|
|
|
|
|
|
|
|
# keyfunc failure
|
|
|
|
def keyfunc(obj):
|
|
|
|
if keyfunc.skip > 0:
|
|
|
|
keyfunc.skip -= 1
|
|
|
|
return obj
|
|
|
|
else:
|
|
|
|
raise ExpectedError
|
|
|
|
|
|
|
|
# keyfunc failure on outer object
|
|
|
|
keyfunc.skip = 0
|
|
|
|
self.assertRaises(ExpectedError, gulp, [None], keyfunc)
|
|
|
|
keyfunc.skip = 1
|
|
|
|
self.assertRaises(ExpectedError, gulp, [None, None], keyfunc)
|
|
|
|
|
2003-01-31 20:10:11 -04:00
|
|
|
def test_ifilter(self):
|
|
|
|
self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4])
|
|
|
|
self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2])
|
2008-02-25 18:42:32 -04:00
|
|
|
self.assertEqual(list(ifilter(bool, [0,1,0,2,0])), [1,2])
|
2003-06-29 17:36:23 -03:00
|
|
|
self.assertEqual(take(4, ifilter(isEven, count())), [0,2,4,6])
|
2003-01-31 20:10:11 -04:00
|
|
|
self.assertRaises(TypeError, ifilter)
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertRaises(TypeError, ifilter, lambda x:x)
|
|
|
|
self.assertRaises(TypeError, ifilter, lambda x:x, range(6), 7)
|
2003-01-31 20:10:11 -04:00
|
|
|
self.assertRaises(TypeError, ifilter, isEven, 3)
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertRaises(TypeError, ifilter(range(6), range(6)).next)
|
2003-02-09 02:40:58 -04:00
|
|
|
|
|
|
|
def test_ifilterfalse(self):
|
|
|
|
self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5])
|
|
|
|
self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0])
|
2008-02-25 18:42:32 -04:00
|
|
|
self.assertEqual(list(ifilterfalse(bool, [0,1,0,2,0])), [0,0,0])
|
2003-06-29 17:36:23 -03:00
|
|
|
self.assertEqual(take(4, ifilterfalse(isEven, count())), [1,3,5,7])
|
2003-02-09 02:40:58 -04:00
|
|
|
self.assertRaises(TypeError, ifilterfalse)
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertRaises(TypeError, ifilterfalse, lambda x:x)
|
|
|
|
self.assertRaises(TypeError, ifilterfalse, lambda x:x, range(6), 7)
|
2003-02-09 02:40:58 -04:00
|
|
|
self.assertRaises(TypeError, ifilterfalse, isEven, 3)
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertRaises(TypeError, ifilterfalse(range(6), range(6)).next)
|
2003-01-31 20:10:11 -04:00
|
|
|
|
|
|
|
def test_izip(self):
|
|
|
|
ans = [(x,y) for x, y in izip('abc',count())]
|
|
|
|
self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertEqual(list(izip('abc', range(6))), zip('abc', range(6)))
|
|
|
|
self.assertEqual(list(izip('abcdef', range(3))), zip('abcdef', range(3)))
|
2003-06-29 17:36:23 -03:00
|
|
|
self.assertEqual(take(3,izip('abcdef', count())), zip('abcdef', range(3)))
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertEqual(list(izip('abcdef')), zip('abcdef'))
|
2003-08-08 02:10:41 -03:00
|
|
|
self.assertEqual(list(izip()), zip())
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertRaises(TypeError, izip, 3)
|
|
|
|
self.assertRaises(TypeError, izip, range(3), 3)
|
|
|
|
# Check tuple re-use (implementation detail)
|
|
|
|
self.assertEqual([tuple(list(pair)) for pair in izip('abc', 'def')],
|
|
|
|
zip('abc', 'def'))
|
2003-05-29 04:18:57 -03:00
|
|
|
self.assertEqual([pair for pair in izip('abc', 'def')],
|
|
|
|
zip('abc', 'def'))
|
2003-05-03 02:59:48 -03:00
|
|
|
ids = map(id, izip('abc', 'def'))
|
|
|
|
self.assertEqual(min(ids), max(ids))
|
2003-05-29 04:18:57 -03:00
|
|
|
ids = map(id, list(izip('abc', 'def')))
|
|
|
|
self.assertEqual(len(dict.fromkeys(ids)), len(ids))
|
2003-01-31 20:10:11 -04:00
|
|
|
|
2007-02-21 01:20:38 -04:00
|
|
|
def test_iziplongest(self):
|
|
|
|
for args in [
|
|
|
|
['abc', range(6)],
|
|
|
|
[range(6), 'abc'],
|
|
|
|
[range(1000), range(2000,2100), range(3000,3050)],
|
|
|
|
[range(1000), range(0), range(3000,3050), range(1200), range(1500)],
|
|
|
|
[range(1000), range(0), range(3000,3050), range(1200), range(1500), range(0)],
|
|
|
|
]:
|
|
|
|
target = map(None, *args)
|
|
|
|
self.assertEqual(list(izip_longest(*args)), target)
|
|
|
|
self.assertEqual(list(izip_longest(*args, **{})), target)
|
|
|
|
target = [tuple((e is None and 'X' or e) for e in t) for t in target] # Replace None fills with 'X'
|
|
|
|
self.assertEqual(list(izip_longest(*args, **dict(fillvalue='X'))), target)
|
2007-03-12 15:07:52 -03:00
|
|
|
|
2007-02-21 01:20:38 -04:00
|
|
|
self.assertEqual(take(3,izip_longest('abcdef', count())), zip('abcdef', range(3))) # take 3 from infinite input
|
|
|
|
|
|
|
|
self.assertEqual(list(izip_longest()), zip())
|
|
|
|
self.assertEqual(list(izip_longest([])), zip([]))
|
|
|
|
self.assertEqual(list(izip_longest('abcdef')), zip('abcdef'))
|
2007-03-12 15:07:52 -03:00
|
|
|
|
2007-02-21 01:20:38 -04:00
|
|
|
self.assertEqual(list(izip_longest('abc', 'defg', **{})), map(None, 'abc', 'defg')) # empty keyword dict
|
|
|
|
self.assertRaises(TypeError, izip_longest, 3)
|
|
|
|
self.assertRaises(TypeError, izip_longest, range(3), 3)
|
|
|
|
|
|
|
|
for stmt in [
|
|
|
|
"izip_longest('abc', fv=1)",
|
2007-03-12 15:07:52 -03:00
|
|
|
"izip_longest('abc', fillvalue=1, bogus_keyword=None)",
|
2007-02-21 01:20:38 -04:00
|
|
|
]:
|
|
|
|
try:
|
|
|
|
eval(stmt, globals(), locals())
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail('Did not raise Type in: ' + stmt)
|
2007-03-12 15:07:52 -03:00
|
|
|
|
2007-02-21 01:20:38 -04:00
|
|
|
# Check tuple re-use (implementation detail)
|
|
|
|
self.assertEqual([tuple(list(pair)) for pair in izip_longest('abc', 'def')],
|
|
|
|
zip('abc', 'def'))
|
|
|
|
self.assertEqual([pair for pair in izip_longest('abc', 'def')],
|
|
|
|
zip('abc', 'def'))
|
|
|
|
ids = map(id, izip_longest('abc', 'def'))
|
|
|
|
self.assertEqual(min(ids), max(ids))
|
|
|
|
ids = map(id, list(izip_longest('abc', 'def')))
|
|
|
|
self.assertEqual(len(dict.fromkeys(ids)), len(ids))
|
|
|
|
|
2008-02-21 23:16:42 -04:00
|
|
|
def test_product(self):
|
|
|
|
for args, result in [
|
2008-03-04 00:17:08 -04:00
|
|
|
([], [()]), # zero iterables
|
2008-02-21 23:16:42 -04:00
|
|
|
(['ab'], [('a',), ('b',)]), # one iterable
|
|
|
|
([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]), # two iterables
|
|
|
|
([range(0), range(2), range(3)], []), # first iterable with zero length
|
|
|
|
([range(2), range(0), range(3)], []), # middle iterable with zero length
|
|
|
|
([range(2), range(3), range(0)], []), # last iterable with zero length
|
|
|
|
]:
|
|
|
|
self.assertEqual(list(product(*args)), result)
|
2008-02-28 22:21:48 -04:00
|
|
|
for r in range(4):
|
|
|
|
self.assertEqual(list(product(*(args*r))),
|
|
|
|
list(product(*args, **dict(repeat=r))))
|
2008-02-21 23:16:42 -04:00
|
|
|
self.assertEqual(len(list(product(*[range(7)]*6))), 7**6)
|
|
|
|
self.assertRaises(TypeError, product, range(6), None)
|
2008-03-05 16:59:58 -04:00
|
|
|
|
2008-03-06 21:33:20 -04:00
|
|
|
def product1(*args, **kwds):
|
|
|
|
pools = map(tuple, args) * kwds.get('repeat', 1)
|
|
|
|
n = len(pools)
|
|
|
|
if n == 0:
|
|
|
|
yield ()
|
|
|
|
return
|
|
|
|
if any(len(pool) == 0 for pool in pools):
|
|
|
|
return
|
|
|
|
indices = [0] * n
|
|
|
|
yield tuple(pool[i] for pool, i in zip(pools, indices))
|
|
|
|
while 1:
|
|
|
|
for i in reversed(range(n)): # right to left
|
|
|
|
if indices[i] == len(pools[i]) - 1:
|
|
|
|
continue
|
|
|
|
indices[i] += 1
|
|
|
|
for j in range(i+1, n):
|
|
|
|
indices[j] = 0
|
|
|
|
yield tuple(pool[i] for pool, i in zip(pools, indices))
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
2008-03-05 16:59:58 -04:00
|
|
|
def product2(*args, **kwds):
|
|
|
|
'Pure python version used in docs'
|
|
|
|
pools = map(tuple, args) * kwds.get('repeat', 1)
|
|
|
|
result = [[]]
|
|
|
|
for pool in pools:
|
|
|
|
result = [x+[y] for x in result for y in pool]
|
|
|
|
for prod in result:
|
|
|
|
yield tuple(prod)
|
|
|
|
|
2008-02-21 23:16:42 -04:00
|
|
|
argtypes = ['', 'abc', '', xrange(0), xrange(4), dict(a=1, b=2, c=3),
|
|
|
|
set('abcdefg'), range(11), tuple(range(13))]
|
|
|
|
for i in range(100):
|
|
|
|
args = [random.choice(argtypes) for j in range(random.randrange(5))]
|
2008-03-04 00:17:08 -04:00
|
|
|
expected_len = prod(map(len, args))
|
|
|
|
self.assertEqual(len(list(product(*args))), expected_len)
|
2008-03-06 21:33:20 -04:00
|
|
|
self.assertEqual(list(product(*args)), list(product1(*args)))
|
2008-03-05 16:59:58 -04:00
|
|
|
self.assertEqual(list(product(*args)), list(product2(*args)))
|
2008-02-21 23:16:42 -04:00
|
|
|
args = map(iter, args)
|
2008-03-04 00:17:08 -04:00
|
|
|
self.assertEqual(len(list(product(*args))), expected_len)
|
2008-02-21 23:16:42 -04:00
|
|
|
|
2008-02-22 22:20:41 -04:00
|
|
|
# Test implementation detail: tuple re-use
|
|
|
|
self.assertEqual(len(set(map(id, product('abc', 'def')))), 1)
|
|
|
|
self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1)
|
2008-02-21 23:16:42 -04:00
|
|
|
|
2003-01-31 20:10:11 -04:00
|
|
|
def test_repeat(self):
|
|
|
|
self.assertEqual(zip(xrange(3),repeat('a')),
|
|
|
|
[(0, 'a'), (1, 'a'), (2, 'a')])
|
2003-02-23 00:40:07 -04:00
|
|
|
self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a'])
|
2003-06-29 17:36:23 -03:00
|
|
|
self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a'])
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertEqual(list(repeat('a', 0)), [])
|
|
|
|
self.assertEqual(list(repeat('a', -3)), [])
|
2003-01-31 20:10:11 -04:00
|
|
|
self.assertRaises(TypeError, repeat)
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertRaises(TypeError, repeat, None, 3, 4)
|
|
|
|
self.assertRaises(TypeError, repeat, None, 'a')
|
2004-09-28 01:45:28 -03:00
|
|
|
r = repeat(1+0j)
|
|
|
|
self.assertEqual(repr(r), 'repeat((1+0j))')
|
|
|
|
r = repeat(1+0j, 5)
|
|
|
|
self.assertEqual(repr(r), 'repeat((1+0j), 5)')
|
|
|
|
list(r)
|
|
|
|
self.assertEqual(repr(r), 'repeat((1+0j), 0)')
|
2003-01-31 20:10:11 -04:00
|
|
|
|
|
|
|
def test_imap(self):
|
|
|
|
self.assertEqual(list(imap(operator.pow, range(3), range(1,7))),
|
|
|
|
[0**1, 1**2, 2**3])
|
|
|
|
self.assertEqual(list(imap(None, 'abc', range(5))),
|
|
|
|
[('a',0),('b',1),('c',2)])
|
2003-06-29 17:36:23 -03:00
|
|
|
self.assertEqual(list(imap(None, 'abc', count())),
|
|
|
|
[('a',0),('b',1),('c',2)])
|
|
|
|
self.assertEqual(take(2,imap(None, 'abc', count())),
|
|
|
|
[('a',0),('b',1)])
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertEqual(list(imap(operator.pow, [])), [])
|
2003-01-31 20:10:11 -04:00
|
|
|
self.assertRaises(TypeError, imap)
|
|
|
|
self.assertRaises(TypeError, imap, operator.neg)
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertRaises(TypeError, imap(10, range(5)).next)
|
|
|
|
self.assertRaises(ValueError, imap(errfunc, [4], [5]).next)
|
|
|
|
self.assertRaises(TypeError, imap(onearg, [4], [5]).next)
|
2003-01-31 20:10:11 -04:00
|
|
|
|
|
|
|
def test_starmap(self):
|
|
|
|
self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))),
|
|
|
|
[0**1, 1**2, 2**3])
|
2003-06-29 17:36:23 -03:00
|
|
|
self.assertEqual(take(3, starmap(operator.pow, izip(count(), count(1)))),
|
|
|
|
[0**1, 1**2, 2**3])
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertEqual(list(starmap(operator.pow, [])), [])
|
2008-01-16 23:02:14 -04:00
|
|
|
self.assertEqual(list(starmap(operator.pow, [iter([4,5])])), [4**5])
|
|
|
|
self.assertRaises(TypeError, list, starmap(operator.pow, [None]))
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertRaises(TypeError, starmap)
|
|
|
|
self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra')
|
|
|
|
self.assertRaises(TypeError, starmap(10, [(4,5)]).next)
|
|
|
|
self.assertRaises(ValueError, starmap(errfunc, [(4,5)]).next)
|
|
|
|
self.assertRaises(TypeError, starmap(onearg, [(4,5)]).next)
|
2003-01-31 20:10:11 -04:00
|
|
|
|
|
|
|
def test_islice(self):
|
|
|
|
for args in [ # islice(args) should agree with range(args)
|
|
|
|
(10, 20, 3),
|
|
|
|
(10, 3, 20),
|
|
|
|
(10, 20),
|
|
|
|
(10, 3),
|
|
|
|
(20,)
|
|
|
|
]:
|
|
|
|
self.assertEqual(list(islice(xrange(100), *args)), range(*args))
|
|
|
|
|
|
|
|
for args, tgtargs in [ # Stop when seqn is exhausted
|
|
|
|
((10, 110, 3), ((10, 100, 3))),
|
|
|
|
((10, 110), ((10, 100))),
|
|
|
|
((110,), (100,))
|
|
|
|
]:
|
|
|
|
self.assertEqual(list(islice(xrange(100), *args)), range(*tgtargs))
|
|
|
|
|
2003-05-02 16:04:37 -03:00
|
|
|
# Test stop=None
|
|
|
|
self.assertEqual(list(islice(xrange(10), None)), range(10))
|
2004-12-05 05:25:51 -04:00
|
|
|
self.assertEqual(list(islice(xrange(10), None, None)), range(10))
|
|
|
|
self.assertEqual(list(islice(xrange(10), None, None, None)), range(10))
|
2003-05-02 16:04:37 -03:00
|
|
|
self.assertEqual(list(islice(xrange(10), 2, None)), range(2, 10))
|
|
|
|
self.assertEqual(list(islice(xrange(10), 1, None, 2)), range(1, 10, 2))
|
|
|
|
|
2005-03-27 16:11:44 -04:00
|
|
|
# Test number of items consumed SF #1171417
|
|
|
|
it = iter(range(10))
|
|
|
|
self.assertEqual(list(islice(it, 3)), range(3))
|
|
|
|
self.assertEqual(list(it), range(3, 10))
|
|
|
|
|
2003-05-02 16:04:37 -03:00
|
|
|
# Test invalid arguments
|
2003-05-02 16:44:20 -03:00
|
|
|
self.assertRaises(TypeError, islice, xrange(10))
|
2003-01-31 20:10:11 -04:00
|
|
|
self.assertRaises(TypeError, islice, xrange(10), 1, 2, 3, 4)
|
|
|
|
self.assertRaises(ValueError, islice, xrange(10), -5, 10, 1)
|
|
|
|
self.assertRaises(ValueError, islice, xrange(10), 1, -5, -1)
|
|
|
|
self.assertRaises(ValueError, islice, xrange(10), 1, 10, -1)
|
|
|
|
self.assertRaises(ValueError, islice, xrange(10), 1, 10, 0)
|
2003-05-02 16:04:37 -03:00
|
|
|
self.assertRaises(ValueError, islice, xrange(10), 'a')
|
|
|
|
self.assertRaises(ValueError, islice, xrange(10), 'a', 1)
|
|
|
|
self.assertRaises(ValueError, islice, xrange(10), 1, 'a')
|
|
|
|
self.assertRaises(ValueError, islice, xrange(10), 'a', 1, 1)
|
|
|
|
self.assertRaises(ValueError, islice, xrange(10), 1, 'a', 1)
|
2007-05-03 17:09:56 -03:00
|
|
|
self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1)
|
2003-01-31 20:10:11 -04:00
|
|
|
|
|
|
|
def test_takewhile(self):
|
|
|
|
data = [1, 3, 5, 20, 2, 4, 6, 8]
|
|
|
|
underten = lambda x: x<10
|
|
|
|
self.assertEqual(list(takewhile(underten, data)), [1, 3, 5])
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertEqual(list(takewhile(underten, [])), [])
|
|
|
|
self.assertRaises(TypeError, takewhile)
|
|
|
|
self.assertRaises(TypeError, takewhile, operator.pow)
|
|
|
|
self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra')
|
|
|
|
self.assertRaises(TypeError, takewhile(10, [(4,5)]).next)
|
|
|
|
self.assertRaises(ValueError, takewhile(errfunc, [(4,5)]).next)
|
2004-09-28 01:45:28 -03:00
|
|
|
t = takewhile(bool, [1, 1, 1, 0, 0, 0])
|
|
|
|
self.assertEqual(list(t), [1, 1, 1])
|
|
|
|
self.assertRaises(StopIteration, t.next)
|
2003-01-31 20:10:11 -04:00
|
|
|
|
|
|
|
def test_dropwhile(self):
|
|
|
|
data = [1, 3, 5, 20, 2, 4, 6, 8]
|
|
|
|
underten = lambda x: x<10
|
|
|
|
self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8])
|
2003-05-03 02:59:48 -03:00
|
|
|
self.assertEqual(list(dropwhile(underten, [])), [])
|
|
|
|
self.assertRaises(TypeError, dropwhile)
|
|
|
|
self.assertRaises(TypeError, dropwhile, operator.pow)
|
|
|
|
self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra')
|
|
|
|
self.assertRaises(TypeError, dropwhile(10, [(4,5)]).next)
|
|
|
|
self.assertRaises(ValueError, dropwhile(errfunc, [(4,5)]).next)
|
2003-01-31 20:10:11 -04:00
|
|
|
|
2003-10-24 05:45:23 -03:00
|
|
|
def test_tee(self):
|
2003-11-12 10:32:26 -04:00
|
|
|
n = 200
|
2003-10-24 05:45:23 -03:00
|
|
|
def irange(n):
|
|
|
|
for i in xrange(n):
|
|
|
|
yield i
|
|
|
|
|
|
|
|
a, b = tee([]) # test empty iterator
|
|
|
|
self.assertEqual(list(a), [])
|
|
|
|
self.assertEqual(list(b), [])
|
|
|
|
|
|
|
|
a, b = tee(irange(n)) # test 100% interleaved
|
|
|
|
self.assertEqual(zip(a,b), zip(range(n),range(n)))
|
|
|
|
|
|
|
|
a, b = tee(irange(n)) # test 0% interleaved
|
|
|
|
self.assertEqual(list(a), range(n))
|
|
|
|
self.assertEqual(list(b), range(n))
|
|
|
|
|
|
|
|
a, b = tee(irange(n)) # test dealloc of leading iterator
|
2003-11-12 10:32:26 -04:00
|
|
|
for i in xrange(100):
|
|
|
|
self.assertEqual(a.next(), i)
|
2003-10-24 05:45:23 -03:00
|
|
|
del a
|
|
|
|
self.assertEqual(list(b), range(n))
|
|
|
|
|
|
|
|
a, b = tee(irange(n)) # test dealloc of trailing iterator
|
2003-11-12 10:32:26 -04:00
|
|
|
for i in xrange(100):
|
|
|
|
self.assertEqual(a.next(), i)
|
2003-10-24 05:45:23 -03:00
|
|
|
del b
|
2003-11-12 10:32:26 -04:00
|
|
|
self.assertEqual(list(a), range(100, n))
|
2003-10-24 05:45:23 -03:00
|
|
|
|
|
|
|
for j in xrange(5): # test randomly interleaved
|
|
|
|
order = [0]*n + [1]*n
|
|
|
|
random.shuffle(order)
|
|
|
|
lists = ([], [])
|
|
|
|
its = tee(irange(n))
|
|
|
|
for i in order:
|
|
|
|
value = its[i].next()
|
|
|
|
lists[i].append(value)
|
|
|
|
self.assertEqual(lists[0], range(n))
|
|
|
|
self.assertEqual(lists[1], range(n))
|
|
|
|
|
2003-11-12 10:32:26 -04:00
|
|
|
# test argument format checking
|
2003-10-24 05:45:23 -03:00
|
|
|
self.assertRaises(TypeError, tee)
|
|
|
|
self.assertRaises(TypeError, tee, 3)
|
|
|
|
self.assertRaises(TypeError, tee, [1,2], 'x')
|
2003-11-12 10:32:26 -04:00
|
|
|
self.assertRaises(TypeError, tee, [1,2], 3, 'x')
|
2003-10-24 05:45:23 -03:00
|
|
|
|
2003-11-12 10:32:26 -04:00
|
|
|
# tee object should be instantiable
|
|
|
|
a, b = tee('abc')
|
|
|
|
c = type(a)('def')
|
|
|
|
self.assertEqual(list(c), list('def'))
|
|
|
|
|
|
|
|
# test long-lagged and multi-way split
|
|
|
|
a, b, c = tee(xrange(2000), 3)
|
|
|
|
for i in xrange(100):
|
|
|
|
self.assertEqual(a.next(), i)
|
|
|
|
self.assertEqual(list(b), range(2000))
|
|
|
|
self.assertEqual([c.next(), c.next()], range(2))
|
|
|
|
self.assertEqual(list(a), range(100,2000))
|
|
|
|
self.assertEqual(list(c), range(2,2000))
|
|
|
|
|
2004-09-28 01:45:28 -03:00
|
|
|
# test values of n
|
|
|
|
self.assertRaises(TypeError, tee, 'abc', 'invalid')
|
2006-09-01 23:58:13 -03:00
|
|
|
self.assertRaises(ValueError, tee, [], -1)
|
2004-09-28 01:45:28 -03:00
|
|
|
for n in xrange(5):
|
|
|
|
result = tee('abc', n)
|
|
|
|
self.assertEqual(type(result), tuple)
|
|
|
|
self.assertEqual(len(result), n)
|
|
|
|
self.assertEqual(map(list, result), [list('abc')]*n)
|
|
|
|
|
2003-11-12 10:32:26 -04:00
|
|
|
# tee pass-through to copyable iterator
|
|
|
|
a, b = tee('abc')
|
|
|
|
c, d = tee(a)
|
|
|
|
self.assert_(a is c)
|
2003-10-26 10:25:56 -04:00
|
|
|
|
2004-09-28 01:45:28 -03:00
|
|
|
# test tee_new
|
|
|
|
t1, t2 = tee('abc')
|
|
|
|
tnew = type(t1)
|
|
|
|
self.assertRaises(TypeError, tnew)
|
|
|
|
self.assertRaises(TypeError, tnew, 10)
|
|
|
|
t3 = tnew(t1)
|
|
|
|
self.assert_(list(t1) == list(t2) == list(t3) == list('abc'))
|
2003-10-26 10:25:56 -04:00
|
|
|
|
2004-10-17 13:40:14 -03:00
|
|
|
# test that tee objects are weak referencable
|
|
|
|
a, b = tee(xrange(10))
|
|
|
|
p = proxy(a)
|
|
|
|
self.assertEqual(getattr(p, '__class__'), type(b))
|
|
|
|
del a
|
|
|
|
self.assertRaises(ReferenceError, getattr, p, '__class__')
|
|
|
|
|
2003-05-02 19:38:07 -03:00
|
|
|
def test_StopIteration(self):
|
2003-08-08 02:10:41 -03:00
|
|
|
self.assertRaises(StopIteration, izip().next)
|
|
|
|
|
2003-12-06 12:23:06 -04:00
|
|
|
for f in (chain, cycle, izip, groupby):
|
2003-05-02 19:38:07 -03:00
|
|
|
self.assertRaises(StopIteration, f([]).next)
|
|
|
|
self.assertRaises(StopIteration, f(StopNow()).next)
|
|
|
|
|
|
|
|
self.assertRaises(StopIteration, islice([], None).next)
|
|
|
|
self.assertRaises(StopIteration, islice(StopNow(), None).next)
|
|
|
|
|
2003-10-24 05:45:23 -03:00
|
|
|
p, q = tee([])
|
|
|
|
self.assertRaises(StopIteration, p.next)
|
|
|
|
self.assertRaises(StopIteration, q.next)
|
|
|
|
p, q = tee(StopNow())
|
|
|
|
self.assertRaises(StopIteration, p.next)
|
|
|
|
self.assertRaises(StopIteration, q.next)
|
|
|
|
|
2003-05-02 19:38:07 -03:00
|
|
|
self.assertRaises(StopIteration, repeat(None, 0).next)
|
|
|
|
|
|
|
|
for f in (ifilter, ifilterfalse, imap, takewhile, dropwhile, starmap):
|
|
|
|
self.assertRaises(StopIteration, f(lambda x:x, []).next)
|
|
|
|
self.assertRaises(StopIteration, f(lambda x:x, StopNow()).next)
|
|
|
|
|
2008-03-06 16:52:01 -04:00
|
|
|
class TestExamples(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_chain(self):
|
|
|
|
self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF')
|
|
|
|
|
|
|
|
def test_chain_from_iterable(self):
|
|
|
|
self.assertEqual(''.join(chain.from_iterable(['ABC', 'DEF'])), 'ABCDEF')
|
|
|
|
|
|
|
|
def test_combinations(self):
|
|
|
|
self.assertEqual(list(combinations('ABCD', 2)),
|
|
|
|
[('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')])
|
|
|
|
self.assertEqual(list(combinations(range(4), 3)),
|
|
|
|
[(0,1,2), (0,1,3), (0,2,3), (1,2,3)])
|
|
|
|
|
2009-01-26 22:58:49 -04:00
|
|
|
def test_combinations_with_replacement(self):
|
|
|
|
self.assertEqual(list(combinations_with_replacement('ABC', 2)),
|
|
|
|
[('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
|
|
|
|
|
2009-01-25 17:04:14 -04:00
|
|
|
def test_compress(self):
|
|
|
|
self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))
|
|
|
|
|
2008-03-06 16:52:01 -04:00
|
|
|
def test_count(self):
|
|
|
|
self.assertEqual(list(islice(count(10), 5)), [10, 11, 12, 13, 14])
|
|
|
|
|
|
|
|
def test_cycle(self):
|
|
|
|
self.assertEqual(list(islice(cycle('ABCD'), 12)), list('ABCDABCDABCD'))
|
|
|
|
|
|
|
|
def test_dropwhile(self):
|
|
|
|
self.assertEqual(list(dropwhile(lambda x: x<5, [1,4,6,4,1])), [6,4,1])
|
|
|
|
|
|
|
|
def test_groupby(self):
|
|
|
|
self.assertEqual([k for k, g in groupby('AAAABBBCCDAABBB')],
|
|
|
|
list('ABCDAB'))
|
|
|
|
self.assertEqual([(list(g)) for k, g in groupby('AAAABBBCCD')],
|
|
|
|
[list('AAAA'), list('BBB'), list('CC'), list('D')])
|
|
|
|
|
|
|
|
def test_ifilter(self):
|
|
|
|
self.assertEqual(list(ifilter(lambda x: x%2, range(10))), [1,3,5,7,9])
|
|
|
|
|
|
|
|
def test_ifilterfalse(self):
|
|
|
|
self.assertEqual(list(ifilterfalse(lambda x: x%2, range(10))), [0,2,4,6,8])
|
|
|
|
|
|
|
|
def test_imap(self):
|
|
|
|
self.assertEqual(list(imap(pow, (2,3,10), (5,2,3))), [32, 9, 1000])
|
|
|
|
|
|
|
|
def test_islice(self):
|
|
|
|
self.assertEqual(list(islice('ABCDEFG', 2)), list('AB'))
|
|
|
|
self.assertEqual(list(islice('ABCDEFG', 2, 4)), list('CD'))
|
|
|
|
self.assertEqual(list(islice('ABCDEFG', 2, None)), list('CDEFG'))
|
|
|
|
self.assertEqual(list(islice('ABCDEFG', 0, None, 2)), list('ACEG'))
|
|
|
|
|
|
|
|
def test_izip(self):
|
|
|
|
self.assertEqual(list(izip('ABCD', 'xy')), [('A', 'x'), ('B', 'y')])
|
|
|
|
|
|
|
|
def test_izip_longest(self):
|
|
|
|
self.assertEqual(list(izip_longest('ABCD', 'xy', fillvalue='-')),
|
|
|
|
[('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')])
|
|
|
|
|
|
|
|
def test_permutations(self):
|
|
|
|
self.assertEqual(list(permutations('ABCD', 2)),
|
|
|
|
map(tuple, 'AB AC AD BA BC BD CA CB CD DA DB DC'.split()))
|
|
|
|
self.assertEqual(list(permutations(range(3))),
|
|
|
|
[(0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0)])
|
|
|
|
|
|
|
|
def test_product(self):
|
|
|
|
self.assertEqual(list(product('ABCD', 'xy')),
|
|
|
|
map(tuple, 'Ax Ay Bx By Cx Cy Dx Dy'.split()))
|
|
|
|
self.assertEqual(list(product(range(2), repeat=3)),
|
|
|
|
[(0,0,0), (0,0,1), (0,1,0), (0,1,1),
|
|
|
|
(1,0,0), (1,0,1), (1,1,0), (1,1,1)])
|
|
|
|
|
|
|
|
def test_repeat(self):
|
|
|
|
self.assertEqual(list(repeat(10, 3)), [10, 10, 10])
|
|
|
|
|
|
|
|
def test_stapmap(self):
|
|
|
|
self.assertEqual(list(starmap(pow, [(2,5), (3,2), (10,3)])),
|
|
|
|
[32, 9, 1000])
|
|
|
|
|
|
|
|
def test_takewhile(self):
|
|
|
|
self.assertEqual(list(takewhile(lambda x: x<5, [1,4,6,4,1])), [1,4])
|
|
|
|
|
|
|
|
|
2003-10-24 05:45:23 -03:00
|
|
|
class TestGC(unittest.TestCase):
|
|
|
|
|
|
|
|
def makecycle(self, iterator, container):
|
|
|
|
container.append(iterator)
|
|
|
|
iterator.next()
|
|
|
|
del container, iterator
|
|
|
|
|
|
|
|
def test_chain(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(chain(a), a)
|
|
|
|
|
2008-03-06 16:52:01 -04:00
|
|
|
def test_chain_from_iterable(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(chain.from_iterable([a]), a)
|
|
|
|
|
|
|
|
def test_combinations(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(combinations([1,2,a,3], 3), a)
|
|
|
|
|
2009-01-26 22:58:49 -04:00
|
|
|
def test_combinations_with_replacement(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(combinations_with_replacement([1,2,a,3], 3), a)
|
|
|
|
|
2009-01-25 17:04:14 -04:00
|
|
|
def test_compress(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(compress('ABCDEF', [1,0,1,0,1,0]), a)
|
|
|
|
|
2003-10-24 05:45:23 -03:00
|
|
|
def test_cycle(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(cycle([a]*2), a)
|
|
|
|
|
2004-09-29 08:40:50 -03:00
|
|
|
def test_dropwhile(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(dropwhile(bool, [0, a, a]), a)
|
|
|
|
|
|
|
|
def test_groupby(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(groupby([a]*2, lambda x:x), a)
|
|
|
|
|
2008-03-06 18:51:36 -04:00
|
|
|
def test_issue2246(self):
|
|
|
|
# Issue 2246 -- the _grouper iterator was not included in GC
|
|
|
|
n = 10
|
|
|
|
keyfunc = lambda x: x
|
|
|
|
for i, j in groupby(xrange(n), key=keyfunc):
|
|
|
|
keyfunc.__dict__.setdefault('x',[]).append(j)
|
|
|
|
|
2003-10-24 05:45:23 -03:00
|
|
|
def test_ifilter(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(ifilter(lambda x:True, [a]*2), a)
|
|
|
|
|
|
|
|
def test_ifilterfalse(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(ifilterfalse(lambda x:False, a), a)
|
|
|
|
|
|
|
|
def test_izip(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(izip([a]*2, [a]*3), a)
|
|
|
|
|
2008-03-06 16:52:01 -04:00
|
|
|
def test_izip_longest(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(izip_longest([a]*2, [a]*3), a)
|
|
|
|
b = [a, None]
|
|
|
|
self.makecycle(izip_longest([a]*2, [a]*3, fillvalue=b), a)
|
|
|
|
|
2003-10-24 05:45:23 -03:00
|
|
|
def test_imap(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(imap(lambda x:x, [a]*2), a)
|
|
|
|
|
|
|
|
def test_islice(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(islice([a]*2, None), a)
|
|
|
|
|
2008-03-06 16:52:01 -04:00
|
|
|
def test_permutations(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(permutations([1,2,a,3], 3), a)
|
|
|
|
|
|
|
|
def test_product(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(product([1,2,a,3], repeat=3), a)
|
|
|
|
|
2004-09-29 08:40:50 -03:00
|
|
|
def test_repeat(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(repeat(a), a)
|
|
|
|
|
2003-10-24 05:45:23 -03:00
|
|
|
def test_starmap(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a)
|
|
|
|
|
2004-09-29 08:40:50 -03:00
|
|
|
def test_takewhile(self):
|
|
|
|
a = []
|
|
|
|
self.makecycle(takewhile(bool, [1, 0, a, a]), a)
|
|
|
|
|
2003-05-29 04:18:57 -03:00
|
|
|
def R(seqn):
|
|
|
|
'Regular generator'
|
|
|
|
for i in seqn:
|
|
|
|
yield i
|
|
|
|
|
|
|
|
class G:
|
|
|
|
'Sequence using __getitem__'
|
|
|
|
def __init__(self, seqn):
|
|
|
|
self.seqn = seqn
|
|
|
|
def __getitem__(self, i):
|
|
|
|
return self.seqn[i]
|
|
|
|
|
|
|
|
class I:
|
|
|
|
'Sequence using iterator protocol'
|
|
|
|
def __init__(self, seqn):
|
|
|
|
self.seqn = seqn
|
|
|
|
self.i = 0
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
def next(self):
|
|
|
|
if self.i >= len(self.seqn): raise StopIteration
|
|
|
|
v = self.seqn[self.i]
|
|
|
|
self.i += 1
|
|
|
|
return v
|
|
|
|
|
|
|
|
class Ig:
|
|
|
|
'Sequence using iterator protocol defined with a generator'
|
|
|
|
def __init__(self, seqn):
|
|
|
|
self.seqn = seqn
|
|
|
|
self.i = 0
|
|
|
|
def __iter__(self):
|
|
|
|
for val in self.seqn:
|
|
|
|
yield val
|
|
|
|
|
|
|
|
class X:
|
|
|
|
'Missing __getitem__ and __iter__'
|
|
|
|
def __init__(self, seqn):
|
|
|
|
self.seqn = seqn
|
|
|
|
self.i = 0
|
|
|
|
def next(self):
|
|
|
|
if self.i >= len(self.seqn): raise StopIteration
|
|
|
|
v = self.seqn[self.i]
|
|
|
|
self.i += 1
|
|
|
|
return v
|
|
|
|
|
|
|
|
class N:
|
|
|
|
'Iterator missing next()'
|
|
|
|
def __init__(self, seqn):
|
|
|
|
self.seqn = seqn
|
|
|
|
self.i = 0
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
class E:
|
|
|
|
'Test propagation of exceptions'
|
|
|
|
def __init__(self, seqn):
|
|
|
|
self.seqn = seqn
|
|
|
|
self.i = 0
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
def next(self):
|
2004-09-27 12:29:05 -03:00
|
|
|
3 // 0
|
2003-05-29 04:18:57 -03:00
|
|
|
|
|
|
|
class S:
|
|
|
|
'Test immediate stop'
|
|
|
|
def __init__(self, seqn):
|
|
|
|
pass
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
def next(self):
|
|
|
|
raise StopIteration
|
|
|
|
|
|
|
|
def L(seqn):
|
|
|
|
'Test multiple tiers of iterators'
|
|
|
|
return chain(imap(lambda x:x, R(Ig(G(seqn)))))
|
|
|
|
|
|
|
|
|
|
|
|
class TestVariousIteratorArgs(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_chain(self):
|
|
|
|
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
self.assertEqual(list(chain(g(s))), list(g(s)))
|
|
|
|
self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s)))
|
2008-02-28 18:30:42 -04:00
|
|
|
self.assertRaises(TypeError, list, chain(X(s)))
|
2003-05-29 04:18:57 -03:00
|
|
|
self.assertRaises(TypeError, list, chain(N(s)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, chain(E(s)))
|
|
|
|
|
2009-01-25 17:04:14 -04:00
|
|
|
def test_compress(self):
|
|
|
|
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
|
|
|
|
n = len(s)
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
self.assertEqual(list(compress(g(s), repeat(1))), list(g(s)))
|
|
|
|
self.assertRaises(TypeError, compress, X(s), repeat(1))
|
|
|
|
self.assertRaises(TypeError, list, compress(N(s), repeat(1)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, compress(E(s), repeat(1)))
|
|
|
|
|
2008-02-21 23:16:42 -04:00
|
|
|
def test_product(self):
|
|
|
|
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
|
|
|
|
self.assertRaises(TypeError, product, X(s))
|
|
|
|
self.assertRaises(TypeError, product, N(s))
|
|
|
|
self.assertRaises(ZeroDivisionError, product, E(s))
|
|
|
|
|
2003-05-29 04:18:57 -03:00
|
|
|
def test_cycle(self):
|
|
|
|
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
tgtlen = len(s) * 3
|
|
|
|
expected = list(g(s))*3
|
|
|
|
actual = list(islice(cycle(g(s)), tgtlen))
|
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
self.assertRaises(TypeError, cycle, X(s))
|
|
|
|
self.assertRaises(TypeError, list, cycle(N(s)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, cycle(E(s)))
|
|
|
|
|
2003-12-06 12:23:06 -04:00
|
|
|
def test_groupby(self):
|
|
|
|
for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
self.assertEqual([k for k, sb in groupby(g(s))], list(g(s)))
|
|
|
|
self.assertRaises(TypeError, groupby, X(s))
|
|
|
|
self.assertRaises(TypeError, list, groupby(N(s)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, groupby(E(s)))
|
|
|
|
|
2003-05-29 04:18:57 -03:00
|
|
|
def test_ifilter(self):
|
|
|
|
for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
self.assertEqual(list(ifilter(isEven, g(s))), filter(isEven, g(s)))
|
|
|
|
self.assertRaises(TypeError, ifilter, isEven, X(s))
|
|
|
|
self.assertRaises(TypeError, list, ifilter(isEven, N(s)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, ifilter(isEven, E(s)))
|
|
|
|
|
|
|
|
def test_ifilterfalse(self):
|
|
|
|
for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
self.assertEqual(list(ifilterfalse(isEven, g(s))), filter(isOdd, g(s)))
|
|
|
|
self.assertRaises(TypeError, ifilterfalse, isEven, X(s))
|
|
|
|
self.assertRaises(TypeError, list, ifilterfalse(isEven, N(s)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, ifilterfalse(isEven, E(s)))
|
|
|
|
|
|
|
|
def test_izip(self):
|
|
|
|
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
self.assertEqual(list(izip(g(s))), zip(g(s)))
|
|
|
|
self.assertEqual(list(izip(g(s), g(s))), zip(g(s), g(s)))
|
|
|
|
self.assertRaises(TypeError, izip, X(s))
|
|
|
|
self.assertRaises(TypeError, list, izip(N(s)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, izip(E(s)))
|
|
|
|
|
2007-02-21 01:20:38 -04:00
|
|
|
def test_iziplongest(self):
|
|
|
|
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
self.assertEqual(list(izip_longest(g(s))), zip(g(s)))
|
|
|
|
self.assertEqual(list(izip_longest(g(s), g(s))), zip(g(s), g(s)))
|
|
|
|
self.assertRaises(TypeError, izip_longest, X(s))
|
|
|
|
self.assertRaises(TypeError, list, izip_longest(N(s)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, izip_longest(E(s)))
|
|
|
|
|
2003-05-29 04:18:57 -03:00
|
|
|
def test_imap(self):
|
|
|
|
for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
self.assertEqual(list(imap(onearg, g(s))), map(onearg, g(s)))
|
|
|
|
self.assertEqual(list(imap(operator.pow, g(s), g(s))), map(operator.pow, g(s), g(s)))
|
|
|
|
self.assertRaises(TypeError, imap, onearg, X(s))
|
|
|
|
self.assertRaises(TypeError, list, imap(onearg, N(s)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, imap(onearg, E(s)))
|
|
|
|
|
|
|
|
def test_islice(self):
|
|
|
|
for s in ("12345", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2])
|
|
|
|
self.assertRaises(TypeError, islice, X(s), 10)
|
|
|
|
self.assertRaises(TypeError, list, islice(N(s), 10))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, islice(E(s), 10))
|
|
|
|
|
|
|
|
def test_starmap(self):
|
|
|
|
for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
ss = zip(s, s)
|
|
|
|
self.assertEqual(list(starmap(operator.pow, g(ss))), map(operator.pow, g(s), g(s)))
|
|
|
|
self.assertRaises(TypeError, starmap, operator.pow, X(ss))
|
|
|
|
self.assertRaises(TypeError, list, starmap(operator.pow, N(ss)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss)))
|
|
|
|
|
|
|
|
def test_takewhile(self):
|
|
|
|
for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
tgt = []
|
|
|
|
for elem in g(s):
|
|
|
|
if not isEven(elem): break
|
|
|
|
tgt.append(elem)
|
|
|
|
self.assertEqual(list(takewhile(isEven, g(s))), tgt)
|
|
|
|
self.assertRaises(TypeError, takewhile, isEven, X(s))
|
|
|
|
self.assertRaises(TypeError, list, takewhile(isEven, N(s)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s)))
|
|
|
|
|
|
|
|
def test_dropwhile(self):
|
|
|
|
for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
tgt = []
|
|
|
|
for elem in g(s):
|
|
|
|
if not tgt and isOdd(elem): continue
|
|
|
|
tgt.append(elem)
|
|
|
|
self.assertEqual(list(dropwhile(isOdd, g(s))), tgt)
|
|
|
|
self.assertRaises(TypeError, dropwhile, isOdd, X(s))
|
|
|
|
self.assertRaises(TypeError, list, dropwhile(isOdd, N(s)))
|
|
|
|
self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s)))
|
|
|
|
|
2003-10-24 05:45:23 -03:00
|
|
|
def test_tee(self):
|
|
|
|
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
|
|
|
|
for g in (G, I, Ig, S, L, R):
|
|
|
|
it1, it2 = tee(g(s))
|
|
|
|
self.assertEqual(list(it1), list(g(s)))
|
|
|
|
self.assertEqual(list(it2), list(g(s)))
|
|
|
|
self.assertRaises(TypeError, tee, X(s))
|
|
|
|
self.assertRaises(TypeError, list, tee(N(s))[0])
|
|
|
|
self.assertRaises(ZeroDivisionError, list, tee(E(s))[0])
|
|
|
|
|
2004-02-10 05:25:40 -04:00
|
|
|
class LengthTransparency(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_repeat(self):
|
2005-09-24 18:23:05 -03:00
|
|
|
from test.test_iterlen import len
|
2004-02-10 05:25:40 -04:00
|
|
|
self.assertEqual(len(repeat(None, 50)), 50)
|
|
|
|
self.assertRaises(TypeError, len, repeat(None))
|
|
|
|
|
2003-08-29 20:09:58 -03:00
|
|
|
class RegressionTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_sf_793826(self):
|
|
|
|
# Fix Armin Rigo's successful efforts to wreak havoc
|
|
|
|
|
|
|
|
def mutatingtuple(tuple1, f, tuple2):
|
|
|
|
# this builds a tuple t which is a copy of tuple1,
|
|
|
|
# then calls f(t), then mutates t to be equal to tuple2
|
|
|
|
# (needs len(tuple1) == len(tuple2)).
|
|
|
|
def g(value, first=[1]):
|
|
|
|
if first:
|
|
|
|
del first[:]
|
|
|
|
f(z.next())
|
|
|
|
return value
|
|
|
|
items = list(tuple2)
|
|
|
|
items[1:1] = list(tuple1)
|
|
|
|
gen = imap(g, items)
|
|
|
|
z = izip(*[gen]*len(tuple1))
|
|
|
|
z.next()
|
|
|
|
|
|
|
|
def f(t):
|
|
|
|
global T
|
|
|
|
T = t
|
|
|
|
first[:] = list(T)
|
|
|
|
|
|
|
|
first = []
|
|
|
|
mutatingtuple((1,2,3), f, (4,5,6))
|
|
|
|
second = list(T)
|
|
|
|
self.assertEqual(first, second)
|
|
|
|
|
2003-05-02 19:38:07 -03:00
|
|
|
|
2004-05-08 16:49:42 -03:00
|
|
|
def test_sf_950057(self):
|
|
|
|
# Make sure that chain() and cycle() catch exceptions immediately
|
|
|
|
# rather than when shifting between input sources
|
|
|
|
|
|
|
|
def gen1():
|
|
|
|
hist.append(0)
|
|
|
|
yield 1
|
|
|
|
hist.append(1)
|
2004-07-18 14:34:03 -03:00
|
|
|
raise AssertionError
|
2004-05-08 16:49:42 -03:00
|
|
|
hist.append(2)
|
|
|
|
|
|
|
|
def gen2(x):
|
|
|
|
hist.append(3)
|
|
|
|
yield 2
|
|
|
|
hist.append(4)
|
|
|
|
if x:
|
|
|
|
raise StopIteration
|
|
|
|
|
|
|
|
hist = []
|
|
|
|
self.assertRaises(AssertionError, list, chain(gen1(), gen2(False)))
|
|
|
|
self.assertEqual(hist, [0,1])
|
|
|
|
|
|
|
|
hist = []
|
|
|
|
self.assertRaises(AssertionError, list, chain(gen1(), gen2(True)))
|
|
|
|
self.assertEqual(hist, [0,1])
|
|
|
|
|
|
|
|
hist = []
|
|
|
|
self.assertRaises(AssertionError, list, cycle(gen1()))
|
|
|
|
self.assertEqual(hist, [0,1])
|
|
|
|
|
2007-01-21 06:28:43 -04:00
|
|
|
class SubclassWithKwargsTest(unittest.TestCase):
|
|
|
|
def test_keywords_in_subclass(self):
|
|
|
|
# count is not subclassable...
|
|
|
|
for cls in (repeat, izip, ifilter, ifilterfalse, chain, imap,
|
2009-01-25 17:04:14 -04:00
|
|
|
starmap, islice, takewhile, dropwhile, cycle, compress):
|
2007-01-21 06:28:43 -04:00
|
|
|
class Subclass(cls):
|
|
|
|
def __init__(self, newarg=None, *args):
|
|
|
|
cls.__init__(self, *args)
|
|
|
|
try:
|
|
|
|
Subclass(newarg=1)
|
|
|
|
except TypeError, err:
|
|
|
|
# we expect type errors because of wrong argument count
|
|
|
|
self.failIf("does not take keyword arguments" in err.args[0])
|
|
|
|
|
|
|
|
|
2003-05-29 04:18:57 -03:00
|
|
|
libreftest = """ Doctest for examples in the library reference: libitertools.tex
|
2003-01-31 20:10:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
>>> amounts = [120.15, 764.05, 823.14]
|
|
|
|
>>> for checknum, amount in izip(count(1200), amounts):
|
|
|
|
... print 'Check %d is for $%.2f' % (checknum, amount)
|
|
|
|
...
|
|
|
|
Check 1200 is for $120.15
|
|
|
|
Check 1201 is for $764.05
|
|
|
|
Check 1202 is for $823.14
|
|
|
|
|
|
|
|
>>> import operator
|
|
|
|
>>> for cube in imap(operator.pow, xrange(1,4), repeat(3)):
|
|
|
|
... print cube
|
|
|
|
...
|
|
|
|
1
|
|
|
|
8
|
|
|
|
27
|
|
|
|
|
|
|
|
>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
|
2003-06-28 02:44:36 -03:00
|
|
|
>>> for name in islice(reportlines, 3, None, 2):
|
2003-01-31 20:10:11 -04:00
|
|
|
... print name.title()
|
|
|
|
...
|
|
|
|
Alex
|
|
|
|
Laura
|
|
|
|
Martin
|
|
|
|
Walter
|
|
|
|
Samuele
|
|
|
|
|
2003-12-06 12:23:06 -04:00
|
|
|
>>> from operator import itemgetter
|
|
|
|
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
|
2006-05-28 16:13:17 -03:00
|
|
|
>>> di = sorted(sorted(d.iteritems()), key=itemgetter(1))
|
2003-12-06 12:23:06 -04:00
|
|
|
>>> for k, g in groupby(di, itemgetter(1)):
|
|
|
|
... print k, map(itemgetter(0), g)
|
|
|
|
...
|
|
|
|
1 ['a', 'c', 'e']
|
|
|
|
2 ['b', 'd', 'f']
|
|
|
|
3 ['g']
|
|
|
|
|
2004-01-20 16:04:40 -04:00
|
|
|
# Find runs of consecutive numbers using groupby. The key to the solution
|
|
|
|
# is differencing with a range so that consecutive numbers all appear in
|
|
|
|
# same group.
|
|
|
|
>>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
|
|
|
|
>>> for k, g in groupby(enumerate(data), lambda (i,x):i-x):
|
|
|
|
... print map(operator.itemgetter(1), g)
|
|
|
|
...
|
|
|
|
[1]
|
|
|
|
[4, 5, 6]
|
|
|
|
[10]
|
|
|
|
[15, 16, 17, 18]
|
|
|
|
[22]
|
|
|
|
[25, 26, 27, 28]
|
|
|
|
|
2008-07-19 20:58:47 -03:00
|
|
|
>>> def take(n, iterable):
|
|
|
|
... "Return first n items of the iterable as a list"
|
|
|
|
... return list(islice(iterable, n))
|
2003-09-08 20:58:40 -03:00
|
|
|
|
2008-07-19 20:58:47 -03:00
|
|
|
>>> def enumerate(iterable, start=0):
|
|
|
|
... return izip(count(start), iterable)
|
2003-01-31 20:10:11 -04:00
|
|
|
|
2008-07-19 20:58:47 -03:00
|
|
|
>>> def tabulate(function, start=0):
|
2003-01-31 20:10:11 -04:00
|
|
|
... "Return function(0), function(1), ..."
|
2008-07-19 20:58:47 -03:00
|
|
|
... return imap(function, count(start))
|
2003-01-31 20:10:11 -04:00
|
|
|
|
|
|
|
>>> def nth(iterable, n):
|
2008-07-19 20:58:47 -03:00
|
|
|
... "Returns the nth item or empty list"
|
2003-02-09 02:40:58 -04:00
|
|
|
... return list(islice(iterable, n, n+1))
|
|
|
|
|
2008-07-19 20:58:47 -03:00
|
|
|
>>> def quantify(iterable, pred=bool):
|
|
|
|
... "Count how many times the predicate is true"
|
|
|
|
... return sum(imap(pred, iterable))
|
|
|
|
|
|
|
|
>>> def padnone(iterable):
|
2003-05-02 16:04:37 -03:00
|
|
|
... "Returns the sequence elements and then returns None indefinitely"
|
2008-07-19 20:58:47 -03:00
|
|
|
... return chain(iterable, repeat(None))
|
2003-05-02 16:04:37 -03:00
|
|
|
|
2008-07-19 20:58:47 -03:00
|
|
|
>>> def ncycles(iterable, n):
|
|
|
|
... "Returns the seqeuence elements n times"
|
|
|
|
... return chain(*repeat(iterable, n))
|
2003-05-02 16:04:37 -03:00
|
|
|
|
|
|
|
>>> def dotproduct(vec1, vec2):
|
|
|
|
... return sum(imap(operator.mul, vec1, vec2))
|
|
|
|
|
2003-10-24 05:45:23 -03:00
|
|
|
>>> def flatten(listOfLists):
|
2008-03-06 21:33:20 -04:00
|
|
|
... return list(chain.from_iterable(listOfLists))
|
2003-10-24 05:45:23 -03:00
|
|
|
|
|
|
|
>>> def repeatfunc(func, times=None, *args):
|
|
|
|
... "Repeat calls to func with specified arguments."
|
|
|
|
... " Example: repeatfunc(random.random)"
|
|
|
|
... if times is None:
|
|
|
|
... return starmap(func, repeat(args))
|
|
|
|
... else:
|
|
|
|
... return starmap(func, repeat(args, times))
|
|
|
|
|
2003-10-26 11:34:50 -04:00
|
|
|
>>> def pairwise(iterable):
|
|
|
|
... "s -> (s0,s1), (s1,s2), (s2, s3), ..."
|
|
|
|
... a, b = tee(iterable)
|
2008-03-06 21:33:20 -04:00
|
|
|
... for elem in b:
|
|
|
|
... break
|
2003-11-12 10:32:26 -04:00
|
|
|
... return izip(a, b)
|
2003-05-02 16:04:37 -03:00
|
|
|
|
2008-03-06 21:33:20 -04:00
|
|
|
>>> def grouper(n, iterable, fillvalue=None):
|
2008-07-30 04:27:30 -03:00
|
|
|
... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
|
2008-03-06 21:33:20 -04:00
|
|
|
... args = [iter(iterable)] * n
|
2008-07-30 22:19:50 -03:00
|
|
|
... return izip_longest(fillvalue=fillvalue, *args)
|
2008-03-06 16:52:01 -04:00
|
|
|
|
|
|
|
>>> def roundrobin(*iterables):
|
2008-07-30 04:27:30 -03:00
|
|
|
... "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
|
2008-03-06 16:52:01 -04:00
|
|
|
... # Recipe credited to George Sakkis
|
|
|
|
... pending = len(iterables)
|
|
|
|
... nexts = cycle(iter(it).next for it in iterables)
|
|
|
|
... while pending:
|
|
|
|
... try:
|
|
|
|
... for next in nexts:
|
|
|
|
... yield next()
|
|
|
|
... except StopIteration:
|
|
|
|
... pending -= 1
|
|
|
|
... nexts = cycle(islice(nexts, pending))
|
|
|
|
|
|
|
|
>>> def powerset(iterable):
|
2009-01-25 17:31:47 -04:00
|
|
|
... "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
|
|
|
|
... s = list(iterable)
|
|
|
|
... return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
|
2008-03-06 16:52:01 -04:00
|
|
|
|
2009-01-02 17:26:45 -04:00
|
|
|
>>> def unique_everseen(iterable, key=None):
|
|
|
|
... "List unique elements, preserving order. Remember all elements ever seen."
|
|
|
|
... # unique_everseen('AAAABBBCCDAABBB') --> A B C D
|
|
|
|
... # unique_everseen('ABBCcAD', str.lower) --> A B C D
|
|
|
|
... seen = set()
|
|
|
|
... seen_add = seen.add
|
|
|
|
... if key is None:
|
|
|
|
... for element in iterable:
|
|
|
|
... if element not in seen:
|
|
|
|
... seen_add(element)
|
|
|
|
... yield element
|
|
|
|
... else:
|
|
|
|
... for element in iterable:
|
|
|
|
... k = key(element)
|
|
|
|
... if k not in seen:
|
|
|
|
... seen_add(k)
|
|
|
|
... yield element
|
|
|
|
|
|
|
|
>>> def unique_justseen(iterable, key=None):
|
|
|
|
... "List unique elements, preserving order. Remember only the element just seen."
|
|
|
|
... # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
|
|
|
|
... # unique_justseen('ABBCcAD', str.lower) --> A B C A D
|
|
|
|
... return imap(next, imap(itemgetter(1), groupby(iterable, key)))
|
|
|
|
|
2003-05-02 16:04:37 -03:00
|
|
|
This is not part of the examples but it tests to make sure the definitions
|
|
|
|
perform as purported.
|
|
|
|
|
2003-09-08 20:58:40 -03:00
|
|
|
>>> take(10, count())
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
2003-05-02 16:04:37 -03:00
|
|
|
>>> list(enumerate('abc'))
|
|
|
|
[(0, 'a'), (1, 'b'), (2, 'c')]
|
|
|
|
|
|
|
|
>>> list(islice(tabulate(lambda x: 2*x), 4))
|
|
|
|
[0, 2, 4, 6]
|
|
|
|
|
|
|
|
>>> nth('abcde', 3)
|
|
|
|
['d']
|
|
|
|
|
2003-10-05 13:47:36 -03:00
|
|
|
>>> quantify(xrange(99), lambda x: x%2==0)
|
2003-08-08 02:10:41 -03:00
|
|
|
50
|
|
|
|
|
2003-10-24 05:45:23 -03:00
|
|
|
>>> a = [[1, 2, 3], [4, 5, 6]]
|
|
|
|
>>> flatten(a)
|
|
|
|
[1, 2, 3, 4, 5, 6]
|
|
|
|
|
|
|
|
>>> list(repeatfunc(pow, 5, 2, 3))
|
|
|
|
[8, 8, 8, 8, 8]
|
|
|
|
|
|
|
|
>>> import random
|
|
|
|
>>> take(5, imap(int, repeatfunc(random.random)))
|
|
|
|
[0, 0, 0, 0, 0]
|
|
|
|
|
2003-10-26 11:34:50 -04:00
|
|
|
>>> list(pairwise('abcd'))
|
|
|
|
[('a', 'b'), ('b', 'c'), ('c', 'd')]
|
2003-05-02 16:04:37 -03:00
|
|
|
|
2003-10-26 11:34:50 -04:00
|
|
|
>>> list(pairwise([]))
|
|
|
|
[]
|
|
|
|
|
|
|
|
>>> list(pairwise('a'))
|
2003-06-18 16:25:37 -03:00
|
|
|
[]
|
|
|
|
|
2003-05-02 16:04:37 -03:00
|
|
|
>>> list(islice(padnone('abc'), 0, 6))
|
|
|
|
['a', 'b', 'c', None, None, None]
|
|
|
|
|
|
|
|
>>> list(ncycles('abc', 3))
|
|
|
|
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
|
|
|
|
|
|
|
|
>>> dotproduct([1,2,3], [4,5,6])
|
|
|
|
32
|
|
|
|
|
2008-03-06 16:52:01 -04:00
|
|
|
>>> list(grouper(3, 'abcdefg', 'x'))
|
|
|
|
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
|
|
|
|
|
|
|
|
>>> list(roundrobin('abc', 'd', 'ef'))
|
|
|
|
['a', 'd', 'e', 'b', 'f', 'c']
|
|
|
|
|
2009-01-25 17:31:47 -04:00
|
|
|
>>> list(powerset([1,2,3]))
|
|
|
|
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
|
2008-03-06 16:52:01 -04:00
|
|
|
|
2009-01-02 17:26:45 -04:00
|
|
|
>>> list(unique_everseen('AAAABBBCCDAABBB'))
|
|
|
|
['A', 'B', 'C', 'D']
|
|
|
|
|
|
|
|
>>> list(unique_everseen('ABBCcAD', str.lower))
|
|
|
|
['A', 'B', 'C', 'D']
|
|
|
|
|
|
|
|
>>> list(unique_justseen('AAAABBBCCDAABBB'))
|
|
|
|
['A', 'B', 'C', 'D', 'A', 'B']
|
|
|
|
|
|
|
|
>>> list(unique_justseen('ABBCcAD', str.lower))
|
|
|
|
['A', 'B', 'C', 'A', 'D']
|
|
|
|
|
2003-01-31 20:10:11 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
__test__ = {'libreftest' : libreftest}
|
|
|
|
|
|
|
|
def test_main(verbose=None):
|
2003-08-29 20:09:58 -03:00
|
|
|
test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC,
|
2007-01-21 06:28:43 -04:00
|
|
|
RegressionTests, LengthTransparency,
|
2008-03-06 16:52:01 -04:00
|
|
|
SubclassWithKwargsTest, TestExamples)
|
2003-05-29 04:18:57 -03:00
|
|
|
test_support.run_unittest(*test_classes)
|
2003-01-31 20:10:11 -04:00
|
|
|
|
|
|
|
# verify reference counting
|
|
|
|
if verbose and hasattr(sys, "gettotalrefcount"):
|
2003-06-29 17:36:23 -03:00
|
|
|
import gc
|
2003-05-02 19:38:07 -03:00
|
|
|
counts = [None] * 5
|
|
|
|
for i in xrange(len(counts)):
|
2003-05-29 04:18:57 -03:00
|
|
|
test_support.run_unittest(*test_classes)
|
2003-06-29 17:36:23 -03:00
|
|
|
gc.collect()
|
2003-05-02 19:38:07 -03:00
|
|
|
counts[i] = sys.gettotalrefcount()
|
2003-01-31 20:10:11 -04:00
|
|
|
print counts
|
|
|
|
|
2003-05-02 16:04:37 -03:00
|
|
|
# doctest the examples in the library reference
|
2003-05-16 20:16:36 -03:00
|
|
|
test_support.run_doctest(sys.modules[__name__], verbose)
|
2003-05-02 16:04:37 -03:00
|
|
|
|
2003-01-31 20:10:11 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main(verbose=True)
|