Context Keys#

Context keys can be used to create multiple contexts.#

from pprint import pprint

from dicfg import ConfigReader
!cat ./configs/config.yml
# adding context1 and context2 as context keys
config_reader = ConfigReader(name="myconfig", context_keys=("context1", "context2"))
default:
    config_int: 1
    config_string: "fire"
    config_list: [1, 2, 3]
    config_none: None
    config_dict:
        sub_config: "water"
# context1 and context2 will have the same content as the default context

pprint(config_reader.read(), sort_dicts=False)
{'context1': {'config_int': 1,
              'config_string': 'fire',
              'config_list': [1, 2, 3],
              'config_none': 'None',
              'config_dict': {'sub_config': 'water'}},
 'context2': {'config_int': 1,
              'config_string': 'fire',
              'config_list': [1, 2, 3],
              'config_none': 'None',
              'config_dict': {'sub_config': 'water'}},
 'default': {'config_int': 1,
             'config_string': 'fire',
             'config_list': [1, 2, 3],
             'config_none': 'None',
             'config_dict': {'sub_config': 'water'}}}

The multiple contexts inherent from the default context and can be independent updated.#

user_config = {
    "myconfig": {
        "default": {"config_int": 2, "config_string": "default"},
        "context1": {"config_string": "context1_string"},
        "context2": {"config_string": "context2_string"},
    },
}

pprint(
    config_reader.read(user_config=user_config),
    sort_dicts=False,
)
{'context1': {'config_int': 2,
              'config_string': 'context1_string',
              'config_list': [1, 2, 3],
              'config_none': 'None',
              'config_dict': {'sub_config': 'water'}},
 'context2': {'config_int': 2,
              'config_string': 'context2_string',
              'config_list': [1, 2, 3],
              'config_none': 'None',
              'config_dict': {'sub_config': 'water'}},
 'default': {'config_int': 2,
             'config_string': 'default',
             'config_list': [1, 2, 3],
             'config_none': 'None',
             'config_dict': {'sub_config': 'water'}}}