Download Biomanus-V2/Experiment1/scripts/common.py from czty/BioOcto: direct link, hf CLI and curl.
- Browser
- Download file 1.75 kB
-
https://huggingface.co/datasets/czty/BioOcto/resolve/main/Biomanus-V2/Experiment1/scripts/common.py
- Command line
-
hf download hf://datasets/czty/BioOcto/Biomanus-V2/Experiment1/scripts/common.py
-
curl -L -o common.py https://huggingface.co/datasets/czty/BioOcto/resolve/main/Biomanus-V2/Experiment1/scripts/common.py
1.75 kB
| #!/usr/bin/env python3 | |
| from __future__ import annotations | |
| import csv | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| from typing import Any, Iterable | |
| def sha256_bytes(data: bytes) -> str: | |
| return hashlib.sha256(data).hexdigest() | |
| def sha256_file(path: Path) -> str: | |
| digest = hashlib.sha256() | |
| with path.open("rb") as handle: | |
| for chunk in iter(lambda: handle.read(1024 * 1024), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def stable_key(seed: int, value: str) -> str: | |
| return hashlib.sha256(f"{seed}:{value}".encode("utf-8")).hexdigest() | |
| def read_json(path: Path) -> Any: | |
| return json.loads(path.read_text(encoding="utf-8")) | |
| def write_json(path: Path, value: Any) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(json.dumps(value, indent=2, ensure_ascii=False) + "\n", encoding="utf-8") | |
| def write_jsonl(path: Path, rows: Iterable[dict[str, Any]]) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| with path.open("w", encoding="utf-8") as handle: | |
| for row in rows: | |
| handle.write(json.dumps(row, ensure_ascii=False, sort_keys=True) + "\n") | |
| def write_csv(path: Path, rows: list[dict[str, Any]], fields: list[str] | None = None) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| if fields is None: | |
| fields = sorted({key for row in rows for key in row}) | |
| with path.open("w", encoding="utf-8", newline="") as handle: | |
| writer = csv.DictWriter(handle, fieldnames=fields, extrasaction="ignore") | |
| writer.writeheader() | |
| writer.writerows(rows) | |
| def resolve_from(base: Path, value: str) -> Path: | |
| path = Path(value) | |
| return path if path.is_absolute() else (base / path).resolve() | |