Compute all differences between two python variables. Parameters ---------- a : object Currently supported: class, dict, list, tuple, ndarray, int, str, bytes, float, StringIO, BytesIO. b : object Must be same type as ``a``. pre : str String to pr
(a, b, pre="", *, allclose=False)
| 758 | |
| 759 | |
| 760 | def object_diff(a, b, pre="", *, allclose=False): |
| 761 | """Compute all differences between two python variables. |
| 762 | |
| 763 | Parameters |
| 764 | ---------- |
| 765 | a : object |
| 766 | Currently supported: class, dict, list, tuple, ndarray, |
| 767 | int, str, bytes, float, StringIO, BytesIO. |
| 768 | b : object |
| 769 | Must be same type as ``a``. |
| 770 | pre : str |
| 771 | String to prepend to each line. |
| 772 | allclose : bool |
| 773 | If True (default False), use assert_allclose. |
| 774 | |
| 775 | Returns |
| 776 | ------- |
| 777 | diffs : str |
| 778 | A string representation of the differences. |
| 779 | """ |
| 780 | pd = _check_pandas_installed(strict=False) |
| 781 | out = "" |
| 782 | if type(a) is not type(b): |
| 783 | # Deal with NamedInt and NamedFloat |
| 784 | for sub in (int, float): |
| 785 | if isinstance(a, sub) and isinstance(b, sub): |
| 786 | break |
| 787 | else: |
| 788 | return f"{pre} type mismatch ({type(a)}, {type(b)})\n" |
| 789 | if inspect.isclass(a): |
| 790 | if inspect.isclass(b) and a != b: |
| 791 | return f"{pre} class mismatch ({a}, {b})\n" |
| 792 | elif isinstance(a, dict): |
| 793 | k1s = _sort_keys(a) |
| 794 | k2s = _sort_keys(b) |
| 795 | m1 = set(k2s) - set(k1s) |
| 796 | if len(m1): |
| 797 | out += pre + f" left missing keys {m1}\n" |
| 798 | for key in k1s: |
| 799 | if key not in k2s: |
| 800 | out += pre + f" right missing key {key}\n" |
| 801 | else: |
| 802 | out += object_diff( |
| 803 | a[key], b[key], pre=(pre + f"[{repr(key)}]"), allclose=allclose |
| 804 | ) |
| 805 | elif isinstance(a, list | tuple): |
| 806 | if len(a) != len(b): |
| 807 | out += pre + f" length mismatch ({len(a)}, {len(b)})\n" |
| 808 | else: |
| 809 | for ii, (xx1, xx2) in enumerate(zip(a, b)): |
| 810 | out += object_diff(xx1, xx2, pre + f"[{ii}]", allclose=allclose) |
| 811 | elif isinstance(a, float): |
| 812 | if not _array_equal_nan(a, b, allclose): |
| 813 | out += pre + f" value mismatch ({a}, {b})\n" |
| 814 | elif isinstance(a, str | int | bytes | np.generic): |
| 815 | if a != b: |
| 816 | out += pre + f" value mismatch ({a}, {b})\n" |
| 817 | elif a is None: |