def update_dict(d, u=None, depth=-<NUM_LIT:1>, take_new=True, default_mapping_type=dict, prefer_update_type=False, copy=False):
u = u or {}<EOL>orig_mapping_type = type(d)<EOL>if prefer_update_type and isinstance(u, Mapping):<EOL><INDENT>dictish = type(u)<EOL><DEDENT>elif isinstance(d, Mapping):<EOL><INDENT>dictish = orig_mapping_type<EOL><DEDENT>else:<EOL><INDENT>dictish = default_mapping_type<EOL><DEDENT>if copy:<EOL><INDENT>d = dictish(d)<EOL><DEDENT>for k, v in viewitems(u):<EOL><INDENT>if isinstance(d, Mapping):<EOL><INDENT>if isinstance(v, Mapping) and not depth == <NUM_LIT:0>:<EOL><INDENT>r = update_dict(d.get(k, dictish()), v, depth=max(depth - <NUM_LIT:1>, -<NUM_LIT:1>), copy=copy)<EOL>d[k] = r<EOL><DEDENT>elif take_new:<EOL><INDENT>d[k] = u[k]<EOL><DEDENT><DEDENT>elif take_new:<EOL><INDENT>d = dictish([(k, u[k])])<EOL><DEDENT><DEDENT>return d<EOL>
Recursively merge (union or update) dict-like objects (Mapping) to the specified depth.

>>> update_dict({'k1': {'k2': 2}}, {'k1': {'k2': {'k3': 3}}, 'k4': 4}) == {'k1': {'k2': {'k3': 3}}, 'k4': 4}
True
>>> update_dict(OrderedDict([('k1', OrderedDict([('k2', 2)]))]), {'k1': {'k2': {'k3': 3}}, 'k4': 4})
OrderedDict([('k1', OrderedDict([('k2', {'k3': 3})])), ('k4', 4)])
>>> update_dict(OrderedDict([('k1', dict([('k2', 2)]))]), {'k1': {'k2': {'k3': 3}}, 'k4': 4})
OrderedDict([('k1', {'k2': {'k3': 3}}), ('k4', 4)])
>>> orig = {'orig_key': 'orig_value'}
>>> updated = update_dict(orig, {'new_key': 'new_value'}, copy=True)
>>> updated == orig
False
>>> updated2 = update_dict(orig, {'new_key2': 'new_value2'})
>>> updated2 == orig
True
>>> update_dict({'k1': {'k2': {'k3': 3}}, 'k4': 4}, {'k1': {'k2': 2}},
...             depth=1, take_new=False) == {'k1': {'k2': 2}, 'k4': 4}
True
>>> update_dict({'k1': {'k2': {'k3': 3}}, 'k4': 4}, None) == {'k1': {'k2': {'k3': 3}}, 'k4': 4}
True
>>> update_dict({'k1': {'k2': {'k3': 3}}, 'k4': 4}, {'k1': ()}) == {'k1': (), 'k4': 4}
True
>>> # FIXME: this result is unexpected the same as for `take_new=False`
>>> update_dict({'k1': {'k2': {'k3': 3}}, 'k4': 4}, {'k1': {'k2': 2}},
...             depth=1, take_new=True) == {'k4': 4, 'k1': {'k2': 2}}
True