@@ -1172,6 +1172,7 @@ def test_islice(self):
1172
1172
(10 , 20 , 3 ),
1173
1173
(10 , 3 , 20 ),
1174
1174
(10 , 20 ),
1175
+ (10 , 10 ),
1175
1176
(10 , 3 ),
1176
1177
(20 ,)
1177
1178
]:
@@ -1198,6 +1199,10 @@ def test_islice(self):
1198
1199
self .assertEqual (list (islice (it , 3 )), list (range (3 )))
1199
1200
self .assertEqual (list (it ), list (range (3 , 10 )))
1200
1201
1202
+ it = iter (range (10 ))
1203
+ self .assertEqual (list (islice (it , 3 , 3 )), [])
1204
+ self .assertEqual (list (it ), list (range (3 , 10 )))
1205
+
1201
1206
# Test invalid arguments
1202
1207
ra = range (10 )
1203
1208
self .assertRaises (TypeError , islice , ra )
@@ -1571,6 +1576,48 @@ def test_takewhile(self):
1571
1576
self .assertEqual (list (takewhile (lambda x : x < 5 , [1 ,4 ,6 ,4 ,1 ])), [1 ,4 ])
1572
1577
1573
1578
1579
+ class TestPurePythonRoughEquivalents (unittest .TestCase ):
1580
+
1581
+ @staticmethod
1582
+ def islice (iterable , * args ):
1583
+ s = slice (* args )
1584
+ start , stop , step = s .start or 0 , s .stop or sys .maxsize , s .step or 1
1585
+ it = iter (range (start , stop , step ))
1586
+ try :
1587
+ nexti = next (it )
1588
+ except StopIteration :
1589
+ # Consume *iterable* up to the *start* position.
1590
+ for i , element in zip (range (start ), iterable ):
1591
+ pass
1592
+ return
1593
+ try :
1594
+ for i , element in enumerate (iterable ):
1595
+ if i == nexti :
1596
+ yield element
1597
+ nexti = next (it )
1598
+ except StopIteration :
1599
+ # Consume to *stop*.
1600
+ for i , element in zip (range (i + 1 , stop ), iterable ):
1601
+ pass
1602
+
1603
+ def test_islice_recipe (self ):
1604
+ self .assertEqual (list (self .islice ('ABCDEFG' , 2 )), list ('AB' ))
1605
+ self .assertEqual (list (self .islice ('ABCDEFG' , 2 , 4 )), list ('CD' ))
1606
+ self .assertEqual (list (self .islice ('ABCDEFG' , 2 , None )), list ('CDEFG' ))
1607
+ self .assertEqual (list (self .islice ('ABCDEFG' , 0 , None , 2 )), list ('ACEG' ))
1608
+ # Test items consumed.
1609
+ it = iter (range (10 ))
1610
+ self .assertEqual (list (self .islice (it , 3 )), list (range (3 )))
1611
+ self .assertEqual (list (it ), list (range (3 , 10 )))
1612
+ it = iter (range (10 ))
1613
+ self .assertEqual (list (self .islice (it , 3 , 3 )), [])
1614
+ self .assertEqual (list (it ), list (range (3 , 10 )))
1615
+ # Test that slice finishes in predictable state.
1616
+ c = count ()
1617
+ self .assertEqual (list (self .islice (c , 1 , 3 , 50 )), [1 ])
1618
+ self .assertEqual (next (c ), 3 )
1619
+
1620
+
1574
1621
class TestGC (unittest .TestCase ):
1575
1622
1576
1623
def makecycle (self , iterator , container ):
@@ -2125,6 +2172,17 @@ def test_permutations_sizeof(self):
2125
2172
... "Return function(0), function(1), ..."
2126
2173
... return map(function, count(start))
2127
2174
2175
+ >>> import collections
2176
+ >>> def consume(iterator, n=None):
2177
+ ... "Advance the iterator n-steps ahead. If n is None, consume entirely."
2178
+ ... # Use functions that consume iterators at C speed.
2179
+ ... if n is None:
2180
+ ... # feed the entire iterator into a zero-length deque
2181
+ ... collections.deque(iterator, maxlen=0)
2182
+ ... else:
2183
+ ... # advance to the empty slice starting at position n
2184
+ ... next(islice(iterator, n, n), None)
2185
+
2128
2186
>>> def nth(iterable, n, default=None):
2129
2187
... "Returns the nth item or a default value"
2130
2188
... return next(islice(iterable, n, None), default)
@@ -2265,6 +2323,14 @@ def test_permutations_sizeof(self):
2265
2323
>>> list(islice(tabulate(lambda x: 2*x), 4))
2266
2324
[0, 2, 4, 6]
2267
2325
2326
+ >>> it = iter(range(10))
2327
+ >>> consume(it, 3)
2328
+ >>> next(it)
2329
+ 3
2330
+ >>> consume(it)
2331
+ >>> next(it, 'Done')
2332
+ 'Done'
2333
+
2268
2334
>>> nth('abcde', 3)
2269
2335
'd'
2270
2336
@@ -2353,6 +2419,7 @@ def test_main(verbose=None):
2353
2419
test_classes = (TestBasicOps , TestVariousIteratorArgs , TestGC ,
2354
2420
RegressionTests , LengthTransparency ,
2355
2421
SubclassWithKwargsTest , TestExamples ,
2422
+ TestPurePythonRoughEquivalents ,
2356
2423
SizeofTest )
2357
2424
support .run_unittest (* test_classes )
2358
2425
0 commit comments