Skip to content

Instantly share code, notes, and snippets.

@Kamori
Last active February 4, 2019 12:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Kamori/7c1e12260bae605e92dbf60bad227741 to your computer and use it in GitHub Desktop.
Save Kamori/7c1e12260bae605e92dbf60bad227741 to your computer and use it in GitHub Desktop.
# Get the keys that don't exist in both dicts
missing = set(val1.keys()).symmetric_difference(set(val2.keys()))
# Get the keys that DO exist in both
found = set(val1.keys()) & set(val2.keys())
diff = len(missing)
matching = len(found)
for resource in missing:
# Lets log the keys that aren't in both dicts and report which one it belonged to
msg = 'Resource {0} not found in {1}'
if not val1.get(resource):
logger.critical(crayons.yellow(msg.format(resource, 'dict1')))
elif not val2.get(resource):
logger.critical(crayons.yellow(msg.format(resource,
'dict2')))
for resource in found:
# Now lets go through each key in the dict and compare their values
obj1 = [val1[resource]]
obj2 = [val2[resource]]
for resource2, resource1 in izip(obj2, obj1):
# now the next layer of the dict lets get their keys and do the same comparison
shared_keys = set(resource1.keys()) & set(resource2.keys())
untested_keys =set(resource1.keys()).symmetric_difference(set(
resource2.keys()))
if len(shared_keys) < 2:
raise Exception('I require more than 2 keys to compare')
obj_differences = {'objects': []}
for key in shared_keys:
res1, res2 = set_overrides(resource1[key],
resource2[key], key)
if res1 != res2:
# Ugly because order of item args matters
obj_differences['objects'].append({key:
{'dict1': resource1[key],
'dict2': resource2[key]}})
diff += 1
if obj_differences['objects']:
msg = 'Resource value not equal in both: {0}'
logger.critical(crayons.yellow(msg.format(resource)))
logger.critical(crayons.yellow('\n {0}'.format(pformat(
obj_differences))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment