eytelwein.main.units module¶
Centralized Unit Registry for Eytelwein Package.
This module provides a singleton implementation of Pint’s UnitRegistry, ensuring consistent unit handling and validation across all calculation modules in the Eytelwein package.
It is based on the existing unit registry implementation but is now centralized for all conveyor calculation modules.
- class eytelwein.main.units.UnitRegistrySingleton[source]¶
Bases:
objectSingleton class to provide a single UnitRegistry instance across all standards.
This ensures consistent unit handling and validation throughout all conveyor calculation standards and modules.
- eytelwein.main.units.configure_registry(registry: UnitRegistry) None[source]¶
Configure the unit registry with settings for conveyor calculations.
- Parameters:
registry – The registry to configure
- eytelwein.main.units.convert_to(quantity: Quantity, unit: str | Unit) Quantity[source]¶
Convert a quantity to the specified unit.
- Parameters:
quantity – The quantity to convert
unit – The target unit
- Returns:
The converted quantity
- Raises:
pint.DimensionalityError – If the conversion is not possible
Example
>>> from eytelwein.main.units import convert_to, get_unit_registry >>> ureg = get_unit_registry() >>> q = ureg.Quantity(1000, "mm") >>> convert_to(q, "meter") <Quantity(1, 'meter')>
- eytelwein.main.units.ensure_quantity(value: float | int | Quantity, unit: str | Unit | None = None) Quantity[source]¶
Ensure a value is a Quantity object with the specified unit.
- Parameters:
value – A value that may be a Quantity or a scalar
unit – The unit to use if value is a scalar
- Returns:
A Quantity object
- Raises:
ValueError – If value is not a Quantity and no unit is provided
Example
>>> from eytelwein.main.units import ensure_quantity, get_unit_registry >>> ureg = get_unit_registry() >>> q1 = ensure_quantity(5, "meter") >>> q2 = ensure_quantity(ureg.Quantity(10, "kg")) >>> print(q1) 5 meter >>> print(q2) 10 kilogram
- eytelwein.main.units.format_quantity(quantity: Quantity, format_spec: str = '.4g') str[source]¶
Format a quantity with the given format specification.
- Parameters:
quantity – The quantity to format
format_spec – The format specification to use
- Returns:
A formatted string representation of the quantity
Example
>>> from eytelwein.main.units import format_quantity, get_unit_registry >>> ureg = get_unit_registry() >>> q = ureg.Quantity(1.2345, "meter") >>> format_quantity(q, ".2f") '1.23 meter'
- eytelwein.main.units.get_unit_registry() UnitRegistry[source]¶
Get the global unit registry instance for Eytelwein.
Use this function throughout all calculation standards to ensure consistent unit handling.
- Returns:
The shared unit registry instance
- Return type:
pint.UnitRegistry
Example
>>> from eytelwein.main.units import get_unit_registry >>> ureg = get_unit_registry() >>> quantity = ureg.Quantity(10, "meter")