comparison sat/tools/common/utils.py @ 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 9d0df638c8b4
children 84f77f04aa08
comparison
equal deleted inserted replaced
3264:9896589487ae 3265:1649bbe8d07e
16 # You should have received a copy of the GNU Affero General Public License 16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 """Misc utils for both backend and frontends""" 19 """Misc utils for both backend and frontends"""
20 20
21 import collections.abc
22
21 23
22 def per_luminance(red, green, blue): 24 def per_luminance(red, green, blue):
23 """Caculate the perceived luminance of a RGB color 25 """Caculate the perceived luminance of a RGB color
24 26
25 @param red(int): 0-1 normalized value of red 27 @param red(int): 0-1 normalized value of red
28 @return (float): 0-1 value of luminance (<0.5 is dark, else it's light) 30 @return (float): 0-1 value of luminance (<0.5 is dark, else it's light)
29 """ 31 """
30 # cf. https://stackoverflow.com/a/1855903, thanks Gacek 32 # cf. https://stackoverflow.com/a/1855903, thanks Gacek
31 33
32 return 0.299 * red + 0.587 * green + 0.114 * blue 34 return 0.299 * red + 0.587 * green + 0.114 * blue
35
36
37 def recursive_update(ori: dict, update: dict):
38 """Recursively update a dictionary"""
39 # cf. https://stackoverflow.com/a/3233356, thanks Alex Martelli
40 for k, v in update.items():
41 if isinstance(v, collections.abc.Mapping):
42 ori[k] = recursive_update(ori.get(k, {}), v)
43 else:
44 ori[k] = v
45 return ori
46