mpacts.core.units. registry_helpers

Miscellaneous methods of the registry writen as separate functions.

copyright:2016 by Pint Authors, see AUTHORS for more details..
license:BSD, see LICENSE for more details.

In order to be able to use this module import it like this:

import mpacts.core.units.registry_helpers
#or assign it to a shorter name
import mpacts.core.units.registry_helpers as reg
mpacts.core.units.registry_helpers._parse_wrap_args(args)
mpacts.core.units.registry_helpers._replace_units(original_units, values_by_name)

Convert a unit compatible type to a UnitsContainer.

Parameters:
  • original_units – a UnitsContainer instance.
  • values_by_name – a map between original names and the new values.
mpacts.core.units.registry_helpers._to_units_container(a)

Convert a unit compatible type to a UnitsContainer, checking if it is string field prefixed with an equal (which is considered a reference)

Return a tuple with the unit container and a boolean indicating if it was a reference.

mpacts.core.units.registry_helpers.check(ureg, *args)

Decorator to for quantity type checking for function inputs.

Use it to ensure that the decorated function input parameters match the expected type of mpacts.core.units quantity.

Use None to skip argument checking.

Parameters:
  • ureg – a UnitRegistry instance.
  • args – iterable of input units.
Returns:

the wrapped function.

Raises:

DimensionalityError if the parameters don’t match dimensions

mpacts.core.units.registry_helpers.wraps(ureg, ret, args, strict=True)

Wraps a function to become mpacts.core.units-aware.

Use it when a function requires a numerical value but in some specific units. The wrapper function will take a mpacts.core.units quantity, convert to the units specified in args and then call the wrapped function with the resulting magnitude.

The value returned by the wrapped function will be converted to the units specified in ret.

Use None to skip argument conversion. Set strict to False, to accept also numerical values.

Parameters:
  • ureg – a UnitRegistry instance.
  • ret – output units.
  • args – iterable of input units.
  • strict – boolean to indicate that only quantities are accepted.
Returns:

the wrapped function.

Raises:

ValueError if strict and one of the arguments is not a Quantity.