Module¶
module
¶
Base class for defining flepimop2 modules.
ModuleBase
¶
Bases: BaseModel
Base class for all flepimop2 modules.
Combines configuration parsing via pydantic with the module-naming
conventions required by the flepimop2 loader. Every concrete module
class must end up with a non-empty module string - either declared
explicitly as a Literal[...] field or resolved from the
module="..." class-keyword shortcut.
Attributes:
| Name | Type | Description |
|---|---|---|
module_namespace |
str | None
|
The |
module |
str
|
The fully-qualified module name. Concrete subclasses should
specialize this to a |
options |
dict[str, Any] | None
|
Optional grab-bag of extra information the module exposes for
|
__init_subclass__(**kwargs)
¶
Process module= and module_namespace= class-keyword arguments.
Only namespace resolution and validation happen here. The actual
Pydantic field specialization for module is deferred to
__pydantic_init_subclass__ so that it runs after Pydantic has
finished building the model and all field defaults are in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional keyword arguments passed to parent classes. |
{}
|
Source code in src/flepimop2/module.py
__pydantic_init_subclass__(**kwargs)
classmethod
¶
Finalize the module field specialization after Pydantic builds the model.
This hook runs after Pydantic has completed model construction, so all field defaults inherited from parent classes are stable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional keyword arguments passed to parent classes. |
{}
|
Source code in src/flepimop2/module.py
from_shorthand(shorthand)
classmethod
¶
Build an instance from shorthand configuration text.
Concrete modules may override this optional hook to support
configuration values shaped like module_name(...). The provided
shorthand is the text inside the parentheses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shorthand
|
str
|
The text contained within the shorthand parentheses. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the module does not support shorthand syntax. |
Source code in src/flepimop2/module.py
option(name, default=RaiseOnMissing)
¶
Retrieve an option value by name, with an optional default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the option to retrieve. |
required |
default
|
Any
|
The default value to return if the option is not found.
Omitting this argument causes a |
RaiseOnMissing
|
Returns:
| Type | Description |
|---|---|
Any
|
The value of the option if found, otherwise the default value. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the option is missing and |
Examples:
>>> from flepimop2.module import ModuleBase
>>> class MyModule(ModuleBase, module="flepimop2.test.mymodule"):
... pass
>>> mod = MyModule.model_validate({"options": {"option1": 42}})
>>> mod.option("option1")
42
>>> mod.option("option2", default="default_value")
'default_value'
>>> mod.option("option2")
Traceback (most recent call last):
...
KeyError: "Option 'option2' not found in module 'flepimop2.test.mymodule'."
>>> class MyModuleWithMissingOption(
... ModuleBase, module="flepimop2.test.noopts"
... ):
... pass
>>> mod = MyModuleWithMissingOption()
>>> mod.option("option1", default="default_value")
'default_value'
>>> mod.option("option1")
Traceback (most recent call last):
...
KeyError: "Option 'option1' not found in module 'flepimop2.test.noopts'."
Source code in src/flepimop2/module.py
patch(other, *, conflict)
¶
Patch this module configuration with another module configuration.
This method treats other as the incoming patch. The default
implementation is intentionally simple: replace wholesale for replace,
and deep-merge dumped model dictionaries for merge.
Module developers can override this method to implement more complex patching
logic if needed (e.g. merging certain subsections or sets of fields while
replacing others). However, they should still try to respect the semantics of
the conflict argument as much as possible to avoid surprising users.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Self
|
The patch to apply to this module. |
required |
conflict
|
Literal[MERGE, REPLACE]
|
How to handle overlapping fields. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The patched module configuration. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in src/flepimop2/module.py
to_yaml_data()
¶
Convert the module configuration into YAML-ready Python objects.
Subclasses can override this to customize just their serialized configuration block without changing patch semantics.
Returns:
| Type | Description |
|---|---|
object
|
A YAML-ready representation of the module configuration. |