ts_shape.utils.validate
Checks for configuration arguments (dicts, limits, names, choices).
Detector configurations are plain dicts and numbers: schedules, recipe specs, line configurations, targets, cost tables, threshold levels. A typo in a key, limits given the wrong way round or a shift name that does not exist used to be computed as given and produced a plausible but wrong result. These helpers make such input fail loudly, with a message that says what is wrong and what is accepted:
- :func:
check_keys-- unknown keys raiseValueErrorwith a did-you-mean; - :func:
check_order--lower > upperraisesValueError; - :func:
check_levels_ordered-- threshold levels in the wrong order raise; - :func:
check_choice-- a value outside its choices raises; - :func:
check_known_names-- names that do not occur in the data (shifts, phases, parts, uuids) raise or emit a :class:~ts_shape.errors.DataQualityWarning; - :func:
check_positive/ :func:check_non_negative.
Every message starts with where (e.g. "ScrapEvents.scrap_cost").
| Name | Description |
|---|---|
suggest | (did you mean 'x'?) for the closest of known, or "". |
check_keys | Raise ValueError when mapping has a key outside allowed. |
check_order | Raise ValueError when lower > upper (or >= when strict). |
check_levels_ordered | Raise ValueError for empty levels or ones out of order. |
check_choice | Raise ValueError when value is not one of choices. |
check_known_names | Report the names that are not in known. |
check_positive | Raise ValueError unless value > 0. |
check_non_negative | Raise ValueError unless value >= 0. |
suggest
suggest(name: Any, known: Iterable[Any]) -> str
(did you mean 'x'?) for the closest of known, or "".
check_keys
check_keys(
mapping: Mapping[str, Any] | None,
allowed: Collection[str],
*,
where: str | None = None,
what: str = "key"
) -> None
Raise ValueError when mapping has a key outside allowed.
| Name | Type | Description | Default |
|---|---|---|---|
mapping | Mapping[str, Any] | None | The configuration dict (None is accepted: nothing to check). | required |
allowed | Collection[str] | The keys the configuration understands. | required |
where | str | None | Message prefix (the calling method). | None |
what | str | What a key is, for the message ("criterion", "metric"). | 'key' |
| Type | Description |
|---|---|
ValueError | Naming the unknown key, the closest allowed one and every allowed key. |
check_order
check_order(
lower: Any,
upper: Any,
*,
names: tuple[str, str] = ("lower", "upper"),
where: str | None = None,
strict: bool = False
) -> None
Raise ValueError when lower > upper (or >= when strict).
None or NaN on either side is not checked (an open bound).
check_levels_ordered
check_levels_ordered(
levels: Sequence[Any],
*,
direction: str = "above",
where: str | None = None,
what: str = "levels"
) -> None
Raise ValueError for empty levels or ones out of order.
direction="above" needs increasing levels (a higher level is more
severe), "below" decreasing ones.
check_choice
check_choice(
value: Any,
choices: Collection[Any],
*,
name: str = "value",
where: str | None = None
) -> None
Raise ValueError when value is not one of choices.
check_known_names
check_known_names(
names: Iterable[Any],
known: Collection[Any],
*,
kind: str,
where: str | None = None,
error: bool = False,
consequence: str = ""
) -> list[Any]
Report the names that are not in known.
| Name | Type | Description | Default |
|---|---|---|---|
names | Iterable[Any] | Names the configuration uses (shift names, phases, parts...). | required |
known | Collection[Any] | The names that exist (in the data or the library). | required |
kind | str | What a name is, for the message ("shift"). | required |
where | str | None | Message prefix. | None |
error | bool | Raise ValueError instead of warning. | False |
consequence | str | What happens to an unknown name (appended to the warning). | '' |
| Type | Description |
|---|---|
list[Any] | The unknown names (empty when every name is known). |
| Type | Description |
|---|---|
ValueError | With error=True, when a name is unknown. |
| Type | Description |
|---|---|
DataQualityWarning | Without error, when a name is unknown. |
check_positive
check_positive(value: Any, *, name: str, where: str | None = None) -> None
Raise ValueError unless value > 0.
check_non_negative
check_non_negative(value: Any, *, name: str, where: str | None = None) -> None
Raise ValueError unless value >= 0.