Skip to content

watlowlib.units

to_pint(unit) — maps a watlowlib.Unit (or a recognised alias string) to a pint-compatible unit string. pint is not a runtime dependency of watlowlib; this helper returns plain strings so downstream tools that use pint can feed them straight into pint.UnitRegistry.parse_expression, while consumers that don't use pint can ignore the output. Lossy by design: gauge/absolute and other distinctions pint doesn't model are dropped.

Public surface

watlowlib.units

Pint-compatible unit-string export.

:func:to_pint maps a :class:watlowlib.Unit (or a recognised alias string) into a pint-compatible unit string. pint itself is not a runtime dependency of :mod:watlowlib — this helper returns plain strings so downstream tools that use pint can feed them straight into pint.UnitRegistry.parse_expression, while consumers that don't use pint can ignore the output.

Lossy by design: if a unit has gauge/absolute or any other distinction pint doesn't model, the loss is accepted — :func:to_pint returns the lossy string. Callers that need the disambiguator handle it themselves.

The mapping table here mirrors the table that previously lived in capa/src/capa/devices/watlow.py. Moving it into :mod:watlowlib means every consumer (capa, the in-house dashboards, downstream clients) shares one source of truth.

to_pint

to_pint(unit)

Return a pint-compatible unit string.

Accepts a :class:Unit, a case-insensitive string alias, or None. Returns:

  • None when unit is None or when a string alias is unrecognised (lossy by design — callers that care distinguish None themselves).
  • "degC" / "degF" / "percent" for the three Watlow units.

String inputs go through :func:watlowlib.registry.units.coerce_unit so capitalisation and the common aliases ("C", "celsius", "degC", "°C", ...) all map the same way as the rest of the library.

Source code in src/watlowlib/units.py
def to_pint(unit: Unit | str | None) -> str | None:
    """Return a pint-compatible unit string.

    Accepts a :class:`Unit`, a case-insensitive string alias, or
    ``None``. Returns:

    - ``None`` when ``unit`` is ``None`` or when a string alias is
      unrecognised (lossy by design — callers that care distinguish
      ``None`` themselves).
    - ``"degC"`` / ``"degF"`` / ``"percent"`` for the three Watlow
      units.

    String inputs go through :func:`watlowlib.registry.units.coerce_unit`
    so capitalisation and the common aliases (``"C"``, ``"celsius"``,
    ``"degC"``, ``"°C"``, ...) all map the same way as the rest of
    the library.
    """
    if unit is None:
        return None
    try:
        resolved = coerce_unit(unit)
    except WatlowValidationError:
        return None
    return _WATLOW_UNIT_TO_PINT.get(resolved)