hgrecco / pint

Operate and manipulate physical quantities in Python
http://pint.readthedocs.org/
Other
2.41k stars 472 forks source link

measurements example does not work #76

Closed mikofski closed 10 years ago

mikofski commented 10 years ago

See Using Measurments

$ python
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> book_length = (20. * ureg.centimeter).plus_minus(2.)
>>> book_length
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\site-packages\pint\measurement.py", line 94, in __repr__
    return "<Measurement({:!r}, {:!r})>".format(self._value, self._error)
  File "c:\Python27\lib\site-packages\pint\quantity.py", line 152, in __format__
    + ' ' + format(units, spec)
ValueError: Invalid conversion specification
mikofski commented 10 years ago

The issue here is in measurements.py on line 94 where __repr__ is defined, I think the formatter should be {!r} instead of {:!r} See Python string docs. IE: without the colon. EG: here is a patch.

--- /c/Python27/lib/site-packages/pint/measurement.py.orig  Mon Dec  9 13:17:41 2013
+++ /c/Python27/lib/site-packages/pint/measurement.py   Mon Dec  9 13:18:42 2013
@@ -91,7 +91,7 @@
         return '{}'.format(self)

     def __repr__(self):
-        return "<Measurement({:!r}, {:!r})>".format(self._value, self._error)
+        return "<Measurement({!r}, {!r})>".format(self._value, self._error)

     def __format__(self, spec):
         if '!' in spec:

Now this yields:

>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> acc = (5.0 * ureg['m/s/s']).plus_minus(0.25)
>>> tim = (37.0 * ureg['s']).plus_minus(0.16)
>>> dis = acc * tim * tim / 2.0
>>> dis
<Measurement(<Quantity(3422.0, 'meter')>, <Quantity(172.0, 'meter')>)>

Is this the output you intended?

FYI: see issue #77 for why I have to use tim * tim instead of pow()

hgrecco commented 10 years ago

Thanks for your interest in this. The plan is to provide a better Measurement class via the uncertainties package. Checkout the _uncertainties branch. The bug in #77 and #76 is due to the fact that this was lagging behind. Would you like to submit a pull request?

mikofski commented 10 years ago

Thanks for your response. I would love to contribute if I find time. Looking forward to the Uncertainties & Pint integration. Hoping for full NumPy/SciPy support for scientists and engineers. Thanks for your work!