Download studies/shared/scripts/write_json_stable.py from lyffseba/xai-studies: direct link, hf CLI and curl.
- Browser
- Download file 924 Bytes
-
https://huggingface.co/datasets/lyffseba/xai-studies/resolve/main/studies/shared/scripts/write_json_stable.py
- Command line
-
hf download hf://datasets/lyffseba/xai-studies/studies/shared/scripts/write_json_stable.py
-
curl -L -o write_json_stable.py https://huggingface.co/datasets/lyffseba/xai-studies/resolve/main/studies/shared/scripts/write_json_stable.py
924 Bytes
| #!/usr/bin/env python3 | |
| """Write JSON only when semantic content changes (ignore generated_at_utc churn).""" | |
| from __future__ import annotations | |
| import json | |
| from copy import deepcopy | |
| from pathlib import Path | |
| def _strip_volatile(obj: dict) -> dict: | |
| data = deepcopy(obj) | |
| data.pop("generated_at_utc", None) | |
| data.pop("fetched", None) # fetch stamp string can churn; keep in FETCHED_AT.txt | |
| return data | |
| def write_json_stable(path: Path, data: dict, *, indent: int = 2) -> bool: | |
| """Return True if file was written/changed.""" | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| text = json.dumps(data, indent=indent) + "\n" | |
| if path.exists(): | |
| try: | |
| old = json.loads(path.read_text()) | |
| if _strip_volatile(old) == _strip_volatile(data): | |
| return False | |
| except (json.JSONDecodeError, OSError): | |
| pass | |
| path.write_text(text) | |
| return True | |