Skip to main content

Disposable notebook fixtures

Own scratch paths and test notebooks for one example or test lifetime.
from fastcore.test import test_eq
from fastcore.nbio import read_nb

Demo scratch files

Examples and tests in later notebooks need small notebooks they can safely mutate. Scratch helpers create disposable artifacts in the system temporary directory and remove them after use, so notebook test runners never create repository data folders. tool_notebook builds the shared reader, executor, reviewer, and example shape in code.

remove_demo_path cleans up an owned scratch file and tolerates repeated cleanup.


source

remove_demo_path

def remove_demo_path(
    path
):

Remove a disposable file or directory if present and return its path.

demo_path names a scratch artifact in the system temporary directory by default and optionally removes any previous copy. It only creates the parent folder; helpers that write content decide what belongs at the path. An explicit base remains available when an example needs a chosen location.

Use demo_path_context when a test needs an arbitrary scratch file or directory and should clean it up after the block. That keeps cleanup policy in one helper instead of repeating try/finally in every test.


source

demo_path

def demo_path(
    name, base:NoneType=None, reset:bool=True
):

Return a scratch path in the system temporary directory or base, optionally resetting it.

obsolete = demo_path("obsolete.txt")
obsolete.write_text("scratch")
remove_demo_path(obsolete)
assert not obsolete.exists()

demo_path reserves a scratch filename and creates its parent directory. It does not create the file. The path stays available to whichever helper writes the example content:

scratch_path = demo_path("foundation_scratch_example.txt")
assert scratch_path.parent.exists() and not scratch_path.exists()
scratch_path.name

Use demo_path_context when a lesson owns a disposable file or directory. Cleanup runs on both success and failure; the yielded path is not retained after the block.


source

demo_path_context

def demo_path_context(
    name, base:NoneType=None, reset:bool=True
):

Yield a demo path and remove it when the block exits.

with demo_path_context("foundation_demo_path_context") as context_path:
    context_path.write_text("temporary evidence", encoding="utf-8")
    assert context_path.read_text(encoding="utf-8") == "temporary evidence"
assert not context_path.exists()
dict(name=context_path.name, exists_after_context=context_path.exists())

Notebook examples can run from the repository root or from nbs/. path_candidates preserves that lookup order and leaves absolute paths unchanged.


source

path_candidates

def path_candidates(
    path
):

Return cwd-relative path candidates for root and notebook-dir execution.

_path_candidate_rows = path_candidates("nbs/00_foundation.ipynb")
expected_candidates = [Path("nbs/00_foundation.ipynb")]
if Path.cwd().name == "nbs": expected_candidates.append(Path("00_foundation.ipynb"))
assert _path_candidate_rows == expected_candidates
assert path_candidates(Path("/tmp/example.ipynb")) == [Path("/tmp/example.ipynb")]

Notebook helpers should fail gently when a path is missing, unreadable, or shaped like ordinary JSON instead of a Jupyter notebook. is_valid_ipynb keeps that contract small and dependency-free.


source

is_valid_ipynb

def is_valid_ipynb(
    path
):

Return whether a file has the required notebook and cell structure.

write_demo_notebook owns the whole lifetime of a scratch notebook: create it, yield its path to the example, then remove it even if the example fails.

write_demo_notebook owns a unique temporary directory when base is omitted. Nested lessons can use the same filename without overwriting each other. Isolation also prevents recursive inspection from discovering another concurrently running lesson’s files during cleanup. An explicit base retains the caller-selected directory.


source

write_demo_notebook

def write_demo_notebook(
    name, cells:NoneType=None, base:NoneType=None, reset:bool=True
):

Write a disposable notebook with an isolated default directory and clean it up.

tool_notebook keeps the shared reader, executor, reviewer, and test shape in code. Each test writes a fresh notebook and removes it afterwards.


source

tool_notebook

def tool_notebook():

Return the canonical notebook used by tool tests and examples.

tool_notebook provides a small notebook with stable addresses for examples and tool tests.

tool_example = tool_notebook()
assert tool_example.cells[0].id == "tool-title"
[cell.id for cell in tool_example.cells]

write_tool_notebook gives each example and test an isolated copy of the canonical tool notebook.


source

write_tool_notebook

def write_tool_notebook(
    name:str='tool.ipynb', base:NoneType=None
):

Write the canonical tool notebook for one example or test.

with write_tool_notebook("fixture_tour.ipynb") as fixture_path:
    fixture_cells = read_nb(fixture_path).cells
    assert fixture_cells[0].id == "tool-title"
[cell.id for cell in fixture_cells]

write_demo_file keeps temporary conversion inputs out of the repository.


source

write_demo_file

def write_demo_file(
    name, source, base:NoneType=None
):

Write one disposable text file for an example or test.

with write_demo_file("lesson.py", "answer = 42") as lesson_path:
    lesson_source = lesson_path.read_text()
    assert lesson_source == "answer = 42"
lesson_source

write_demo_notebook creates a real temporary .ipynb and removes it when the context closes. Examples can check the actual file format and cleanup without leaving repository artifacts:

with write_demo_notebook("00_foundation_tour.ipynb") as path:
    demo_nb = read_nb(path)
    assert is_valid_ipynb(path)
    fixture_summary = dict(cells=len(demo_nb.cells), types=[cell.cell_type for cell in demo_nb.cells])
assert not path.exists()
fixture_summary
with write_demo_notebook("00_foundation_bad.ipynb") as bad_path:
    bad_path.write_text("{}", encoding="utf-8")
    assert not is_valid_ipynb(bad_path)
    assert not is_valid_ipynb(bad_path.with_suffix(".missing.ipynb"))