Skip to content

Instantly share code, notes, and snippets.

@soldni
Created May 11, 2015 20:52
Show Gist options
  • Save soldni/bab31335c99ffd3fde7d to your computer and use it in GitHub Desktop.
Save soldni/bab31335c99ffd3fde7d to your computer and use it in GitHub Desktop.
import hashlib
import json
import sys
def hash_obj(obj):
try:
return hashlib.md5(json.dumps(obj)).hexdigest()
except TypeError:
pass
if type(obj) is dict:
outobj = {}
for k, v in obj.iteritems():
try:
outobj[k] = json.dumps(v)
except TypeError:
pass
elif type(obj) in (list, tuple, set):
outobj = []
for v in obj:
try:
outobj.append(json.dumps(v))
except TypeError:
pass
else:
print('[error] obj can not be hashed', file=sys.stderr)
sys.exit(1)
return hashlib.md5(json.dumps(outobj)).hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment