Sharedsupport / 2015-01-06 pint.sagewsOpen in CoCalc
Examples for support purposes...

Pint is now in SageMathCloud!

from pint import UnitRegistry
ureg = UnitRegistry()
distance = 24.0 * ureg.meter
print(distance)
24.0000000000000 meter
time = 8.0 * ureg.second
print(time)
8.00000000000000 second
print(repr(time))
<Quantity(8.00000000000000, 'second')>
print(distance.magnitude)
24.0000000000000
print(distance.units)
meter
print(distance.dimensionality)
[length]
speed = distance / time
print(speed)
3.00000000000000 meter / second
speed.to(ureg.inch / ureg.minute )
<Quantity(7086.61417322834, 'inch / minute')>
print(speed)
3.00000000000000 meter / second
speed.ito(ureg.inch / ureg.minute )
print(speed)
7086.61417322834 inch / minute
# should fail!
speed.to(ureg.joule)
Error in lines 2-2 Traceback (most recent call last): File "/projects/4a5f0542-5873-4eed-a85c-a18c706e8bcd/.sagemathcloud/sage_server.py", line 865, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/usr/local/sage/sage-6.4/local/lib/python2.7/site-packages/pint/quantity.py", line 243, in to magnitude = self._convert_magnitude_not_inplace(other, *contexts, **ctx_kwargs) File "/usr/local/sage/sage-6.4/local/lib/python2.7/site-packages/pint/quantity.py", line 198, in _convert_magnitude_not_inplace return self._REGISTRY.convert(self._magnitude, self._units, other) File "/usr/local/sage/sage-6.4/local/lib/python2.7/site-packages/pint/unit.py", line 1041, in convert raise DimensionalityError(src, dst, src_dim, dst_dim) DimensionalityError: Cannot convert from 'inch / minute' ([length] / [time]) to 'joule' ([length] ** 2 * [mass] / [time] ** 2)