"""Compare two policy.json files structurally: same keys, same choices and labels, numbers equal within a tolerance. python tests/parity/compare_policy.py A.json B.json [--tol 1e-9] """ import json import math import sys def walk(a, b, path, out, tol): if isinstance(a, dict) and isinstance(b, dict): for k in sorted(set(a) | set(b)): if k == "id" and path == "": continue if k not in a or k not in b: out["keys"].append(f"{path}/{k} only in {'B' if k not in a else 'A'}") continue walk(a[k], b[k], f"{path}/{k}", out, tol) elif isinstance(a, list) and isinstance(b, list): if len(a) != len(b): out["other"].append(f"{path}: length {len(a)} vs {len(b)}") for i, (x, y) in enumerate(zip(a, b)): walk(x, y, f"{path}[{i}]", out, tol) elif isinstance(a, (int, float)) and isinstance(b, (int, float)) and not isinstance(a, bool) and not isinstance(b, bool): d = abs(a - b) / max(1.0, abs(a), abs(b)) out["max"] = max(out["max"], d) if d > tol: out["numbers"].append(f"{path}: {a} vs {b}") elif a != b: out["other"].append(f"{path}: {a!r} vs {b!r}") def compare(fa, fb, tol=1e-9): out = {"keys": [], "numbers": [], "other": [], "max": 0.0} walk(json.load(open(fa)), json.load(open(fb)), "", out, tol) return out if __name__ == "__main__": tol = float(sys.argv[sys.argv.index("--tol") + 1]) if "--tol" in sys.argv else 1e-9 o = compare(sys.argv[1], sys.argv[2], tol) for k in ("keys", "other", "numbers"): if o[k]: print(f"{len(o[k])} {k} differ, e.g.:", *o[k][:8], sep="\n ") print(f"largest relative difference: {o['max']:.3g}") sys.exit(1 if o["keys"] or o["other"] or o["numbers"] else 0)