add util method to merge dictionaries recursively with priority

This commit is contained in:
Maximilian Keßler 2021-09-17 10:22:04 +02:00
parent 758ace4ffe
commit 56637a3b16

View file

@ -1,4 +1,6 @@
from datetime import datetime
from typing import Dict
import warnings
from config import MAX_LEN
@ -21,3 +23,17 @@ def generate_short_title(title):
def get_week(d=datetime.today()):
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