MCPcopy Create free account
hub / github.com/cxapython/catvm / dict_hash

Function dict_hash

selenium/scrapy_splash/utils.py:24–48  ·  view source on GitHub ↗

Return a hash for a dict, based on its contents

(obj, start='')

Source from the content-addressed store, hash-verified

22
23
24def dict_hash(obj, start=''):
25 """ Return a hash for a dict, based on its contents """
26 h = hashlib.sha1(to_bytes(start))
27 h.update(to_bytes(obj.__class__.__name__))
28 if isinstance(obj, dict):
29 for key, value in sorted(obj.items()):
30 h.update(to_bytes(key))
31 h.update(to_bytes(dict_hash(value)))
32 elif isinstance(obj, (list, tuple)):
33 for el in obj:
34 h.update(to_bytes(dict_hash(el)))
35 else:
36 # basic types
37 if isinstance(obj, bool):
38 value = str(int(obj))
39 elif isinstance(obj, (six.integer_types, float)):
40 value = str(obj)
41 elif isinstance(obj, (six.text_type, bytes)):
42 value = obj
43 elif obj is None:
44 value = b''
45 else:
46 raise ValueError("Unsupported value type: %s" % obj.__class__)
47 h.update(to_bytes(value))
48 return h.hexdigest()
49
50
51def _process(value, sha=False):

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected