Unit registry#

class ansys.units.unit_registry.UnitRegistry(config: str = 'cfg.yaml', other: Mapping[str, Mapping[str, Any]] = {})#

Bases: object

A representation of valid Unit instances.

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 YAML configuration 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 UnitRegistry instance.

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:
Unit

The registered unit attached on this instance.

Raises:
UnitAlreadyRegistered

If a unit with the same name already exists on this instance or is built-in.

ValueError

If unit is empty or factor is not finite.

exception ansys.units.unit_registry.UnitAlreadyRegistered(name: str)#

Bases: ValueError

Raised when a unit has previously been registered.