Skip to main content
Version: 0.0.98

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 raise ValueError with a did-you-mean;
  • :func:check_order -- lower > upper raises ValueError;
  • :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").

Functions
NameDescription
suggest (did you mean 'x'?) for the closest of known, or "".
check_keysRaise ValueError when mapping has a key outside allowed.
check_orderRaise ValueError when lower > upper (or >= when strict).
check_levels_orderedRaise ValueError for empty levels or ones out of order.
check_choiceRaise ValueError when value is not one of choices.
check_known_namesReport the names that are not in known.
check_positiveRaise ValueError unless value > 0.
check_non_negativeRaise ValueError unless value >= 0.

suggest​

functionsource
suggest(name: Any, known: Iterable[Any]) -> str

(did you mean 'x'?) for the closest of known, or "".

check_keys​

functionsource
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.

Parameters
NameTypeDescriptionDefault
mappingMapping[str, Any] | NoneThe configuration dict (None is accepted: nothing to check).required
allowedCollection[str]The keys the configuration understands.required
wherestr | NoneMessage prefix (the calling method).None
whatstrWhat a key is, for the message ("criterion", "metric").'key'
Raises
TypeDescription
ValueErrorNaming the unknown key, the closest allowed one and every allowed key.

check_order​

functionsource
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​

functionsource
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​

functionsource
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​

functionsource
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.

Parameters
NameTypeDescriptionDefault
namesIterable[Any]Names the configuration uses (shift names, phases, parts...).required
knownCollection[Any]The names that exist (in the data or the library).required
kindstrWhat a name is, for the message ("shift").required
wherestr | NoneMessage prefix.None
errorboolRaise ValueError instead of warning.False
consequencestrWhat happens to an unknown name (appended to the warning).''
Returns
TypeDescription
list[Any]The unknown names (empty when every name is known).
Raises
TypeDescription
ValueErrorWith error=True, when a name is unknown.
Warns
TypeDescription
DataQualityWarningWithout error, when a name is unknown.

check_positive​

functionsource
check_positive(value: Any, *, name: str, where: str | None = None) -> None

Raise ValueError unless value > 0.

check_non_negative​

functionsource
check_non_negative(value: Any, *, name: str, where: str | None = None) -> None

Raise ValueError unless value >= 0.