The following steps show you how to use the Pint package to keep track of units in calculations:
- First, we need to create a UnitRegistry object:
ureg = pint.UnitRegistry(system="mks")
- To create a quantity with a unit, we multiply the number by the appropriate attribute of the registry object:
distance = 5280 * ureg.feet
- We can change the units of the quantity using one of the available conversion methods:
print(distance.to("miles"))
print(distance.to_base_units())
print(distance.to_base_units().to_compact())
The output of these print statements is as follows:
0.9999999999999999 mile
1609.3439999999998 meter
1.6093439999999999 kilometer
- We wrap a routine to make it expect an argument in seconds and output a result in meters:
@ureg.wraps(ureg.meter, ureg.second)
def calc_depth(dropping_time...