Spaces:
Sleeping
Sleeping
Download O2LaF/o2laf/utils/file_util.py from blanchon/O2LaF-Demo: direct link, hf CLI and curl.
- Browser
- Download file 811 Bytes
-
https://huggingface.co/spaces/blanchon/O2LaF-Demo/resolve/main/O2LaF/o2laf/utils/file_util.py
- Command line
-
hf download hf://spaces/blanchon/O2LaF-Demo/O2LaF/o2laf/utils/file_util.py
-
curl -L -o file_util.py https://huggingface.co/spaces/blanchon/O2LaF-Demo/resolve/main/O2LaF/o2laf/utils/file_util.py
811 Bytes
| """Filesystem helpers.""" | |
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| import os | |
| from pathlib import Path | |
| def sha256_file(path: str | os.PathLike) -> str: | |
| """SHA256 of a file, read incrementally so large checkpoints stay out of memory.""" | |
| digest = hashlib.sha256() | |
| with Path(path).open('rb') as handle: | |
| while chunk := handle.read(8 << 20): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def write_json_atomic(path: str | os.PathLike, payload) -> None: | |
| """Write ``payload`` as JSON via a temporary file and an atomic rename.""" | |
| path = Path(path) | |
| temporary = path.with_suffix(path.suffix + f'.tmp.{os.getpid()}') | |
| temporary.write_text(json.dumps(payload, indent=2, sort_keys=True) + '\n', encoding='utf-8') | |
| os.replace(temporary, path) | |