22 lines
853 B
Python
22 lines
853 B
Python
"""What identifies a notebook's content: its cells' sources, without outputs or execution counts."""
|
|
|
|
import copy
|
|
import hashlib
|
|
import json
|
|
|
|
|
|
def without_outputs(notebook: dict) -> dict:
|
|
"""The notebook as written: code cells keep their source and lose their outputs."""
|
|
cleared = copy.deepcopy(notebook)
|
|
for cell in cleared["cells"]:
|
|
if cell["cell_type"] == "code":
|
|
cell["outputs"], cell["execution_count"] = [], None
|
|
return cleared
|
|
|
|
|
|
def notebook_sha256(notebook: dict) -> str:
|
|
"""Changes when a cell's type or source changes, not when the notebook is run again."""
|
|
cells = [[cell["cell_type"], "".join(cell["source"]) if isinstance(cell["source"], list) else cell["source"]]
|
|
for cell in notebook["cells"]]
|
|
return hashlib.sha256(json.dumps(cells, ensure_ascii=False).encode()).hexdigest()
|