mini.builder¶
mini.builder turns plain dictionaries into Python objects. It plays nicely with mini.config 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 mini.builder 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 mini.builder 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.
Example with mini.config¶
from mini.builder import build
from mini.config import load
cfg = load("configs/model.py")
model = build(cfg["model"])
optimizer = build(cfg["optimizer"], params=model.parameters())
API reference ¶
| CLASS | DESCRIPTION |
|---|---|
Registry |
Lightweight registry for named constructors. |
| FUNCTION | DESCRIPTION |
|---|---|
build |
Build an object from a configuration mapping. |
register |
Decorator that registers a class or function in the given registry (default: global). |
Registry ¶
build ¶
build(cfg: Union[Dict, list, tuple, Any], registry: Registry | None = REGISTRY, recursive: bool = True) -> Any
Build an object from a configuration mapping.
Looks up cfg["type"] in the provided registry (or treats it as a dotted
import path), forwards keyword arguments, and recursively instantiates nested
dicts/lists when recursive is True. Use the special "*" key for positional
arguments and prefix "partial:" on the type value to return a factory
instead of calling the constructor immediately.
register ¶
Decorator that registers a class or function in the given registry (default: global).