Skip to content

Instantly share code, notes, and snippets.

@nicwolff
Created August 28, 2018 22:22
Show Gist options
  • Save nicwolff/12ce6f2061c4937fd0e3f38f30f825e2 to your computer and use it in GitHub Desktop.
Save nicwolff/12ce6f2061c4937fd0e3f38f30f825e2 to your computer and use it in GitHub Desktop.
assert_contains: Is every value in nested data structure p at the same path in f?
def assert_contains(full, partial):
"""Is every value in nested data structure p at the same path in f?"""
assert isinstance(full, type(partial)), '{} != {}'.format(full, partial)
if isinstance(partial, dict):
for k, v in partial.items():
assert k in full, "No key '{}' in {}".format(k, dict(full))
assert_contains(full[k], v)
return True
if isinstance(partial, (list, tuple)):
for i, partial_item in enumerate(partial):
assert_contains(full[i], partial_item)
return True
assert full == partial, '{} != {}'.format(full, partial)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment