data_utils.py 991 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # @FileName :data_utils.py
  4. # @Time :2025/5/21 16:16
  5. # @Author :David
  6. # @Company: shenyang JY
  7. def deep_update(target, source):
  8. """
  9. 递归将 source 字典的内容合并到 target 字典中
  10. 规则:
  11. 1. 若 key 在 target 和 source 中都存在,且值均为字典 → 递归合并
  12. 2. 若 key 在 source 中存在但 target 不存在 → 直接添加
  13. 3. 若 key 在 source 中存在且类型不为字典 → 覆盖 target 的值
  14. """
  15. for key, value in source.items():
  16. # 如果 target 中存在该 key 且双方值都是字典 → 递归合并
  17. if key in target and isinstance(target[key], dict) and isinstance(value, dict):
  18. deep_update(target[key], value)
  19. else:
  20. # 直接覆盖或添加(包括非字典类型或 target 中不存在该 key 的情况)
  21. target[key] = value
  22. return target
  23. if __name__ == "__main__":
  24. run_code = 0