Repository Public
1"""Shared helpers for the fixture generators.23Run a generator with: /opt/homebrew/bin/python3 Tests/<Target>Tests/Generators/<script>.py4Fixtures are written next to the tests in ../Fixtures/<name>.json with full double precision5(repr round-trip). NaN is stored as null and infinities as the strings "inf"/"-inf", because6strict JSON has no literals for them.7"""8import json9import math10import os1112import numpy as np1314FIXTURES = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "Fixtures")151617def clean(o):18 if isinstance(o, dict):19 return {str(k): clean(v) for k, v in o.items()}20 if isinstance(o, (list, tuple)):21 return [clean(v) for v in o]22 if isinstance(o, np.ndarray):23 return clean(o.tolist())24 if isinstance(o, (np.bool_, bool)):25 return bool(o)26 if isinstance(o, (np.integer,)):27 return int(o)28 if isinstance(o, (np.floating, float)):29 f = float(o)30 if math.isnan(f):31 return None32 if math.isinf(f):33 return "inf" if f > 0 else "-inf"34 return f35 if hasattr(o, "to_numpy"): # pandas Series / DataFrame36 return clean(o.to_numpy())37 if hasattr(o, "tolist"):38 return clean(o.tolist())39 if hasattr(o, "_asdict"): # scipy result tuples40 return clean(list(o))41 return o424344def dump(name, obj, fixtures=None):45 path = os.path.join(fixtures or FIXTURES, name + ".json")46 os.makedirs(os.path.dirname(path), exist_ok=True)47 with open(path, "w") as f:48 json.dump(clean(obj), f, allow_nan=False)49 print("wrote", os.path.normpath(path))50