Return a hash for a dict, based on its contents
(obj, start='')
| 22 | |
| 23 | |
| 24 | def 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 | |
| 51 | def _process(value, sha=False): |
no outgoing calls
no test coverage detected