tools (common/utils): new recursive_update method for dicts

This commit is contained in:
Goffi 2020-04-29 13:53:00 +02:00
commit 794b5be810

View file

@ -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