Unit¶
- class component_model.unit.Unit(quantity: bool | int | float | str | Enum | None = None)¶
Bases:
objectHelper class to store and manage units and display units, i.e. base unit of variable and unit differences ‘outside’(display units) and ‘inside’(base units) the model.
- Parameters:
quantity (bool, int, float, str, Enum, None) – The quantity to be disected for unit definition. 3 possibilites:
None (*) – no units. Instantiates an ‘empty’ Unit object
str (*) – is parsed to disect the unit. If a unit is identified this is treated as a variable with units (and possibly display units) If no unit is identified this is treated as a free string variable (no units). Use ‘None’ to ensure free str.
bool (*) – variables with no units and no display units
int – variables with no units and no display units
Enum – variables with no units and no display units
one Unit object represents one scalar variable.
many variables do not have units (i.e. str, Enum, int variables). If These get the unit .u=””
variables without separate display units get the display unit .du=None and a reduced set of properties
only float variables may have separate display units and transformations
- __init__(quantity: bool | int | float | str | Enum | None = None)¶
Methods
__init__([quantity])compatible(quantity[, no_unit, strict])Check whether the supplied quantity 'q' is compatible with this unit.
derivative(baseunits[, tu])Construct units for a derivative variable of basevars.
ensure_unit_registry([system, autoconvert])identity(val)linear(val, intercept, slope)make(quantity[, no_unit])Parse quantity and return the resulting value and its unit object.
make_tuple(quantities[, no_unit])Make a tuple of values and Unit objects from the tuple of quantities, using make().
parse_quantity(quantity)Parse the provided quantity in terms of magnitude and unit, if provided as string.
slope(val, slope)Identify base units and calculate the transformations between display and base units.
- classmethod derivative(baseunits: tuple[Unit, ...], tu: str = 's') tuple[tuple[float, ...], tuple[Unit, ...]]¶
Construct units for a derivative variable of basevars. tu is the time unit.
- classmethod ensure_unit_registry(system: str = 'SI', autoconvert: bool = True)¶
- classmethod identity(val: Any) Any¶
- classmethod linear(val: float, intercept: float, slope: float) float¶
- classmethod make(quantity: bool | int | float | str | Enum, no_unit: bool = False) tuple[tuple[bool | int | float | str | Enum], tuple[Unit]]¶
Parse quantity and return the resulting value and its unit object. If no_unit, only a default object is generated.
- classmethod make_tuple(quantities: tuple[bool | int | float | str | Enum, ...] | list[bool | int | float | str | Enum] | ndarray, no_unit: bool = False) tuple[tuple[bool | int | float | str | Enum, ...], tuple[Unit, ...]]¶
Make a tuple of values and Unit objects from the tuple of quantities, using make().
- classmethod slope(val: float, slope: float) float¶
- compatible(quantity: bool | int | float | str | Enum, no_unit: bool = False, strict: bool = True) tuple[bool, bool | int | float | str | Enum]¶
Check whether the supplied quantity ‘q’ is compatible with this unit. If strict==True, the supplied quantity shall be in display units.
- parse_quantity(quantity: bool | int | float | str | Enum) bool | int | float | str | Enum¶
Parse the provided quantity in terms of magnitude and unit, if provided as string. If another type is provided, no units are assumed.
- Parameters:
quantity – the quantity to disect. Should be provided as string, but also the trivial cases (int,float,Enum) are allowed.
warning (A free string should not be used and leads to a)
- Returns:
the magnitude in base units, the base unit and the unit as given (display units), together with the conversion functions between the units.
- val_unit_display(q: Quantity[int | float]) int | float¶
Identify base units and calculate the transformations between display and base units.
- Returns:
The numerical value of q. As side effect
* the unit u is set. Might be dimensionless
* the display unit du is set to None if same as unit, else –
it is set to the display unit name and
the transformations to_base and from_base are set.