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:
- Standard type annotations (
int,float,bool, etc.) - Default value (unannotated attributes infer types from default values)
- Field factory functions provided for creating fields with constraints
dataclasses.fieldsame as any other dataclass
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 | |
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 | |
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 | |