INNER CODE UNIT · Python

digest

i3T4AN/KADATH · kadath/engine.py:39

def digest(value: str) -> str:
    return hashlib.sha256(value.encode()).hexdigest()


def hash_file(path: Path) -> tuple[int, str]:
    hasher = hashlib.sha256(); size = 0
    with path.open("rb") as stream:
        while True:
            chunk = stream.read(1024 * 1024)
            if not chunk: break
            size += len(chunk); hasher.update(chunk)
    return size, hasher.hexdigest()


def atomic_write_json(path: Path, payload: Any) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    temporary = path.with_name(path.name + ".tmp-" + uuid.uuid4().hex)
    encoded = json.dumps(payload, indent=2, sort_keys=True, default=str)

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…