Last active
August 31, 2015 16:05
-
-
Save adnelson/965deed7b6df273964c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def raise_msg(msg, path): | |
if len(path) > 0: | |
msg = 'At path {}: {}'.format('.'.join(path), msg) | |
raise AssertionError(msg) | |
def list_diff(list1, list2, path): | |
if len(list1) != len(list2): | |
raise_msg('List lengths differ: {} vs {}' | |
.format(len(list1), len(list2)), path) | |
for i, (obj1, obj2) in enumerate(zip(list1, list2)): | |
diff(obj1, obj2, path + [str(i)]) | |
def dict_diff(d1, d2, path): | |
if set(d1.keys()) != set(d2.keys()): | |
raise_msg('keys differ: {} vs {}' | |
.format(set(d1.keys()), set(d2.keys())), path) | |
for k, v1 in d1.items(): | |
diff(v1, d2[k], path + [k]) | |
def diff(obj1, obj2, path=None): | |
if path is None: | |
path = [] | |
if type(obj1) != type(obj2): | |
raise_msg('Objects {} and {}, different types: {} vs {}' | |
.format(obj1, obj2, type(obj1), type(obj2)), path) | |
elif isinstance(obj1, dict): | |
dict_diff(obj1, obj2, path) | |
elif isinstance(obj1, list): | |
list_diff(obj1, obj2, path) | |
elif obj1 != obj2: | |
raise_msg('Not equal: {} vs {}'.format(obj1, obj2), path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment