Class initialisation is currently done using a mixture of kwargs, calls to get_config inside __init__, as well as a variety of factory functions such as create_xxx_from_config. We can partially improve this by standardising how classes are intialised from config by adding a new classmethod similar to this:
@classmethod
def from_config(cls, config=None, logger=None, **kwargs):
"""
"""
config_name = cls.__name__ if cls._config_name is None else cls._config_name
config = get_config() if config is None else config
logger = get_logger() if logger is None else logger
instance_kwargs = config.get(config_name, {})
instance_kwargs.update(**kwargs)
logger.debug(f"Creating {cls.__name__} instance with kwargs: {instance_kwargs}")
return cls(config=config, logger=logger, **instance_kwargs)
Configured class instances can then be created like this: instance = Class.from_config()
This would allow us to remove calls to get_config() inside classes where possible (except for config items that need to be kept dynamic) as well as removing all the create_xxx_from_config functions.
Class initialisation is currently done using a mixture of kwargs, calls to
get_config
inside__init__
, as well as a variety of factory functions such ascreate_xxx_from_config
. We can partially improve this by standardising how classes are intialised from config by adding a newclassmethod
similar to this:Configured class instances can then be created like this:
instance = Class.from_config()
This would allow us to remove calls to
get_config()
inside classes where possible (except for config items that need to be kept dynamic) as well as removing all thecreate_xxx_from_config
functions.