# Disposable notebook fixtures


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

``` python
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`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#tool_notebook)
builds the shared reader, executor, reviewer, and example shape in code.

[`remove_demo_path`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#remove_demo_path)
cleans up an owned scratch file and tolerates repeated cleanup.

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/fixtures.py#L50"
target="_blank" style="float:right; font-size:smaller">source</a>

### remove_demo_path

``` python
def remove_demo_path(
    path
):
```

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

[`demo_path`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#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`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#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.

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/fixtures.py#L58"
target="_blank" style="float:right; font-size:smaller">source</a>

### demo_path

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

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

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

[`demo_path`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#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:

``` python
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`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#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.

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/fixtures.py#L68"
target="_blank" style="float:right; font-size:smaller">source</a>

### demo_path_context

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

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

``` python
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`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#path_candidates)
preserves that lookup order and leaves absolute paths unchanged.

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/fixtures.py#L75"
target="_blank" style="float:right; font-size:smaller">source</a>

### path_candidates

``` python
def path_candidates(
    path
):
```

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

``` python
_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`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#is_valid_ipynb)
keeps that contract small and dependency-free.

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/fixtures.py#L103"
target="_blank" style="float:right; font-size:smaller">source</a>

### is_valid_ipynb

``` python
def is_valid_ipynb(
    path
):
```

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

[`write_demo_notebook`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#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`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#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.

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/fixtures.py#L116"
target="_blank" style="float:right; font-size:smaller">source</a>

### write_demo_notebook

``` python
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`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#tool_notebook)
keeps the shared reader, executor, reviewer, and test shape in code.
Each test writes a fresh notebook and removes it afterwards.

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/fixtures.py#L128"
target="_blank" style="float:right; font-size:smaller">source</a>

### tool_notebook

``` python
def tool_notebook():
```

*Return the canonical notebook used by tool tests and examples.*

[`tool_notebook`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#tool_notebook)
provides a small notebook with stable addresses for examples and tool
tests.

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

[`write_tool_notebook`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#write_tool_notebook)
gives each example and test an isolated copy of the canonical tool
notebook.

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/fixtures.py#L144"
target="_blank" style="float:right; font-size:smaller">source</a>

### write_tool_notebook

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

*Write the canonical tool notebook for one example or test.*

``` python
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`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#write_demo_file)
keeps temporary conversion inputs out of the repository.

------------------------------------------------------------------------

<a
href="https://github.com/MorsCerta-crypto/nbskill/blob/main/nbskill/fixtures.py#L153"
target="_blank" style="float:right; font-size:smaller">source</a>

### write_demo_file

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

*Write one disposable text file for an example or test.*

``` python
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`](https://MorsCerta-crypto.github.io/nbskill/fixtures.html#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:

``` python
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
```

``` python
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"))
```
