Question

Using the 'units module for performing basic conversions between units. However it does not seem to be possible to perform an easy conversion between liters and milliliters...why?

>>> from units import unit

>>> one_liter = unit('L')(1)   
>>> one_liter
Quantity(1, LeafUnit('L', False))

>>> unit('mL')(one_liter)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/heinz/src/units/lib/python2.6/site-packages/units/abstract.py", line 23, in __call__
    raise IncompatibleUnitsError()
units.exception.IncompatibleUnitsError
Was it helpful?

Solution

You need to call define_units() in the predefined sub-module to use this package.

import units
import units.predefined

units.predefined.define_units()
one_liter = unit('L')(1)
unit('mL')(one_liter)

I think this package could have been better designed -- as you've discovered, it's easy to accidentally use user-defined units rather than standard ones.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top