Skip to content

Commit c8eb497

Browse files
committed
Merge branch 'official-count' of https://github.com/xZise/python-future into xZise-official-count
2 parents c66309c + 1b8ef51 commit c8eb497

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/future/backports/misc.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ def ceil(x):
4646

4747
from itertools import islice
4848

49+
if PY26:
50+
# itertools.count in Py 2.6 doesn't accept a step parameter
51+
def count(start=0, step=1):
52+
while True:
53+
yield start
54+
start += step
55+
else:
56+
from itertools import count
57+
58+
4959
if PY3:
5060
try:
5161
from _thread import get_ident
@@ -85,6 +95,10 @@ def wrapper(self):
8595
return decorating_function
8696

8797

98+
# OrderedDict Shim from Raymond Hettinger, python core dev
99+
# http://code.activestate.com/recipes/576693-ordered-dictionary-for-py24/
100+
# here to support version 2.6.
101+
88102
################################################################################
89103
### OrderedDict
90104
################################################################################

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy