Skip to content

Model

Model utilities.

Model dataclass

Model(*, preset: str | None = None)

Bases: Params

Base class for reusable models with parameters.

Declare parameters as class attributes in the same form as dataclass fields with any of:

CLI arguments are parsed within run. A handler is registered to invoke run if not called manually and only one Model subclass is defined.

A presets class attribute may declare a selection of Preset objects.

Implement build to construct and return model geometry. Access parameter values as instance attributes.

Note

Subclasses are created as dataclasses automatically. Do not decorate subclasses with @dataclass.

Example
class MyModel(Model):
    width = Float(40.0, min=10, max=100)
    thickness = Float(3.0, min=1, max=10)

    presets = (Preset("small", width=15.0, thickness=2.0),)

    def build(self):
        return Box(self.width, self.width, self.thickness)

build

build() -> Model.Build

Build and return model geometry.

Info

Override this method in your subclass with your model code.

Tip

Access resolved parameter values via instance attributes (e.g. self.width would be the resolved value for a parameter called width).

Source code in bdbox/model.py
67
68
69
70
71
72
73
74
75
76
77
78
def build(self) -> Model.Build:
    """Build and return model geometry.

    Info:
        Override this method in your subclass with your model code.

    Tip:
        Access resolved parameter values via instance attributes (e.g.
        ``self.width`` would be the resolved value for a parameter called
        ``width``).
    """
    raise NotImplementedError

run classmethod

run() -> None

Parse CLI arguments, build the model, and retrieve geometry.

Calls build with the resolved parameter values and passes the result to show.

Info

Call this to build and use your model geometry.

Note

If run is not called explicitly, and a single Model subclass is defined in __main__, run will be called automatically when Python finishes.

Source code in bdbox/model.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
@classmethod
def run(cls) -> None:
    """Parse CLI arguments, build the model, and retrieve geometry.

    Calls [``build``][bdbox.model.Model.build] with the resolved
    parameter values and passes the result to
    [``show``][bdbox.geometry.show].

    Info:
        Call this to build and use your model geometry.

    Note:
        If ``run`` is not called explicitly, and a single
        [``Model``][bdbox.model.Model] subclass is defined in
        [``__main__``][__main__], ``run`` will be called automatically when
        Python finishes.
    """
    atexit.unregister(Model._atexit_handler)
    if (
        cls.__module__ == "__main__"
        and (mm := sys.modules.get(cls.__module__))
        and getattr(mm, "__file__", None)
        and Model._main_info.filename
    ):
        mm.__file__ = Model._main_info.filename
    cli_result = cls.cli_config().instance_from_cli(prog=cls.__name__)
    show(cli_result.params.build())
    cli_result.action()

with_preset classmethod

with_preset(preset: str, **overrides: Any) -> Self

Create a new instance with values from a preset applied.

Parameters:

  • preset (str) –

    Name of the preset to apply.

  • **overrides (Any, default: {} ) –

    Additional field values to apply after the preset.

Source code in bdbox/parameters/parameters.py
107
108
109
110
111
112
113
114
115
@classmethod
def with_preset(cls, preset: str, **overrides: Any) -> Self:
    """Create a new instance with values from a preset applied.

    Args:
        preset: Name of the preset to apply.
        **overrides: Additional field values to apply after the preset.
    """
    return cls(preset=preset, **overrides)