From 56637a3b16b399cd8fcbea3f1869a50a533bb71f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 17 Sep 2021 10:22:04 +0200 Subject: [PATCH] add util method to merge dictionaries recursively with priority --- scripts/utils.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/scripts/utils.py b/scripts/utils.py index 52452bd..d49401f 100644 --- a/scripts/utils.py +++ b/scripts/utils.py @@ -1,4 +1,6 @@ from datetime import datetime +from typing import Dict +import warnings from config import MAX_LEN @@ -20,4 +22,18 @@ def generate_short_title(title): def get_week(d=datetime.today()): - return (int(d.strftime("%W")) + 52 - 5) % 52 \ No newline at end of file + return (int(d.strftime("%W")) + 52 - 5) % 52 + + +def merge_dictionaries(main: Dict, fallback: Dict): + merged = main + for key in fallback.keys(): + if key not in main.keys(): + merged[key] = fallback[key] + elif type(fallback[key]) == dict: + if not type(merged[key]) == dict: + warnings.warn( + f"Main dictionary has invalid format. Replacing entry with key {key} with fallback value.") + merged[key] = fallback[key] + merged[key] = merge_dictionaries(merged[key], fallback[key]) + return merged