def _check_value_recursively(key, val, haystack):
"""
Check **resursively** if there is a given key with a given value in the
given dictionary.
..warning:
This is geared at JSON dictionaries, so some corner cases are ignored,
we assume all iterables are either arrays or dicts
:param key: the key to look for
:param val: value to look for
:param haystack: the dictionary
"""
if isinstance(haystack, list):
return any([_check_value_recursively(key, val, l) for l in haystack])
elif isinstance(haystack, dict):
if not key in haystack:
return any([_check_value_recursively(key, val, d) for k, d in haystack.items()
if isinstance(d, list) or isinstance(d, dict)])
else:
return haystack[key] == val
else:
return False
评论列表
文章目录