Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding from_config as a classmethod in PanBase #1142

Open
danjampro opened this issue Aug 4, 2021 · 0 comments
Open

Consider adding from_config as a classmethod in PanBase #1142

danjampro opened this issue Aug 4, 2021 · 0 comments

Comments

@danjampro
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant