Posts

Showing posts from September, 2013

Make your functions units-aware with Pint 0.3

Image
Pint is Python package to define, operate and manipulate physical quantities: the product of a numerical value and a unit of measurement. It allows arithmetic operations between them and conversions from and to different units.

>>> from pint import UnitRegistry >>> ureg = UnitRegistry() >>> q1 = 24. * ureg.meter >>> q2 = 10. * ureg.centimeters >>> q1 + q2 <Quantity(24.1, 'meter')> It provides a comprehensive and extensive list of physical units, prefixes and constants defined in a standalone text file. The registry can parse prefixed and pluralized forms of units resulting in a much shorter and maintainable unit definition list.

It also provides great NumPy integration, with implicit unit conversion and an emphasis on correctness.
>>> import numpy as np >>> np.cos([0, 45] * ureg.degrees) <Quantity([ 1. 0.70710678], 'dimensionless')> >>> np.cos([0, 45] * ureg.meter) Tracebac…