diff options
author | Brett Cannon <bcannon@gmail.com> | 2008-08-09 23:30:55 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2008-08-09 23:30:55 (GMT) |
commit | 83e818415a591a14677ae6985d3bb0a035cd4c48 (patch) | |
tree | 629873089c3a9496cc43af8f4c6572e2917768f7 /Lib | |
parent | c1b76e4aaa094eae58abe96628b958c8fe40639d (diff) | |
download | cpython-83e818415a591a14677ae6985d3bb0a035cd4c48.zip cpython-83e818415a591a14677ae6985d3bb0a035cd4c48.tar.gz cpython-83e818415a591a14677ae6985d3bb0a035cd4c48.tar.bz2 |
Copy reduce() to _functools so to have functools.reduce() not raise a warning
from usage under -3.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/functools.py | 3 | ||||
-rw-r--r-- | Lib/test/test_functools.py | 45 |
2 files changed, 45 insertions, 3 deletions
diff --git a/Lib/functools.py b/Lib/functools.py index 30e1d24..a54f030 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -7,8 +7,7 @@ # Copyright (C) 2006 Python Software Foundation. # See C source code for _functools credits/copyright -from _functools import partial -from __builtin__ import reduce +from _functools import partial, reduce # update_wrapper() and wraps() are tools to help write # wrapper functions that can handle naive introspection diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 6012f9f..30d419d 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -267,6 +267,48 @@ class TestWraps(TestUpdateWrapper): self.assertEqual(wrapper.dict_attr, f.dict_attr) +class TestReduce(unittest.TestCase): + + def test_reduce(self): + class Squares: + + def __init__(self, max): + self.max = max + self.sofar = [] + + def __len__(self): return len(self.sofar) + + def __getitem__(self, i): + if not 0 <= i < self.max: raise IndexError + n = len(self.sofar) + while n <= i: + self.sofar.append(n*n) + n += 1 + return self.sofar[i] + + reduce = functools.reduce + self.assertEqual(reduce(lambda x, y: x+y, ['a', 'b', 'c'], ''), 'abc') + self.assertEqual( + reduce(lambda x, y: x+y, [['a', 'c'], [], ['d', 'w']], []), + ['a','c','d','w'] + ) + self.assertEqual(reduce(lambda x, y: x*y, range(2,8), 1), 5040) + self.assertEqual( + reduce(lambda x, y: x*y, range(2,21), 1L), + 2432902008176640000L + ) + self.assertEqual(reduce(lambda x, y: x+y, Squares(10)), 285) + self.assertEqual(reduce(lambda x, y: x+y, Squares(10), 0), 285) + self.assertEqual(reduce(lambda x, y: x+y, Squares(0), 0), 0) + self.assertRaises(TypeError, reduce) + self.assertRaises(TypeError, reduce, 42, 42) + self.assertRaises(TypeError, reduce, 42, 42, 42) + self.assertEqual(reduce(42, "1"), "1") # func is never called with one item + self.assertEqual(reduce(42, "", "1"), "1") # func is never called with one item + self.assertRaises(TypeError, reduce, 42, (42, 42)) + + + def test_main(verbose=None): import sys @@ -275,7 +317,8 @@ def test_main(verbose=None): TestPartialSubclass, TestPythonPartial, TestUpdateWrapper, - TestWraps + TestWraps, + TestReduce, ) test_support.run_unittest(*test_classes) |