Skip to content

Base

Define the root of every configurable class of the library.

BaseConfig

Bases: BaseEstimator, ABC

Root of every configurable class of the library: it stores its settings and speaks three verbs.

The constructor of a child stores every setting unmodified under its own name, as in scikit-learn, so get_params, set_params, and clone work on every class. The three verbs call the abstract hooks _check, _prepare, and _run, which every child implements, itself or through the base of its family, and every verb returns the object, as in scikit-learn:

  • check() validates the settings through _check() and marks the object as checked.
  • prepare(context) prepares the checked object against the one thing it serves through _prepare(context), and sets the attributes that end in _.
  • run(inputs, final=False) performs the one action of the checked and prepared object on its one input through _run(inputs), and keeps what the action produces in attributes that end in _; a last call with final=True also releases what preparing acquired through _release(), an optional hook that releases nothing by default.

Examples:

>>> from skcomplex.base import BaseConfig
>>> class DoublingConfig(BaseConfig):
...     def __init__(self, factor=2):
...         self.factor = factor
...
...     def _check(self):
...         pass
...
...     def _prepare(self, context):
...         pass
...
...     def _run(self, inputs):
...         self.doubled_ = self.factor * inputs
>>> DoublingConfig().check().prepare().run(3).doubled_
6

check()

Validate the settings of the object and mark it as checked.

Returns:

Name Type Description
self Self

The object.

Raises:

Type Description
ValidationError

A setting is invalid, named in the message.

Source code in src/skcomplex/base.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def check(self) -> Self:
    """Validate the settings of the object and mark it as checked.

    Returns:
        self:
            The object.

    Raises:
        ValidationError:
            A setting is invalid, named in the message.
    """
    self._check()
    self._checked = True
    return self

prepare(context=None)

Prepare the checked object against the one thing it serves, setting its attributes that end in _.

Parameters:

Name Type Description Default
context ContextT | None

What the object serves, such as the system of a compiler, the problem of a solver, or the source of an analysis.

None

Returns:

Name Type Description
self Self

The prepared object.

Raises:

Type Description
NotCheckedError

The object was not checked with check.

Source code in src/skcomplex/base.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def prepare(self, context: ContextT | None = None) -> Self:
    """Prepare the checked object against the one thing it serves, setting its attributes that end in `_`.

    Args:
        context:
            What the object serves, such as the system of a compiler, the problem of a solver, or the source of an
            analysis.

    Returns:
        self:
            The prepared object.

    Raises:
        NotCheckedError:
            The object was not checked with `check`.
    """
    self._is_checked()
    self._prepare(context)
    self._prepared = True
    return self

run(inputs=None, final=False)

Perform the one action of the checked object on its one input, keeping its results in attributes.

Parameters:

Name Type Description Default
inputs InputT | None

The input of the action, such as the state a solver advances or the values a law reads.

None
final bool

Whether this is the last call, after which the object releases what preparing acquired.

False

Returns:

Name Type Description
self Self

The object, with the results of the action in its attributes that end in _.

Raises:

Type Description
NotCheckedError

The object was not checked with check.

NotPreparedError

The object was not prepared with prepare.

Source code in src/skcomplex/base.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def run(self, inputs: InputT | None = None, final: bool = False) -> Self:
    """Perform the one action of the checked object on its one input, keeping its results in attributes.

    Args:
        inputs:
            The input of the action, such as the state a solver advances or the values a law reads.

        final:
            Whether this is the last call, after which the object releases what preparing acquired.

    Returns:
        self:
            The object, with the results of the action in its attributes that end in `_`.

    Raises:
        NotCheckedError:
            The object was not checked with `check`.
        NotPreparedError:
            The object was not prepared with `prepare`.
    """
    self._is_checked()
    self._is_prepared()
    self._run(inputs)
    if final:
        self._release()
    return self