laser.measles.components.utils
laser.measles.components.utils
Component utilities for the laser-measles package.
This module provides utilities for creating and managing components in the laser-measles package. The main feature is a decorator that makes it easier to create components with parameters.
laser.measles.components.utils.component(cls=None, **default_params)
Decorator for creating components with default parameters.
This decorator makes it easier to create components with parameters by: 1. Allowing default parameters to be specified at class definition time 2. Creating a factory function that can be used to create component instances 3. Preserving type hints and docstrings
Parameters
cls : Type[BaseComponent], optional The component class to decorate. If None, returns a decorator function. **default_params Default parameters to use when creating the component instance.
Returns
Union[Type[BaseComponent], Callable] If cls is provided, returns a factory function for creating component instances. If cls is None, returns a decorator function.
Examples
Basic usage:
@component ... class MyComponent(BaseComponent): ... def init(self, model, verbose=False, param1=1, param2=2): ... super().init(model, verbose) ... self.param1 = param1 ... self.param2 = param2
With default parameters:
@component(param1=10, param2=20) ... class MyComponent(BaseComponent): ... def init(self, model, verbose=False, param1=1, param2=2): ... super().init(model, verbose) ... self.param1 = param1 ... self.param2 = param2
Using the factory:
Create with default parameters
MyComponent.create(model)
Create with custom parameters
MyComponent.create(model, param1=100, param2=200)
laser.measles.components.utils.create_component(component_class, params=None)
Helper function to create a component instance with parameters.
This function creates a callable object that will instantiate the component with the given parameters when called by the model.
Parameters
component_class : Type[BaseComponent] The component class to instantiate **kwargs Parameters to pass to the component constructor
Returns
Callable[[Any, Any], BaseComponent] A function that creates the component instance when called by the model
Examples
model.components = [ ... create_component(MyComponent, params=MyComponentParams), ... AnotherComponent, ... ]