Module firkin :: Class Family
[hide private]
[frames] | no frames]

Class Family

source code

object --+
         |
        Family
Known Subclasses:
SIFamily

A Family is a collection of units which are derived from a common base unit.
>>> f=Family(base='gallon')
>>> f.add('barrel', 36, 'gallon')
>>> f.add('kilderkin', 0.5, 'barrel')
>>> f.add('firkin', 0.5, 'kilderkin')
>>> f.convert(1, 'firkin', 'gallon')
(Decimal("9.00"), 'gallon')
>>> f.autoconvert(3, 'kilderkin')
(Decimal("1.50"), 'barrel')


Instance Methods [hide private]
 
__init__(self, base, name=None)
Constructor
source code
 
add(self, name, factor, other=None)
Add another unit to this family
source code
 
convert(self, amount, unit, dest)
Convert amount of unit to unit dest
source code
 
autoconvert(self, amount, unit)
Convert amount of unit to the unit that fits best.
source code
 
__contains__(self, item)
item in self
source code
 
__getitem__(self, index)
self[index]
source code
 
__setitem__(self, index, value)
self[index]=foo
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, base, name=None)
(Constructor)

source code 
Constructor
Parameters:
  • base (string) - common base unit
  • name (string) - name for this family. That name can be used with UnitManager to refer to this family. If None base is used as name.
Overrides: object.__init__

add(self, name, factor, other=None)

source code 
Add another unit to this family
Parameters:
  • name (string) - The unit's name
  • factor (anything that to_decimal can use) - factor to multiply other to get this unit
  • other (string) - Unit this one is based on. If None the base unit is used
Raises:
  • KeyError - if other isn't known
  • TypeError - if factor has a wrong type

convert(self, amount, unit, dest)

source code 
Convert amount of unit to unit dest
Returns:
a tupel (new amount, new unit)
Raises:
  • KeyError - if unit or other isn't known
  • TypeError - if amount has a wrong type

autoconvert(self, amount, unit)

source code 
Convert amount of unit to the unit that fits best.
Returns:
a tupel (new amount, new unit)
Raises:
  • KeyError - if unit isn't known
  • TypeError - if amount has a wrong type

__contains__(self, item)
(In operator)

source code 

item in self

True if item is a known unit

__getitem__(self, index)
(Indexing operator)

source code 

self[index]

Get the factor you need to mutiply the base unit with to get unit index
Raises:
  • KeyError - if unit index is not known

__setitem__(self, index, value)
(Index assignment operator)

source code 

self[index]=foo

Set value as the factor you need to mutiply the base unit with to get unit index