Class UnitManager
source code
object --+
|
UnitManager
A UnitManager hold a collection of Family objects and
allows to convert between their units.
>>> um=UnitManager()
>>> um.add(SIFamily(base='l', name='liter'))
>>>
>>> f=Family(name='f', base='gallon')
>>> f.add('barrel', 36, 'gallon')
>>> f.add('kilderkin', 0.5, 'barrel')
>>> f.add('firkin', 0.5, 'kilderkin')
>>> um.add(f, other='liter', factor=4.54609)
>>>
>>> um.add(Family(base='°C'))
>>> um.add(Family(base='°F'), other='°C', offset=(-32/1.8), factor=5.0/9)
>>> um.convert_to_unit(1e4, 'ml', 'l')
(Decimal("10.0000"), 'l')
>>> um.convert_to_unit(1, 'firkin', 'gallon')
(Decimal("9.00"), 'gallon')
>>> um.convert_to_family(1, 'firkin', 'liter')
(Decimal("40.9148100"), 'l')
>>> um.convert_to_family(1, 'l', 'f')
(Decimal("0.219969248299"), 'gallon')
>>> um.convert_to_family(1, 'barrel', 'f')
(Decimal("1.00"), 'barrel')
>>> um.convert_to_unit(32, '°F', '°C')
(Decimal("-8E-12"), '\xc2\xb0C')
>>> um.convert_to_unit(100, '°C', '°F')
(Decimal("212.0"), '\xc2\xb0F')
>>> um.convert_to_unit(1, '°C', 'ml')
(None, None)
|
|
|
|
|
add(self,
family,
other=None,
factor=1,
offset=0,
groups=None)
Add another Family object. |
source code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Inherited from object:
__delattr__,
__getattribute__,
__hash__,
__new__,
__reduce__,
__reduce_ex__,
__repr__,
__setattr__,
__str__
|
|
Inherited from object:
__class__
|
Constructor
- Overrides:
object.__init__
|
add(self,
family,
other=None,
factor=1,
offset=0,
groups=None)
| source code
|
Add another Family
object.
If other is not None a conversion path
family = other * factor + offset
and its reverse path are added.
- Raises:
KeyError - if other is a string and there's no known unit with this
name
TypeError - if factor or offset has a wrong type
AttributeError - if family has a wrong type
|
Finds the shortest conversion path between start and
end.
- Parameters:
start (name of a Family that has been added by add) - family to start with
end (name of a Family that has been added by add) - family to end with
- Returns:
- list of Items to convert start to end or
None if no such path can be found
|
convert_to_family(self,
amount,
unit,
family)
| source code
|
Convert amount of unit to the unit in family that
fits best.
See Family.autoconvert.
- Returns:
- Tupel (new amount, new unit) or (None, None) if conversion is
not possible
- Raises:
KeyError - if unit is not known
KeyError - if family is a string an no family with that name is
known
TypeError - if amount has a wrong type
|
|
Convert amount of unit to unit dest.
See Family.convert.
- Returns:
- Tupel (new amount, new unit) or (None, None) if conversion is
not possible.
- Raises:
KeyError - if unit or dest is not known
TypeError - if amount has a wrong type
|
convert_to_group(self,
amount,
unit,
group)
| source code
|
Convert amount of unit to the unit in group that
fits best.
See convert_to_family.
- Returns:
- Tupel (new amount, new unit) or (None, None) if conversion is
not possible
- Raises:
KeyError - if unit is not known
KeyError - if group is not known
TypeError - if amount has a wrong type
|