@@ -1192,6 +1192,7 @@ def test_islice(self):
1192
1192
(10 , 20 , 3 ),
1193
1193
(10 , 3 , 20 ),
1194
1194
(10 , 20 ),
1195
+ (10 , 10 ),
1195
1196
(10 , 3 ),
1196
1197
(20 ,)
1197
1198
]:
@@ -1218,6 +1219,10 @@ def test_islice(self):
1218
1219
self .assertEqual (list (islice (it , 3 )), list (range (3 )))
1219
1220
self .assertEqual (list (it ), list (range (3 , 10 )))
1220
1221
1222
+ it = iter (range (10 ))
1223
+ self .assertEqual (list (islice (it , 3 , 3 )), [])
1224
+ self .assertEqual (list (it ), list (range (3 , 10 )))
1225
+
1221
1226
# Test invalid arguments
1222
1227
ra = range (10 )
1223
1228
self .assertRaises (TypeError , islice , ra )
@@ -1604,6 +1609,48 @@ def test_takewhile(self):
1604
1609
self .assertEqual (list (takewhile (lambda x : x < 5 , [1 ,4 ,6 ,4 ,1 ])), [1 ,4 ])
1605
1610
1606
1611
1612
+ class TestPurePythonRoughEquivalents (unittest .TestCase ):
1613
+
1614
+ @staticmethod
1615
+ def islice (iterable , * args ):
1616
+ s = slice (* args )
1617
+ start , stop , step = s .start or 0 , s .stop or sys .maxsize , s .step or 1
1618
+ it = iter (range (start , stop , step ))
1619
+ try :
1620
+ nexti = next (it )
1621
+ except StopIteration :
1622
+ # Consume *iterable* up to the *start* position.
1623
+ for i , element in zip (range (start ), iterable ):
1624
+ pass
1625
+ return
1626
+ try :
1627
+ for i , element in enumerate (iterable ):
1628
+ if i == nexti :
1629
+ yield element
1630
+ nexti = next (it )
1631
+ except StopIteration :
1632
+ # Consume to *stop*.
1633
+ for i , element in zip (range (i + 1 , stop ), iterable ):
1634
+ pass
1635
+
1636
+ def test_islice_recipe (self ):
1637
+ self .assertEqual (list (self .islice ('ABCDEFG' , 2 )), list ('AB' ))
1638
+ self .assertEqual (list (self .islice ('ABCDEFG' , 2 , 4 )), list ('CD' ))
1639
+ self .assertEqual (list (self .islice ('ABCDEFG' , 2 , None )), list ('CDEFG' ))
1640
+ self .assertEqual (list (self .islice ('ABCDEFG' , 0 , None , 2 )), list ('ACEG' ))
1641
+ # Test items consumed.
1642
+ it = iter (range (10 ))
1643
+ self .assertEqual (list (self .islice (it , 3 )), list (range (3 )))
1644
+ self .assertEqual (list (it ), list (range (3 , 10 )))
1645
+ it = iter (range (10 ))
1646
+ self .assertEqual (list (self .islice (it , 3 , 3 )), [])
1647
+ self .assertEqual (list (it ), list (range (3 , 10 )))
1648
+ # Test that slice finishes in predictable state.
1649
+ c = count ()
1650
+ self .assertEqual (list (self .islice (c , 1 , 3 , 50 )), [1 ])
1651
+ self .assertEqual (next (c ), 3 )
1652
+
1653
+
1607
1654
class TestGC (unittest .TestCase ):
1608
1655
1609
1656
def makecycle (self , iterator , container ):
@@ -2158,6 +2205,17 @@ def test_permutations_sizeof(self):
2158
2205
... "Return function(0), function(1), ..."
2159
2206
... return map(function, count(start))
2160
2207
2208
+ >>> import collections
2209
+ >>> def consume(iterator, n=None):
2210
+ ... "Advance the iterator n-steps ahead. If n is None, consume entirely."
2211
+ ... # Use functions that consume iterators at C speed.
2212
+ ... if n is None:
2213
+ ... # feed the entire iterator into a zero-length deque
2214
+ ... collections.deque(iterator, maxlen=0)
2215
+ ... else:
2216
+ ... # advance to the empty slice starting at position n
2217
+ ... next(islice(iterator, n, n), None)
2218
+
2161
2219
>>> def nth(iterable, n, default=None):
2162
2220
... "Returns the nth item or a default value"
2163
2221
... return next(islice(iterable, n, None), default)
@@ -2298,6 +2356,14 @@ def test_permutations_sizeof(self):
2298
2356
>>> list(islice(tabulate(lambda x: 2*x), 4))
2299
2357
[0, 2, 4, 6]
2300
2358
2359
+ >>> it = iter(range(10))
2360
+ >>> consume(it, 3)
2361
+ >>> next(it)
2362
+ 3
2363
+ >>> consume(it)
2364
+ >>> next(it, 'Done')
2365
+ 'Done'
2366
+
2301
2367
>>> nth('abcde', 3)
2302
2368
'd'
2303
2369
@@ -2386,6 +2452,7 @@ def test_main(verbose=None):
2386
2452
test_classes = (TestBasicOps , TestVariousIteratorArgs , TestGC ,
2387
2453
RegressionTests , LengthTransparency ,
2388
2454
SubclassWithKwargsTest , TestExamples ,
2455
+ TestPurePythonRoughEquivalents ,
2389
2456
SizeofTest )
2390
2457
support .run_unittest (* test_classes )
2391
2458
0 commit comments