Created
May 11, 2015 20:52
-
-
Save soldni/bab31335c99ffd3fde7d 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
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