def merge_dictionaries(a, b, path=None, update=True):
if path is None:<EOL><INDENT>path = []<EOL><DEDENT>for key in b:<EOL><INDENT>if key in a:<EOL><INDENT>if isinstance(a[key], dict) and isinstance(b[key], dict):<EOL><INDENT>merge_dictionaries(a[key], b[key], path + [str(key)])<EOL><DEDENT>elif a[key] == b[key]:<EOL><INDENT>pass <EOL><DEDENT>elif isinstance(a[key], list) and isinstance(b[key], list):<EOL><INDENT>for idx, val in enumerate(b[key]):<EOL><INDENT>a[key][idx] = merge_dictionaries(a[key][idx],<EOL>b[key][idx],<EOL>path + [str(key), str(idx)],<EOL>update=update)<EOL><DEDENT><DEDENT>elif update:<EOL><INDENT>a[key] = b[key]<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>' % '<STR_LIT:.>'.join(path + [str(key)]))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>a[key] = b[key]<EOL><DEDENT><DEDENT>return a<EOL>
Merge two dictionaries recursively.

From https://stackoverflow.com/a/25270947