mirror of
https://github.com/allaunthefox/Research-Stack.git
synced 2026-08-13 12:40:35 +00:00
Create 4-Infrastructure/lib/ with canonical implementations of:
- hashing.py: sha256_bytes, sha256_text, sha256_file
- q16.py: Q16_16 fixed-point constants and arithmetic
- jsonl.py: load_json, load_jsonl, write_jsonl, stable_json, canonical_json_bytes
- fraction_utils.py: Fraction serialization helpers for hardware probes
Refactor 66 files across 4-Infrastructure/{hardware,shim,infra} and
5-Applications/{scripts,tools-scripts,hutter_prize,text-to-cad} to import
from the shared library instead of maintaining local copies.
4-Infrastructure/auto/lib/q16.py now re-exports from lib.q16.
Net: -743 lines (490 added, 1233 removed)
Build: not applicable (Python-only change, py_compile verified on all 71 files)
Co-Authored-By: Allaun Silverfox <bigdataiscoming+9i37y6j2@protonmail.com>
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
"""Shared Fraction serialization helpers for standard-model hardware probes."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from fractions import Fraction
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
def fraction_str(value: Fraction) -> str:
|
|
return str(value.numerator) if value.denominator == 1 else f"{value.numerator}/{value.denominator}"
|
|
|
|
|
|
def fraction_json(value: Fraction) -> dict[str, Any]:
|
|
return {
|
|
"fraction": fraction_str(value),
|
|
"numerator": value.numerator,
|
|
"denominator": value.denominator,
|
|
"decimal": float(value),
|
|
}
|
|
|
|
|
|
def parse_fraction_json(item: dict[str, Any]) -> Fraction:
|
|
return Fraction(int(item["numerator"]), int(item["denominator"]))
|
|
|
|
|
|
def vector_from_json(items: dict[str, dict[str, Any]]) -> dict[str, Fraction]:
|
|
return {key: parse_fraction_json(value) for key, value in items.items()}
|
|
|
|
|
|
def vector_json(vector: dict[str, Fraction]) -> dict[str, dict[str, Any]]:
|
|
return {key: fraction_json(value) for key, value in sorted(vector.items())}
|
|
|
|
|
|
def projection_from_json(
|
|
items: dict[str, dict[str, dict[str, Any]]],
|
|
) -> dict[str, dict[str, Fraction]]:
|
|
return {
|
|
axis: {
|
|
col: parse_fraction_json(cell) for col, cell in row.items()
|
|
}
|
|
for axis, row in items.items()
|
|
}
|