mirror of
https://github.com/Dielee/volvo2mqtt.git
synced 2026-08-12 10:49:52 +02:00
cleanup api data handling
This commit is contained in:
+17
@@ -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
|
||||
Reference in New Issue
Block a user