add util method to merge dictionaries recursively with priority
This commit is contained in:
parent
758ace4ffe
commit
56637a3b16
1 changed files with 17 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue