Improve Measurement formatting.

close #76
This commit is contained in:
Hernan Grecco 2014-03-02 20:48:04 -03:00
parent 19c164afe4
commit cb39fec995
3 changed files with 9 additions and 7 deletions

View File

@ -45,7 +45,7 @@ Measurements support the same formatting codes as Quantity. For example, to pret
.. doctest::
>>> print('{:.02f!p}'.format(book_length))
>>> print('{:.02fP}'.format(book_length))
(20.00 ± 2.00) centimeter

View File

@ -64,7 +64,6 @@ class _Measurement(object):
return '{0}'.format(self)
def __format__(self, spec):
if 'L' in spec:
newpm = pm = r' \pm '
pars = _FORMATS['L']['parentheses_fmt']
@ -88,7 +87,6 @@ class _Measurement(object):
newspec = spec.replace('H', '')
pars = _FORMATS['H']['parentheses_fmt']
mag = format(self.magnitude, newspec).replace(pm, sp + newpm + sp)
return pars.format(mag) + ' ' + format(self.units, spec)

View File

@ -37,11 +37,15 @@ class TestMeasurement(TestCase):
self.assertEqual(repr(m), '<Measurement(4.00, 0.10, second ** 2)>')
#self.assertEqual('{:!s}'.format(m), '(4.00 +/- 0.10) second ** 2')
#self.assertEqual('{:!r}'.format(m), '<Measurement(4.0-, 0.10, second ** 2)>')
self.assertEqual('{0:!P}'.format(m), '(4.00 ± 0.10) second²')
self.assertEqual('{0:!L}'.format(m), r'\left(4.00 \pm 0.10\right) second^{2}')
self.assertEqual('{0:!H}'.format(m), '(4.00 &plusmn; 0.10) second<sup>2</sup>')
self.assertEqual('{0:!C}'.format(m), '(4.00+/-0.10) second**2')
self.assertEqual('{0:P}'.format(m), '(4.00 ± 0.10) second²')
self.assertEqual('{0:L}'.format(m), r'\left(4.00 \pm 0.10\right) second^{2}')
self.assertEqual('{0:H}'.format(m), '(4.00 &plusmn; 0.10) second<sup>2</sup>')
self.assertEqual('{0:C}'.format(m), '(4.00+/-0.10) second**2')
self.assertEqual('{0:.1f}'.format(m), '(4.0 +/- 0.1) second ** 2')
self.assertEqual('{0:.1fP}'.format(m), '(4.0 ± 0.1) second²')
self.assertEqual('{0:.1fL}'.format(m), r'\left(4.0 \pm 0.1\right) second^{2}')
self.assertEqual('{0:.1fH}'.format(m), '(4.0 &plusmn; 0.1) second<sup>2</sup>')
self.assertEqual('{0:.1fC}'.format(m), '(4.0+/-0.1) second**2')
def test_raise_build(self):
v, u = self.Q_(1.0, 's'), self.Q_(.1, 's')