This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

Provides support for quantities and units, which strictly disallow invalid operations between incompatible quantities. For example, we cannot add 2 metres to 5 seconds, because this doesn’t make sense.

Why?

From Wikipedia:

The Mars Climate Orbiter was intended to enter orbit at an altitude of 140-150 km (460,000-500,000 ft.) above Mars. However, a navigation error caused the spacecraft to reach as low as 57 km (190,000 ft.). The spacecraft was destroyed by atmospheric stresses and friction at this low altitude. The navigation error arose because a NASA subcontractor (Lockheed Martin) used Imperial units (pound-seconds) instead of the metric system.

Installation

This module is distributed via PyPI. So, you can do:

pip install units

or:

easy_install units

or, you can download a bundle yourself at http://pypi.python.org/pypi/units/

If you want the latest:

pip install -e hg+https://bitbucket.org/adonohue/units/#egg=units

How to Use

Make Quantities

Units are objects that you use to make quantities:

>>> from units import unit
>>> metre = unit('m')
>>> print(metre(7) + metre(11))
18.00 m

You can mix and match these quantities in some ways:

>>> from units import unit
>>> metre = unit('m')
>>> second = unit('s')
>>> print(metre(10) / second(2))
5.00 m / s
>>> print(metre(10) ** 3)
1000.00 m * m * m

But if you make a mistake, you get a safety net:

>>> from units import unit
>>> try:
...   unit('m')(5) + unit('s')(5)
... except units.exception.IncompatibleUnitsError:
...   print('Got an error!')
Got an error!

Make Your Own Units

Before you start making your own units, you should check out the units that you get for free:

>>> import units.predefined
>>> units.predefined.define_units()

It includes all the official SI units, some units for measuring time such as days and weeks, units for volumes like cups, gallons and litres, imperial units and more.

You’ve already seen how to make your own simple units. You call the unit function and give it a string:

>>> from units import unit
>>> blog = unit('blog')
>>> print(blog(3))
3.00 blog

These units are automatically incompatible with other units.

You can combine units with multiplication and division to make new units:

>>> from units import unit
>>> blogs_per_network = unit('blog') / unit('network')
>>> print(blogs_per_network(2.34))
2.34 blog / network

There’s a built-in shortcut for making new units that are scalar multiples of other units:

>>> from units import unit, scaled_unit
>>> sickle = scaled_unit('sickle', 'knut', 29)
>>> galleon = scaled_unit('galleon', 'sickle', 17)
>>> knut = unit('knut')
>>> galleon(3.0) + sickle(1.0) - knut(25.0) == knut(1483)
True

There’s also a shortcut for giving names to slightly more complicated units:

>>> from units import unit, named_unit
>>> from units.predefined import define_units
>>> define_units()
>>> twp = named_unit('tweetpack', ['tweet', 'meme'], ['day'], 5)
>>> # A tweetpack is 5 tweetmemes per day
>>> print(twp(2))
2.00 tweetpack
>>> tweet, meme, day = [unit(x) for x in ['tweet', 'meme', 'day']]
>>> print(twp(5) - (tweet(5) * meme(4) / day(2)))
3.00 tweetpack

If two units are compatible, you can convert between them easily:

>>> from units import unit
>>> from units.predefined import define_units
>>> define_units()
>>> furlongs_per_fortnight = unit('fur') / unit('fortnight')
>>> kph = unit('km') / unit('h')
>>> print(furlongs_per_fortnight(kph(100)))
167024.58 fur / fortnight

You can also use lower-level constructors to make your own units and quantities. The ways shown above are easier, though.

Warnings

This module doesn’t solve problems with numerical accuracy or floating point conversions, and Python 2 vs. 3 issues abound:

from units import unit
unit('m')(5) / unit('m')(7)
# 0 in Python 2.x, 0.7142857142857143 in Python 3.

More dangerously, certain internal operations have implicit arithmetic that can surprise you:

from units import unit, scaled_unit
sickle = scaled_unit('sickle', 'knut', 29)
galleon = scaled_unit('galleon', 'sickle', 17)
knut = unit('knut')
galleon(3) + sickle(1) - knut(25) == galleon(3)
# True in Python 2.x, False in Python 3

Using Modified Python

In units-enhanced Python, you can do:

print(2cm / 0.5 s)
-> 4.0 cm / s

Units-enhanced Python is a version of PyPy with built-in support for units. You can find it in the unitPython directory. Essentially, apply the supplied patches to r66797 of PyPy. If you’re on a suitable UNIX, the included unitPython/unitPython.sh does this for you.

@requires: U{Python<http://python.org/>} >= 2.5 @since: 2009-Aug-10 @status: under development

Release History

Release History

0.06

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.05

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.04

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.03

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.02

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.01

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.00

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
units-0.06.tar.gz (19.0 kB) Copy SHA256 Checksum SHA256 Source Feb 25, 2013

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting