Functional Weave
Code in TypeScript

charts.extent@1.0.1

impl/python.py

808 bytes · the Python implementation · view raw

import math
from typing import List, Optional, Sequence


def extent(values: Sequence[Optional[float]]) -> Optional[List[float]]:
    """[min, max] of a series, skipping gaps (None); None when nothing is left.

    min() over the raw list would fail on None rather than skip it.
    """
    lo = 0.0
    hi = 0.0
    seen = False
    for v in values:
        if v is None:
            continue
        if isinstance(v, bool) or not isinstance(v, (int, float)) or not math.isfinite(v):
            raise TypeError("values must be finite numbers or null, received %r" % (v,))
        if not seen:
            lo = hi = float(v)
            seen = True
        else:
            if v < lo:
                lo = float(v)
            if v > hi:
                hi = float(v)
    return [lo, hi] if seen else None