Skip to content

Instantly share code, notes, and snippets.

@fudomunro
fudomunro / dict_format.py
Created March 9, 2012 14:51
Recursively format a dictionary of strings
def dict_format(original, **kwargs):
"""Recursively format the values in *original* with *kwargs*.
>>> sample = {"key": "{value}", "sub-dict": {"sub-key": "sub-{value}"}}
>>> dict_format(sample, value="Bob") == \
{'key': 'Bob', 'sub-dict': {'sub-key': 'sub-Bob'}}
True
"""
new = {}
for key, value in original.items():
if type(value) == type({}):