Check out the all new PyPI! (More information here)

skip to navigation
skip to content

Unum 4.1.0

Units in Python

Latest Version: 4.1.4

Unum stands for ‘unit-numbers’. It is a Python module that allows you to define and manipulate quantities with units attached such as 60 seconds, 500 watts, 42 miles-per-hour, 100 kg per square meter, 14400 bits per second, 30 dollars, and so on.

Features include: - Exceptions for incorrect use of units. - Automatic and manual conversion between compatible units. - Easily extended to arbitrary units. - Integration with any type supporting arithmetic operations, including Numpy arrays and standard library types like complex and fractions.Fraction. - Customizable output formatting.

Example

For a simple example, let’s can calculate Usain Bolt’s average speed during his record-breaking performance in the 2008 Summer Olympics:

>>> from unum.units import * # Load a number of common units.
>>> distance = 100*m
>>> time = 9.683*s
>>> speed = distance / time
>>> speed
10.3273778788 [m/s]
>>> speed.asUnit(mile/h)
23.1017437978 [mile/h]

If we do something dimensionally incorrect, we get an exception rather than silently computing a correct result. Let’s try calculating his kinetic energy using an erroneous formula:

>>> KE = 86*kg * speed / 2 # Should be speed squared!
>>> KE.asUnit(J)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "unum\__init__.py", line 171, in asUnit
    s, o = self.matchUnits(other)
  File "unum\__init__.py", line 258, in matchUnits
    raise IncompatibleUnitsError(self, other)
unum.IncompatibleUnitsError: [kg.m/s] can't be used with [J]

The exception pinpoints the problem, allowing us to examine the units and fix the formula:

>>> KE = 86*kg * speed**2 / 2
>>> KE.asUnit(J)
4586.15355558 [J]
 
File Type Py Version Uploaded on Size
Unum-4.1.0-py2.6.egg (md5) Python Egg 2.6 2009-11-05 23KB
Unum-4.1.0.tar.gz (md5) Source 2009-11-05 10KB