openpectus.lang.exec.units

Attributes

cache_folder

ureg

Q_

logger

QUANTITY_UNIT_MAP

QUANTITY_PINT_MAP

Map quantity names to pint dimensions or None if not a pint dimension.

FLOAT_COMPARE_DELTA

The precision to use when comparing float values for equality.

BASE_VALID_UNITS

Functions

add_unit(unit[, unit_relation, quantity_relation, ...])

Add a unit and possibly a quantity to the registry.

get_supported_units()

is_supported_unit(unit)

get_volume_units()

convert_value_to_unit(value, source_unit, target_unit)

Convert a value with a unit to the value in another unit.

get_unit_quantity_name(unit)

For a supported unit that is not None, return its quantity name. Raises ValueError if the unit is not supported.

_get_quantity_name_for_pint_dim(pint_dimension)

get_compatible_unit_names(unit)

For a supported unit, return its compatible units. If the unit is not supported, a ValueError is raised.

are_comparable(unit_a, unit_b)

Determine whether two supported units are comparable, i.e. have the same quantity name.

compare_values(op, value_a, unit_a, value_b, unit_b)

Compare two values using the provided operator (<, <=, >, >=, =, ==, !=) and return the comparison result.

as_float(value)

Parse string value as a float and return it. If the value is not a number, return None.

as_int(value)

Parse string value as int and return it. If the value is not an int, return None.

_get_pint_unit(unit)

Module Contents

openpectus.lang.exec.units.cache_folder
openpectus.lang.exec.units.ureg
openpectus.lang.exec.units.Q_
openpectus.lang.exec.units.logger
openpectus.lang.exec.units.QUANTITY_UNIT_MAP: dict[str, list[str]]
openpectus.lang.exec.units.QUANTITY_PINT_MAP: dict[str, str]

Map quantity names to pint dimensions or None if not a pint dimension.

openpectus.lang.exec.units.FLOAT_COMPARE_DELTA = 0.01

The precision to use when comparing float values for equality.

openpectus.lang.exec.units.BASE_VALID_UNITS = ['L', 'h', 'min', 's', 'mL', 'CV', 'DV', 'g', 'kg']
openpectus.lang.exec.units.add_unit(unit, unit_relation=None, quantity_relation=None, quantity=None)

Add a unit and possibly a quantity to the registry. An example of a unit, quantity pair could be the quantity “length” and the unit “cm”.

Case 1: Unit defined in relation to an existing quantity and unit Suppose the quantity “length” and unit “cm” are defined but the unit “mm” is not defined. Add the unit and unit relation: >> add_unit(“mm”, quantity=”length” unit_relation=”10 mm = 1 cm”)

Case 2: Unit defined with quantity in relation to existing quantity Consider a mass flux quantity defined as “flux = [mass]/[time]/[area]” and an accompanying unit “kg/m2/h”. Add the unit and quantity relationship: >> add_unit(“kg/m2/h”, quantity_relation={“flux”: “[mass]/[time]/[area]”})

Case 3: Unit defined without relation to any existing units or quantities Pint does not support the quantity “absorbance” or the associated unit “AU” out of the box and defines no useful quantities or units. Add the unit and quantity: >> add_unit(“AU”, quantity=”absorbance”)

Parameters:
  • unit (str)

  • unit_relation (None | str)

  • quantity_relation (None | dict[str, str])

  • quantity (None | str)

openpectus.lang.exec.units.get_supported_units()
Return type:

list[str | None]

openpectus.lang.exec.units.is_supported_unit(unit)
Parameters:

unit (str)

Return type:

bool

openpectus.lang.exec.units.get_volume_units()
openpectus.lang.exec.units.convert_value_to_unit(value, source_unit, target_unit)

Convert a value with a unit to the value in another unit.

Raises ValueError if the units are not compatible.

Parameters:
  • value (float | int)

  • source_unit (str)

  • target_unit (str)

Return type:

float

openpectus.lang.exec.units.get_unit_quantity_name(unit)

For a supported unit that is not None, return its quantity name. Raises ValueError if the unit is not supported.

Parameters:

unit (str)

Return type:

str

openpectus.lang.exec.units._get_quantity_name_for_pint_dim(pint_dimension)
Parameters:

pint_dimension (str)

Return type:

str | None

openpectus.lang.exec.units.get_compatible_unit_names(unit)

For a supported unit, return its compatible units. If the unit is not supported, a ValueError is raised.

Parameters:

unit (str | None)

Return type:

list[str]

openpectus.lang.exec.units.are_comparable(unit_a, unit_b)

Determine whether two supported units are comparable, i.e. have the same quantity name.

For example, ‘cm’ and ‘m’ are comparable because they have the same quantity name, ‘length’.

If one or both units are not supported, raise ValueError.

Parameters:
  • unit_a (str | None)

  • unit_b (str | None)

Return type:

bool

openpectus.lang.exec.units.compare_values(op, value_a, unit_a, value_b, unit_b)

Compare two values using the provided operator (<, <=, >, >=, =, ==, !=) and return the comparison result.

If units are given, the units are checked for comparability. The units may be different as long as they are comparable.

In case of error, a ValueError is raised.

Parameters:
  • op (str)

  • value_a (str)

  • unit_a (str | None)

  • value_b (str)

  • unit_b (str | None)

Return type:

bool

openpectus.lang.exec.units.as_float(value)

Parse string value as a float and return it. If the value is not a number, return None.

Parameters:

value (str)

Return type:

float | None

openpectus.lang.exec.units.as_int(value)

Parse string value as int and return it. If the value is not an int, return None.

Parameters:

value (str)

Return type:

int | None

openpectus.lang.exec.units._get_pint_unit(unit)
Parameters:

unit (str | None)

Return type:

pint.Unit | None