@@ -802,6 +802,7 @@ def test_islice(self):
802
802
(10 , 20 , 3 ),
803
803
(10 , 3 , 20 ),
804
804
(10 , 20 ),
805
+ (10 , 10 ),
805
806
(10 , 3 ),
806
807
(20 ,)
807
808
]:
@@ -826,6 +827,10 @@ def test_islice(self):
826
827
self .assertEqual (list (islice (it , 3 )), range (3 ))
827
828
self .assertEqual (list (it ), range (3 , 10 ))
828
829
830
+ it = iter (range (10 ))
831
+ self .assertEqual (list (islice (it , 3 , 3 )), [])
832
+ self .assertEqual (list (it ), range (3 , 10 ))
833
+
829
834
# Test invalid arguments
830
835
self .assertRaises (TypeError , islice , xrange (10 ))
831
836
self .assertRaises (TypeError , islice , xrange (10 ), 1 , 2 , 3 , 4 )
@@ -1084,6 +1089,48 @@ def test_takewhile(self):
1084
1089
self .assertEqual (list (takewhile (lambda x : x < 5 , [1 ,4 ,6 ,4 ,1 ])), [1 ,4 ])
1085
1090
1086
1091
1092
+ class TestPurePythonRoughEquivalents (unittest .TestCase ):
1093
+
1094
+ @staticmethod
1095
+ def islice (iterable , * args ):
1096
+ s = slice (* args )
1097
+ start , stop , step = s .start or 0 , s .stop or sys .maxint , s .step or 1
1098
+ it = iter (xrange (start , stop , step ))
1099
+ try :
1100
+ nexti = next (it )
1101
+ except StopIteration :
1102
+ # Consume *iterable* up to the *start* position.
1103
+ for i , element in izip (xrange (start ), iterable ):
1104
+ pass
1105
+ return
1106
+ try :
1107
+ for i , element in enumerate (iterable ):
1108
+ if i == nexti :
1109
+ yield element
1110
+ nexti = next (it )
1111
+ except StopIteration :
1112
+ # Consume to *stop*.
1113
+ for i , element in izip (xrange (i + 1 , stop ), iterable ):
1114
+ pass
1115
+
1116
+ def test_islice_recipe (self ):
1117
+ self .assertEqual (list (self .islice ('ABCDEFG' , 2 )), list ('AB' ))
1118
+ self .assertEqual (list (self .islice ('ABCDEFG' , 2 , 4 )), list ('CD' ))
1119
+ self .assertEqual (list (self .islice ('ABCDEFG' , 2 , None )), list ('CDEFG' ))
1120
+ self .assertEqual (list (self .islice ('ABCDEFG' , 0 , None , 2 )), list ('ACEG' ))
1121
+ # Test items consumed.
1122
+ it = iter (xrange (10 ))
1123
+ self .assertEqual (list (self .islice (it , 3 )), range (3 ))
1124
+ self .assertEqual (list (it ), range (3 , 10 ))
1125
+ it = iter (xrange (10 ))
1126
+ self .assertEqual (list (self .islice (it , 3 , 3 )), [])
1127
+ self .assertEqual (list (it ), range (3 , 10 ))
1128
+ # Test that slice finishes in predictable state.
1129
+ c = count ()
1130
+ self .assertEqual (list (self .islice (c , 1 , 3 , 50 )), [1 ])
1131
+ self .assertEqual (next (c ), 3 )
1132
+
1133
+
1087
1134
class TestGC (unittest .TestCase ):
1088
1135
1089
1136
def makecycle (self , iterator , container ):
@@ -1577,6 +1624,17 @@ def __init__(self, newarg=None, *args):
1577
1624
... "Return function(0), function(1), ..."
1578
1625
... return imap(function, count(start))
1579
1626
1627
+ >>> import collections
1628
+ >>> def consume(iterator, n=None):
1629
+ ... "Advance the iterator n-steps ahead. If n is None, consume entirely."
1630
+ ... # Use functions that consume iterators at C speed.
1631
+ ... if n is None:
1632
+ ... # feed the entire iterator into a zero-length deque
1633
+ ... collections.deque(iterator, maxlen=0)
1634
+ ... else:
1635
+ ... # advance to the empty slice starting at position n
1636
+ ... next(islice(iterator, n, n), None)
1637
+
1580
1638
>>> def nth(iterable, n, default=None):
1581
1639
... "Returns the nth item or a default value"
1582
1640
... return next(islice(iterable, n, None), default)
@@ -1678,6 +1736,14 @@ def __init__(self, newarg=None, *args):
1678
1736
>>> list(islice(tabulate(lambda x: 2*x), 4))
1679
1737
[0, 2, 4, 6]
1680
1738
1739
+ >>> it = iter(xrange(10))
1740
+ >>> consume(it, 3)
1741
+ >>> next(it)
1742
+ 3
1743
+ >>> consume(it)
1744
+ >>> next(it, 'Done')
1745
+ 'Done'
1746
+
1681
1747
>>> nth('abcde', 3)
1682
1748
'd'
1683
1749
@@ -1753,7 +1819,8 @@ def __init__(self, newarg=None, *args):
1753
1819
def test_main (verbose = None ):
1754
1820
test_classes = (TestBasicOps , TestVariousIteratorArgs , TestGC ,
1755
1821
RegressionTests , LengthTransparency ,
1756
- SubclassWithKwargsTest , TestExamples )
1822
+ SubclassWithKwargsTest , TestExamples ,
1823
+ TestPurePythonRoughEquivalents )
1757
1824
test_support .run_unittest (* test_classes )
1758
1825
1759
1826
# verify reference counting
0 commit comments