Skip to content

loader

loader ¤

Registration helpers + YAML/dict loaders for lambda rules.

A lambda rule lives in two places once registered:

  • :data:ts_shape.eventlog.taxonomy.REGISTRY — the :class:~ts_shape.eventlog.taxonomy.LabelRule mapping that :func:~ts_shape.eventlog.to_event_log consults.
  • :data:ts_shape.eventlog.archetypes.ARCHETYPE_BY_METHOD — the archetype classification that the coverage tests use to enforce standard_attrs completeness.

Both mutations happen through :func:register_lambda_rule. The coverage test test_no_orphan_registry_entries is configured to exempt entries whose class name starts with Lambda, so registered lambda rules do not need a real Python class on disk.

register_lambda_rule ¤

register_lambda_rule(spec: RuleSpec) -> LambdaDetector

Register spec in REGISTRY + ARCHETYPE_BY_METHOD; return runnable detector.

The trigger expression is compiled here, so a bad expression fails at registration rather than at the first evaluation.

Parameters:

Name Type Description Default
spec RuleSpec

The rule to register.

required

Returns:

Type Description
LambdaDetector

The runnable :class:LambdaDetector.

Raises:

Type Description
ValueError

If the spec is malformed, the expression does not compile (:class:UnsafeExpression), the standard_attrs mapping uses unknown keys, the archetype's required keys are missing, or a rule with the same (class_name, method_name) is already registered.

unregister_lambda_rule ¤

unregister_lambda_rule(
    class_name: str, method_name: str
) -> None

Remove a rule previously installed by :func:register_lambda_rule.

Used by tests to keep the global REGISTRY clean between cases. No-op if the rule was not registered. Deprecated class / method names resolve to their current names, as in the registry lookup.

Parameters:

Name Type Description Default
class_name str

The rule's class name.

required
method_name str

The rule's method name.

required

load_dicts ¤

load_dicts(
    entries: Iterable[Mapping[str, Any]],
) -> list[LambdaDetector]

Compile and register an iterable of dict rules, all or nothing.

Every rule is validated and its trigger compiled before any is registered; if one fails, the rules registered by this call are removed again and the error is raised.

Parameters:

Name Type Description Default
entries Iterable[Mapping[str, Any]]

Rule mappings with the :class:RuleSpec fields; trigger is a mapping with expression (and optional min_duration_s, group_by -- a column name or a list of them).

required

Returns:

Name Type Description
One list[LambdaDetector]

class:LambdaDetector per rule, in input order.

Raises:

Type Description
ValueError

For a malformed rule, a duplicate registration or an expression that does not compile (:class:UnsafeExpression).

load_yaml ¤

load_yaml(path: str | Path) -> list[LambdaDetector]

Load and register every rule under a YAML file's rules: key.

YAML is imported lazily so :mod:ts_shape.eventlog does not gain a hard runtime dependency on PyYAML for users who only use built-in detectors. Install ts-shape[yaml] to enable this loader, or use :func:load_dicts with a hand-built list.

Parameters:

Name Type Description Default
path str | Path

Path to a YAML file with a top-level rules: list.

required

Returns:

Name Type Description
One list[LambdaDetector]

class:LambdaDetector per rule (see :func:load_dicts).

Raises:

Type Description
ImportError

When PyYAML is not installed.

ValueError

When the file has no rules: list or a rule is invalid.