Posts

Showing posts from January, 2013

How many Pints do you want? Units in Python

Image
I tried many of the Python Units packages available when I started adding physical quantities (magnitude and units) support to Lantz. There are really great packages out there providing NumPy support and a broad definition list including units and constants.

However these packages define the units within the Python code. This results in an unnecessary large and difficult to maintain package. Additionally, translation to other languages and changing to an incompatible system (eg. from SI to Gaussian) is impossible without changing the codebase.

Pint is a units package for Python that keeps the definitions in a separate text file.  The text file is really easy to read and modify, allowing you to update the unit definitions without changing the code. For each unit, you just type the relation with other units. Aliases can be added after the second equal. For example, here are the definitions of hour an minute:
hour = 60 * minute = h = hr
minute = 60 * second = min It is very natural. Prefix…