collections.py 840 B

1234567891011121314151617181920
  1. def deep_update(base_dict, update_with):
  2. """
  3. Updates the base_dict (eg. settings/base.py settings) with the values present
  4. in the update_with dict
  5. """
  6. # iterate over items in the update_with dict
  7. for key, value in update_with.items():
  8. if isinstance(value, dict): # update_with dict value is a dict, i.e. update_with[key] = {}
  9. base_dict_value = base_dict.get(key)
  10. # check if base_dict value is also a dict
  11. if isinstance(base_dict_value, dict): # check if base_dict[key] = {}
  12. deep_update(base_dict_value, value) # recurse
  13. else:
  14. base_dict[key] = value # else update the base dict with whatever dict value in update_with
  15. else:
  16. base_dict[key] = value
  17. # return the updated base_dict
  18. return base_dict