Unit registry#
- class ansys.units.unit_registry.UnitRegistry(config: str = 'cfg.yaml', other: Mapping[str, Mapping[str, Any]] = {})#
Bases:
objectA representation of valid
Unitinstances.All base and derived units loaded from the configuration file, cfg.yaml, on package initialization are provided by default.
- Parameters:
- config: str, optional
Path of a
YAMLconfiguration file, which can be a custom file, and defaults to the provided file,cfg.yaml. Custom configuration files must match the format of the default configuration file.- other: dict, optional
Dictionary for additional units.
Examples
>>> from ansys.units import UnitRegistry, Unit >>> ureg = UnitRegistry() >>> assert ureg.kg == Unit(units="kg") >>> fps = Unit("ft s^-1") >>> ureg.foot_per_sec = fps
- register_unit(*, unit: str, composition: str, factor: float) Unit#
Register a new derived unit on this
UnitRegistryinstance.This is instance-scoped: it affects only this registry and does not mutate global state or other registries.
- Parameters:
- unit: str
The symbol/name of the new unit (e.g., “Q”). Preferred keyword.
- composition: str
A valid unit composition using existing configured units (e.g., “N m”).
- factor: float
Scale factor that relates the composition to this unit.
- Returns:
UnitThe registered unit attached on this instance.
- Raises:
UnitAlreadyRegisteredIf a unit with the same name already exists on this instance or is built-in.
ValueErrorIf
unitis empty orfactoris not finite.
- exception ansys.units.unit_registry.UnitAlreadyRegistered(name: str)#
Bases:
ValueErrorRaised when a unit has previously been registered.