specbuild¶
specbuild turns plain dictionaries into Python objects. It plays nicely with cfgx but also works as a standalone helper.
Essentials¶
- Register classes and functions with
@register()for short names. - Call
buildto instantiate registered types or dotted import paths. - Opt into recursive instantiation for nested configs.
- Use
"*"for positional args. - Prefix with
"partial:"to create factories instead of calling constructors immediately. - Create extra
Registryinstances when you want isolated component sets.
Basic usage¶
-
Register components
-
Instantiate from config
-
Fallback to import paths
Feature highlights¶
Registry shortcuts¶
from specbuild import register, build
@register("Custom")
class Block: ...
build({"type": "Custom", "width": 256})
Recursive structures¶
cfg = {
"type": "Model",
"encoder": {"type": "Encoder", "channels": 64},
"layers": [
{"type": "Layer", "units": 128},
{"type": "Layer", "units": 256},
],
}
model = build(cfg)
Positional arguments¶
Use the special "*" key to pass positional arguments:
Partial factories¶
@register()
def loss_fn(pred, target, weight):
return ((pred - target) ** 2).mean() * weight
loss_fn = build({"type": "partial:loss_fn", "weight": 0.5})
loss = loss_fn(pred, target)
Custom registries¶
from specbuild import Registry, build
optim_registry = Registry()
@optim_registry.register()
class ToyOptim: ...
optim = build({"type": "ToyOptim"}, registry=optim_registry)
Pass registry=None to skip registry lookups entirely.