Skip to content

Other

rigour.units

normalize_unit(unit)

Normalize a unit string to its standard abbreviation.

Parameters:

Name Type Description Default
unit str

The unit string to normalize.

required

Returns:

Name Type Description
str str

The normalized unit abbreviation.

Source code in rigour/units.py
def normalize_unit(unit: str) -> str:
    """
    Normalize a unit string to its standard abbreviation.

    Args:
        unit (str): The unit string to normalize.

    Returns:
        str: The normalized unit abbreviation.
    """
    return UNITS.get(unit.lower(), unit)

rigour.boolean

bool_text(value)

Convert a boolean value to a string representation. If the input is None, it returns None. If the input is True, it returns 't'. If the input is False, it returns 'f'.

Source code in rigour/boolean.py
4
5
6
7
8
9
def bool_text(value: bool | None) -> str | None:
    """Convert a boolean value to a string representation. If the input is None, it returns
    None. If the input is True, it returns 't'. If the input is False, it returns 'f'."""
    if value is None:
        return None
    return "t" if value else "f"

text_bool(text) cached

Convert a string representation of a boolean value to a boolean. If the input is None or an empty string, it returns None.

Source code in rigour/boolean.py
@cache
def text_bool(text: str | None) -> bool | None:
    """Convert a string representation of a boolean value to a boolean. If the input is None
    or an empty string, it returns None."""
    if text is None or len(text) == 0:
        return None
    text = text.lower()
    return text.startswith("t") or text.startswith("y") or text.startswith("1")

rigour.env

env_bool(name, default)

Ensure the env returns a boolean.

Source code in rigour/env.py
def env_bool(name: str, default: bool) -> bool:
    """Ensure the env returns a boolean."""
    value = env_opt(name)
    if value is None:
        return default
    value = value.lower().strip()
    if value in TRUE_VALUES:
        return True
    if value in FALSE_VALUES:
        return False
    return default

env_float(name, default)

Ensure the env returns an float.

Source code in rigour/env.py
def env_float(name: str, default: float) -> float:
    """Ensure the env returns an float."""
    try:
        return float(env.get(name, default))
    except (ValueError, TypeError):
        return default

env_int(name, default)

Ensure the env returns an int.

Source code in rigour/env.py
def env_int(name: str, default: int) -> int:
    """Ensure the env returns an int."""
    try:
        return int(env.get(name, default))
    except (ValueError, TypeError):
        return default

env_opt(name)

Get an optional environment variable.

Source code in rigour/env.py
def env_opt(name: str) -> str | None:
    """Get an optional environment variable."""
    return stringify(env.get(name))

env_str(name, default)

Ensure the env returns a string even on Windows (#100).

Source code in rigour/env.py
def env_str(name: str, default: str) -> str:
    """Ensure the env returns a string even on Windows (#100)."""
    value = env_opt(name)
    return default if value is None else value