cleanup api data handling

This commit is contained in:
Linus Dietz
2023-06-22 19:22:54 +02:00
parent 1d07644a9c
commit bbc6ccb840
2 changed files with 51 additions and 36 deletions
+17
View File
@@ -0,0 +1,17 @@
def keys_exists(element, *keys):
""""
Check if *keys (nested) exists in `element` (dict).
Thanks stackoverflow: https://stackoverflow.com/questions/43491287/elegant-way-to-check-if-a-nested-key-exists-in-a-dict
"""
if not isinstance(element, dict):
raise AttributeError('keys_exists() expects dict as first argument.')
if len(keys) == 0:
raise AttributeError('keys_exists() expects at least two arguments, one given.')
_element = element
for key in keys:
try:
_element = _element[key]
except KeyError:
return False
return True