Unit registry#
- class ansys.units.unit_registry.UnitRegistry(config: str = 'cfg.yaml', other: Mapping[str, Mapping[str, Any]] = {}, custom_units: Sequence[Mapping[str, Any]] | None = None)#
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 (uses config file format).
- custom_units
listofdict,optional List of custom units to register during construction. Each dict must have keys
"unit","composition", and"factor"matching theregister_unit()signature.
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 custom units at construction:
>>> ur = UnitRegistry(custom_units=[ ... {"unit": "micron", "composition": "m", "factor": 1e-6}, ... {"unit": "inch", "composition": "m", "factor": 0.0254}, ... ]) >>> q = ur.Quantity(10, "micron")
- Quantity(value: int | float | Sequence[float], units: str | Unit) Quantity#
Create a Quantity using this registry’s units.
This method allows creating quantities with instance-registered units using string names.
- Parameters:
- Returns:
QuantityThe created quantity.
Examples
>>> ur = UnitRegistry() >>> ur.register_unit(unit="micron", composition="m", factor=1e-6) >>> q = ur.Quantity(1, "micron") >>> print(q) 1.0 micron
- get_unit(name: str) Unit#
Look up a unit by name from this registry.
Checks instance-registered units first, then falls back to built-in units and compound unit strings. This allows string-based lookup of custom registered units as well as standard unit expressions.
- Parameters:
- name
str The unit name or compound unit string to look up (e.g., “micron”, “m”, “kg mol^-1”).
- name
- Returns:
UnitThe unit object.
- Raises:
AttributeErrorIf the unit is not found in this registry or cannot be parsed.
- register_alias(alias: str, canonical: str) None#
Register a new alias for an existing unit.
The alias is added to the global alias table so that it can be used anywhere a unit string is accepted (
Unit(),Quantity(), etc.).- Parameters:
- Raises:
AliasAlreadyRegisteredIf
aliasis already registered as a unit or alias.ValueErrorIf
canonicalis not a known unit or alias.
- 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. The registered unit can be accessed as an attribute (e.g.,
ur.micron) or viaget_unit()for string-based lookup.- Parameters:
- unit: str
The symbol/name of the new unit (e.g., “micron”).
- composition: str
A valid unit composition using existing configured units (e.g., “m”).
- factor: float
Scale factor that relates the composition to this unit.
- Returns:
UnitThe registered unit attached on this instance.
- Raises:
UnitNameAlreadyRegisteredIf a unit with the same name already exists on this instance or as a built-in. This is a name-only check and does not detect equivalent definitions with different names.
ValueErrorIf
unitis empty orfactoris not finite.
Notes
The name collision check is superficial (name-only). Two units with different names but equivalent definitions (same composition and factor) can both be registered without error.
Examples
>>> ur = UnitRegistry() >>> ur.register_unit(unit="micron", composition="m", factor=1e-6) >>> q = ur.Quantity(1, "micron") # Use registry's Quantity method
- exception ansys.units.unit_registry.UnitNameAlreadyRegistered(name: str)#
Bases:
ValueErrorRaised when a unit name conflicts with an existing name.
This is a name-only check. Units with different names but equivalent definitions (same composition and factor) are not detected.
- exception ansys.units.unit_registry.AliasAlreadyRegistered(name: str)#
Bases:
ValueErrorRaised when an alias conflicts with an existing unit or alias.