cliconfig.config_routines

Functions to manipulate config as dict with yaml files and CLI.

make_config(*default_config_paths, process_list=None, add_default_processing=True, fallback='', no_cli=False)

Make a config from default config(s) and CLI argument(s) with processing.

The function uses the CLI Config routines parse_cli() to parse the CLI arguments and merge them with merge_flat_paths_processing(), applying the pre-merge and post-merge processing functions on each merge.

Parameters:
  • default_config_paths (Tuple[str]) – Paths to default configs. They are merged in order and new keys are allowed.

  • process_list (Optional[List[Processing]], optional) – The list of processing to apply during each merge. None for empty list. By default None.

  • add_default_processing (bool, optional) – If add_default_processing is True, the default processings (found on DefaultProcessings) are added to the list of processings. By default True.

  • fallback (str, optional) – Path of the configuration to use if no additional config is provided with --config. No fallback config if empty string (default), in that case, the config is the default configs plus the CLI arguments.

  • no_cli (bool, optional) – If True, the CLI arguments are not parsed and the config is only built from the default_config_paths in input and the fallback argument is ignored. By default False.

Raises:

ValueError – If additional configs have new keys that are not in default configs.

Returns:

config – The nested built config. Contains the config dict (config.dict) and the processing list (config.process_list) which can be used to apply further processing routines.

Return type:

Config

Note

Setting additional arguments from CLI that are not in default configs does NOT raise an error but only a warning. This ensures the compatibility with other CLI usage (e.g notebook, argparse, etc.)

Examples

# main.py
config = make_config('data.yaml', 'model.yaml', 'train.yaml')
$ python main.py -- config [bestmodel.yaml,mydata.yaml] \
      --architecture.layers.hidden_dim=64
load_config(path, default_config_paths=None, process_list=None, *, add_default_processing=True)

Load config from a file and merge into optional default configs.

First merge the default configs together (if any), then load the config from path, apply the post-load processing, and finally merge the loaded config.

Parameters:
  • path (str) – The path to the file to load the configuration.

  • default_config_paths (Optional[List[str]], optional) – Paths to default configs. They are merged in order, new keys are allowed. Then, the loaded config is merged into the result. None for no default configs. By default None.

  • process_list (Optional[List[Processing]]) – The list of processing to apply after loading and for the merges. If None, no processing is applied. By default None.

  • add_default_processing (bool, optional) – If add_default_processing is True, the default processings (found on DefaultProcessings) are added to the list of processings. By default True.

Returns:

config – The nested loaded config. Contains the config dict (config.dict) and the processing list (config.process_list) which can be used to apply further processing routines.

Return type:

Dict[str, Any]

Note

If default configs are provided, the function does not allow new keys for the loaded config. This is for helping the user to see how to adapt the config file if the default configs have changed.

save_config(config, path)

Save a config and apply pre-save processing before saving.

Alias for save_processing().

Parameters:
  • config (Dict[str, Any]) – The config to save.

  • path (str) – The path to the yaml file to save the dict.

Return type:

None

show_config(config)

Show the config dict in a pretty way.

The config dict is automatically unflattened before printing.

Parameters:

config (Config) – The config to show.

Return type:

None

flatten_config(config)

Flatten a config.

Parameters:

config (Config) – The config to flatten.

Returns:

confg – The config containing a flattened dict.

Return type:

Config

unflatten_config(config)

Unflatten a config.

Parameters:

config (Config) – The config to unflatten.

Returns:

config – The config containing an unflattened dict.

Return type:

Config

update_config(config, other, *, allow_new_keys=False)

Update a config with a new dict or config with processing triggering.

The pre-merge, post-merge and end-build processings will be triggered. The resulting config is unflattened.

Parameters:
  • config (Config) – The config to update.

  • other (Config | dict) – The config or dict to update the config with.

  • allow_new_keys (bool, optional) – If True, allow new keys in the other config. By default False.

Returns:

config – The updated config.

Return type:

Config

copy_config(config)

Copy a config.

Parameters:

config (Config) – The config to copy.

Returns:

config – The copied config.

Return type:

Config