Skip to content

Commit

Permalink
Add count from 2.7 to 2.6
Browse files Browse the repository at this point in the history
The future.types.newrange already has a _count method which is now moved to the
backports to allow make this public.
  • Loading branch information
xZise committed Jun 20, 2015
1 parent af3d26e commit 1b8ef51
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/future/backports/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ def ceil(x):
return int(oldceil(x))


if PY26:
# itertools.count in Py 2.6 doesn't accept a step parameter
def count(start=0, step=1):
while True:
yield start
start += step
else:
from itertools import count


# OrderedDict Shim from Raymond Hettinger, python core dev
# http://code.activestate.com/recipes/576693-ordered-dictionary-for-py24/
# here to support version 2.6.
Expand Down
10 changes: 2 additions & 8 deletions src/future/types/newrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from collections import Sequence, Iterator
from itertools import islice

from future.backports.misc import count

class newrange(Sequence):
"""
Expand Down Expand Up @@ -141,7 +142,7 @@ class range_iterator(Iterator):
"""An iterator for a :class:`range`.
"""
def __init__(self, range_):
self._stepper = islice(_count(range_.start, range_.step), len(range_))
self._stepper = islice(count(range_.start, range_.step), len(range_))

def __iter__(self):
return self
Expand All @@ -150,11 +151,4 @@ def next(self):
return next(self._stepper)


# itertools.count in Py 2.6 doesn't accept a step parameter
def _count(start=0, step=1):
while True:
yield start
start += step


__all__ = ['newrange']
57 changes: 57 additions & 0 deletions tests/test_future/test_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
"""
Tests for the backported class:`range` class.
"""
from itertools import count as it_count

from future.backports.misc import count
from future.tests.base import unittest, skip26


class CountTest(unittest.TestCase):

"""Test the count function."""

def _test_count_func(self, func):
self.assertEqual(next(func(1)), 1)
self.assertEqual(next(func(start=1)), 1)

c = func()
self.assertEqual(next(c), 0)
self.assertEqual(next(c), 1)
self.assertEqual(next(c), 2)
c = func(1, 1)
self.assertEqual(next(c), 1)
self.assertEqual(next(c), 2)
c = func(step=1)
self.assertEqual(next(c), 0)
self.assertEqual(next(c), 1)
c = func(start=1, step=1)
self.assertEqual(next(c), 1)
self.assertEqual(next(c), 2)

c = func(-1)
self.assertEqual(next(c), -1)
self.assertEqual(next(c), 0)
self.assertEqual(next(c), 1)
c = func(1, -1)
self.assertEqual(next(c), 1)
self.assertEqual(next(c), 0)
self.assertEqual(next(c), -1)
c = func(-1, -1)
self.assertEqual(next(c), -1)
self.assertEqual(next(c), -2)
self.assertEqual(next(c), -3)

def test_count(self):
"""Test the count function."""
self._test_count_func(count)

@skip26
def test_own_count(self):
"""Test own count implementation."""
self._test_count_func(it_count)


if __name__ == '__main__':
unittest.main()

0 comments on commit 1b8ef51

Please sign in to comment.