summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2010-11-25 16:08:06 +0000
committerEric Smith <eric@trueblade.com>2010-11-25 16:08:06 +0000
commit1bbe14e9ead6274abb61313705e5f90fe15aa5c5 (patch)
treeee5906e7bcac8bac30bb6d905bcccf018bb90aed /Lib
parent2eb0fc9e20a8612ca55b1e4a23b32deba270f382 (diff)
downloadcpython-1bbe14e9ead6274abb61313705e5f90fe15aa5c5.tar.gz
Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/decimal.py5
-rw-r--r--Lib/test/test_complex.py24
-rw-r--r--Lib/test/test_decimal.py12
-rw-r--r--Lib/test/test_float.py7
-rw-r--r--Lib/test/test_types.py30
5 files changed, 61 insertions, 17 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py
index 5a9f840771..f379ee57ce 100644
--- a/Lib/decimal.py
+++ b/Lib/decimal.py
@@ -5991,7 +5991,7 @@ _exact_half = re.compile('50*$').match
#
# A format specifier for Decimal looks like:
#
-# [[fill]align][sign][0][minimumwidth][,][.precision][type]
+# [[fill]align][sign][#][0][minimumwidth][,][.precision][type]
_parse_format_specifier_regex = re.compile(r"""\A
(?:
@@ -5999,6 +5999,7 @@ _parse_format_specifier_regex = re.compile(r"""\A
(?P<align>[<>=^])
)?
(?P<sign>[-+ ])?
+(?P<alt>\#)?
(?P<zeropad>0)?
(?P<minimumwidth>(?!0)\d+)?
(?P<thousands_sep>,)?
@@ -6214,7 +6215,7 @@ def _format_number(is_negative, intpart, fracpart, exp, spec):
sign = _format_sign(is_negative, spec)
- if fracpart:
+ if fracpart or spec['alt']:
fracpart = spec['decimal_point'] + fracpart
if exp != 0 or spec['type'] in 'eE':
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
index 2810b36718..cc21aa7a62 100644
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -555,8 +555,28 @@ class ComplexTest(unittest.TestCase):
self.assertEqual(format(1.5e21+3j, '^40,.2f'), ' 1,500,000,000,000,000,000,000.00+3.00j ')
self.assertEqual(format(1.5e21+3000j, ',.2f'), '1,500,000,000,000,000,000,000.00+3,000.00j')
- # alternate is invalid
- self.assertRaises(ValueError, (1.5+0.5j).__format__, '#f')
+ # Issue 7094: Alternate formatting (specified by #)
+ self.assertEqual(format(1+1j, '.0e'), '1e+00+1e+00j')
+ self.assertEqual(format(1+1j, '#.0e'), '1.e+00+1.e+00j')
+ self.assertEqual(format(1+1j, '.0f'), '1+1j')
+ self.assertEqual(format(1+1j, '#.0f'), '1.+1.j')
+ self.assertEqual(format(1.1+1.1j, 'g'), '1.1+1.1j')
+ self.assertEqual(format(1.1+1.1j, '#g'), '1.10000+1.10000j')
+
+ # Alternate doesn't make a difference for these, they format the same with or without it
+ self.assertEqual(format(1+1j, '.1e'), '1.0e+00+1.0e+00j')
+ self.assertEqual(format(1+1j, '#.1e'), '1.0e+00+1.0e+00j')
+ self.assertEqual(format(1+1j, '.1f'), '1.0+1.0j')
+ self.assertEqual(format(1+1j, '#.1f'), '1.0+1.0j')
+
+ # Misc. other alternate tests
+ self.assertEqual(format((-1.5+0.5j), '#f'), '-1.500000+0.500000j')
+ self.assertEqual(format((-1.5+0.5j), '#.0f'), '-2.+0.j')
+ self.assertEqual(format((-1.5+0.5j), '#e'), '-1.500000e+00+5.000000e-01j')
+ self.assertEqual(format((-1.5+0.5j), '#.0e'), '-2.e+00+5.e-01j')
+ self.assertEqual(format((-1.5+0.5j), '#g'), '-1.50000+0.500000j')
+ self.assertEqual(format((-1.5+0.5j), '.0g'), '-2+0.5j')
+ self.assertEqual(format((-1.5+0.5j), '#.0g'), '-2.+0.5j')
# zero padding is invalid
self.assertRaises(ValueError, (1.5+0.5j).__format__, '010f')
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 611ef55007..3036170aeb 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -818,6 +818,18 @@ class DecimalFormatTest(unittest.TestCase):
# issue 6850
('a=-7.0', '0.12345', 'aaaa0.1'),
+
+ # Issue 7094: Alternate formatting (specified by #)
+ ('.0e', '1.0', '1e+0'),
+ ('#.0e', '1.0', '1.e+0'),
+ ('.0f', '1.0', '1'),
+ ('#.0f', '1.0', '1.'),
+ ('g', '1.1', '1.1'),
+ ('#g', '1.1', '1.1'),
+ ('.0g', '1', '1'),
+ ('#.0g', '1', '1.'),
+ ('.0%', '1.0', '100%'),
+ ('#.0%', '1.0', '100.%'),
]
for fmt, d, result in test_values:
self.assertEqual(format(Decimal(d), fmt), result)
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py
index e0479db274..0072133aae 100644
--- a/Lib/test/test_float.py
+++ b/Lib/test/test_float.py
@@ -706,11 +706,8 @@ class RoundTestCase(unittest.TestCase):
def test(fmt, value, expected):
# Test with both % and format().
self.assertEqual(fmt % value, expected, fmt)
- if not '#' in fmt:
- # Until issue 7094 is implemented, format() for floats doesn't
- # support '#' formatting
- fmt = fmt[1:] # strip off the %
- self.assertEqual(format(value, fmt), expected, fmt)
+ fmt = fmt[1:] # strip off the %
+ self.assertEqual(format(value, fmt), expected, fmt)
for fmt in ['%e', '%f', '%g', '%.0e', '%.6f', '%.20g',
'%#e', '%#f', '%#g', '%#.20e', '%#.15f', '%#.3g']:
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index d16dbba173..8a98a03567 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -396,13 +396,9 @@ class TypesTests(unittest.TestCase):
self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt)))
def test_float__format__(self):
- # these should be rewritten to use both format(x, spec) and
- # x.__format__(spec)
-
def test(f, format_spec, result):
- assert type(f) == float
- assert type(format_spec) == str
self.assertEqual(f.__format__(format_spec), result)
+ self.assertEqual(format(f, format_spec), result)
test(0.0, 'f', '0.000000')
@@ -516,9 +512,27 @@ class TypesTests(unittest.TestCase):
self.assertRaises(ValueError, format, 1e-100, format_spec)
self.assertRaises(ValueError, format, -1e-100, format_spec)
- # Alternate formatting is not supported
- self.assertRaises(ValueError, format, 0.0, '#')
- self.assertRaises(ValueError, format, 0.0, '#20f')
+ # Alternate float formatting
+ test(1.0, '.0e', '1e+00')
+ test(1.0, '#.0e', '1.e+00')
+ test(1.0, '.0f', '1')
+ test(1.0, '#.0f', '1.')
+ test(1.1, 'g', '1.1')
+ test(1.1, '#g', '1.10000')
+ test(1.0, '.0%', '100%')
+ test(1.0, '#.0%', '100.%')
+
+ # Issue 7094: Alternate formatting (specified by #)
+ test(1.0, '0e', '1.000000e+00')
+ test(1.0, '#0e', '1.000000e+00')
+ test(1.0, '0f', '1.000000' )
+ test(1.0, '#0f', '1.000000')
+ test(1.0, '.1e', '1.0e+00')
+ test(1.0, '#.1e', '1.0e+00')
+ test(1.0, '.1f', '1.0')
+ test(1.0, '#.1f', '1.0')
+ test(1.0, '.1%', '100.0%')
+ test(1.0, '#.1%', '100.0%')
# Issue 6902
test(12345.6, "0<20", '12345.60000000000000')