Functional Weave
Code in Python

energy.degree-days@1.0.0

impl/python.py

1,078 bytes · the Python implementation · view raw

from typing import Sequence

from .energy_degree_days_types import DegreeDays

LIMIT = 1000  # 100.0 C in tenths


def _check_temperature(what: str, value: object) -> None:
    if not isinstance(value, int) or isinstance(value, bool) or value < -LIMIT or value > LIMIT:
        raise ValueError("%s must be whole tenths of a degree from -1000 to 1000, received %s" % (what, value))


def degree_days(daily_mean_temperatures: Sequence[int], base_temperature: int) -> DegreeDays:
    """Heating and cooling degree days in tenths: each day adds how far its
    mean fell below (heating) or rose above (cooling) the base.
    """
    _check_temperature("base temperature", base_temperature)
    heating = 0
    cooling = 0
    for mean in daily_mean_temperatures:
        _check_temperature("daily mean temperature", mean)
        if mean < base_temperature:
            heating += base_temperature - mean
        elif mean > base_temperature:
            cooling += mean - base_temperature
    return DegreeDays(heating=heating, cooling=cooling, days=len(daily_mean_temperatures))