UnitManager¶
-
class
bookkeep.
UnitManager
(clients, *args, **kwargs)[source]¶ Create a UnitManager object for handling units of measure of a list of dictionaries (clients). When an item in UnitManger changes, all dictionaries in clients with the same key change values accordingly.
Parameters
clients: [list] All dictionaries managed by UnitManager object.
*args: Key/units pairs.
**kwargs: Key/units pairs.
Class Attribute
Quantity: pint Quantity class for compatibility.Examples
Convert units of all clients using a UnitManager.
Create client dictionaries:
>>> car = {'weight': 4000, 'velocity': 50} >>> plane = {'weight': 175000, 'velocity': 600}
Create a UnitManager object:
>>> um = UnitManager([car, plane], weight='lbs', velocity='mph') >>> um UnitManager: {'weight': 'lbs', 'velocity': 'mph'}
Change units of clients:
>>> um['weight'] = 'kg' >>> um['velocity'] = 'km/hr' >>> car {'weight': 1814.36948, 'velocity': 80.46719999999999} >>> plane {'weight': 79378.66475000001, 'velocity': 965.6063999999999}
Quantity objects are also compatible with UnitManager objects, so long as they are set as the “Quantity” class attribute.
Set “Quantity” attribute:
>>> from pint import UnitRegistry >>> ureg = UnitRegistry() >>> UnitManager.Quantity = Q_ = ureg.Quantity
Set a Quantity object and change units:
>>> car['weight'] = Q_(4000, 'lb') >>> um['weight'] = 'kg' >>> car {'weight': <Quantity(1814.36948, 'kilogram')>, 'velocity': 80.46719999999999>}
-
class
Quantity
¶
-
class