Skip to content

Instantly share code, notes, and snippets.

@ZwodahS
Created May 22, 2017 12:40
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 ZwodahS/243585aa8a23d7255403beec439cc823 to your computer and use it in GitHub Desktop.
Save ZwodahS/243585aa8a23d7255403beec439cc823 to your computer and use it in GitHub Desktop.
Hashing dictionary
import reprlib
import hashlib
import base64
# Idea taken from http://stackoverflow.com/questions/5884066/hashing-a-dictionary
class DictHasher(object):
def __init__(self):
self._repr_funcs = [
([int, str, float, bool], lambda data: data),
([tuple, list], lambda data: tuple([self._make_repr(e) for e in data])),
([dict, ], lambda data: tuple(sorted((k, self._make_repr(v)) for k, v in data.items()))),
([set, frozenset], lambda data: tuple(sorted(self._make_repr(e) for e in o))),
]
self.encode_func = base64.urlsafe_b64encode
self.hash_func = lambda x :hashlib.sha1(x).digest()
def _make_repr(self, o):
if o is None:
return o
for types, func in self._repr_funcs:
if isinstance(o, tuple(types)):
return func(o)
raise ValueError("type {type} is not representable. Please provide a function".format(type=type(o).name))
def add_repr_func(self, types, func):
self._repr_funcs.append((types, func))
def hash(self, obj):
rep = reprlib.repr(self._make_repr(obj)).encode("utf-8")
hashed = self.hash_func(rep)
encoded = self.encode_func(hashed).decode("utf-8")
return encoded
hasher = DictHasher()
hash_obj = hasher.hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment