# HG changeset patch # User Goffi # Date 1588161180 -7200 # Node ID 1649bbe8d07e06f73fd862f17ed4cb47be2d6e73 # Parent 9896589487ae613d9e2702746df3c8ff4985ecc9 tools (common/utils): new `recursive_update` method for dicts diff -r 9896589487ae -r 1649bbe8d07e sat/tools/common/utils.py --- 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 +