ounce

A simple Python package to manipulate units. Fast. Not fancy. No magic. No dependencies. A faster and less fancy counterpart to Pint.

ounce.units_class(units)[source]

Returns ‘length’, ‘weight’, ‘angle’, ‘time’, or ‘time_rate’.

>>> units_class('cm')
'length'
ounce.units_in_class(uclass)[source]

Return a list of all units in uclass, where uclass is e.g. length’, ‘weight’, ‘angle, ‘time’, ‘time_rate’.

ounce.default_units(unit_system='metric', exceptions={})[source]

Get the default unit for a given unit system. unit_class is ‘length’, ‘weight’, ‘angle’, or ‘time’. unit_system is either ‘metric’ or ‘united_states’.

ounce.raw(units)[source]

Returns a raw units conversion factor. Try not to use this. Use factor() or convert() instead.

ounce.factor(from_units, to_units, units_class=None)[source]

Return a conversion factor:

>>> value_in_cm = 25
>>> value_in_cm * factor('cm', 'mm')
250

class: If specified, the class of the units must match the class provided.

ounce.convert(value, from_units, to_units, units_class=None)[source]

Convert a number from one unit to another.

class: If specified, the class of the units must match the class provided.

Returns a tuple with the converted value and the units.

>>> value_cm = 25
>>> value, units = convert(value_cm, 'cm', 'mm')
>>> value
250
>>> units
'mm'
ounce.convert_list(a_list, from_units, to_units)[source]

Convenience helper to convert a list of numbers from one unit to another.

Unlike convert(), does not return a tuple.

>>> convert_list([10, 20, 30], 'cm', 'mm')
[100, 200, 300]
ounce.convert_to_default(value, from_units, defaults)[source]

Convert a number from the given unit to a default for the given unit system.

Returns a tuple with the converted value and the units.

>>> value_cm = 100
>>> convert_to_default(value_cm, 'cm', {'length': 'in', 'weight': 'lb', 'angle': 'deg', 'time': 'yr'})
(39.3701, 'in')
ounce.convert_to_system_default(value, from_units, to_unit_system='metric')[source]

Convert a number from the given unit to a default for the given unit system.

Returns a tuple with the converted value and the units.

>>> value_cm = 100
>>> convert_to_system_default(value_cm, 'cm', 'united_states')
(39.3701, 'in')
ounce.prettify(value, units, precision=None)[source]

Take a value, and units, and return rounded values in the default metric and United States units.

The default precision values vary per unit. Specifying an integer for precision will override those defaults.

>>> prettify(182.13992, 'cm'),
(182.0, 'cm', 71.75, 'in')
>>> prettify(182.13992, 'cm', precision=1)
(182.1, 'cm', 71.7, 'in')
>>> prettify(182.13992, 'cm', precision=2)
(182.14, 'cm', 71.71, 'in')
ounce.all_units

Return a list of all supported units.

ounce.all_units_classes

Return a list of all supported units classes, e.g. ‘length’, ‘weight’, ‘angle, ‘time’, ‘time_rate’.

ounce.lengths

List of all supported length units.

ounce.weights

List of all supported weight units.

ounce.angles

List of all supported angle units.

ounce.times

List of all supported time units.

ounce.time_rates

List of all supported time-rate units.

Versioning

This library adheres to Semantic Versioning.