testsAndMisc/python_pkg/shared/coerce.py
Krzysztof kuhy Rudnicki 038e08d2be feat: split oversized modules for 500-line limit, fix kasa coverage gap
Split diet_guard/_gatelock.py, wake_alarm/_alarm.py, and the
usage_report.py/_usage_report_parsing.py pair into focused
sub-modules so every Python file is <= 500 lines, satisfying
test_file_length.py. Install python-kasa into .venv (declared in
requirements but missing after the 3.13->3.14 venv upgrade),
fixing 8 failing smart_plug tests and restoring 100% coverage.

Also includes prior in-progress work from the working tree: the
wake_alarm Progress/View/Hardware field-grouping refactor,
brother_printer query module + tests, diet_guard foodbank/state/cli
updates, new shared coerce/logging_setup helpers, morning_routine
orchestrator tweaks, dwm window-manager config, gaming scripts, and
misc maintenance/digital-wellbeing script updates.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-14 07:19:37 +02:00

24 lines
755 B
Python

"""Small value-coercion helpers shared across python_pkg subpackages."""
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