mathy/mathstream/number.py
2025-11-05 07:52:44 +01:00

28 lines
906 B
Python

import hashlib
from pathlib import Path
LOG_DIR = Path("./instance/log")
class StreamNumber:
def __init__(self, file_path):
self.path = Path(file_path)
if not self.path.exists():
raise FileNotFoundError(self.path)
self.hash = hashlib.sha1(str(self.path).encode()).hexdigest()[:10]
def __repr__(self):
return f"<StreamNumber {self.path.name}>"
def stream(self, chunk_size=4096):
"""Yield chunks of digits as strings."""
with open(self.path, "r", encoding="utf-8") as f:
while chunk := f.read(chunk_size):
yield chunk.strip().replace(",", ".")
def write_stage(self, stage, data: str):
"""Write intermediate stage result."""
stage_file = LOG_DIR / f"{self.hash}_stage_{stage}.bin"
with open(stage_file, "wb") as f:
f.write(data.encode())
return stage_file