NicolaDonelli / py4ai-core

MIT License
0 stars 0 forks source link

Improve BasicConfig.update method #5

Closed NicolaDonelli closed 2 years ago

NicolaDonelli commented 2 years ago

I'd change the current implementation of py4ai.core.config.BaseConfig.update method from:

    def update(self, my_dict: dict) -> "BaseConfig":

        meta = union(
            self.config.meta,
            {
                "updated_params": my_dict,
                "modification_datetime": datetime.now().astimezone(
                    tz=pytz.timezone("Europe/Rome")
                ),
            },
        )
        return type(self)(Configuration(union(dict(self.config), my_dict), meta))

to

    def update(self, other: Union[dict, Configuration]):
        if isinstance(other, dict):
            config = Configuration(union(self.config._dict, other), self.config.meta, self.config.meta["load_remote"])
            return type(self)(config)
        elif isinstance(other, Configuration):
            return type(self)(self.config.update(other))
        else:
            raise ValueError(f"Type {type(other)} cannot be merged to Configuration")

which is more general and better typed