Interpolation: build_config#
from pprint import pprint
from dicfg import ConfigReader, build_config
!cat ./configs/config.yml
config_reader = ConfigReader(name="myconfig")
default:
config_int: 1
config_string: "fire"
config_list: [1, 2, 3]
config_none: None
config_dict:
sub_config: "water"
Config files (.yml, ‘.yaml’, and ‘json’) can be interpolated in a config#
!cat ./interpolation.yml
sub_config: 'earth'
user_config = {"myconfig": {"default": {"config_dict": "./interpolation.yml"}}}
pprint(config_reader.read(user_config), sort_dicts=False)
{'default': {'config_int': 1,
'config_string': 'fire',
'config_list': [1, 2, 3],
'config_none': 'None',
'config_dict': {'sub_config': 'earth'}}}
Config values can be interpolated in a config by referencing with $(key) and building the config with build_config#
user_config = {
"myconfig": {"default": {"config_dict": {"sub_config": "${config_string}"}}}
}
config = config_reader.read(user_config)
pprint(config, sort_dicts=False)
print("\ndefault config build:\n")
pprint(build_config(config["default"]), sort_dicts=False)
{'default': {'config_int': 1,
'config_string': 'fire',
'config_list': [1, 2, 3],
'config_none': 'None',
'config_dict': {'sub_config': '${config_string}'}}}
default config build:
{'config_int': 1,
'config_string': 'fire',
'config_list': [1, 2, 3],
'config_none': None,
'config_dict': {'sub_config': 'fire'}}
Environment variables can be interpolated by using ${$env.VAR}#
user_config = {"myconfig": {"default": {"env_user": "${$env.USER}"}}}
config = config_reader.read(user_config)
pprint(config, sort_dicts=False)
print("\ndefault config build:\n")
pprint(build_config(config["default"]), sort_dicts=False)
{'default': {'config_int': 1,
'config_string': 'fire',
'config_list': [1, 2, 3],
'config_none': 'None',
'config_dict': {'sub_config': 'water'},
'env_user': '${$env.USER}'}}
default config build:
{'config_int': 1,
'config_string': 'fire',
'config_list': [1, 2, 3],
'config_none': None,
'config_dict': {'sub_config': 'water'},
'env_user': 'mart'}
Python objects can be interpolated by using ${$module.Object}#
user_config = {
"myconfig": {"default": {"version": "${$dicfg.__version__}"}},
}
config = config_reader.read(user_config)
pprint(config, sort_dicts=False)
objects = build_config(config["default"])
print("\n build objects: \n")
pprint(objects)
{'default': {'config_int': 1,
'config_string': 'fire',
'config_list': [1, 2, 3],
'config_none': 'None',
'config_dict': {'sub_config': 'water'},
'version': '${$dicfg.__version__}'}}
build objects:
{'config_dict': {'sub_config': 'water'},
'config_int': 1,
'config_list': [1, 2, 3],
'config_none': None,
'config_string': 'fire',
'version': '0.0.4'}
String values can be interpolated in a config by referencing with $(key) and building the config with build_config#
user_config = {
"myconfig": {
"default": {"combined_values": "${config_string}_${config_int}"}
}
}
config = config_reader.read(user_config)
pprint(config, sort_dicts=False)
print("\ndefault config build:\n")
pprint(build_config(config["default"]), sort_dicts=False)
{'default': {'config_int': 1,
'config_string': 'fire',
'config_list': [1, 2, 3],
'config_none': 'None',
'config_dict': {'sub_config': 'water'},
'combined_values': '${config_string}_${config_int}'}}
default config build:
{'config_int': 1,
'config_string': 'fire',
'config_list': [1, 2, 3],
'config_none': None,
'config_dict': {'sub_config': 'water'},
'combined_values': 'fire_1'}