def test_merge_nested_dicts_with_empty_section(dct_class): a = dct_class(x=dct_class(one=1, two=2), y=dct_class(three=3, four=4)) b = dct_class(x=dct_class(one=1, two=2), y=dct_class()) # merge b into a assert merge_dicts(a, b) == a # merge a into b assert merge_dicts(b, a) == a
def test_merge_simple_dicts(dct_class): a = dct_class(x=1, y=2, z=3) b = dct_class(z=100, a=101) # merge b into a assert merge_dicts(a, b) == dct_class(x=1, y=2, z=100, a=101) # merge a into b assert merge_dicts(b, a) == dct_class(x=1, y=2, z=3, a=101)
def test_merge_nested_dicts_reverse_order(dct_class): a = dct_class(x=dct_class(one=1, two=2), y=dct_class(three=3, four=4), z=0) b = dct_class(x=dct_class(one=1, two=20), y=dct_class(four=40, five=5)) # merge b into a assert merge_dicts(a, b) == dct_class( x=dct_class(one=1, two=20), y=dct_class(three=3, four=40, five=5), z=0 ) assert merge_dicts(b, a) == dct_class( x=dct_class(one=1, two=2), y=dct_class(three=3, four=4, five=5), z=0 )
def load_configuration( path: str, user_config_path: str = None, env_var_prefix: str = None ) -> Config: """ Loads a configuration from a known location. Args: - path (str): the path to the TOML configuration file - user_config_path (str): an optional path to a user config file. If a user config is provided, it will be used to update the main config prior to interpolation - env_var_prefix (str): any env vars matching this prefix will be used to create configuration values Returns: - Config """ # load default config default_config = load_toml(path) # load user config if user_config_path and os.path.isfile(str(interpolate_env_vars(user_config_path))): user_config = load_toml(user_config_path) # merge user config into default config default_config = cast( dict, collections.merge_dicts(default_config, user_config) ) # interpolate after user config has already been merged config = interpolate_config(default_config, env_var_prefix=env_var_prefix) config = process_task_defaults(config) validate_config(config) return config
def load_configuration( config_path: str, env_var_prefix: str = None, merge_into_config: Config = None, env: dict = None, ) -> Config: """ Given a `config_path` with a toml configuration file, returns a Config object. Args: - config_path (str): the path to the toml configuration file - env_var_prefix (str): if provided, environment variables starting with this prefix will be added as configuration settings. - merge_into_config (Config): if provided, the configuration loaded from `config_path` will be merged into a copy of this configuration file. The merged Config is returned. """ # load default config config = load_config_file(config_path, env_var_prefix=env_var_prefix or "", env=env) if merge_into_config is not None: config = collections.merge_dicts(merge_into_config, config) validate_config(config) return config
def __init__(self, *args: Any, **kwargs: Any) -> None: init = {} # Initialize with config context init.update(config.get("context", {})) # Overwrite with explicit args init.update(dict(*args, **kwargs)) # Merge in config (with explicit args overwriting) init["config"] = merge_dicts(config, init.get("config", {})) super().__init__(init)
def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) if "context" in config: self.update(config.context) if "config" in self: new_config = merge_dicts(config, self["config"]) # order matters self["config"] = as_nested_dict(new_config, dct_class=Config) else: self["config"] = config
def __call__(self, *args: MutableMapping, **kwargs: Any) -> Iterator["Context"]: """ A context manager for setting / resetting the Prefect context Example: import prefect.context with prefect.context(dict(a=1, b=2), c=3): print(prefect.context.a) # 1 """ previous_context = self.copy() try: new_context = dict(*args, **kwargs) if "config" in new_context: new_context["config"] = merge_dicts(self.get("config", {}), new_context["config"]) self.update(new_context) # type: ignore yield self finally: self.clear() self.update(previous_context)
def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) if "context" in config: self.update(config.context) self["config"] = merge_dicts(config, self.get("config", {})) # order matters