Commits

Sergey Astanin committed ff7f451

split.py: same source for Python2 and Python3

Comments (0)

Files changed (1)

 """Functions to split or partition sequences."""
 
 from collections import deque
+from platform import python_version_tuple
 
 __all__ = [ "partition", "chop" ]
 __author__ = "Sergey Astanin"
 __license__ = "MIT"
 __version__ = "0.2"
 
+if python_version_tuple()[0] == "2":
+    _range = xrange
+else:
+    _range = range
+
 class _SplitSeq:
     """
     Lazily process a sequence in single pass and split into two.
             return these.popleft()
         else:
             while 1: # exit on StopIteration
-                n = self.seq.next()
+                n = next(self.seq)
                 if cond(n):
                     return n
                 else:
 
     >>> def odd(x): return x%2 != 0
     >>> odds, evens = partition(odd, range(10))
-    >>> odds.next()
+    >>> next(odds)
     1
-    >>> odds.next()
+    >>> next(odds)
     3
     >>> list(evens)
     [0, 2, 4, 6, 8]
     head = []
     xs = iter(sequence)
     try:
-        for i in xrange(n):
-            x = xs.next()
+        for i in _range(n):
+            x = next(xs)
             head.append(x)
         return head, xs
     except StopIteration: