Using ChainMap to use configs with precedence
Jan 26, 2021
This is a simple code preview how to use ChainMap to combine multiple config files into one with precedence. Attaching also Jupyter Notebook.
from collections import ChainMap
from pprint import pprintlocal_config = {
"user": "localuser",
"pass": "localpass",
"host": "localhost"
}global_config = {
"user": "globaluser",
"pass": "globalpass",
"host": "globalhost",
"timeout": 15
}config = ChainMap(local_config, global_config)
pprint(config)for k,v in config.items():
print(f"{k}:{v}")