@@ -191,6 +191,21 @@ def permutations2(iterable, r=None):
191191 self .assertEqual (len (set (map (id , permutations ('abcde' , 3 )))), 1 )
192192 self .assertNotEqual (len (set (map (id , list (permutations ('abcde' , 3 ))))), 1 )
193193
194+ def test_compress (self ):
195+ self .assertEqual (list (compress ('ABCDEF' , [1 ,0 ,1 ,0 ,1 ,1 ])), list ('ACEF' ))
196+ self .assertEqual (list (compress ('ABCDEF' , [0 ,0 ,0 ,0 ,0 ,0 ])), list ('' ))
197+ self .assertEqual (list (compress ('ABCDEF' , [1 ,1 ,1 ,1 ,1 ,1 ])), list ('ABCDEF' ))
198+ self .assertEqual (list (compress ('ABCDEF' , [1 ,0 ,1 ])), list ('AC' ))
199+ self .assertEqual (list (compress ('ABC' , [0 ,1 ,1 ,1 ,1 ,1 ])), list ('BC' ))
200+ n = 10000
201+ data = chain .from_iterable (repeat (range (6 ), n ))
202+ selectors = chain .from_iterable (repeat ((0 , 1 )))
203+ self .assertEqual (list (compress (data , selectors )), [1 ,3 ,5 ] * n )
204+ self .assertRaises (TypeError , compress , None , range (6 )) # 1st arg not iterable
205+ self .assertRaises (TypeError , compress , range (6 ), None ) # 2nd arg not iterable
206+ self .assertRaises (TypeError , compress , range (6 )) # too few args
207+ self .assertRaises (TypeError , compress , range (6 ), None ) # too many args
208+
194209 def test_count (self ):
195210 self .assertEqual (zip ('abc' ,count ()), [('a' , 0 ), ('b' , 1 ), ('c' , 2 )])
196211 self .assertEqual (zip ('abc' ,count (3 )), [('a' , 3 ), ('b' , 4 ), ('c' , 5 )])
@@ -701,6 +716,9 @@ def test_combinations(self):
701716 self .assertEqual (list (combinations (range (4 ), 3 )),
702717 [(0 ,1 ,2 ), (0 ,1 ,3 ), (0 ,2 ,3 ), (1 ,2 ,3 )])
703718
719+ def test_compress (self ):
720+ self .assertEqual (list (compress ('ABCDEF' , [1 ,0 ,1 ,0 ,1 ,1 ])), list ('ACEF' ))
721+
704722 def test_count (self ):
705723 self .assertEqual (list (islice (count (10 ), 5 )), [10 , 11 , 12 , 13 , 14 ])
706724
@@ -781,6 +799,10 @@ def test_combinations(self):
781799 a = []
782800 self .makecycle (combinations ([1 ,2 ,a ,3 ], 3 ), a )
783801
802+ def test_compress (self ):
803+ a = []
804+ self .makecycle (compress ('ABCDEF' , [1 ,0 ,1 ,0 ,1 ,0 ]), a )
805+
784806 def test_cycle (self ):
785807 a = []
786808 self .makecycle (cycle ([a ]* 2 ), a )
@@ -934,6 +956,15 @@ def test_chain(self):
934956 self .assertRaises (TypeError , list , chain (N (s )))
935957 self .assertRaises (ZeroDivisionError , list , chain (E (s )))
936958
959+ def test_compress (self ):
960+ for s in ("123" , "" , range (1000 ), ('do' , 1.2 ), xrange (2000 ,2200 ,5 )):
961+ n = len (s )
962+ for g in (G , I , Ig , S , L , R ):
963+ self .assertEqual (list (compress (g (s ), repeat (1 ))), list (g (s )))
964+ self .assertRaises (TypeError , compress , X (s ), repeat (1 ))
965+ self .assertRaises (TypeError , list , compress (N (s ), repeat (1 )))
966+ self .assertRaises (ZeroDivisionError , list , compress (E (s ), repeat (1 )))
967+
937968 def test_product (self ):
938969 for s in ("123" , "" , range (1000 ), ('do' , 1.2 ), xrange (2000 ,2200 ,5 )):
939970 self .assertRaises (TypeError , product , X (s ))
@@ -1125,7 +1156,7 @@ class SubclassWithKwargsTest(unittest.TestCase):
11251156 def test_keywords_in_subclass (self ):
11261157 # count is not subclassable...
11271158 for cls in (repeat , izip , ifilter , ifilterfalse , chain , imap ,
1128- starmap , islice , takewhile , dropwhile , cycle ):
1159+ starmap , islice , takewhile , dropwhile , cycle , compress ):
11291160 class Subclass (cls ):
11301161 def __init__ (self , newarg = None , * args ):
11311162 cls .__init__ (self , * args )
@@ -1262,10 +1293,6 @@ def __init__(self, newarg=None, *args):
12621293... for n in xrange(2**len(pairs)):
12631294... yield set(x for m, x in pairs if m&n)
12641295
1265- >>> def compress(data, selectors):
1266- ... "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
1267- ... return (d for d, s in izip(data, selectors) if s)
1268-
12691296>>> def combinations_with_replacement(iterable, r):
12701297... "combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC"
12711298... pool = tuple(iterable)
@@ -1361,9 +1388,6 @@ def __init__(self, newarg=None, *args):
13611388>>> map(sorted, powerset('ab'))
13621389[[], ['a'], ['b'], ['a', 'b']]
13631390
1364- >>> list(compress('abcdef', [1,0,1,0,1,1]))
1365- ['a', 'c', 'e', 'f']
1366-
13671391>>> list(combinations_with_replacement('abc', 2))
13681392[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')]
13691393
0 commit comments