changeset 3265:1649bbe8d07e

tools (common/utils): new `recursive_update` method for dicts
author Goffi <goffi@goffi.org>
date Wed, 29 Apr 2020 13:53:00 +0200
parents 9896589487ae
children 8ec5ddb4e759
files sat/tools/common/utils.py
diffstat 1 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/sat/tools/common/utils.py	Sat Apr 25 15:54:26 2020 +0200
+++ b/sat/tools/common/utils.py	Wed Apr 29 13:53:00 2020 +0200
@@ -18,6 +18,8 @@
 
 """Misc utils for both backend and frontends"""
 
+import collections.abc
+
 
 def per_luminance(red, green, blue):
     """Caculate the perceived luminance of a RGB color
@@ -30,3 +32,15 @@
     # cf. https://stackoverflow.com/a/1855903, thanks Gacek
 
     return 0.299 * red + 0.587 * green + 0.114 * blue
+
+
+def recursive_update(ori: dict, update: dict):
+    """Recursively update a dictionary"""
+    # cf. https://stackoverflow.com/a/3233356, thanks Alex Martelli
+    for k, v in update.items():
+        if isinstance(v, collections.abc.Mapping):
+            ori[k] = recursive_update(ori.get(k, {}), v)
+        else:
+            ori[k] = v
+    return ori
+