Skip to main content

Units in Python

Project description

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]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Unum-4.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

Unum-4.1.0-py2.6.egg (24.5 kB view details)

Uploaded Source

File details

Details for the file Unum-4.1.0.tar.gz.

File metadata

  • Download URL: Unum-4.1.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Unum-4.1.0.tar.gz
Algorithm Hash digest
SHA256 ca7982b7f959bc4c3c51dccac41563c48a0e58019707c47eb7e4360c370d550a
MD5 6089ff2ffab9fc5ddf7e0953ca58b312
BLAKE2b-256 04cbf7f6b176ebdcfc82e7ffcf0565797874400a6c5504fe56e7482ff784fddd

See more details on using hashes here.

File details

Details for the file Unum-4.1.0-py2.6.egg.

File metadata

  • Download URL: Unum-4.1.0-py2.6.egg
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Unum-4.1.0-py2.6.egg
Algorithm Hash digest
SHA256 260e6357387d2123bac35d3ea976f027bc8def01feffefff7045f9e578ed50b6
MD5 dc737ab3f1c0b4b372bb3980e1eddc4d
BLAKE2b-256 037a901d28d392c3f24b472c378e911151381c1ac788f9dec312edd217c8a1a0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page