tools (common/utils): new recursive_update method for dicts
This commit is contained in:
parent
a5aac84c6a
commit
794b5be810
1 changed files with 14 additions and 0 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue