Skip to content

Instantly share code, notes, and snippets.

@dagwieers
Last active October 26, 2018 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagwieers/24bd4cee971dea9aa2b3e839613fb623 to your computer and use it in GitHub Desktop.
Save dagwieers/24bd4cee971dea9aa2b3e839613fb623 to your computer and use it in GitHub Desktop.
Initial code
ignored_keys = ('hash', 'timestamp')
def filter_items(d):
''' Filter out keys when returning items() '''
l = []
# Ensure we return sorted tuples
for k, v in sorted(d.items()):
if k not in ignored_keys:
l.append((k, v))
return l
def is_update_required(original, proposed):
''' Compare two data-structures '''
if type(original) != type(proposed):
print "Datatypes don't match: {0} vs {1}".format(type(original), type(proposed))
return True
if isinstance(original, list) or isinstance(original, tuple):
if len(original) != len(proposed):
print "Lengths don't match: {0} vs {1}".format(len(original), len(proposed))
return True
for a, b in zip(original, proposed):
if is_update_required(a, b):
return True
elif isinstance(original, dict):
# Turn dictionaries into list of tuples, and re-evaluate
if is_update_required(filter_items(original), filter_items(proposed)):
return True
else: # Works for sets, integers, strings, ...
if original != proposed:
print "Values don't match: {0} vs {1}".format(original, proposed)
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment