Skip to content

Instantly share code, notes, and snippets.

@rturowicz
Last active December 23, 2015 12:18
Show Gist options
  • Save rturowicz/6633985 to your computer and use it in GitHub Desktop.
Save rturowicz/6633985 to your computer and use it in GitHub Desktop.
compare dictionaries content
from hashlib import md5
from cPickle import dumps
def make_hashes(data):
ret = [md5(dumps(data)).hexdigest()]
ret.extend([
md5(dumps(v)).hexdigest() for v in data.values()
])
return ret
def check_hashes(data, last_data):
new = []
last_data = last_data.split(',')
if last_data[0] != md5(dumps(data)).hexdigest():
for i in data:
if md5(dumps(data[i])).hexdigest() not in last_data[1:]:
new.append(i)
if len(new) > 0:
return False, new
return True, new
# d = {
# 1: {'tmp1': 1, 'tmp2': 2},
# 2: {'tmp1': 3, 'tmp2': 4},
# 3: {'tmp1': 5, 'tmp2': 7}
# }
# src_string = ','.join(make_hashes(d))
# or from db
src_string = '995a56cae0ad5ce853f0e1825e921ce0,2c0d7ba176ff1364a6ba87329dabd216,baf2d71dba6ac10eb341e36d6d8bb9c7,010290979ec01aefd6778724d6a75d5b'
d2 = {
1: {'tmp1': 1, 'tmp2': 2},
2: {'tmp1': 3, 'tmp2': 4},
3: {'tmp1': 5, 'tmp2': 7}
}
same, new = check_hashes(d2, src_string)
print src_string
if same:
print 'same'
else:
print 'different:'
for i in new:
print d2[i]
print ','.join(make_hashes(d2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment