Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61104,61110-61112,61114-61115,61117-61125 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61118 | raymond.hettinger | 2008-02-28 23:30:42 +0100 (Thu, 28 Feb 2008) | 1 line
Have itertools.chain() consume its inputs lazily instead of building a tuple of iterators at the outset.
........
r61119 | raymond.hettinger | 2008-02-28 23:46:41 +0100 (Thu, 28 Feb 2008) | 1 line
Add alternate constructor for itertools.chain().
........
r61123 | mark.dickinson | 2008-02-29 03:16:37 +0100 (Fri, 29 Feb 2008) | 2 lines
Add __format__ method to Decimal, to support PEP 3101
........
r61124 | raymond.hettinger | 2008-02-29 03:21:48 +0100 (Fri, 29 Feb 2008) | 1 line
Handle the repeat keyword argument for itertools.product().
........
r61125 | mark.dickinson | 2008-02-29 04:29:17 +0100 (Fri, 29 Feb 2008) | 2 lines
Fix docstring typo.
........
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index ae36dcb..a772615 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -610,6 +610,98 @@
self.assertEqual(eval('Decimal(10)' + sym + 'E()'),
'10' + rop + 'str')
+class DecimalFormatTest(unittest.TestCase):
+ '''Unit tests for the format function.'''
+ def test_formatting(self):
+ # triples giving a format, a Decimal, and the expected result
+ test_values = [
+ ('e', '0E-15', '0e-15'),
+ ('e', '2.3E-15', '2.3e-15'),
+ ('e', '2.30E+2', '2.30e+2'), # preserve significant zeros
+ ('e', '2.30000E-15', '2.30000e-15'),
+ ('e', '1.23456789123456789e40', '1.23456789123456789e+40'),
+ ('e', '1.5', '1.5e+0'),
+ ('e', '0.15', '1.5e-1'),
+ ('e', '0.015', '1.5e-2'),
+ ('e', '0.0000000000015', '1.5e-12'),
+ ('e', '15.0', '1.50e+1'),
+ ('e', '-15', '-1.5e+1'),
+ ('e', '0', '0e+0'),
+ ('e', '0E1', '0e+1'),
+ ('e', '0.0', '0e-1'),
+ ('e', '0.00', '0e-2'),
+ ('.6e', '0E-15', '0.000000e-9'),
+ ('.6e', '0', '0.000000e+6'),
+ ('.6e', '9.999999', '9.999999e+0'),
+ ('.6e', '9.9999999', '1.000000e+1'),
+ ('.6e', '-1.23e5', '-1.230000e+5'),
+ ('.6e', '1.23456789e-3', '1.234568e-3'),
+ ('f', '0', '0'),
+ ('f', '0.0', '0.0'),
+ ('f', '0E-2', '0.00'),
+ ('f', '0.00E-8', '0.0000000000'),
+ ('f', '0E1', '0'), # loses exponent information
+ ('f', '3.2E1', '32'),
+ ('f', '3.2E2', '320'),
+ ('f', '3.20E2', '320'),
+ ('f', '3.200E2', '320.0'),
+ ('f', '3.2E-6', '0.0000032'),
+ ('.6f', '0E-15', '0.000000'), # all zeros treated equally
+ ('.6f', '0E1', '0.000000'),
+ ('.6f', '0', '0.000000'),
+ ('.0f', '0', '0'), # no decimal point
+ ('.0f', '0e-2', '0'),
+ ('.0f', '3.14159265', '3'),
+ ('.1f', '3.14159265', '3.1'),
+ ('.4f', '3.14159265', '3.1416'),
+ ('.6f', '3.14159265', '3.141593'),
+ ('.7f', '3.14159265', '3.1415926'), # round-half-even!
+ ('.8f', '3.14159265', '3.14159265'),
+ ('.9f', '3.14159265', '3.141592650'),
+
+ ('g', '0', '0'),
+ ('g', '0.0', '0.0'),
+ ('g', '0E1', '0e+1'),
+ ('G', '0E1', '0E+1'),
+ ('g', '0E-5', '0.00000'),
+ ('g', '0E-6', '0.000000'),
+ ('g', '0E-7', '0e-7'),
+ ('g', '-0E2', '-0e+2'),
+ ('.0g', '3.14159265', '3'), # 0 sig fig -> 1 sig fig
+ ('.1g', '3.14159265', '3'),
+ ('.2g', '3.14159265', '3.1'),
+ ('.5g', '3.14159265', '3.1416'),
+ ('.7g', '3.14159265', '3.141593'),
+ ('.8g', '3.14159265', '3.1415926'), # round-half-even!
+ ('.9g', '3.14159265', '3.14159265'),
+ ('.10g', '3.14159265', '3.14159265'), # don't pad
+
+ ('%', '0E1', '0%'),
+ ('%', '0E0', '0%'),
+ ('%', '0E-1', '0%'),
+ ('%', '0E-2', '0%'),
+ ('%', '0E-3', '0.0%'),
+ ('%', '0E-4', '0.00%'),
+
+ ('.3%', '0', '0.000%'), # all zeros treated equally
+ ('.3%', '0E10', '0.000%'),
+ ('.3%', '0E-10', '0.000%'),
+ ('.3%', '2.34', '234.000%'),
+ ('.3%', '1.234567', '123.457%'),
+ ('.0%', '1.23', '123%'),
+
+ ('e', 'NaN', 'NaN'),
+ ('f', '-NaN123', '-NaN123'),
+ ('+g', 'NaN456', '+NaN456'),
+ ('.3e', 'Inf', 'Infinity'),
+ ('.16f', '-Inf', '-Infinity'),
+ ('.0g', '-sNaN', '-sNaN'),
+
+ ('', '1.00', '1.00'),
+ ]
+ for fmt, d, result in test_values:
+ self.assertEqual(format(Decimal(d), fmt), result)
+
class DecimalArithmeticOperatorsTest(unittest.TestCase):
'''Unit tests for all arithmetic operators, binary and unary.'''
@@ -1351,6 +1443,7 @@
DecimalExplicitConstructionTest,
DecimalImplicitConstructionTest,
DecimalArithmeticOperatorsTest,
+ DecimalFormatTest,
DecimalUseOfContextTest,
DecimalUsabilityTest,
DecimalPythonAPItests,
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index ba726b5..b1c1033 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -54,7 +54,14 @@
self.assertEqual(list(chain('abc')), list('abc'))
self.assertEqual(list(chain('')), [])
self.assertEqual(take(4, chain('abc', 'def')), list('abcd'))
- self.assertRaises(TypeError, chain, 2, 3)
+ self.assertRaises(TypeError, list,chain(2, 3))
+
+ def test_chain_from_iterable(self):
+ self.assertEqual(list(chain.from_iterable(['abc', 'def'])), list('abcdef'))
+ self.assertEqual(list(chain.from_iterable(['abc'])), list('abc'))
+ self.assertEqual(list(chain.from_iterable([''])), [])
+ self.assertEqual(take(4, chain.from_iterable(['abc', 'def'])), list('abcd'))
+ self.assertRaises(TypeError, list, chain.from_iterable([2, 3]))
def test_combinations(self):
self.assertRaises(TypeError, combinations, 'abc') # missing r argument
@@ -298,6 +305,9 @@
([range(2), range(3), range(0)], []), # last iterable with zero length
]:
self.assertEqual(list(product(*args)), result)
+ for r in range(4):
+ self.assertEqual(list(product(*(args*r))),
+ list(product(*args, **dict(repeat=r))))
self.assertEqual(len(list(product(*[range(7)]*6))), 7**6)
self.assertRaises(TypeError, product, range(6), None)
argtypes = ['', 'abc', '', range(0), range(4), dict(a=1, b=2, c=3),
@@ -684,7 +694,7 @@
for g in (G, I, Ig, S, L, R):
self.assertEqual(list(chain(g(s))), list(g(s)))
self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s)))
- self.assertRaises(TypeError, chain, X(s))
+ self.assertRaises(TypeError, list, chain(X(s)))
self.assertRaises(TypeError, chain, N(s))
self.assertRaises(ZeroDivisionError, list, chain(E(s)))