dmlcloud.factory_from_cfg
- dmlcloud.factory_from_cfg(config, *args, **kwargs)
Creates a factory function from a configuration dictionary or a string.
If a string is provided, it is assumed to be the path to the factory function (or class).
If a dictionary is provided, it must contain a “factory” key with the path to the factory function. Additional keys in the dictionary are passed as keyword arguments to the factory function.
- Parameters:
config (Mapping | str) – Configuration dictionary or string with the path to the factory function.
*args – Additional positional arguments to pass to the factory function.
**kwargs – Additional keyword arguments to pass to the factory function.
- Returns:
A factory function with the provided configuration and arguments.
- Return type:
Callable
- Raises:
ImportError – If the factory function cannot be imported.
KeyError – If the configuration dictionary does not contain the “factory” key.
Example
>>> factory = dml.factory_from_cfg('datetime.date', 2025, month=1, day=1) >>> factory <class 'datetime.date'> >>> factory() datetime.date(2025, 1, 1) >>> factory(month=12, day=31) datetime.date(2025, 12, 31)
Instead of providing a string, you can also use a configuration dictionary:
>>> config = {'factory': 'datetime.date', 'year': 2025, 'month': 1, 'day': 1} >>> factory = dml.factory_from_cfg(config) >>> factory() datetime.date(2025, 1, 1)