Hash a reasonable python object. Parameters ---------- x : object Object to hash. Can be anything comprised of nested versions of: {dict, list, tuple, ndarray, str, bytes, float, int, None}. h : hashlib HASH object | None Optional, object to add the hash to.
(x, h=None)
| 625 | |
| 626 | |
| 627 | def object_hash(x, h=None): |
| 628 | """Hash a reasonable python object. |
| 629 | |
| 630 | Parameters |
| 631 | ---------- |
| 632 | x : object |
| 633 | Object to hash. Can be anything comprised of nested versions of: |
| 634 | {dict, list, tuple, ndarray, str, bytes, float, int, None}. |
| 635 | h : hashlib HASH object | None |
| 636 | Optional, object to add the hash to. None creates an MD5 hash. |
| 637 | |
| 638 | Returns |
| 639 | ------- |
| 640 | digest : int |
| 641 | The digest resulting from the hash. |
| 642 | """ |
| 643 | if h is None: |
| 644 | h = _empty_hash() |
| 645 | if hasattr(x, "keys"): |
| 646 | # dict-like types |
| 647 | keys = _sort_keys(x) |
| 648 | for key in keys: |
| 649 | object_hash(key, h) |
| 650 | object_hash(x[key], h) |
| 651 | elif isinstance(x, bytes): |
| 652 | # must come before "str" below |
| 653 | h.update(x) |
| 654 | elif isinstance(x, str | float | int | type(None)): |
| 655 | h.update(str(type(x)).encode("utf-8")) |
| 656 | h.update(str(x).encode("utf-8")) |
| 657 | elif isinstance(x, np.ndarray | np.number | np.bool_): |
| 658 | x = np.asarray(x) |
| 659 | h.update(str(x.shape).encode("utf-8")) |
| 660 | h.update(str(x.dtype).encode("utf-8")) |
| 661 | h.update(x.tobytes()) |
| 662 | elif isinstance(x, datetime): |
| 663 | object_hash(_dt_to_stamp(x)) |
| 664 | elif sparse.issparse(x): |
| 665 | h.update(str(type(x)).encode("utf-8")) |
| 666 | if not isinstance(x, sparse.csr_array | sparse.csc_array): |
| 667 | raise RuntimeError(f"Unsupported sparse type {type(x)}") |
| 668 | h.update(x.data.tobytes()) |
| 669 | h.update(x.indices.tobytes()) |
| 670 | h.update(x.indptr.tobytes()) |
| 671 | elif hasattr(x, "__len__"): |
| 672 | # all other list-like types |
| 673 | h.update(str(type(x)).encode("utf-8")) |
| 674 | for xx in x: |
| 675 | object_hash(xx, h) |
| 676 | else: |
| 677 | raise RuntimeError(f"unsupported type: {type(x)} ({x})") |
| 678 | return int(h.hexdigest(), 16) |
| 679 | |
| 680 | |
| 681 | def object_size(x, memo=None): |