CLI Config - Build your python configurations with flexibility and simplicity.

CLI-Config-logo

CLI Config is a lightweight library that provides routines to merge nested configs and set parameters from command line. It contains many routines to create and manipulate the config as flatten or nested python dictionaries. It also provides processing functions that can change the whole configuration before and after each config manipulation.

The package was initially designed for machine learning experiments where the number of parameters is huge and a lot of them have to be set by the user between each experiment. If your project matches this description, this package is for you!

Documentation

Pypi package

Source code (Github)

Release PythonVersion License

Ruff_logo Black_logo

Ruff Flake8 Pydocstyle MyPy PyLint

Tests Coverage Documentation Status

Official badge : Config

Installation

In a new virtual environment, simply install the package via pypi:

pip install cliconfig

This package is OS independent and supported on Linux, macOS and Windows.

Quick start

Create yaml file(s) that contain your default configuration. All the parameters should be listed (see later to organize them simply in case of big config files).

# default1.yaml
param1: 1
param2: 1
letters:
  letter1: a
  letter2: b

# default2.yaml
param1: 1
param2: 2  # will override param2 from default1.yaml
letters.letter3: c  # add a new parameter

Get your config in your python code:

# main.py
from cliconfig import make_config

config = make_config('default1.yaml', 'default2.yaml')

Add additional config file(s) that represent your experiments. They will override the default values.

# first.yaml
letters:
  letter3: C  # equivalent to "letters.letter3: 'C'"

# second.yaml
param1: -1
letters.letter1: A

Please note that new parameters that are not present in the default configs are not allowed. This restriction is in place to prevent potential typos in the config files from going unnoticed. It also enhances the readability of the default config files and ensures retro-compatibility (see later to circumnavigate it for particular cases). This restriction apart, the package allows a complete liberty of config manipulation.

Run your code with the additional config files AND eventually some other parameters from command line. Please respect the exact syntax for spaces and equal signs.

python main.py --config first.yaml,second.yaml --param2=-2 --letters.letter2='B'

If you have multiple config files it is possible to pass a list with brackets. Be careful, using --config=first.yaml will NOT be recognized as an additional config file (space is important) but as a parameter called "config" with value "first.yaml" (it then raises an error if no "config" parameter is on the default config).

Now the config look like this:

Config:
    param1: -1  # overridden by second.yaml
    param2: -2  # overridden by command line args
    letters:
        letter1: A  # overridden by second.yaml
        letter2: B  # overridden by command line args
        letter3: C  # overridden by first.yaml

You can also manipulate your config with the following functions:

from cliconfig import load_config, save_config, show_config, update_config
show_config(config)  # print config
config.dict  # config as native dict
config.dict['letters']['letter1']  # access parameter via dict
config.letters.letter1  # access parameter via dots
config.letters.letter1 = 'G'  # modify parameter
del config.letters.letter1  # delete parameter
# Update config with a dict or another config
config = update_config(config, {'letters': {'letter1': 'H'}})
# Save the config as a yaml file
save_config(config, 'myconfig.yaml')
# Load the config and merge with the default configs if provided
# (useful if default configs were updated)
config = load_config('myconfig.yaml', default_config_paths=['default1.yaml', 'default2.yaml'])

The config object is just a wrapper around the config dict that allows to access the parameters via dots (and containing the list of processings, see the Processing section for details). That's all! Therefore, the config object is very light and simple to use.

While the config object is simple, the possibilities to manipulate the config via processings are endless. See the next section for some default features. One of the core idea of this package is to make it easy to add your own config features for your specific needs.

Use tags

By default, the package provides some "tags" represented as strings that start with '@' and are placed at the end of a key containing a parameter. These tags change the way the configuration is processed.

The default tags include:

  • @merge_add, @merge_before, and @merge_after: These tags merge the dictionary loaded from the specified value (which should be a YAML path) into the current configuration. @merge_add allows only the merging of new keys and is useful for splitting non-overlapping sub-configurations into multiple files. @merge_before merges the current dictionary onto the loaded one, while @merge_after merges the loaded dictionary onto the current one. These tags are used to organize multiple config files.
  • @copy: This tag copies a parameter value from another parameter name. The value associated to the parameter with this tag should be a string that represents the flattened key. The copied value is then protected from further updates but will be dynamically updated if the copied key change during a merge.
  • @def: This tag evaluate an expression to define the parameter value. The value associated to a parameter tagged with @def can contain any parameter name of the configuration. The most useful operators and built-in functions are supported, the random and math packages are also supported as well as some (safe) numpy, jax, tensorflow, torch functions. If/else statements and comprehension lists are also supported.
  • @type:<my type>: This tag checks if the key matches the specified type <my type> after each update, even if the tag is no longer present. It tries to convert the type if it is not the good one. It supports basic types as well as unions (using either "Union" or "|"), optional values, nested list/set/tuple/dict. For instance: my_param@type:List[Dict[str, int|float]]: [{"a": 0}].
  • @select: This tag select param/sub-config(s) to keep and delete the other param/sub-configs in the same parent config. The tagged key is not deleted if it is in the parent config.
  • @delete: This tag deletes the param/sub-config from the config before merging. It is usefull to trigger a processing without keeping the key in the config.
  • @new: This tag allows adding new key(s) to the config that are not already present in the default config(s). It can be used for single parameter or a sub-config. Disclaimer: it is preferable to have exhaustive default config(s) instead of abusing this tag for readability and for security concerning typos.
  • @dict: This tag allows to have a dictionary object instead of a sub-config where you can modify the keys (see the Edge cases section)

The tags are applied in a particular order that ensure no conflict between them.

Please note that the tags serve as triggers for internal processing and will be automatically removed from the key before you can use it. The tags are designed to give instructions to python without being visible in the config.

It is also possible to combine multiple tags. For example:

# main.yaml
path_1@merge_add: sub1.yaml
path_2@merge_add: sub2.yaml
config3.selection@delete@select: config3.param1

# sub1.yaml
config1:
  param@copy@type:int: config2.param
  param2@type:float: 1  # int: wrong type -> converted to float

# sub2.yaml
config2.param: 2
config3:
  param1@def: "[(config1.param2 + config2.param) / 2] * 2 if config2.param else None"
  param2: 3
my_dict@dict:
  key1: 1
  key2: 2

Note that can also use YAML tags separated with "@" (like key: !tag@tag2 value) to add tags instead of putting them in the parameter name (like key@tag@tag2: value).

Here main.yaml is interpreted like:

path_1: sub1.yaml
path_2: sub2.yaml
config1:
  param: 2  # the value of config2.param
  param2: 1.0  # converted to float
config2:
  param: 2
config3:
  param1: [1.5, 1.5]
  # param2 is deleted because it is not in the selection
my_dict: {key1: 1, key2: 2}  # (changing the whole dict further is allowed)

Then, all the parameters in config1 have enforced types, changing config2.param will also update config1.param accordingly (which is protected by direct update). Finally, changing config1.param2 or config2.param will update config3.param1 accordingly until a new value is set for config3.param1.

These side effects are not visible in the config but stored on processing objects. They are objects that find the tags, remove them from config and apply a modification. These processing are powerful tools that can be used to highly customize the configuration at each step of the process.

You can easily create your own processing (associated to a tag or not). The way to do it and a further explanation of them is available in the Processing section of the documentation.

Edge cases

  • YAML does not recognize "None" as a None object, but interprets it as a string. If you wish to set a None object, you can use "null" or "Null" instead.

  • Please note that YAML does not natively support tuples and sets, and therefore they cannot be used directly in YAML files. However, you can use either cliconfig type conversion (with @type:<tuple/set> followed by a list) or cliconfig definition (with @def followed by a string) to define a set or a tuple. Example:

# config.yaml
my_tuple@type:tuple: [1, 2, 3]
my_tuple2@def: "(1, 2, 3)"
my_set@type:set: [1, 2, 3]
my_set2@def: "{1, 2, 3}"

Note that with @def you can also create lists, sets and dicts by comprehension.

  • In the context of this package, dictionaries are treated as sub-configurations, which means that modifying or adding keys directly in additional configs may not be possible (because only default configurations allow adding new keys). If you need to have a dictionary object where you want to modify the keys, consider using the @dict tag:

For instance:

# default.yaml
logging:
  metrics: [train loss, val loss]
  styles@dict: {train_loss: red, val_loss: blue}
# additional.yaml
logging:
  metrics: [train loss, val acc]
  styles@dict: {train_loss: red, val_acc: cyan}

This will not raises an error with the tag @dict.

The dictionary can be accessed with the dot notation like this: config.logging.styles.val_acc like a sub-config (and will return "cyan" here).

  • "@" is a special character used by the package to identify tags. You can't use it in your parameters names if there are not intended to be tags (but you can use it in your values). It will raise an error if you try to do so.

  • "dict" and "process_list" are reserved names of config attributes and should not be used as sub-configs or parameters names. If you try to do so, you will not able to access them via dots (config.<something>).

Processing

Processings are powerful tools to modify the config at each step of the lifecycle of a configuration. More precisely, you can use processings to modify the full configuration before and after each merge, after loading, and before saving the config.

The processings are applied via a processing object that have five methods (called "processing" to simplify): premerge, postmerge, endbuild, postload and presave. These names correspond to the timing they are applied. Each processing has the signature:

def premerge(self, flat_config: Config) -> Config:
    ...
    return flat_config

Where Config is a simple class containing only two attributes (and no methods): dict that is the configuration dict and process_list, the list of processing objects (we discuss this in a section below). Note that it is also the class of the object returned by the make_config function.

They only take a flat config as input i.e a config containing a dict of depth 1 with dot-separated keys and return the modified flat dict (and keep it flat!).

In this section, you will learn how they work and how to create your own to make whatever you want with the config.

Why a flat dict?

The idea is that when we construct a config, we manipulate dictionaries that contain both nested sub-dictionaries and flat keys simultaneously. To simplify this process, the dictionaries are systematically flattened before merging. This approach makes things simpler and prevents duplicated keys within the same configuration, as shown in the example:

config = {'a': {'b': 1}, 'a.b': 2}

More generally, all config modifications are performed using flat dictionaries during config construction, and the same applies to processings. For processings, it is even more interesting as you can have access to the full sub-config names to make your processing if needed.

However, it's important to note that after building your config with make_config, the dict will be unflattened to its normal nested configuration structure.

Processing order

The order in which the processings are triggered is crucial because they modify the config and consequently affect the behavior of subsequent processings. To manage this order, the processing class have five float attributes representing the order of the five processing methods: premerge, postmerge, endbuild, postload, and presave.

Here's a basic example to illustrate the significance of the order:

# config1.yaml
merge@merge_add@delete: config2.yaml
param: 1
# config2.yaml
param2: 2

In this example, we want to build a global config using config1.yaml. This file contains only half of the parameters, and the other half is in config2.yaml. Then, we add a key with the name of our choice, here "merge", tagged with @merge_add to merge config2.yaml before the global config update. We add the @delete tag to delete the key "merge" before merging with the global config because in this case, there is no key with the name "merge" in the global config, and it would raise an error as it is not possible to ass new keys.

@merge_add and @delete has both only a pre-merge effect. Let's check the orders. It is -20.0 for merge and 30.0 for delete. So merge trigger first, add param2 and the "merge" key is deleted after it. If the orders were reversed, the key would have been deleted before merge processing and so the param2 would not have been updated with the value of 2 and the resulting configuration would potentially not have been the expected one at all.

Therefore, it is crucial to carefully manage the order when creating your own processings!

Some useful ranges to choose your order:

  • not important: order = 0 (default)
  • if it checks/modifies the config before applying any processing: order < -25
  • if it adds new parameters: -25 < order < -5
  • if it updates a value based on itself: -5 < order < 5
  • if it updates a value based on other keys: 5 < order < 15
  • if it checks a property on a value: 15 < order < 25
  • if it deletes other key(s) but you want to trigger the tags before: 25 < order < 35
  • final check/modification after all processings: order > 35

Note: a pre-merge order should not be greater than 1000, the order of the default processing ProcessCheckTags that raise an error if tags still exist at the end of the pre-merge step.

Create basic processing

Processing that modify a single value

One of the most useful kind of processing look for parameters which names match a certain pattern (e.g a prefix or a suffix) or contain a specific tag and modify their values depending on their current ones.

To simplify the creation of such a process, we provide the create_processing_value function. This function allows you to quickly create a processing that matches a regular expression or a specific tag name (in which case the tag is removed after pre-merging). You specify the function to be applied on the value to modify it, and optionally, the order of the processing. Additionally, there is a persistent argument, which is a boolean value indicating whether encountering the tag (if a tag is used) once in a parameter name will continue to trigger the processing for this parameter even after the tag is removed. By default, it is False. Finally, you can set the processing type (pre-merge, post-merge, etc.) at your convenience. Default is pre-merge.

Here's an example to illustrate:

proc = create_processing_value(lambda x: str(x), 'premerge', tag_name='convert_str', persistent=True)
config = make_config(default_config, process_list=[proc])

In this example, the config {"subconfig.param@convert_str": 1} will be converted to {"subconfig.param": "1"}. Moreover, the keys subconfig.param will be permanently converted to strings before every merge.

It's worth noting that you can also use functions that have side effects without necessarily changing the value itself. For example, you can use a function to check if a certain condition is met by the value.

It is also possible to pass the flat config as a second argument to the function. For example:

# config.yaml
param: 1
param2@eval: "config.param + 1"
proc = create_processing_value(
    lambda x, config: eval(x, {"config": config}),
    tag_name="eval",
    persistent=False,
)
# (Note that the `eval` function is not safe and the code above
# should not be used in case of untrusted config)

Here the value of param2 will be evaluated to 2 at pre-merge step.

Pre-merge/post-merge processing that protect a property from being modified

Another useful kind of processing is a processing that ensure to keep a certain property on the value. For this kind of processing, you can use create_processing_keep_property. It takes a function that returns the property from the value, the regex or the tag name like the previous function, and the order of the pre-merge and the post-merge.

The pre-merge processing looks for keys that match the tag or the regex, apply the function on the value and store the result (= the "property"). The post-merge and end-build processing will check that the property is the same as the one stored during pre-merge. If not, it will raise an error.

Examples:

A processing that enforce the types of all the parameters to be constant (equal to the type of the first value encountered):

create_processing_keep_property(
    type,
    regex=".*",
    premerge_order=15.0,
    postmerge_order=15.0,
    endbuild_order=15.0
)

A processing that protect parameters tagged with @protect from being changed:

create_processing_keep_property(
    lambda x: x,
    tag_name="protect",
    premerge_order=15.0,
    postmerge_order=15.0,
    endbuild_order=15.0
)

Each time you choose the order 15.0 because it is a good value for processing that made checks on the values. Indeed, processings that change the values such as ProcessCopy have an order that is generally $\leq$ 10.0.

It is also possible to pass the flat config as a second argument to the function similarly to create_processing_value.

Create your processing classes (Advanced)

To create your own processing classes and unlock more possibilities, you simply need to overload the methods of the Processing class to modify the config at the desired timings. To do so, you often need to manipulate tags.

Manipulate the tags

Tags are useful for triggering a processing, as we have seen. However, we need to be cautious because tagging a key modifies its name and can lead to conflicts when using processing. To address this issue, we provide tag routines in cliconfig.tag_routines. These routines include:

  • is_tag_in: Checks if a tag is in a key. It looks for the exact tag name. If full_key is True, it looks for all the flat key, including sub-configs (default: False)
  • clean_tag: Removes a specific tag (based on its exact name) from a key. It is helpful to remove the tag after pre-merging.
  • clean_all_tags: Removes all tags from a key. This is helpful each time you need the true parameter name.
  • clean_dict_tags: Removes all tags from a dictionary and returns the cleaned dict along with a list of keys that contained tags. This is helpful to get all the parameter names of a full dict with tags.

With these tools, we can write a processing, for example, that searches for all parameters with a tag @look ant that prints their sorted values at the end of the post-merging.

class ProcessPrintSorted(Processing):
    """Print the parameters tagged with "@look", sorted by value on post-merge."""

    def __init__(self) -> None:
        super().__init__()
        self.looked_keys: Set[str] = set()
        # Pre-merge just look for the tag so order is not important
        self.premerge_order = 0.0
        # Post-merge should be after the copy processing if we want the final values
        # on post-merge
        self.postmerge_order = 15.0

    def premerge(self, flat_config: Config) -> Config:
        """Pre-merge processing."""
        # Browse a freeze version of the dict (because we will modify it to remove tags)
        items = list(flat_config.dict.items())
        for flat_key, value in items:
            if is_tag_in(flat_key, "look"):  # Check if the key contains the tag
                # Remove the tag and update the dict
                new_key = clean_tag(flat_key, "look")
                flat_config.dict[new_key] = value
                del flat_config.dict[flat_key]
                # Store the key
                clean_key = clean_all_tags(key)  # remove all tags = true parameter name
                self.looked_keys.add(clean_key)
        return flat_config

    def postmerge(self, flat_config: Config) -> Config:
        """Post-merge processing."""
        values = []
        for key in self.looked_keys:
            # IMPORTANT
            # ("if key in flat_config.dict:" is important in case of some keys were
            # removed or if multiple dicts with different parameters are seen by
            # the processing)
            if key in flat_config.dict:
                values.append(flat_config.dict[key])
        print("The sorted looked values are: ", sorted(values))
        # If we don't want to keep the looked keys for further print:
        self.looked_keys = set()

        return flat_config

# And to use it:
config = make_config("main.yaml", process_list=[ProcessPrintSorted()])

Important note: After all pre-merge processings, the config should no longer contains tags as they should be removed by pre-merge processings. Otherwise, a security processing raises an error. It is then not necessary to take care on tags on post-merge, and pre-save.

Merge, save or load configs in processing

The key concept is that as long as we deal with processings, the elementary operations on the config are not actually to merge, save, and load a config, but rather:

  • Applying pre-merge processing, then merging, then applying post-merge processing.
  • Applying end-build processing at the end of the config building.
  • Applying pres-ave processing and then saving a config.
  • Loading a config and then applying post-load processing.

These three operations are in cliconfig.process_routines and called merge_processing, end_build_processing, save_processing, and load_processing, respectively. They take as input a Config object that contains as we see the list of processing.

Now, the trick is that sometimes we want to apply these operations to the processing themselves, particularly when we want to modify a part of the configuration instead of just a single parameter (such as merging two configurations). This is why it is particularly useful to have access to the full Config object and not only the dict.

For example, consider the tag @merge_add, which triggers a processing before merging and merges the config loaded from a specified path (the value) into the current config. We may want to see what happens if we merge a config that also contains a @merge_add tag within it:

# main.yaml
config_path1@merge_add: path1.yaml
# path1.yaml
param1: 1
config_path2@merge_add: path2.yaml
# path2.yaml
param2: 2

Now, let's consider we want to merge the config main.yaml with another config. During the pre-merge processing, we encounter the tag @merge_add. This tag is removed, and the config found at path1.yaml will be merged into the main.yaml config. However before this, it triggers the pre-merging.

Therefore, before the merge path1.yaml, the processing discovers the key config_path2@merge_add and merges the config found at path2.yaml into path1.yaml. Then, path1.yaml is merged into main.yaml. Finally, the resulting configuration can be interpreted as follows:

{'param1': 1, 'param2': 2, 'config_path1': 'path1.yaml', 'config_path2': 'path2.yaml'}

before being merged itself with another config. Note that is not only a processing that allows to organize the configuration on multiple files. In fact, it also allows you for instance to choose a particular configuration among several ones by setting the path as value of the tagged key (as long as this config is on the default configs).

Change processing list in processing (Still more advanced)

Note that the processing functions receive the list of processing objects as an input and update as an attribute of the processing object. This means that it is possible to manually modify this list in processing functions.

Warning: The processing list to apply during pre/post-merge, pre-save and post-load are determined before the first processing is applied. Therefore, you can't add or remove processing and expect it to be effective during the current merge/save/load. However, if you modify their internal variables it will be effective immediately.

Here an example of a processing that remove the type check of a parameter in ProcessTyping processing. It is then possible for instance to force another type (it is not possible otherwise).

from cliconfig.processing.builtin import ProcessTyping

class ProcessBypassTyping(Processing):
    """Bypass type check of ProcessTyping for parameters tagged with "@bypass_typing".

    In pre-merge it looks for a parameter with the tag "@bypass_typing",
    removes it and change the internal ProcessTyping variables to avoid
    checking the type of the parameter with ProcessTyping.
    """

    def __init__(self) -> None:
        super().__init__()
        self.bypassed_forced_types: Dict[str, tuple] = {}
        # Before ProcessTyping pre-merge to let it change the type
        self.premerge_order = 1.0

    def premerge(self, flat_config: Config) -> Config:
        """Pre-merge processing."""
        items = list(flat_config.dict.items())
        for flat_key, value in items:
            if is_tag_in(flat_key, "bypass_typing"):
                new_key = clean_tag(flat_key, "bypass_typing")
                flat_config.dict[new_key] = value
                del flat_config.dict[flat_key]
                clean_key = clean_all_tags(flat_key)
                for processing in flat_config.process_list:
                    if (isinstance(processing, ProcessTyping)
                            and clean_key in processing.forced_types):
                        forced_type = processing.forced_types.pop(clean_key)
                        self.bypassed_forced_types[clean_key] = forced_type
        return flat_config

# Without bypass:
config1 = Config({"a@type:int": 0}, [ProcessBypassTyping(), ProcessTyping()])
config2 = Config({"a@type:str": "a"}, [])
config = merge_flat_processing(config1, config2)
# > Error: try to change the forced type of "a" from int to str

# With bypass:
config1 = Config({"a@type:int": 0}, [ProcessBypassTyping(), ProcessTyping()])
config2 = Config({"a@bypass_typing@type:str": "a"}, [])
config = merge_flat_processing(config1, config2)
# > No error

Alternative ways to create a config

From a python dict

from cliconfig import Config
my_dict = {'param1': 1, 'param2': 2}
config = Config(my_dict)

You can also add built-in or custom processings:

from cliconfig import Config, create_processing_value
from cliconfig.processing.builtin import ProcessCopy
my_dict = {'param1': 1, 'param2': 2}
my_proc = create_processing_value(lambda x: x+1, "premerge", tag_name='add1')
config = Config(my_dict, [my_proc, ProcessCopy()])

From a yaml file without command line arguments (useful for notebooks)

from cliconfig import make_config
config = make_config('my_yaml_file.yaml', no_cli=True)

You can merge multiple yaml files that will be considered as default configs (new parameter names are allowed).

from cliconfig import make_config
config = make_config('config1.yaml', 'config2.yaml', no_cli=True)

You can also pass a list of processing objects like usual.

From a config (make a copy)

from cliconfig.config_routines import copy_config
config2 = copy_config(config)

From two dicts (or configs) to merge one into the other

from cliconfig import Config, update_config
new_config = update_config(Config(config1), config2)  # if config1 is a dict
new_config = update_config(config1, config2)  # if config1 is a Config

These two lines work whether the config2 is a dict or a Config. Note that the second config will override the first one.

From a list of arguments

Assuming the arguments are under the format ['--key1=value1', '--key2.key3=value2']:

from cliconfig import Config, unflatten_config
from cliconfig.cli_parser import parse_cli
my_args = ['--key1=value1', '--key2.key3=value2']
config = Config(parse_cli(my_args)[0])  # flat
config = unflatten_config(config)

From a yaml formatted string

from yaml import safe_load
from cliconfig import Config, unflatten_config
yaml_txt = """
a:
  d: [2, 3]
  b.c: {d: 4, e: 5}
"""
config = Config(safe_load(yaml_txt))  # mix flat and nested
config = unflatten_config(config)

Hyperparameter search with Weights&Biases

Making hyperparameter search easier and more effective with Weights&Biases sweeps! This example shows you how to combine them with cliconfig supporting nested configuration:

# main.py
from cliconfig.config_routines import update_config
from cliconfig.dict_routines import flatten
import wandb

def main() -> None:
    """Main function."""
    # Create a cliconfig based on CLI
    config = make_config('default.yaml')
    # Initialize wandb to create wandb.config eventually modified by sweep
    # Note that the config is flattened because wandb sweep does not support
    # nested config (yet)
    wandb.init(config=flatten(config.dict))
    # Sync the cliconfig with wandb.config
    config = update_config(config, wandb.config)
    # Now the config is eventually updated with the sweep,
    # unflattened and ready to be used

    run(config)

if __name__ == '__main__':
    main()

Now you can create your sweep configuration use wandb sweep either from CLI or from python following the wandb tutorial.

For instance with a configuration containing train and data sub-configurations:

# sweep.yaml
program: main.py
method: bayes
metric:
  name: val_loss
  goal: minimize
parameters:
    train.learning_rate:
        distribution: log_uniform_values
        min: 0.0001
        max: 0.1
    train.optimizer.name:
        values: ["adam", "sgd"]
    data.batch_size:
        values: [32, 64, 128]
$ wandb sweep sweep.yaml
sweep_id: ...
$ wandb agent <sweep_id>

This makes a bayesian search over the learning rate, the optimizer and the batch size to minimize the final validation loss.

How to contribute

For development, install the package dynamically and dev requirements with:

pip install -e .
pip install -r requirements-dev.txt

Everyone can contribute to CLI Config, and we value everyone’s contributions. Please see our contributing guidelines for more information 🤗

 1# Copyright © 2023  Valentin Goldité
 2#
 3#    This program is free software: you can redistribute it and/or modify
 4#    it under the terms of the MIT License.
 5#    This program is distributed in the hope that it will be useful,
 6#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 7#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 8#    This project is free to use for COMMERCIAL USE, MODIFICATION,
 9#    DISTRIBUTION and PRIVATE USE as long as the original license is
10#    include as well as this copy right notice.
11"""# CLI Config - Build your python configurations with flexibility and simplicity.
12
13.. include:: ../DOCUMENTATION.md
14"""
15
16from cliconfig import (
17    _logger,
18    base,
19    cli_parser,
20    config_routines,
21    dict_routines,
22    process_routines,
23    processing,
24    tag_routines,
25)
26from cliconfig._logger import create_logger
27from cliconfig._version import __version__, __version_tuple__
28from cliconfig.base import Config
29from cliconfig.config_routines import (
30    flatten_config,
31    load_config,
32    make_config,
33    save_config,
34    show_config,
35    unflatten_config,
36    update_config,
37)
38from cliconfig.dict_routines import flatten as flatten_dict
39from cliconfig.dict_routines import unflatten as unflatten_dict
40from cliconfig.process_routines import (
41    merge_flat_paths_processing,
42    merge_flat_processing,
43)
44from cliconfig.processing.base import Processing
45from cliconfig.processing.builtin import DefaultProcessings
46from cliconfig.processing.create import (
47    create_processing_keep_property,
48    create_processing_value,
49)
50
51_CLICONFIG_LOGGER = create_logger()
52
53__all__ = [
54    "__version__",
55    "__version_tuple__",
56    "_CLICONFIG_LOGGER",
57    "_logger",
58    "Config",
59    "DefaultProcessings",
60    "Processing",
61    "base",
62    "cli_parser",
63    "config_routines",
64    "create_processing_keep_property",
65    "create_processing_value",
66    "dict_routines",
67    "flatten_config",
68    "flatten_dict",
69    "make_config",
70    "load_config",
71    "merge_flat_paths_processing",
72    "merge_flat_processing",
73    "process_routines",
74    "processing",
75    "save_config",
76    "show_config",
77    "tag_routines",
78    "unflatten_config",
79    "unflatten_dict",
80    "update_config",
81]

API Documentation

class Config:
 12class Config:
 13    """Class for configuration.
 14
 15    Config object contains the config dict and the processing list
 16    and no methods except `__init__`, `__repr__`, `__eq__`,
 17    `__getattribute__`, `__setattr__` and `__delattr__`.
 18    The Config objects are mutable and not hashable.
 19
 20    Parameters
 21    ----------
 22    config_dict : Dict[str, Any]
 23        The config dict.
 24    process_list : Optional[List[Processing]], optional
 25        The list of Processing objects. If None, an empty list is used.
 26        The default is None.
 27
 28    Examples
 29    --------
 30    >>> config = Config({"a": 1, "b": {"c": 2}})
 31    >>> config.dict
 32    {"a": 1, "b": {"c": 2}}
 33    >>> config.process_list
 34    []
 35    >>> config.b
 36    Config({"c": 2}, [])
 37    >>> config.b.c
 38    2
 39    """
 40
 41    def __init__(
 42        self,
 43        config_dict: Dict[str, Any],
 44        process_list: Optional[List["Processing"]] = None,
 45    ) -> None:
 46        self.dict = config_dict
 47        self.process_list = process_list if process_list else []
 48
 49    def __dir__(self) -> List[str]:
 50        """List of attributes, sub-configurations and parameters."""
 51        return ["dict", "process_list"] + list(self.dict.keys())
 52
 53    def __repr__(self) -> str:
 54        """Representation of Config object."""
 55        process_classes = [process.__class__.__name__ for process in self.process_list]
 56        return f"Config({self.dict}, {process_classes})"
 57
 58    def __eq__(self, other: Any) -> bool:
 59        """Equality operator.
 60
 61        Two Config objects are equal if their dicts are equal and their
 62        lists of Processing objects are equal (order doesn't matter).
 63        """
 64        if (
 65            isinstance(other, Config)
 66            and self.dict == other.dict
 67            and len(self.process_list) == len(other.process_list)
 68        ):
 69            equal = True
 70            for processing in self.process_list:
 71                equal = equal and processing in other.process_list
 72            for processing in other.process_list:
 73                equal = equal and processing in self.process_list
 74            return equal
 75        return False
 76
 77    def __getattribute__(self, __name: str) -> Any:
 78        """Get attribute, sub-configuration or parameter.
 79
 80        The dict should be nested (unflattened). If it is not the case,
 81        you can apply `cliconfig.dict_routines.flatten` on `config.dict`
 82        to unflatten it.
 83        """
 84        if __name in ["dict", "process_list"]:
 85            return super().__getattribute__(__name)
 86        if __name not in self.dict:
 87            keys = ", ".join(self.dict.keys())
 88            raise AttributeError(  # pylint: disable=raise-missing-from
 89                f"Config has no attribute '{__name}'. Available keys are: {keys}."
 90            )
 91        if isinstance(self.dict[__name], dict):
 92            # If the attribute is a dict, return a Config object
 93            # so that we can access the nested keys with multiple dots
 94            return Config(self.dict[__name], process_list=self.process_list)
 95        return self.dict[__name]
 96
 97    def __setattr__(self, __name: str, value: Any) -> None:
 98        """Set attribute, sub-configuration or parameter.
 99
100        The dict should be nested (unflattened). If it is not the case,
101        you can apply `cliconfig.dict_routines.flatten` on `config.dict`
102        to unflatten it.
103        """
104        if __name in ["dict", "process_list"]:
105            super().__setattr__(__name, value)
106        else:
107            self.dict[__name] = value
108
109    def __delattr__(self, __name: str) -> None:
110        """Delete attribute, sub-configuration or parameter.
111
112        The dict should be nested (unflattened). If it is not the case,
113        you can apply `cliconfig.dict_routines.flatten` on `config.dict`
114        to unflatten it.
115        """
116        if __name in ["dict", "process_list"]:
117            super().__delattr__(__name)
118        else:
119            del self.dict[__name]

Class for configuration.

Config object contains the config dict and the processing list and no methods except __init__, __repr__, __eq__, __getattribute__, __setattr__ and __delattr__. The Config objects are mutable and not hashable.

Parameters
  • config_dict (Dict[str, Any]): The config dict.
  • process_list (Optional[List[Processing]], optional): The list of Processing objects. If None, an empty list is used. The default is None.
Examples
>>> config = Config({"a": 1, "b": {"c": 2}})
>>> config.dict
{"a": 1, "b": {"c": 2}}
>>> config.process_list
[]
>>> config.b
Config({"c": 2}, [])
>>> config.b.c
2
class DefaultProcessings:
946class DefaultProcessings:
947    """Default list of built-in processings.
948
949    To add these processings to a Config instance, use:
950    ```python
951    config.process_list += DefaultProcessings().list
952    ```
953
954    The current default processing list contains:
955     * ProcessCheckTags: protect against '@' in keys at the end of pre-merge)
956     * ProcessMerge (@merge_all, @merge_before, @merge_after): merge multiple
957       files into one.
958     * ProcessCopy (@copy): persistently copy a value from one key to an other
959       and protect it
960     * ProcessTyping (@type:X): force the type of parameter to any type X.
961     * ProcessSelect (@select): select sub-config(s) to keep and delete the
962       other sub-configs in the same parent config.
963     * ProcessDelete (@delete): delete the parameter tagged with @delete on
964       pre-merge.
965    """
966
967    def __init__(self) -> None:
968        self.list: List[Processing] = [
969            ProcessCheckTags(),
970            ProcessMerge(),
971            ProcessCopy(),
972            ProcessDef(),
973            ProcessTyping(),
974            ProcessSelect(),
975            ProcessDelete(),
976            ProcessDict(),
977            ProcessNew(),
978        ]

Default list of built-in processings.

To add these processings to a Config instance, use:

config.process_list += DefaultProcessings().list

The current default processing list contains:

  • ProcessCheckTags: protect against '@' in keys at the end of pre-merge)
  • ProcessMerge (@merge_all, @merge_before, @merge_after): merge multiple files into one.
  • ProcessCopy (@copy): persistently copy a value from one key to an other and protect it
  • ProcessTyping (@type:X): force the type of parameter to any type X.
  • ProcessSelect (@select): select sub-config(s) to keep and delete the other sub-configs in the same parent config.
  • ProcessDelete (@delete): delete the parameter tagged with @delete on pre-merge.
class Processing:
12class Processing:
13    """Processing base class.
14
15    Each processing classes contains pre-merge, post-merge, pre-save
16    and post-load processing. They are used with routines that apply
17    processing in `cliconfig.process_routines` and
18    `cliconfig.config_routines`.
19
20    That are applied in the order defined
21    by the order attribute in case of multiple processing.
22    """
23
24    def __init__(self) -> None:
25        self.premerge_order = 0.0
26        self.postmerge_order = 0.0
27        self.endbuild_order = 0.0
28        self.presave_order = 0.0
29        self.postload_order = 0.0
30
31    def premerge(self, flat_config: Config) -> Config:
32        """Pre-merge processing.
33
34        Function applied to the flat config to modify it
35        before merging. It takes a flat config and returns a flat config.
36        """
37        return flat_config
38
39    def postmerge(self, flat_config: Config) -> Config:
40        """Post-merge processing.
41
42        Function applied to the flat config to modify it
43        after merging . It takes a flat config and returns a flat config.
44        """
45        return flat_config
46
47    def endbuild(self, flat_config: Config) -> Config:
48        """End-build processing.
49
50        Function applied to the flat config to modify it at the end of
51        a building process (typically `cliconfig.config_routines.make_config`
52        or `cliconfig.config_routines.load_config`).
53        It takes a flat config and returns a flat config.
54        """
55        return flat_config
56
57    def presave(self, flat_config: Config) -> Config:
58        """Pre-save processing.
59
60        Function applied to the flat config to modify it before
61        saving. It takes a flat config and returns a flat config.
62        """
63        return flat_config
64
65    def postload(self, flat_config: Config) -> Config:
66        """Post-load processing.
67
68        Function applied to the flat config to modify it after
69        loading. It takes a flat config and returns a flat config.
70        """
71        return flat_config
72
73    def __repr__(self) -> str:
74        """Representation of Processing object."""
75        return f"{self.__class__.__name__}"
76
77    def __eq__(self, __value: object) -> bool:
78        """Equality operator.
79
80        Two processing are equal if they are the same class and add the same
81        attributes (accessed with `__dict__`).
82        """
83        equal = (
84            isinstance(__value, self.__class__) and self.__dict__ == __value.__dict__
85        )
86        return equal

Processing base class.

Each processing classes contains pre-merge, post-merge, pre-save and post-load processing. They are used with routines that apply processing in cliconfig.process_routines and cliconfig.config_routines.

That are applied in the order defined by the order attribute in case of multiple processing.

def premerge(self, flat_config: Config) -> Config:
31    def premerge(self, flat_config: Config) -> Config:
32        """Pre-merge processing.
33
34        Function applied to the flat config to modify it
35        before merging. It takes a flat config and returns a flat config.
36        """
37        return flat_config

Pre-merge processing.

Function applied to the flat config to modify it before merging. It takes a flat config and returns a flat config.

def postmerge(self, flat_config: Config) -> Config:
39    def postmerge(self, flat_config: Config) -> Config:
40        """Post-merge processing.
41
42        Function applied to the flat config to modify it
43        after merging . It takes a flat config and returns a flat config.
44        """
45        return flat_config

Post-merge processing.

Function applied to the flat config to modify it after merging . It takes a flat config and returns a flat config.

def endbuild(self, flat_config: Config) -> Config:
47    def endbuild(self, flat_config: Config) -> Config:
48        """End-build processing.
49
50        Function applied to the flat config to modify it at the end of
51        a building process (typically `cliconfig.config_routines.make_config`
52        or `cliconfig.config_routines.load_config`).
53        It takes a flat config and returns a flat config.
54        """
55        return flat_config

End-build processing.

Function applied to the flat config to modify it at the end of a building process (typically make_config or load_config). It takes a flat config and returns a flat config.

def presave(self, flat_config: Config) -> Config:
57    def presave(self, flat_config: Config) -> Config:
58        """Pre-save processing.
59
60        Function applied to the flat config to modify it before
61        saving. It takes a flat config and returns a flat config.
62        """
63        return flat_config

Pre-save processing.

Function applied to the flat config to modify it before saving. It takes a flat config and returns a flat config.

def postload(self, flat_config: Config) -> Config:
65    def postload(self, flat_config: Config) -> Config:
66        """Post-load processing.
67
68        Function applied to the flat config to modify it after
69        loading. It takes a flat config and returns a flat config.
70        """
71        return flat_config

Post-load processing.

Function applied to the flat config to modify it after loading. It takes a flat config and returns a flat config.

def create_processing_keep_property( func: Callable, regex: Optional[str] = None, tag_name: Optional[str] = None, premerge_order: float = 0.0, postmerge_order: float = 0.0, endbuild_order: float = 0.0) -> Processing:
142def create_processing_keep_property(
143    func: Callable,
144    regex: Optional[str] = None,
145    tag_name: Optional[str] = None,
146    premerge_order: float = 0.0,
147    postmerge_order: float = 0.0,
148    endbuild_order: float = 0.0,
149) -> Processing:
150    """Create a processing object that keep a property from a value using tag or regex.
151
152    The pre-merge processing looks for keys that match the tag or the regex, apply
153    the function func on the value and store the result (= the "property"):
154    `property = func(flat_dict[key])`.
155    The post-merge processing will check that the property is the same as the one
156    stored during pre-merge. If not, it will raise a ValueError.
157
158    It also possible to pass the flat config as a second argument of the function
159    `func`. In this case, the function apply
160    `property = func(flat_dict[key], flat_config)`.
161
162    Parameters
163    ----------
164    func : Callable
165        The function to apply to the value (and eventually the flat config)
166        to define the property to keep.
167        property = func(flat_dict[key]) or func(flat_dict[key], flat_config)
168    regex : Optional[str]
169        The regex to match the key.
170    tag_name : Optional[str]
171        The tag (without "@") to match the key. The values are modified when
172        triggering the pattern ".*@<tag_name>.*" and the tag is removed from the key.
173    premerge_order : float, optional
174        The pre-merge order, by default 0.0
175    postmerge_order : float, optional
176        The post-merge order, by default 0.0
177    endbuild_order : float, optional
178        The end-build order, by default 0.0
179
180    Raises
181    ------
182    ValueError
183        If both tag and regex are provided or if none of them are provided.
184
185    Returns
186    -------
187    Processing
188        The processing object with the pre-merge and post-merge methods.
189
190    Examples
191    --------
192    A processing that enforce the types of all the parameters to be constant
193    (equal to the type of the first value encountered):
194
195    ```python
196    create_processing_keep_property(
197        type,
198        regex=".*",
199        premerge_order=15.0,
200        postmerge_order=15.0,
201        endbuild_order=15.0
202    )
203    ```
204
205    A processing that protect parameters tagged with @protect from being changed:
206
207    ```python
208    create_processing_keep_property(
209        lambda x: x,
210        tag_name="protect",
211        premerge_order=15.0,
212        postmerge_order=15.0
213    )
214    ```
215    """
216    if tag_name is not None:
217        if regex is not None:
218            raise ValueError("You must provide a tag or a regex but not both.")
219    else:
220        if regex is None:
221            raise ValueError(
222                "You must provide a tag or a regex (to trigger the value update)."
223            )
224    processing = _ProcessingKeepProperty(
225        func,
226        regex=regex,
227        tag_name=tag_name,
228        premerge_order=premerge_order,
229        postmerge_order=postmerge_order,
230        endbuild_order=endbuild_order,
231    )
232    return processing

Create a processing object that keep a property from a value using tag or regex.

The pre-merge processing looks for keys that match the tag or the regex, apply the function func on the value and store the result (= the "property"): property = func(flat_dict[key]). The post-merge processing will check that the property is the same as the one stored during pre-merge. If not, it will raise a ValueError.

It also possible to pass the flat config as a second argument of the function func. In this case, the function apply property = func(flat_dict[key], flat_config).

Parameters
  • func (Callable): The function to apply to the value (and eventually the flat config) to define the property to keep. property = func(flat_dict[key]) or func(flat_dict[key], flat_config)
  • regex (Optional[str]): The regex to match the key.
  • tag_name (Optional[str]): The tag (without "@") to match the key. The values are modified when triggering the pattern ".@." and the tag is removed from the key.
  • premerge_order (float, optional): The pre-merge order, by default 0.0
  • postmerge_order (float, optional): The post-merge order, by default 0.0
  • endbuild_order (float, optional): The end-build order, by default 0.0
Raises
  • ValueError: If both tag and regex are provided or if none of them are provided.
Returns
  • Processing: The processing object with the pre-merge and post-merge methods.
Examples

A processing that enforce the types of all the parameters to be constant (equal to the type of the first value encountered):

create_processing_keep_property(
    type,
    regex=".*",
    premerge_order=15.0,
    postmerge_order=15.0,
    endbuild_order=15.0
)

A processing that protect parameters tagged with @protect from being changed:

create_processing_keep_property(
    lambda x: x,
    tag_name="protect",
    premerge_order=15.0,
    postmerge_order=15.0
)
def create_processing_value( func: Union[Callable[[Any], Any], Callable[[Any, Config], Any]], processing_type: str = 'premerge', *, regex: Optional[str] = None, tag_name: Optional[str] = None, order: float = 0.0, persistent: bool = False) -> Processing:
 15def create_processing_value(
 16    func: Union[Callable[[Any], Any], Callable[[Any, Config], Any]],
 17    processing_type: str = "premerge",
 18    *,
 19    regex: Optional[str] = None,
 20    tag_name: Optional[str] = None,
 21    order: float = 0.0,
 22    persistent: bool = False,
 23) -> Processing:
 24    r"""Create a processing object that modifies a value in config using tag or regex.
 25
 26    The processing is applied on pre-merge. It triggers when the key matches
 27    the tag or the regex. The function apply `flat_dict[key] = func(flat_dict[key])`.
 28    You must only provide one of tag or regex. If tag is provided, the tag will be
 29    removed from the key during pre-merge.
 30
 31    It also possible to pass the flat config as a second argument of the function
 32    `func`. In this case, the function apply
 33    `flat_dict[key] = func(flat_dict[key], flat_config)`.
 34
 35    Parameters
 36    ----------
 37    func : Callable
 38        The function to apply to the value (and eventually the flat config)
 39        to make the new value so that:
 40        flat_dict[key] = func(flat_dict[key]) or func(flat_dict[key], flat_config)
 41    processing_type : str, optional
 42        One of "premerge", "postmerge", "presave", "postload" or "endbuild".
 43        Timing to apply the value update. In all cases the tag is removed on pre-merge.
 44        By default "premerge".
 45    regex : Optional[str]
 46        The regex to match the key.
 47    tag_name : Optional[str]
 48        The tag (without "@") to match the key. The tag is removed on pre-merge.
 49    order : int, optional
 50        The pre-merge order. By default 0.0.
 51    persistent : bool, optional
 52        If True, the processing will be applied on all keys that have already
 53        matched the tag before. By nature, using regex make the processing
 54        always persistent. By default, False.
 55
 56    Raises
 57    ------
 58    ValueError
 59        If both tag and regex are provided or if none of them are provided.
 60    ValueError
 61        If the processing type is not one of "premerge", "postmerge", "presave",
 62        "postload" or "endbuild".
 63
 64    Returns
 65    -------
 66    processing : Processing
 67        The processing object with the pre-merge method.
 68
 69
 70    Examples
 71    --------
 72    With the following config and 2 processings:
 73
 74    ```yaml
 75    # config.yaml
 76    neg_number1: 1
 77    neg_number2: 1
 78    neg_number3@add1: 1
 79    ```
 80
 81    ```python
 82    # main.py
 83    proc2 = create_processing_value(
 84        lambda val: -val,
 85        regex="neg_number.*",
 86        order=0.0
 87    )
 88    proc1 = create_processing_value(
 89        lambda val: val + 1,
 90        tag_name="add1",
 91        order=1.0
 92    )
 93    ```
 94
 95    When config.yaml is merged with an other config, it will be considered
 96    before merging as:
 97
 98    ```python
 99    {'number1': -1, 'number2': -1, 'number3': 0}
100    ```
101
102    Using the config as a second argument of the function:
103
104    ```yaml
105    # config.yaml
106    param1: 1
107    param2@eval: "config.param1 + 1"
108    ```
109
110    ```python
111    # main.py
112    proc = create_processing_value(
113        lambda val, config: eval(val, {'config': config}),
114        processing_type='postmerge',
115        tag_name='eval',
116        persistent=False
117    )
118    ```
119
120    After config.yaml is merged with another config, param2 will be evaluated
121    as 2 (except if config.param1 has changed with a processing before).
122    """
123    if tag_name is not None:
124        if regex is not None:
125            raise ValueError("You must provide a tag or a regex but not both.")
126    else:
127        if regex is None:
128            raise ValueError(
129                "You must provide a tag or a regex (to trigger the value update)."
130            )
131    proc = _ProcessingValue(
132        func,
133        processing_type,
134        regex=regex,
135        tag_name=tag_name,
136        order=order,
137        persistent=persistent,
138    )
139    return proc

Create a processing object that modifies a value in config using tag or regex.

The processing is applied on pre-merge. It triggers when the key matches the tag or the regex. The function apply flat_dict[key] = func(flat_dict[key]). You must only provide one of tag or regex. If tag is provided, the tag will be removed from the key during pre-merge.

It also possible to pass the flat config as a second argument of the function func. In this case, the function apply flat_dict[key] = func(flat_dict[key], flat_config).

Parameters
  • func (Callable): The function to apply to the value (and eventually the flat config) to make the new value so that: flat_dict[key] = func(flat_dict[key]) or func(flat_dict[key], flat_config)
  • processing_type (str, optional): One of "premerge", "postmerge", "presave", "postload" or "endbuild". Timing to apply the value update. In all cases the tag is removed on pre-merge. By default "premerge".
  • regex (Optional[str]): The regex to match the key.
  • tag_name (Optional[str]): The tag (without "@") to match the key. The tag is removed on pre-merge.
  • order (int, optional): The pre-merge order. By default 0.0.
  • persistent (bool, optional): If True, the processing will be applied on all keys that have already matched the tag before. By nature, using regex make the processing always persistent. By default, False.
Raises
  • ValueError: If both tag and regex are provided or if none of them are provided.
  • ValueError: If the processing type is not one of "premerge", "postmerge", "presave", "postload" or "endbuild".
Returns
  • processing (Processing): The processing object with the pre-merge method.
Examples

With the following config and 2 processings:

# config.yaml
neg_number1: 1
neg_number2: 1
neg_number3@add1: 1
# main.py
proc2 = create_processing_value(
    lambda val: -val,
    regex="neg_number.*",
    order=0.0
)
proc1 = create_processing_value(
    lambda val: val + 1,
    tag_name="add1",
    order=1.0
)

When config.yaml is merged with an other config, it will be considered before merging as:

{'number1': -1, 'number2': -1, 'number3': 0}

Using the config as a second argument of the function:

# config.yaml
param1: 1
param2@eval: "config.param1 + 1"
# main.py
proc = create_processing_value(
    lambda val, config: eval(val, {'config': config}),
    processing_type='postmerge',
    tag_name='eval',
    persistent=False
)

After config.yaml is merged with another config, param2 will be evaluated as 2 (except if config.param1 has changed with a processing before).

def flatten_config(config: Config) -> Config:
251def flatten_config(config: Config) -> Config:
252    """Flatten a config.
253
254    Parameters
255    ----------
256    config : Config
257        The config to flatten.
258
259    Returns
260    -------
261    confg : Config
262        The config containing a flattened dict.
263    """
264    config.dict = flatten(config.dict)
265    return config

Flatten a config.

Parameters
  • config (Config): The config to flatten.
Returns
  • confg (Config): The config containing a flattened dict.
def flatten_dict(in_dict: Dict[str, Any]) -> Dict[str, Any]:
158def flatten(in_dict: Dict[str, Any]) -> Dict[str, Any]:
159    """Flatten dict then return it (flat keys are built with dots).
160
161    Work even if in_dict is a mix of nested and flat dictionaries.
162    For instance like this:
163
164    >>> flatten({'a.b': {'c': 1}, 'a': {'b.d': 2}, 'a.e': {'f.g': 3}})
165    {'a.b.c': 1, 'a.b.d': 2, 'a.e.f.g': 3}
166
167    Parameters
168    ----------
169    in_dict : Dict[str, Any]
170        The dict to flatten. It can be nested, already flat or a mix of both.
171
172    Raises
173    ------
174    ValueError
175        If dict has some conflicting keys (like `{'a.b': <x>, 'a': {'b': <y>}}`).
176
177    Returns
178    -------
179    flat_dict : Dict[str, Any]
180        The flattened dict.
181
182    Examples
183    --------
184    ```python
185    >>> flatten({'a.b': 1, 'a': {'c': 2}, 'd': 3})
186    {'a.b': 1, 'a.c': 2, 'd': 3}
187    >>> flatten({'a.b': {'c': 1}, 'a': {'b.d': 2}, 'a.e': {'f.g': 3}})
188    {'a.b.c': 1, 'a.b.d': 2, 'a.e.f.g': 3}
189    >>> flatten({'a.b': 1, 'a': {'b': 1}})
190    ValueError: duplicated key 'a.b'
191    >>> flatten({'a.b': 1, 'a': {'c': {}}, 'a.c': 3})
192    {'a.b': 1, 'a.c': 3}
193    ```
194    .. note::
195        Nested empty dict are ignored even if they are conflicting (see last example).
196    """
197    flat_dict = _flatten(in_dict, reducer="dot")
198    return flat_dict

Flatten dict then return it (flat keys are built with dots).

Work even if in_dict is a mix of nested and flat dictionaries. For instance like this:

>>> flatten({'a.b': {'c': 1}, 'a': {'b.d': 2}, 'a.e': {'f.g': 3}})
{'a.b.c': 1, 'a.b.d': 2, 'a.e.f.g': 3}
Parameters
  • in_dict (Dict[str, Any]): The dict to flatten. It can be nested, already flat or a mix of both.
Raises
  • ValueError: If dict has some conflicting keys (like {'a.b': <x>, 'a': {'b': <y>}}).
Returns
  • flat_dict (Dict[str, Any]): The flattened dict.
Examples
>>> flatten({'a.b': 1, 'a': {'c': 2}, 'd': 3})
{'a.b': 1, 'a.c': 2, 'd': 3}
>>> flatten({'a.b': {'c': 1}, 'a': {'b.d': 2}, 'a.e': {'f.g': 3}})
{'a.b.c': 1, 'a.b.d': 2, 'a.e.f.g': 3}
>>> flatten({'a.b': 1, 'a': {'b': 1}})
ValueError: duplicated key 'a.b'
>>> flatten({'a.b': 1, 'a': {'c': {}}, 'a.c': 3})
{'a.b': 1, 'a.c': 3}

Nested empty dict are ignored even if they are conflicting (see last example).

def make_config( *default_config_paths: str, process_list: Optional[List[Processing]] = None, add_default_processing: bool = True, fallback: str = '', no_cli: bool = False) -> Config:
 25def make_config(
 26    *default_config_paths: str,
 27    process_list: Optional[List[Processing]] = None,
 28    add_default_processing: bool = True,
 29    fallback: str = "",
 30    no_cli: bool = False,
 31) -> Config:
 32    r"""Make a config from default config(s) and CLI argument(s) with processing.
 33
 34    The function uses the CLI Config routines `cliconfig.cli_parser.parse_cli`
 35    to parse the CLI arguments and merge them with
 36    `cliconfig.process_routines.merge_flat_paths_processing`, applying
 37    the pre-merge and post-merge processing functions on each merge.
 38
 39    Parameters
 40    ----------
 41    default_config_paths : Tuple[str]
 42        Paths to default configs. They are merged in order and new keys
 43        are allowed.
 44    process_list: Optional[List[Processing]], optional
 45        The list of processing to apply during each merge. None for empty list.
 46        By default None.
 47    add_default_processing : bool, optional
 48        If add_default_processing is True, the default processings
 49        (found on `cliconfig.processing.builtin.DefaultProcessings`) are added to
 50        the list of processings. By default True.
 51    fallback : str, optional
 52        Path of the configuration to use if no additional config is provided
 53        with `--config`. No fallback config if empty string (default),
 54        in that case, the config is the default configs plus the CLI arguments.
 55    no_cli : bool, optional
 56        If True, the CLI arguments are not parsed and the config is only
 57        built from the default_config_paths in input and the
 58        fallback argument is ignored. By default False.
 59
 60    Raises
 61    ------
 62    ValueError
 63        If additional configs have new keys that are not in default configs.
 64
 65    Returns
 66    -------
 67    config : Config
 68        The nested built config. Contains the config dict (config.dict) and
 69        the processing list (config.process_list) which can be used to apply
 70        further processing routines.
 71
 72    Examples
 73    --------
 74    ```python
 75    # main.py
 76    config = make_config('data.yaml', 'model.yaml', 'train.yaml')
 77    ```
 78
 79    ```script
 80    python main.py -- config [bestmodel.yaml,mydata.yaml] \
 81        --architecture.layers.hidden_dim=64
 82    ```
 83
 84    .. note::
 85        Setting additional arguments from CLI that are not in default configs
 86        does NOT raise an error but only a warning. This ensures the compatibility
 87        with other CLI usage (e.g notebook, argparse, etc.)
 88    """
 89    logger = cliconfig._CLICONFIG_LOGGER  # pylint: disable=W0212
 90    # Create the processing list
 91    process_list_: List[Processing] = [] if process_list is None else process_list
 92    if add_default_processing:
 93        process_list_ += DefaultProcessings().list
 94    config = Config({}, process_list_)
 95    if no_cli:
 96        additional_config_paths: List[str] = []
 97        cli_params_dict: Dict[str, Any] = {}
 98    else:
 99        additional_config_paths, cli_params_dict = parse_cli(sys.argv)
100        if not additional_config_paths and fallback:
101            # Add fallback config
102            additional_config_paths = [fallback]
103    # Merge default configs and additional configs
104    for i, paths in enumerate([default_config_paths, additional_config_paths]):
105        # Allow new keys for default configs only
106        allow_new_keys = i == 0
107        for path in paths:
108            config = merge_flat_paths_processing(
109                config,
110                path,
111                allow_new_keys=allow_new_keys,
112                preprocess_first=False,  # Already processed
113            )
114
115    # Allow new keys for CLI parameters but do not merge them and raise
116    # warning.
117    cli_params_dict = flatten(cli_params_dict)
118    new_keys, keys = [], list(cli_params_dict.keys())
119    for key in keys:
120        if (
121            not is_tag_in(key, "new", full_key=True)
122            and clean_all_tags(key) not in config.dict
123        ):
124            # New key: delete it
125            new_keys.append(clean_all_tags(key))
126            del cli_params_dict[key]
127    if new_keys:
128        new_keys_message = "  - " + "\n  - ".join(new_keys)
129        message = (
130            "[CONFIG] New keys found in CLI parameters "
131            f"that will not be merged:\n{new_keys_message}"
132        )
133        logger.warning(message)
134    # Merge CLI parameters
135    cli_params_config = Config(cli_params_dict, [])
136    config = merge_flat_processing(
137        config, cli_params_config, allow_new_keys=False, preprocess_first=False
138    )
139    message = (
140        f"[CONFIG] Merged {len(default_config_paths)} default config(s), "
141        f"{len(additional_config_paths)} additional config(s) and "
142        f"{len(cli_params_dict)} CLI parameter(s)."
143    )
144    logger.info(message)
145    config = end_build_processing(config)
146    config.dict = unflatten(config.dict)
147    return config

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

The function uses the CLI Config routines cliconfig.cli_parser.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 (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.
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

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.)

def load_config( path: str, default_config_paths: Optional[List[str]] = None, process_list: Optional[List[Processing]] = None, *, add_default_processing: bool = True) -> Config:
150def load_config(
151    path: str,
152    default_config_paths: Optional[List[str]] = None,
153    process_list: Optional[List[Processing]] = None,
154    *,
155    add_default_processing: bool = True,
156) -> Config:
157    """Load config from a file and merge into optional default configs.
158
159    First merge the default configs together (if any), then load the config
160    from path, apply the post-load processing, and finally merge the loaded
161    config.
162
163    Parameters
164    ----------
165    path : str
166        The path to the file to load the configuration.
167    default_config_paths : Optional[List[str]], optional
168        Paths to default configs. They are merged in order, new keys are allowed.
169        Then, the loaded config is merged into the result. None for no default configs.
170        By default None.
171    process_list: Optional[List[Processing]]
172        The list of processing to apply after loading and for the merges.
173        If None, no processing is applied. By default None.
174    add_default_processing : bool, optional
175        If add_default_processing is True, the default processings
176        (found on `cliconfig.processing.builtin.DefaultProcessings`)
177        are added to the list of processings. By default True.
178
179    Returns
180    -------
181    config: Dict[str, Any]
182        The nested loaded config. Contains the config dict (config.dict) and
183        the processing list (config.process_list) which can be used to apply
184        further processing routines.
185
186    .. note::
187        If default configs are provided, the function does not allow new keys
188        for the loaded config. This is for helping the user to see how to
189        adapt the config file if the default configs have changed.
190    """
191    # Crate process_list
192    process_list_: List[Processing] = [] if process_list is None else process_list
193    if add_default_processing:
194        process_list_ += DefaultProcessings().list
195
196    config = Config({}, process_list_)
197    if default_config_paths:
198        for config_path in default_config_paths:
199            config = merge_flat_paths_processing(
200                config,
201                config_path,
202                allow_new_keys=True,
203                preprocess_first=False,  # Already processed
204            )
205    loaded_config = load_processing(path, config.process_list)
206    # Update the config list from loaded_config in config
207    config.process_list = loaded_config.process_list
208    # Merge the loaded config into the config and
209    # disallow new keys for loaded config
210    # if default configs are provided
211    config = merge_flat_processing(
212        config,
213        loaded_config,
214        allow_new_keys=default_config_paths is None,
215        preprocess_first=False,
216    )
217    config = end_build_processing(config)
218    config.dict = unflatten(config.dict)
219    return config

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 (Dict[str, Any]): 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.
  • 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.
def merge_flat_paths_processing( config_or_path1: Union[str, Config], config_or_path2: Union[str, Config], *, additional_process: Optional[List[Processing]] = None, allow_new_keys: bool = True, preprocess_first: bool = True, preprocess_second: bool = True, postprocess: bool = True) -> Config:
100def merge_flat_paths_processing(
101    config_or_path1: Union[str, Config],
102    config_or_path2: Union[str, Config],
103    *,
104    additional_process: Optional[List[Processing]] = None,
105    allow_new_keys: bool = True,
106    preprocess_first: bool = True,
107    preprocess_second: bool = True,
108    postprocess: bool = True,
109) -> Config:
110    """Flatten, merge and apply processing to two configs or their yaml paths.
111
112    Similar to `merge_flat_processing` but allows to pass configs
113    or their yaml paths. Work even if the configs have a mix of nested and flat dicts.
114    If both arguments are configs, the process lists are merged before applying
115    the processing. The duplicate processings (with same internal variables)
116    are removed.
117
118    Parameters
119    ----------
120    config_or_path1 : Union[str, Config]
121        The first config or its path.
122    config_or_path2 : Union[str, Config]
123        The second config or its path, to merge into first config.
124    additional_process : Optional[List[Processing]], optional
125        Additional processings to apply to the merged config. It can
126        be useful to merge a config from its path while it has some specific
127        processings.
128    allow_new_keys : bool, optional
129        If True, new keys (that are not in config1) are allowed in config2.
130        Otherwise, it raises an error. By default True.
131    preprocess_first : bool, optional
132        If True, apply pre-merge processing to config1. By default True.
133    preprocess_second : bool, optional
134        If True, apply pre-merge processing to config2. By default True.
135    postprocess : bool, optional
136        If True, apply post-merge processing to the merged config. By default True.
137
138    Raises
139    ------
140    ValueError
141        If allow_new_keys is False and config2 has new keys that are not in config1.
142    ValueError
143        If there are conflicting keys when flatten one of the dicts.
144
145    Returns
146    -------
147    flat_config : Config
148        The merged flat config.
149    """
150    configs = []
151    for config_or_path in [config_or_path1, config_or_path2]:
152        if isinstance(config_or_path, str):
153            config_dict = load_dict(config_or_path)
154            config = Config(config_dict, [])
155        elif isinstance(config_or_path, Config):
156            config = config_or_path
157        elif isinstance(config_or_path, dict):
158            raise ValueError(
159                "config_or_path must be a Config instance or a path to a yaml file "
160                "but you passed a dict. If you want to use it as a valid input, "
161                "you should use Config(<input dict>, []) instead."
162            )
163        else:
164            raise ValueError(
165                "config_or_path must be a Config instance or a path to a yaml file."
166            )
167        configs.append(config)
168    config1, config2 = configs[0], configs[1]
169    if additional_process is not None:
170        config1.process_list.extend(additional_process)
171        config2.process_list.extend(additional_process)
172    flat_config = merge_flat_processing(
173        config1,
174        config2,
175        allow_new_keys=allow_new_keys,
176        preprocess_first=preprocess_first,
177        preprocess_second=preprocess_second,
178        postprocess=postprocess,
179    )
180    return flat_config

Flatten, merge and apply processing to two configs or their yaml paths.

Similar to merge_flat_processing but allows to pass configs or their yaml paths. Work even if the configs have a mix of nested and flat dicts. If both arguments are configs, the process lists are merged before applying the processing. The duplicate processings (with same internal variables) are removed.

Parameters
  • config_or_path1 (Union[str, Config]): The first config or its path.
  • config_or_path2 (Union[str, Config]): The second config or its path, to merge into first config.
  • additional_process (Optional[List[Processing]], optional): Additional processings to apply to the merged config. It can be useful to merge a config from its path while it has some specific processings.
  • allow_new_keys (bool, optional): If True, new keys (that are not in config1) are allowed in config2. Otherwise, it raises an error. By default True.
  • preprocess_first (bool, optional): If True, apply pre-merge processing to config1. By default True.
  • preprocess_second (bool, optional): If True, apply pre-merge processing to config2. By default True.
  • postprocess (bool, optional): If True, apply post-merge processing to the merged config. By default True.
Raises
  • ValueError: If allow_new_keys is False and config2 has new keys that are not in config1.
  • ValueError: If there are conflicting keys when flatten one of the dicts.
Returns
  • flat_config (Config): The merged flat config.
def merge_flat_processing( config1: Config, config2: Config, *, allow_new_keys: bool = True, preprocess_first: bool = True, preprocess_second: bool = True, postprocess: bool = True) -> Config:
22def merge_flat_processing(
23    config1: Config,
24    config2: Config,
25    *,
26    allow_new_keys: bool = True,
27    preprocess_first: bool = True,
28    preprocess_second: bool = True,
29    postprocess: bool = True,
30) -> Config:
31    """Flatten and merge config2 into config1 and apply pre and post processing.
32
33    Work even if the config dicts have a mix of nested and flat dictionaries.
34    If both arguments are configs, the process lists are merged before applying
35    the processing. The duplicate processings (with same internal variables)
36    are removed.
37
38    Parameters
39    ----------
40    config1 : Config
41        The first config.
42    config2 : Config
43        The second dict to merge into config1.
44    allow_new_keys : bool, optional
45        If True, new keys (that are not in config1) are allowed in config2.
46        Otherwise, it raises an error. By default True.
47    preprocess_first : bool, optional
48        If True, apply pre-merge processing to config1. By default True.
49    preprocess_second : bool, optional
50        If True, apply pre-merge processing to config2. By default True.
51    postprocess : bool, optional
52        If True, apply post-merge processing to the merged config. By default True.
53
54    Raises
55    ------
56    ValueError
57        If allow_new_keys is False and config2 has new keys that are not in config1.
58    ValueError
59        If there are conflicting keys when flatten one of the dicts.
60
61    Returns
62    -------
63    flat_config : Config
64        The merged flat config.
65    """
66    # Flatten the dictionaries
67    config1.dict, config2.dict = _flat_before_merge(config1.dict, config2.dict)
68    # Get the process list of the merge
69    process_list = config1.process_list
70    for process in config2.process_list:
71        # NOTE 2 processings are equal if they are the same class and add the same
72        # attributes.
73        if process not in process_list:
74            process_list.append(process)
75    # Apply the pre-merge processing
76    if preprocess_first:
77        config1.process_list = process_list
78        pre_order_list = sorted(process_list, key=lambda x: x.premerge_order)
79        for processing in pre_order_list:
80            config1 = processing.premerge(config1)
81        process_list = config1.process_list
82    if preprocess_second:
83        config2.process_list = process_list
84        pre_order_list = sorted(process_list, key=lambda x: x.premerge_order)
85        for processing in pre_order_list:
86            config2 = processing.premerge(config2)
87        process_list = config2.process_list
88    # Merge the dictionaries
89    flat_dict = merge_flat(config1.dict, config2.dict, allow_new_keys=allow_new_keys)
90    # Create the new config
91    flat_config = Config(flat_dict, process_list)
92    # Apply the postmerge processing
93    if postprocess:
94        post_order_list = sorted(process_list, key=lambda x: x.postmerge_order)
95        for processing in post_order_list:
96            flat_config = processing.postmerge(flat_config)
97    return flat_config

Flatten and merge config2 into config1 and apply pre and post processing.

Work even if the config dicts have a mix of nested and flat dictionaries. If both arguments are configs, the process lists are merged before applying the processing. The duplicate processings (with same internal variables) are removed.

Parameters
  • config1 (Config): The first config.
  • config2 (Config): The second dict to merge into config1.
  • allow_new_keys (bool, optional): If True, new keys (that are not in config1) are allowed in config2. Otherwise, it raises an error. By default True.
  • preprocess_first (bool, optional): If True, apply pre-merge processing to config1. By default True.
  • preprocess_second (bool, optional): If True, apply pre-merge processing to config2. By default True.
  • postprocess (bool, optional): If True, apply post-merge processing to the merged config. By default True.
Raises
  • ValueError: If allow_new_keys is False and config2 has new keys that are not in config1.
  • ValueError: If there are conflicting keys when flatten one of the dicts.
Returns
  • flat_config (Config): The merged flat config.
def save_config(config: Config, path: str) -> None:
222def save_config(config: Config, path: str) -> None:
223    """Save a config and apply pre-save processing before saving.
224
225    Alias for `cliconfig.process_routines.save_processing`.
226
227    Parameters
228    ----------
229    config : Dict[str, Any]
230        The config to save.
231    path : str
232        The path to the yaml file to save the dict.
233    """
234    save_processing(config, path)

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

Alias for cliconfig.process_routines.save_processing.

Parameters
  • config (Dict[str, Any]): The config to save.
  • path (str): The path to the yaml file to save the dict.
def show_config(config: Config) -> None:
237def show_config(config: Config) -> None:
238    """Show the config dict in a pretty way.
239
240    The config dict is automatically unflattened before printing.
241
242    Parameters
243    ----------
244    config : Config
245        The config to show.
246    """
247    print("Config:")
248    show_dict(config.dict, start_indent=1)

Show the config dict in a pretty way.

The config dict is automatically unflattened before printing.

Parameters
  • config (Config): The config to show.
def unflatten_config(config: Config) -> Config:
268def unflatten_config(config: Config) -> Config:
269    """Unflatten a config.
270
271    Parameters
272    ----------
273    config : Config
274        The config to unflatten.
275
276    Returns
277    -------
278    config : Config
279        The config containing an unflattened dict.
280    """
281    config.dict = unflatten(config.dict)
282    return config

Unflatten a config.

Parameters
  • config (Config): The config to unflatten.
Returns
  • config (Config): The config containing an unflattened dict.
def unflatten_dict(flat_dict: Dict[str, Any]) -> Dict[str, Any]:
201def unflatten(flat_dict: Dict[str, Any]) -> Dict[str, Any]:
202    """Unflatten a flat dict then return it.
203
204    Parameters
205    ----------
206    flat_dict : Dict[str, Any]
207        The dict to unflatten. Must be a fully flat dict (depth of 1 with keys
208        separated by dots).
209
210    Raises
211    ------
212    ValueError
213        If flat_dict is not flat and then found conflicts.
214
215    Returns
216    -------
217    unflat_dict : Dict[str, Any]
218        The output nested dict.
219
220    Examples
221    --------
222    >>> unflatten({'a.b': 1, 'a.c': 2, 'c': 3})
223    {'a': {'b': 1, 'c': 2}, 'c': 3}
224    >>> unflatten({'a.b': 1, 'a': {'c': 2}})
225    ValueError: duplicated key 'a'
226    The dict must be flatten before calling unflatten function.
227    """
228    try:
229        unflat_dict = _unflatten(flat_dict, splitter="dot")
230    except ValueError as exc:
231        raise ValueError(
232            "The dict must be flatten before calling unflatten function."
233        ) from exc
234    return unflat_dict

Unflatten a flat dict then return it.

Parameters
  • flat_dict (Dict[str, Any]): The dict to unflatten. Must be a fully flat dict (depth of 1 with keys separated by dots).
Raises
  • ValueError: If flat_dict is not flat and then found conflicts.
Returns
  • unflat_dict (Dict[str, Any]): The output nested dict.
Examples
>>> unflatten({'a.b': 1, 'a.c': 2, 'c': 3})
{'a': {'b': 1, 'c': 2}, 'c': 3}
>>> unflatten({'a.b': 1, 'a': {'c': 2}})
ValueError: duplicated key 'a'
The dict must be flatten before calling unflatten function.
def update_config( config: Config, other: Union[Dict[str, Any], Config], *, allow_new_keys: bool = False) -> Config:
285def update_config(
286    config: Config,
287    other: Union[Dict[str, Any], Config],
288    *,
289    allow_new_keys: bool = False,
290) -> Config:
291    """Update a config with a new dict or config with processing triggering.
292
293    The pre-merge, post-merge and end-build processings will be triggered.
294    The resulting config is unflattened.
295
296    Parameters
297    ----------
298    config : Config
299        The config to update.
300    other : Config | dict
301        The config or dict to update the config with.
302    allow_new_keys : bool, optional
303        If True, allow new keys in the other config. By default False.
304
305    Returns
306    -------
307    config : Config
308        The updated config.
309    """
310    other_config = Config(other, []) if isinstance(other, dict) else other
311    config = merge_flat_processing(
312        config,
313        other_config,
314        allow_new_keys=allow_new_keys,
315        preprocess_first=False,
316    )
317    config = end_build_processing(config)
318    config = unflatten_config(config)
319    return config

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 (Config): The updated config.