diet-guard/diet_guard/_coerce.py
Krzysztof kuhy Rudnicki 843f5e0221 Extract diet_guard from testsAndMisc as a standalone repo
Rewrites python_pkg.diet_guard imports to diet_guard, vendors the
shared as_float coercion helper, drops the monorepo PYTHONPATH from
install.sh and the systemd unit (package is now pip-installed), and
scaffolds standalone lint/test config matching testsAndMisc's real
enforced bar (pylint --fail-under=10 with tests excluded and the
use-implicit-booleaness/consider-using-with disables, mypy's actual
disabled-error-code set, ruff ALL, bandit, 100% branch coverage).
2026-06-22 12:18:39 +02:00

24 lines
733 B
Python

"""Small value-coercion helpers for diet_guard."""
from __future__ import annotations
def as_float(value: object) -> float:
"""Coerce a stored field to ``float``, defaulting to 0.0.
Booleans are rejected (they are an ``int`` subclass but never a real numeric
measurement here) and any non-numeric value yields 0.0, so callers reading
semi-structured log/bank data get a safe number without guarding each read.
Args:
value: A value read back from a JSON-ish store.
Returns:
The value as a float, or 0.0 when it is absent, a bool, or non-numeric.
"""
if isinstance(value, bool):
return 0.0
if isinstance(value, (int, float)):
return float(value)
return 0.0