Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install keas.unit

How to install keas.unit

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install keas.unit
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.0.0 Available View build log
Windows (64-bit)
1.0.0 Available View build log
Mac OS X (10.5+)
1.0.0 Available View build log
Linux (32-bit)
1.0.0 Available View build log
Linux (64-bit)
1.0.0 Available View build log
 
License
ZPL 2.1
Imports
Lastest release
version 1.0.0 on Jan 5th, 2011

Unit Converter

The unit converter is a simple utility to convert units. While it may seem to be a trivial task initially, there are many different conventions and combinations. Instead of inventing yet another unit conversion utility, this package reuses the units shell command, which has the best implementation that I have found out there.

>>> from keas.unit import unit

The first step is to instantiate the unit converter object:

>>> converter = unit.UnitConverter()

By default the converter uses the following path for the units command:

>>> converter.executable
'/usr/bin/units'

So let's make a simple unit conversion:

>>> converter.convert('atm', 'kPa')
Decimal("101.325")

There are several options that can be set on the conversion object. The first one is the output format, which allows you to specify the precision on the output. Of course, we then convert this output to a decimal:

>>> converter.format
>>> converter.format = '%.15g' # 15 decimal places
>>> converter.convert('atm', 'kPa')
Decimal("101.325")

Next you can specify additional unit conversion data filenames, so that custom conversions can be loaded:

>>> import os
>>> datafile = os.path.join(
...     os.path.dirname(unit.__file__), 'test-conversions.dat')
>>> converter.filenames
>>> converter.filenames = ('', datafile)
>>> converter.convert('keas', 'l')
Decimal("0.54321")
>>> converter.filenames = None

An empty name means that the default data file should be loaded as well.

The final option allows you to set whether the '-' character should be treated as a multiplication operator, since it is used as such in some notations.

>>> converter.minusAsProduct
False
>>> converter.minusAsProduct = True
>>> converter.convert('m-m-m', 'l')
Decimal("1000")

Finally, let's have a look at some error scenarios.

  1. There is no known conversion from one unit to another:
>>> converter.convert('kg', 'm')
Traceback (most recent call last):
...
UnitConversionError: ('/usr/bin/units -t -o %.15g -p "kg" "m"',
'conformability error')
  1. One of the units is unknown:
>>> converter.convert('kg', 'foo')
Traceback (most recent call last):
...
UnitConversionError: ('/usr/bin/units -t -o %.15g -p "kg" "foo"',
"Unknown unit 'foo'")
  1. One of the units contains an invalid expression:
>>> converter.convert('kg', 'm *')
Traceback (most recent call last):
...
UnitConversionError: ('/usr/bin/units -t -o %.15g -p "kg" "m *"',
"Error in 'm *': Parse error")

CHANGES

1.0.0 (2009-07-26)
  • Initial release.

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.