Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
42 changes: 42 additions & 0 deletions .github/normalize_floats_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json
import sys
from pathlib import Path

SIG_DIGITS = 12


def normalize(obj):
if isinstance(obj, float):
# Use 8 significant digits (auto scientific notation if needed)
return float(format(obj, f".{SIG_DIGITS}g"))

elif isinstance(obj, dict):
return {k: normalize(v) for k, v in obj.items()}

elif isinstance(obj, list):
return [normalize(v) for v in obj]

return obj


def process_file(path):
path = Path(path)

try:
with path.open("r") as f:
original = json.load(f)
except Exception:
return # skip invalid JSON

normalized = normalize(original)

# Only write if something actually changed
if normalized != original:
with path.open("w") as f:
json.dump(normalized, f, indent=2, sort_keys=False)
f.write("\n")


if __name__ == "__main__":
for file in sys.argv[1:]:
process_file(file)
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ repos:
args: ["--schemafile", ".github/theory_scheme.json"]
- repo: local
hooks:
- id: normalize-json-floats
name: Normalize JSON floats (12 significant digits)
entry: python .github/normalize_floats_json.py
language: python
types: [json]
files: ^theory/.*\.json$
- id: format-json
name: Format JSON Files
entry: python .github/format_json.py
Expand Down
Loading
Loading