Source code for openairbearing.core.params
"""Parameter metadata for input dataclasses.
Every input number is stored in SI. ``param`` attaches what a form, a plot
axis or a schema needs to show it: a label, the SI unit, the unit people
read it in and the factor from SI to that unit.
"""
from __future__ import annotations
from dataclasses import field
__all__ = ["param"]
[docs]
def param(
default,
label: str,
unit: str = "",
display: str | None = None,
scale: float = 1.0,
minimum: float | None = None,
maximum: float | None = None,
choices: tuple | None = None,
help: str | None = None,
follows: str | None = None,
):
"""Dataclass field with display metadata.
The metadata is not checked here; it is for whoever shows or reads the
value.
default: the default value, in SI.
label: name of the value as shown to people.
unit: SI unit of the stored value.
display: unit shown to people (default: the SI unit), with shown value =
stored value × scale.
minimum, maximum: limits of the stored value, in SI.
choices: the values allowed, for a value picked from a list.
help: a sentence or two on what the value means, for a tooltip next to
its label.
follows: name of another field of the same dataclass whose value this
one takes while it is not given. The default is then None, which
means "not given"; a number holds the value whatever the other
field is.
"""
meta = {
"label": label,
"unit": unit,
"display": unit if display is None else display,
"scale": scale,
}
if minimum is not None:
meta["minimum"] = minimum
if maximum is not None:
meta["maximum"] = maximum
if choices is not None:
meta["choices"] = tuple(choices)
if help is not None:
meta["help"] = help
if follows is not None:
if default is not None:
raise ValueError("A field that follows another has the default None.")
meta["follows"] = follows
return field(default=default, metadata=meta)