Skip to content

Base

Define the base of the parts of a system.

BasePart(label)

Bases: BaseConfig[ContextT, InputT]

Base of every part of a system: its elements, its interactions, the laws they return, and the system itself.

Every part has a label, returns the laws it contributes from _get_laws, and implements three hooks that the verbs check, prepare, and run call:

  • _check() validates the arguments of the part, after check validates the label.
  • _prepare(context) prepares the part against its context and sets its attributes that end in _.
  • _run(inputs) sets what the part contributes: laws_, the laws of an element, an interaction, or a system, or value_, the value of a law.

A part is generic in the context of prepare and the input of run, which every base of parts names: a law is prepared against the path of its owner, an element against the space of its system.

A part declares which arguments of its constructor are states, parameters, or roles by annotating them with Annotated and a StateSpec, a ParameterSpec, or a RoleSpec, and check validates every declared spec. A part must be checked before it is prepared or run.

Examples:

>>> from skcomplex.core.base import BasePart
>>> class DoublingPart(BasePart):
...     def __init__(self, label, factor=2):
...         super().__init__(label)
...         self.factor = factor
...
...     def _check(self):
...         pass
...
...     def _prepare(self, context=None):
...         pass
...
...     def _get_laws(self, namespace):
...         return []
...
...     def _run(self, value):
...         self.doubled_ = self.factor * value
>>> DoublingPart('doubling').check().prepare().run(3).doubled_
6

Parameters:

Name Type Description Default
label str

The label of the part, unique among the parts of its system.

required
Source code in src/skcomplex/core/base.py
60
61
62
63
64
65
66
67
def __init__(self, label: str) -> None:
    """Store the label of the part.

    Args:
        label:
            The label of the part, unique among the parts of its system.
    """
    self.label = label

check()

Validate the label and the arguments of the part and mark it as checked.

Returns:

Name Type Description
part Self

The part itself.

Raises:

Type Description
ValidationError

The label or an argument is invalid, named in the message.

Source code in src/skcomplex/core/base.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def check(self) -> Self:
    """Validate the label and the arguments of the part and mark it as checked.

    Returns:
        part:
            The part itself.

    Raises:
        ValidationError:
            The label or an argument is invalid, named in the message.
    """
    self._check_label()
    for spec in self._get_specs().values():
        spec.check()
    return super().check()