project/analysis/util/iter.py

21 lines
604 B
Python

def json_path(obj: dict, key: str):
"""Query a nested dict with a dot-separated path"""
if not type(obj) is dict:
return None
if "." not in key:
if key not in obj:
return None
return obj[key]
child_key = key.split(".")
if child_key[0] not in obj:
return None
return json_path(obj[child_key[0]], ".".join(child_key[1:]))
def combinate(settings: dict, entry: dict) -> bool: # TODO: better name...
"""combine all settings {<key>: <expected>} with entry using AND"""
result = True
for key, value in settings.items():
result = result and json_path(entry, key) == value
return result