Skip to content

Commit 1b8ef51

Browse files
committed
Add count from 2.7 to 2.6
The future.types.newrange already has a _count method which is now moved to the backports to allow make this public.
1 parent af3d26e commit 1b8ef51

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

src/future/backports/misc.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ def ceil(x):
2222
return int(oldceil(x))
2323

2424

25+
if PY26:
26+
# itertools.count in Py 2.6 doesn't accept a step parameter
27+
def count(start=0, step=1):
28+
while True:
29+
yield start
30+
start += step
31+
else:
32+
from itertools import count
33+
34+
2535
# OrderedDict Shim from Raymond Hettinger, python core dev
2636
# http://code.activestate.com/recipes/576693-ordered-dictionary-for-py24/
2737
# here to support version 2.6.

src/future/types/newrange.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from collections import Sequence, Iterator
2222
from itertools import islice
2323

24+
from future.backports.misc import count
2425

2526
class newrange(Sequence):
2627
"""
@@ -141,7 +142,7 @@ class range_iterator(Iterator):
141142
"""An iterator for a :class:`range`.
142143
"""
143144
def __init__(self, range_):
144-
self._stepper = islice(_count(range_.start, range_.step), len(range_))
145+
self._stepper = islice(count(range_.start, range_.step), len(range_))
145146

146147
def __iter__(self):
147148
return self
@@ -150,11 +151,4 @@ def next(self):
150151
return next(self._stepper)
151152

152153

153-
# itertools.count in Py 2.6 doesn't accept a step parameter
154-
def _count(start=0, step=1):
155-
while True:
156-
yield start
157-
start += step
158-
159-
160154
__all__ = ['newrange']

tests/test_future/test_count.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Tests for the backported class:`range` class.
4+
"""
5+
from itertools import count as it_count
6+
7+
from future.backports.misc import count
8+
from future.tests.base import unittest, skip26
9+
10+
11+
class CountTest(unittest.TestCase):
12+
13+
"""Test the count function."""
14+
15+
def _test_count_func(self, func):
16+
self.assertEqual(next(func(1)), 1)
17+
self.assertEqual(next(func(start=1)), 1)
18+
19+
c = func()
20+
self.assertEqual(next(c), 0)
21+
self.assertEqual(next(c), 1)
22+
self.assertEqual(next(c), 2)
23+
c = func(1, 1)
24+
self.assertEqual(next(c), 1)
25+
self.assertEqual(next(c), 2)
26+
c = func(step=1)
27+
self.assertEqual(next(c), 0)
28+
self.assertEqual(next(c), 1)
29+
c = func(start=1, step=1)
30+
self.assertEqual(next(c), 1)
31+
self.assertEqual(next(c), 2)
32+
33+
c = func(-1)
34+
self.assertEqual(next(c), -1)
35+
self.assertEqual(next(c), 0)
36+
self.assertEqual(next(c), 1)
37+
c = func(1, -1)
38+
self.assertEqual(next(c), 1)
39+
self.assertEqual(next(c), 0)
40+
self.assertEqual(next(c), -1)
41+
c = func(-1, -1)
42+
self.assertEqual(next(c), -1)
43+
self.assertEqual(next(c), -2)
44+
self.assertEqual(next(c), -3)
45+
46+
def test_count(self):
47+
"""Test the count function."""
48+
self._test_count_func(count)
49+
50+
@skip26
51+
def test_own_count(self):
52+
"""Test own count implementation."""
53+
self._test_count_func(it_count)
54+
55+
56+
if __name__ == '__main__':
57+
unittest.main()

0 commit comments

Comments
 (0)