Model#
mmodel model building favors composition over inheritance.
The Model
class takes three components: graph, handler, and
modifiers. A handler handles the graph calculation and data
flow, and a modifier is a wrapper that applies simple modifications
to the callable. The handler class is passed to the “handler” argument,
and if the handler requires additional arguments, the keyword arguments
can also be passed to the Model
class. The modifier argument
accepts a list of modifier decorators. If the decorators have
arguments, the argument should be applied first.
The resulting Model
instance is a callable that behaves like a function.
The Model
class also provides parameter checking and default value
filling functionality at the __call__
level. Note that handlers
and modifiers require keyword inputs.
model
module#
- class mmodel.model.Model(name, graph, handler, handler_kwargs: dict | None = None, modifiers: list | None = None, returns: list | None = None, defaults: dict | None = None, doc: str = '', **kwargs)[source]#
Bases:
object
Create the model callable.
- Parameters:
name (str) – Model name
graph (object) – Graph instance (digraph)
handler (class) – Handler class that handles model execution and the keyword arguments.
handler_kwargs (dict) – keyword arguments for the handler class.
modifiers (list) – modifiers used for the whole graph model executable.
returns (list) – The order of model returns; defaults to the topological search.
doc (str) – model docstring
defaults (dict) – default values for the model signature.
kw_only (bool) – whether to convert signature to keyword-only signature
- property defaults#
Shallow copy of the defaults.
- edit(**kwargs)[source]#
Edit components of the model to create a new model.
Editing the graph component of the model is not recommended. Although it does create a new model, “edit” is for building a new model with the same graph.
- property graph#
The graph attribute outputs a copy of the graph.
- property handler_kwargs#
Shallow copy of the handler arguments.
- property modifiers#
Shallow copy of the modifiers.
- property order#
The order of the node execution.
- property returns#
Shallow copy of the returns.
- property signature#
Model signature for inspection.
- visualize(outfile=None)[source]#
Draw the graph of the model.
Draws the default styled graph.
- Parameters:
style (str) – there are three styles, plain, short, and verbose. Plain shows nodes only, short shows part of the metadata, and long shows all the metadata.
export (str) – filename to save the graph as. The file extension is needed.