Using ChainMap to use configs with precedence

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 pprint
local_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}")

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Martin Koníček
Martin Koníček

Written by Martin Koníček

AWS and Python developer and Linux enthusiastic.

No responses yet

Write a response