From 794b5be8108b0e612835d14cb48e7473531b3ee2 Mon Sep 17 00:00:00 2001 From: Goffi Date: Wed, 29 Apr 2020 13:53:00 +0200 Subject: [PATCH] tools (common/utils): new `recursive_update` method for dicts --- sat/tools/common/utils.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sat/tools/common/utils.py b/sat/tools/common/utils.py index de81007f..a7e4054a 100644 --- a/sat/tools/common/utils.py +++ b/sat/tools/common/utils.py @@ -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 @@ def per_luminance(red, green, blue): # 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 +