import copy


def merge_dicts(dict1, dict2, make_copy=True):
  """ Recursively merges dict2 with dict1. Otherwise if both dict1 and dict2
  have a key, it is overwritten by the value in dict2. """
  if make_copy:
    dict1 = copy.copy(dict1)
  for key, value in dict2.iteritems():
    if isinstance(dict1.get(key), dict) and isinstance(value, dict):
      value = merge_dicts(dict1[key], value, make_copy=False)
    dict1[key] = value
  return dict1