Skip to content

13. Override a Config Without Changing It

Difficulty: Medium · Topic: Core Python & data structures

Pipeline configs are nested dicts. Write with_override(config, path, value): return a new config in which the dotted path (such as 'db.pool.size') is set to value.

Your code runs in CPython on our servers against 5 tests, including hidden ones.

Sample test

cfg = {'db': {'host': 'h', 'pool': {'size': 5}}, 'debug': False}
new = with_override(cfg, 'db.pool.size', 20)
(new, cfg)

Expected: ({'db': {'host': 'h', 'pool': {'size': 20}}, 'debug': False}, {'db': {'host': 'h', 'pool': {'size': 5}}, 'debug': False})

Hints

Hint 1config.copy() and dict(config) copy only the top level: nested dicts are shared.
Hint 2copy.deepcopy(config) copies every level.
Hint 3Walk the path with node = node.setdefault(part, {}) for every part but the last.

Learn the concepts

More Python problems

All Python problems