Layered settings with ChainMap

EasyCollections Programs · 16 of 20

Write a Python program that looks up settings in command-line, environment and default layers.

Advertisement
Output
firefox 2 True

Approach

  1. ChainMap(cli, env, defaults) searches left to right.

Solution

Python
from collections import ChainMap defaults = {"browser": "chromium", "retries": 0, "headless": True}env = {"retries": 2}cli = {"browser": "firefox"}settings = ChainMap(cli, env, defaults)print(settings["browser"], settings["retries"], settings["headless"])
Output
firefox 2 True

Learn the concept

New to this topic? Read the lesson, then come back.

More collections programs

← All 20 Collections Programs

Leave a Comment