aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-05-24 00:34:46 +0000
committerEugene Triguba <hello@eugenetriguba.com>2026-05-24 00:34:46 +0000
commit5b5ef24910874bf842b91caaa0b7febc46645842 (patch)
tree144cf307473ef3c9409cbd7c0d7b7b9578fea530
parentRename init to pkg-init (diff)
downloadnb-5b5ef24910874bf842b91caaa0b7febc46645842.zip
Refactor core utilities from export into core module
-rw-r--r--nb/clean.py2
-rw-r--r--nb/cli.py2
-rw-r--r--nb/core.py52
-rw-r--r--nb/export.py50
-rw-r--r--nb/test.py14
-rw-r--r--nbs/clean.ipynb2
-rw-r--r--nbs/cli.ipynb5
-rw-r--r--nbs/core.ipynb374
-rw-r--r--nbs/export.ipynb526
-rw-r--r--nbs/test.ipynb80
10 files changed, 594 insertions, 513 deletions
diff --git a/nb/clean.py b/nb/clean.py
index 1d97104..d90530d 100644
--- a/nb/clean.py
+++ b/nb/clean.py
@@ -4,7 +4,7 @@
import nbformat
from nbformat import NotebookNode
-from nb.export import read_nb, PathOrFileLike
+from nb.core import read_nb,PathOrFileLike
# cell: b436a7c3
def _clean_cell_output(cell:NotebookNode)->NotebookNode:
diff --git a/nb/cli.py b/nb/cli.py
index 385307d..350ca3c 100644
--- a/nb/cli.py
+++ b/nb/cli.py
@@ -7,7 +7,7 @@ from pathlib import Path
import nbformat
from nb import export_nb,clean_nb
-from nb.export import read_nb
+from nb.core import read_nb
from nb.config import get_config,Config
# cell: 96737c5a
diff --git a/nb/core.py b/nb/core.py
index 559c259..b1373f4 100644
--- a/nb/core.py
+++ b/nb/core.py
@@ -1,9 +1,59 @@
# AUTOGENERATED! DO NOT EDIT!
# cell: 6a2cc7f8-6c65-4a7c-890c-3cf9d1cd314e
-import inspect
+import enum,inspect,re
from typing import Callable
+import nbformat
+from nbformat import NotebookNode
+
+# cell: d9d0a18c
+type PathOrFileLike = str|Path|IO[str]
+
+# cell: ca71fdf5
+def read_nb(
+ fp:PathOrFileLike, # Where to read the notebook from.
+ v:int=4, # The notebook format major version to read the notebook as.
+)->NotebookNode:
+ "Read in a notebook as a dictionary-like NotebookNode."
+ return nbformat.read(fp, as_version=v)
+
+# cell: 2ffd7bcf
+class CellType(enum.StrEnum):
+ "Possible Jupyter notebook cell types."
+ CODE = 'code'
+ MD = 'markdown'
+ RAW = 'raw'
+
+# cell: a5b24f3a
+def filter_cells(
+ nb:NotebookNode, # A notebook that has been read in via `read_nb`.
+ ct:CellType, # The cells to filter down to.
+)->list[NotebookNode]:
+ "Retrieve specific cell types from a notebook."
+ return list(filter(lambda c: c['cell_type'] == ct, nb['cells']))
+
+# cell: 40433e19
+def mk_directive_re(
+ name:str, # The name of the directive
+ arg_re:str|None=None, # A regex pattern to match an argument for the directive
+)->re.Pattern:
+ "Compile a regex to match a directive, optionally supporting an argument."
+ arg = rf':\s*{arg_re}' if arg_re else ''
+ return re.compile(rf'^\s*#\|\s*{name}{arg}\s*$')
+
+# cell: 535b3ce2
+def has_directive(src:str)->bool:
+ "Check whether a line or block of source code has a nb directive."
+ from nb.export import DIRECTIVES as EXPORT_DIRECTIVES
+ return any(any(r.match(l) for r in EXPORT_DIRECTIVES) for l in src.splitlines())
+
+# cell: d02bf0bc
+def strip_directives(src:str|list[str])->str:
+ "Remove all nb directives from the source code."
+ src = src if isinstance(src, list) else src.splitlines()
+ return "\n".join(filter(lambda l: not has_directive(l), src))
+
# cell: c4875cc0-2d87-4c87-8789-7dc950be06c6
def callable_name(c: Callable) -> str:
"Get the name of the callable `c`."
diff --git a/nb/export.py b/nb/export.py
index 4c869d2..5d0a33c 100644
--- a/nb/export.py
+++ b/nb/export.py
@@ -8,61 +8,17 @@ from typing import IO
import nbformat
from nbformat import NotebookNode
-# cell: 29a65b22
-type PathOrFileLike = str|Path|IO[str]
-
-# cell: 27202360
-def read_nb(
- fp:PathOrFileLike, # Where to read the notebook from.
- v:int=4, # The notebook format major version to read the notebook as.
-)->NotebookNode:
- "Read in a notebook as a dictionary-like NotebookNode."
- return nbformat.read(fp, as_version=v)
-
-# cell: 1ee0d459
-class CellType(enum.StrEnum):
- "Possible Jupyter notebook cell types."
- CODE = 'code'
- MD = 'markdown'
- RAW = 'raw'
-
-# cell: 133ebd88
-def filter_cells(
- nb:NotebookNode, # A notebook that has been read in via `read_nb`.
- ct:CellType, # The cells to filter down to.
-)->list[NotebookNode]:
- "Retrieve specific cell types from a notebook."
- return list(filter(lambda c: c['cell_type'] == ct, nb['cells']))
-
-# cell: 83a898c2
-def _mk_directive_re(
- name:str, # The name of the directive
- arg_re:str|None=None, # A regex pattern to match an argument for the directive
-)->re.Pattern:
- "Compile a regex to match a directive, optionally supporting an argument."
- arg = rf':\s*{arg_re}' if arg_re else ''
- return re.compile(rf'^\s*#\|\s*{name}{arg}\s*$')
+from nb.core import read_nb,CellType,filter_cells,mk_directive_re,strip_directives,PathOrFileLike
# cell: ac846aaa
-DIRECTIVE_DEFAULT_EXP_RE = _mk_directive_re('default_exp', arg_re=r'(?P<module>\w+(\.\w+)*)')
+DIRECTIVE_DEFAULT_EXP_RE = mk_directive_re('default_exp', arg_re=r'(?P<module>\w+(\.\w+)*)')
# cell: 5b4e8fe6
-DIRECTIVE_EXPORT_RE = _mk_directive_re('export')
+DIRECTIVE_EXPORT_RE = mk_directive_re('export')
# cell: 828159b2
DIRECTIVES = [DIRECTIVE_DEFAULT_EXP_RE, DIRECTIVE_EXPORT_RE]
-# cell: d327883e
-def has_directive(src:str)->bool:
- "Check whether a line or block of source code has a nb directive."
- return any(any(r.match(l) for r in DIRECTIVES) for l in src.splitlines())
-
-# cell: b6f47497
-def strip_directives(src:str|list[str])->str:
- "Remove all nb directives from the source code."
- src = src if isinstance(src, list) else src.splitlines()
- return "\n".join(filter(lambda l: not has_directive(l), src))
-
# cell: d1f32654
def is_cell_exported(cell:NotebookNode)->bool:
"Check whether the code cell should be exported."
diff --git a/nb/test.py b/nb/test.py
index b70ff1f..6f3e9f1 100644
--- a/nb/test.py
+++ b/nb/test.py
@@ -67,8 +67,8 @@ def expect(
if negstr is None: negstr = f'not {cmp.__name__}'
args_src, _ = get_call_args(frames_up)
lines = [f"{repr(a)} {negstr} {repr(b)}"]
- if repr(a) != args_src[0]: lines.append(f" where {repr(a)} = {args_src[0]}")
- if repr(b) != args_src[1]: lines.append(f" where {repr(b)} = {args_src[1]}")
+ if len(args_src) > 0 and repr(a) != args_src[0]: lines.append(f" where {repr(a)} = {args_src[0]}")
+ if len(args_src) > 1 and repr(b) != args_src[1]: lines.append(f" where {repr(b)} = {args_src[1]}")
assert cmp(a, b), "\n".join(lines)
# cell: d1514687
@@ -86,6 +86,16 @@ def nequal(a, b):
"Assert that a != b"
expect(a, b, operator.ne, negstr='==', frames_up=2)
+# cell: 9523b71e
+def is_true(a):
+ "Assert that a is truthy."
+ expect(bool(a), True, operator.eq, negstr='is not truthy', frames_up=2)
+
+# cell: f76c0add
+def is_false(a):
+ "Assert that a is falsy."
+ expect(bool(a), False, operator.eq, negstr='is not falsy', frames_up=2)
+
# cell: 664f33bf
@contextlib.contextmanager
def env(**kwargs) -> Generator[None, None, None]:
diff --git a/nbs/clean.ipynb b/nbs/clean.ipynb
index 67519a0..ced2c8c 100644
--- a/nbs/clean.ipynb
+++ b/nbs/clean.ipynb
@@ -33,7 +33,7 @@
"import nbformat\n",
"from nbformat import NotebookNode\n",
"\n",
- "from nb.export import read_nb, PathOrFileLike"
+ "from nb.core import read_nb,PathOrFileLike"
]
},
{
diff --git a/nbs/cli.ipynb b/nbs/cli.ipynb
index c6a8df9..7126e7c 100644
--- a/nbs/cli.ipynb
+++ b/nbs/cli.ipynb
@@ -46,7 +46,7 @@
"import nbformat\n",
"\n",
"from nb import export_nb,clean_nb\n",
- "from nb.export import read_nb\n",
+ "from nb.core import read_nb\n",
"from nb.config import get_config,Config"
]
},
@@ -298,7 +298,8 @@
" mk_nb_cfg(tmp, nb_dir=tmp, export_dir=tmp)\n",
" run_cli('export', '--clean')\n",
" test.equal((Path(tmp)/'tmp.py').read_text(), mk_nb_src(DIRTY_VALID_EXPORTED_CELLS))\n",
- " test.equal(cleaned.cells[1].execution_count, None)"
+ " with open(nb_path) as f: cleaned_nb = nbformat.v4.reads(f.read())\n",
+ " test.equal(cleaned_nb.cells[1].execution_count, None)"
]
},
{
diff --git a/nbs/core.ipynb b/nbs/core.ipynb
index b3f58f4..6e050ae 100644
--- a/nbs/core.ipynb
+++ b/nbs/core.ipynb
@@ -30,8 +30,11 @@
"outputs": [],
"source": [
"#| export\n",
- "import inspect\n",
- "from typing import Callable"
+ "import enum,inspect,re\n",
+ "from typing import Callable\n",
+ "\n",
+ "import nbformat\n",
+ "from nbformat import NotebookNode"
]
},
{
@@ -41,6 +44,9 @@
"metadata": {},
"outputs": [],
"source": [
+ "import contextlib\n",
+ "from pathlib import Path\n",
+ "from tempfile import TemporaryDirectory\n",
"from nb import test"
]
},
@@ -55,6 +61,370 @@
},
{
"cell_type": "markdown",
+ "id": "bdc4df64",
+ "metadata": {},
+ "source": [
+ "## Notebooks"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3f99ca39",
+ "metadata": {},
+ "source": [
+ "### Reading Notebooks"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "268db6ac",
+ "metadata": {},
+ "source": [
+ "To read in `ipynb` files, we can use a library provided Jupyter called `nbformat`."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d9d0a18c",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "type PathOrFileLike = str|Path|IO[str]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ca71fdf5",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "def read_nb(\n",
+ " fp:PathOrFileLike, # Where to read the notebook from.\n",
+ " v:int=4, # The notebook format major version to read the notebook as.\n",
+ ")->NotebookNode:\n",
+ " \"Read in a notebook as a dictionary-like NotebookNode.\"\n",
+ " return nbformat.read(fp, as_version=v)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "b4ca48d4",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "nb = nbformat.v4.new_notebook()\n",
+ "with TemporaryDirectory() as tmp, contextlib.chdir(tmp):\n",
+ " with open('nb.ipynb', mode='w+') as fp: nbformat.write(nb, fp)\n",
+ " for fp in ['nb.ipynb', Path('nb.ipynb')]:\n",
+ " nb = read_nb(fp)\n",
+ " test.equal(list(nb.keys()), ['cells', 'metadata', 'nbformat', 'nbformat_minor'])\n",
+ " test.equal(type(nb), NotebookNode)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "a9abdd44",
+ "metadata": {},
+ "source": [
+ "We can see that the top-level format of a notebook is:\n",
+ "- `cells`: A list of objects for each cell in the notebook.\n",
+ "- `metadata`: Free-form notebook level metadata used by tools and editors.\n",
+ "- `nbformat`: Notebook format major version\n",
+ "- `nbformat_minor`: Notebook format minor version"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "772d813e",
+ "metadata": {},
+ "source": [
+ "### Filtering Notebooks"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "8cf77bcc",
+ "metadata": {},
+ "source": [
+ "Within cells, there are generally 3 types of cells:\n",
+ "- `code`\n",
+ "- `markdown`\n",
+ "- `raw`"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2ffd7bcf",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "class CellType(enum.StrEnum):\n",
+ " \"Possible Jupyter notebook cell types.\"\n",
+ " CODE = 'code'\n",
+ " MD = 'markdown'\n",
+ " RAW = 'raw'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a5b24f3a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "def filter_cells(\n",
+ " nb:NotebookNode, # A notebook that has been read in via `read_nb`.\n",
+ " ct:CellType, # The cells to filter down to.\n",
+ ")->list[NotebookNode]:\n",
+ " \"Retrieve specific cell types from a notebook.\"\n",
+ " return list(filter(lambda c: c['cell_type'] == ct, nb['cells']))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "807f0146",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "nb = nbformat.v4.new_notebook(cells=[\n",
+ " nbformat.v4.new_code_cell(),\n",
+ " nbformat.v4.new_markdown_cell(),\n",
+ " nbformat.v4.new_raw_cell(),\n",
+ "])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "5b89c5d8",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "code_cell = filter_cells(nb, CellType.CODE)[0]\n",
+ "test.equal(list(code_cell.keys()), ['id', 'cell_type', 'metadata', 'execution_count', 'source', 'outputs'])\n",
+ "test.equal(code_cell['cell_type'], CellType.CODE)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "170144a7",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "md_cell = filter_cells(nb, CellType.MD)[0]\n",
+ "test.equal(list(md_cell.keys()), ['id', 'cell_type', 'source', 'metadata'])\n",
+ "test.equal(md_cell['cell_type'], CellType.MD)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "edbe00dd",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "raw_cell = filter_cells(nb, CellType.RAW)[0]\n",
+ "test.equal(list(raw_cell.keys()), ['id', 'cell_type', 'source', 'metadata'])\n",
+ "test.equal(raw_cell['cell_type'], CellType.RAW)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "558e2214",
+ "metadata": {},
+ "source": [
+ "Within each cell object, they will all have:\n",
+ "- `id`: A unique identifier for that cell.\n",
+ "- `cell_type`: One of the 3 cell type names.\n",
+ "- `source`: The content of the cell, whether it is prose or code.\n",
+ "- `metadata`: Free-form metadata for that cell used by various tools/editors.\n",
+ "\n",
+ "Additionally, `code` cell types will have:\n",
+ "- `outputs`: The stdout/stderr of the cell after the code is executed.\n",
+ "- `execution_count`: The number of times the cell was run."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "e4d56d07",
+ "metadata": {},
+ "source": [
+ "### Directives"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "97162699",
+ "metadata": {},
+ "source": [
+ "A regular expression ran against each line in a cell should allow us to find each directive we support. Since we'll need to re-create this regression expression for a few directives, we'll create a small helper."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "40433e19",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "def mk_directive_re(\n",
+ " name:str, # The name of the directive\n",
+ " arg_re:str|None=None, # A regex pattern to match an argument for the directive\n",
+ ")->re.Pattern:\n",
+ " \"Compile a regex to match a directive, optionally supporting an argument.\"\n",
+ " arg = rf':\\s*{arg_re}' if arg_re else ''\n",
+ " return re.compile(rf'^\\s*#\\|\\s*{name}{arg}\\s*$')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ac907b7c",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "_TEST_DIRECTIVE_RE = mk_directive_re('test')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "40f7de66",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "test_dir_match = [\n",
+ " ' #| test \\t \\n', # Lots of whitespace around each token\n",
+ " '#|test', # No whitespace around each token\n",
+ "]\n",
+ "for line in test_dir_match:\n",
+ " test.is_true(_TEST_DIRECTIVE_RE.match(line))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f115789b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "test_dir_no_match = [\n",
+ " '| test', # Missing #\n",
+ " '# test', # Missing |\n",
+ " '#| testt', # Misspelled directive\n",
+ " '#| test test', # Extra word after directive\n",
+ "]\n",
+ "for line in test_dir_no_match:\n",
+ " test.is_false(_TEST_DIRECTIVE_RE.match(line))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "3f58f798",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "_TEST_ARG_DIRECTIVE_RE = mk_directive_re('test', arg_re=r'\\w')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2bbc9f93",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "test_dir_arg_match = [\n",
+ " ' #| test: \\t a \\n', # Lots of whitespace around each token\n",
+ " '#|test:b', # No whitespace around each token\n",
+ "]\n",
+ "for line in test_dir_arg_match:\n",
+ " test.is_true(_TEST_ARG_DIRECTIVE_RE.match(line))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "c994255e",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "test_arg_dir_no_match = [\n",
+ " '#| test a', # Missing :\n",
+ " '#| test: ', # Missing arg\n",
+ " '#| test: aa', # Invalid arg (should be only 1 letter)\n",
+ "]\n",
+ "for line in test_dir_no_match + test_arg_dir_no_match:\n",
+ " test.is_false(_TEST_ARG_DIRECTIVE_RE.match(line))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "535b3ce2",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "def has_directive(src:str)->bool:\n",
+ " \"Check whether a line or block of source code has a nb directive.\"\n",
+ " from nb.export import DIRECTIVES as EXPORT_DIRECTIVES\n",
+ " return any(any(r.match(l) for r in EXPORT_DIRECTIVES) for l in src.splitlines())"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "b42fe082",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "test.is_true(has_directive('#| default_exp: bootstrap'))\n",
+ "test.is_true(has_directive('#| export'))\n",
+ "test.is_true(has_directive('max(1,20)\\n#| export'))\n",
+ "test.is_false(has_directive('# comment\\nmax(1, 20)\\n'))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d02bf0bc",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "def strip_directives(src:str|list[str])->str:\n",
+ " \"Remove all nb directives from the source code.\"\n",
+ " src = src if isinstance(src, list) else src.splitlines()\n",
+ " return \"\\n\".join(filter(lambda l: not has_directive(l), src))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "9eca0c79",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "test.equal(strip_directives('#| export\\na=5'), 'a=5')\n",
+ "test.equal(strip_directives('#| default_exp: bootstrap\\na=5'), 'a=5')\n",
+ "test.equal(strip_directives(['a=10', 'a+=10', '#|export']), 'a=10\\na+=10')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
"id": "2b6822dd",
"metadata": {},
"source": [
diff --git a/nbs/export.ipynb b/nbs/export.ipynb
index 87e7ade..1c7cdb9 100644
--- a/nbs/export.ipynb
+++ b/nbs/export.ipynb
@@ -35,7 +35,9 @@
"from typing import IO\n",
"\n",
"import nbformat\n",
- "from nbformat import NotebookNode"
+ "from nbformat import NotebookNode\n",
+ "\n",
+ "from nb.core import read_nb,CellType,filter_cells,mk_directive_re,strip_directives,PathOrFileLike"
]
},
{
@@ -46,7 +48,8 @@
"outputs": [],
"source": [
"import os,json,tempfile\n",
- "from io import StringIO"
+ "from io import StringIO\n",
+ "from nb import test"
]
},
{
@@ -54,7 +57,7 @@
"id": "a682b39f",
"metadata": {},
"source": [
- "# Exporting Notebooks to Python Files"
+ "# Exporting Notebooks"
]
},
{
@@ -62,13 +65,13 @@
"id": "a21a7e2c",
"metadata": {},
"source": [
- "To get our notebook-driven source code bootstrapped, we'll first need to be able to convert `ipynb` files to `py` files. Without that, we'd be unable to later import code we want to use in one notebook from another notebook because it's not in a plain importable Python file yet.\n",
+ "To support notebook-driven development, we need to be able to convert `ipynb` notebooks to `py` files. Without that, we wouldn't be able to create Python libraries that could be used by others or import code we want to use in one notebook from another notebook because it's not in a plain importable Python file.\n",
"\n",
- "To accomplish that, for any given code well, we'll need to know:\n",
+ "To export notebooks, we'll need to know:\n",
"- What Python file are we exporting our notebook to?\n",
- "- Which cells are we exporting to our Python file?\n",
+ "- Which cells are being exported to a Python file?\n",
"\n",
- "The approach tools like [nbdev](https://nbdev.fast.ai/) or [nbexport](https://github.com/danhorner/nbexport) take is to have certain directives that determine the code export behavior:\n",
+ "The approach tools like [nbdev](https://nbdev.fast.ai/) or [nbexport](https://github.com/danhorner/nbexport) take is to have certain directives that determine the code export behavior. We'll use a similar approach, but we'll only support 2 directives for code export:\n",
"- `#|default_exp: mymodule`: Set the output module for this notebook\n",
"- `#|export`: Export this cell to the corresponding Python file"
]
@@ -78,370 +81,21 @@
"id": "3b670537",
"metadata": {},
"source": [
- "To export a notebook, we'll need to:\n",
- "1. Read in a ipynb notebook\n",
- "2. Retrieve all code cells from the notebook\n",
+ "The general approach we'll use for exporting notebooks to Python files is:\n",
+ "1. Read in a `ipynb` notebook (`nb.core.read_nb`)\n",
+ "2. Retrieve all code cells from the notebook (`nb.core.filter_cells`)\n",
"3. Find all cells that should be exported:\n",
- " - Each notebook must have 1 (and only 1) `#| default_exp`. When a `#| default_exp` is found, it must be a single word e.g. `mymodule` or a dotted path e.g. `mymodules.other_module`. If it is a dotted path, the submodules created should have `__init__.py` files also created alongside them.\n",
- " - Any cells with an `#| export` explicit directive should be marked for export.\n",
+ " - Each notebook must have 1 (and only 1) `#| default_exp`. When a `#| default_exp` is found, it must be a single word (e.g. `mymodule`) or a dotted path (e.g. `mymodules.other_module`). If it is a dotted path, the submodules created should have `__init__.py` files created alongside them.\n",
+ " - Any cells with an `#| export` directive should be marked for export to the generated Python file.\n",
"4. Write out each cell sequentially into the Python file, stripping out any directives. When each cell is written out, it should have a comment above it describing the cell ID it came from."
]
},
{
"cell_type": "markdown",
- "id": "f1c95690",
- "metadata": {},
- "source": [
- "## Reading Notebooks"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "037fb28c",
- "metadata": {},
- "source": [
- "To read in `ipynb` files, we can use a library provided Jupyter called `nbformat`. And simply because we'll be reading in this notebook as our working copy while developing this export behavior, let's add a raw cell just so we have an example of all 3 cell types."
- ]
- },
- {
- "cell_type": "raw",
- "id": "d9f3cb10",
- "metadata": {},
- "source": [
- "This is a raw cell just to have an example."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "4c0671ed",
- "metadata": {},
- "source": [
- "Now, we can read in our current notebook:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "29a65b22",
- "metadata": {},
- "outputs": [],
- "source": [
- "#| export\n",
- "type PathOrFileLike = str|Path|IO[str]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "27202360",
- "metadata": {},
- "outputs": [],
- "source": [
- "#| export\n",
- "def read_nb(\n",
- " fp:PathOrFileLike, # Where to read the notebook from.\n",
- " v:int=4, # The notebook format major version to read the notebook as.\n",
- ")->NotebookNode:\n",
- " \"Read in a notebook as a dictionary-like NotebookNode.\"\n",
- " return nbformat.read(fp, as_version=v)\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "a689a07b",
- "metadata": {},
- "outputs": [],
- "source": [
- "for fp in ['export.ipynb', Path('export.ipynb')]:\n",
- " nb = read_nb(fp)\n",
- " assert list(nb.keys()) == ['cells', 'metadata', 'nbformat', 'nbformat_minor']\n",
- " assert type(nb) == NotebookNode"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "7d133fff",
- "metadata": {},
- "source": [
- "We can see that the top-level format of a notebook is:\n",
- "- `cells`: A list of objects for each cell in the notebook.\n",
- "- `metadata`: Free-form notebook level metadata used by tools and editors.\n",
- "- `nbformat`: Notebook format major version\n",
- "- `nbformat_minor`: Notebook format minor version"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "10a6afcb",
- "metadata": {},
- "source": [
- "Within cells, there are generally 3 types of cells:\n",
- "- `code`\n",
- "- `markdown`\n",
- "- `raw`"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "1ee0d459",
- "metadata": {},
- "outputs": [],
- "source": [
- "#| export\n",
- "class CellType(enum.StrEnum):\n",
- " \"Possible Jupyter notebook cell types.\"\n",
- " CODE = 'code'\n",
- " MD = 'markdown'\n",
- " RAW = 'raw'"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "46ed0445",
- "metadata": {},
- "source": [
- "## Retrieving All Code Cells"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "133ebd88",
- "metadata": {},
- "outputs": [],
- "source": [
- "#| export\n",
- "def filter_cells(\n",
- " nb:NotebookNode, # A notebook that has been read in via `read_nb`.\n",
- " ct:CellType, # The cells to filter down to.\n",
- ")->list[NotebookNode]:\n",
- " \"Retrieve specific cell types from a notebook.\"\n",
- " return list(filter(lambda c: c['cell_type'] == ct, nb['cells']))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "76917bc5",
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'cell_type': 'code',\n",
- " 'execution_count': None,\n",
- " 'id': '07ebb5d3',\n",
- " 'metadata': {},\n",
- " 'outputs': [],\n",
- " 'source': '%%capture\\n%load_ext autoreload\\n%autoreload 2'}"
- ]
- },
- "execution_count": null,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "code_cell = filter_cells(nb, CellType.CODE)[0]\n",
- "assert list(code_cell.keys()) == ['cell_type', 'execution_count', 'id', 'metadata', 'outputs', 'source']\n",
- "assert code_cell['cell_type'] == CellType.CODE\n",
- "code_cell"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "4186fffe",
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'cell_type': 'markdown',\n",
- " 'id': 'a682b39f',\n",
- " 'metadata': {},\n",
- " 'source': '# Exporting Notebooks to Python Files'}"
- ]
- },
- "execution_count": null,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "md_cell = filter_cells(nb, CellType.MD)[0]\n",
- "assert list(md_cell.keys()) == ['cell_type', 'id', 'metadata', 'source']\n",
- "assert md_cell['cell_type'] == CellType.MD\n",
- "md_cell"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "46e56abf",
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'cell_type': 'raw',\n",
- " 'id': 'd9f3cb10',\n",
- " 'metadata': {},\n",
- " 'source': 'This is a raw cell just to have an example.'}"
- ]
- },
- "execution_count": null,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "raw_cell = filter_cells(nb, CellType.RAW)[0]\n",
- "assert list(raw_cell.keys()) == ['cell_type', 'id', 'metadata', 'source']\n",
- "assert raw_cell['cell_type'] == CellType.RAW\n",
- "raw_cell"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "4852b261",
- "metadata": {},
- "source": [
- "Within each cell object, they will all have:\n",
- "- `id`: A unique identifier for that cell.\n",
- "- `cell_type`: One of the 3 cell type names.\n",
- "- `metadata`: Free-form metadata for that cell used by various tools/editors.\n",
- "- `source`: The content of the cell, whether it is prose or code.\n",
- "\n",
- "Additionally, `code` cell types will have:\n",
- "- `outputs`: The stdout/stderr of the cell after the code is executed.\n",
- "- `execution_count`: The number of times the cell was run."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6b7469e3",
- "metadata": {},
- "source": [
- "## Find Code Cells to Export"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "798249d5",
- "metadata": {},
- "source": [
- "### Matching Directives"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "f671cafa",
- "metadata": {},
- "source": [
- "A regular expression ran against each line in a cell should allow us to find each directive we support. Since we'll need to re-create this regression expression for a few directives, we'll create a small helper."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "83a898c2",
- "metadata": {},
- "outputs": [],
- "source": [
- "#| export\n",
- "def _mk_directive_re(\n",
- " name:str, # The name of the directive\n",
- " arg_re:str|None=None, # A regex pattern to match an argument for the directive\n",
- ")->re.Pattern:\n",
- " \"Compile a regex to match a directive, optionally supporting an argument.\"\n",
- " arg = rf':\\s*{arg_re}' if arg_re else ''\n",
- " return re.compile(rf'^\\s*#\\|\\s*{name}{arg}\\s*$')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "698d769b",
+ "id": "6f599791",
"metadata": {},
- "outputs": [],
"source": [
- "_TEST_DIRECTIVE_RE = _mk_directive_re('test')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "f6d654fd",
- "metadata": {},
- "outputs": [],
- "source": [
- "test_dir_match = [\n",
- " ' #| test \\t \\n', # Lots of whitespace around each token\n",
- " '#|test', # No whitespace around each token\n",
- "]\n",
- "for line in test_dir_match:\n",
- " assert _TEST_DIRECTIVE_RE.match(line), f\"No match: {line!r}\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "02f06ee6",
- "metadata": {},
- "outputs": [],
- "source": [
- "test_dir_no_match = [\n",
- " '| test', # Missing #\n",
- " '# test', # Missing |\n",
- " '#| testt', # Misspelled directive\n",
- " '#| test test', # Extra word after directive\n",
- "]\n",
- "for line in test_dir_no_match:\n",
- " assert not _TEST_DIRECTIVE_RE.match(line), f\"Unexpected match: {line!r}\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "285e2320",
- "metadata": {},
- "outputs": [],
- "source": [
- "_TEST_ARG_DIRECTIVE_RE = _mk_directive_re('test', arg_re=r'\\w')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "83fd068c",
- "metadata": {},
- "outputs": [],
- "source": [
- "test_dir_arg_match = [\n",
- " ' #| test: \\t a \\n', # Lots of whitespace around each token\n",
- " '#|test:b', # No whitespace around each token\n",
- "]\n",
- "for line in test_dir_arg_match:\n",
- " assert _TEST_ARG_DIRECTIVE_RE.match(line), f\"No match: {line!r}\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "dd9945ab",
- "metadata": {},
- "outputs": [],
- "source": [
- "test_arg_dir_no_match = [\n",
- " '#| test a', # Missing :\n",
- " '#| test: ', # Missing arg\n",
- " '#| test: aa', # Invalid arg (should be only 1 letter)\n",
- "]\n",
- "for line in test_dir_no_match + test_arg_dir_no_match:\n",
- " assert not _TEST_ARG_DIRECTIVE_RE.match(line), f\"Unexpected match: {line!r}\""
+ "## Directives"
]
},
{
@@ -449,7 +103,7 @@
"id": "dfd6ac40",
"metadata": {},
"source": [
- "#### Supporting the default_exp directive"
+ "### Supporting the default_exp directive"
]
},
{
@@ -460,7 +114,7 @@
"outputs": [],
"source": [
"#| export\n",
- "DIRECTIVE_DEFAULT_EXP_RE = _mk_directive_re('default_exp', arg_re=r'(?P<module>\\w+(\\.\\w+)*)')"
+ "DIRECTIVE_DEFAULT_EXP_RE = mk_directive_re('default_exp', arg_re=r'(?P<module>\\w+(\\.\\w+)*)')"
]
},
{
@@ -476,8 +130,8 @@
"]\n",
"for line, module in matching_cells:\n",
" match = DIRECTIVE_DEFAULT_EXP_RE.match(line)\n",
- " assert match, f\"No match: {line!r}\"\n",
- " assert match.group('module') == module"
+ " test.is_true(match)\n",
+ " test.equal(match.group('module'), module)"
]
},
{
@@ -495,7 +149,7 @@
" '#| default_exp: bootstrap..', # Too many dots without a word in between\n",
"]\n",
"for line in not_matching_cells:\n",
- " assert not DIRECTIVE_DEFAULT_EXP_RE.match(line), f\"Unexpected match: {line!r}\""
+ " test.is_false(DIRECTIVE_DEFAULT_EXP_RE.match(line))"
]
},
{
@@ -503,7 +157,7 @@
"id": "50495b37",
"metadata": {},
"source": [
- "#### Supporting the export directive"
+ "### Supporting the export directive"
]
},
{
@@ -514,7 +168,7 @@
"outputs": [],
"source": [
"#| export\n",
- "DIRECTIVE_EXPORT_RE = _mk_directive_re('export')"
+ "DIRECTIVE_EXPORT_RE = mk_directive_re('export')"
]
},
{
@@ -522,7 +176,7 @@
"id": "a7a778d2",
"metadata": {},
"source": [
- "#### All Directives"
+ "### All Directives"
]
},
{
@@ -541,7 +195,7 @@
"id": "21e3d799",
"metadata": {},
"source": [
- "### Identifying Cells to Export"
+ "## Identifying Cells to Export"
]
},
{
@@ -549,7 +203,7 @@
"id": "295b4a99",
"metadata": {},
"source": [
- "So, we should now have all the pieces to determine find all cells we should be exporting and the module we should be exporting to.\n",
+ "So, we should now have all the pieces to find all cells we should be exporting and the module we should be exporting to.\n",
"\n",
"**Algorithm:**\n",
"1. Always check if there is a `default_exp` directive. There must be one (and only one) `default_exp` directive.\n",
@@ -559,119 +213,93 @@
{
"cell_type": "code",
"execution_count": null,
- "id": "d327883e",
+ "id": "d1f32654",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
- "def has_directive(src:str)->bool:\n",
- " \"Check whether a line or block of source code has a nb directive.\"\n",
- " return any(any(r.match(l) for r in DIRECTIVES) for l in src.splitlines())"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "6ea5e776",
- "metadata": {},
- "outputs": [],
- "source": [
- "assert has_directive('#| default_exp: bootstrap')\n",
- "assert has_directive('#| export')\n",
- "assert has_directive('max(1,20)\\n#| export')\n",
- "assert not has_directive('# comment\\nmax(1, 20)\\n')"
+ "def is_cell_exported(cell:NotebookNode)->bool:\n",
+ " \"Check whether the code cell should be exported.\"\n",
+ " return any(DIRECTIVE_EXPORT_RE.match(l) for l in cell['source'].splitlines())"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "b6f47497",
+ "id": "a61db327",
"metadata": {},
"outputs": [],
"source": [
- "#| export\n",
- "def strip_directives(src:str|list[str])->str:\n",
- " \"Remove all nb directives from the source code.\"\n",
- " src = src if isinstance(src, list) else src.splitlines()\n",
- " return \"\\n\".join(filter(lambda l: not has_directive(l), src))"
+ "test.is_true(is_cell_exported(nbformat.v4.new_code_cell('#| export')))\n",
+ "test.is_true(is_cell_exported(nbformat.v4.new_code_cell('import re\\n#| export')))"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "7acbb8ff",
+ "id": "b06462cc",
"metadata": {},
"outputs": [],
"source": [
- "assert strip_directives('#| export\\na=5') == 'a=5'\n",
- "assert strip_directives('#| default_exp: bootstrap\\na=5') == 'a=5'\n",
- "assert strip_directives(['a=10', 'a+=10', '#|export']) == 'a=10\\na+=10'"
+ "test.is_false(is_cell_exported(nbformat.v4.new_code_cell(('import re'))))\n",
+ "test.is_false(is_cell_exported(nbformat.v4.new_code_cell(('1+1\\nfrom re import Pattern'))))"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "d1f32654",
+ "id": "ddc5c9e6",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
- "def is_cell_exported(cell:NotebookNode)->bool:\n",
- " \"Check whether the code cell should be exported.\"\n",
- " return any(DIRECTIVE_EXPORT_RE.match(l) for l in cell['source'].splitlines())"
+ "def get_default_export(cells:list[NotebookNode])->str:\n",
+ " \"Retrieve the `default_exp` directive argument from a list of cells.\"\n",
+ " matches = [\n",
+ " m.group('module')\n",
+ " for c in cells\n",
+ " for l in c['source'].splitlines()\n",
+ " if c['cell_type'] == CellType.CODE and (m:=DIRECTIVE_DEFAULT_EXP_RE.match(l))\n",
+ " ]\n",
+ " if len(matches) > 1: raise ValueError('More than one default_exp directive')\n",
+ " if len(matches) == 0: raise ValueError('Missing default_exp directive')\n",
+ " return matches[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "64175ad3",
+ "id": "7310f069",
"metadata": {},
"outputs": [],
"source": [
- "mk_cell = lambda s,id=1: dict(id=id,source=s,cell_type=CellType.CODE)\n",
- "assert is_cell_exported(mk_cell('#| export'))\n",
- "assert is_cell_exported(mk_cell('import re\\n#| export'))\n",
- "assert not is_cell_exported(mk_cell('import re'))\n",
- "assert not is_cell_exported(mk_cell('1+1\\nfrom re import Pattern'))"
+ "nb = nbformat.v4.new_notebook(cells=[nbformat.v4.new_code_cell('#| default_exp: export')])\n",
+ "test.equal(get_default_export(nb.cells), 'export')"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "ddc5c9e6",
+ "id": "55fd5319",
"metadata": {},
"outputs": [],
"source": [
- "#| export\n",
- "def get_default_export(cells:list[NotebookNode])->str:\n",
- " \"Retrieve the `default_exp` directive argument from a list of cells.\"\n",
- " matches = [\n",
- " m.group('module')\n",
- " for c in cells\n",
- " for l in c['source'].splitlines()\n",
- " if c['cell_type'] == CellType.CODE and (m:=DIRECTIVE_DEFAULT_EXP_RE.match(l))\n",
- " ]\n",
- " if len(matches) > 1: raise ValueError('More than one default_exp directive')\n",
- " if len(matches) == 0: raise ValueError('Missing default_exp directive')\n",
- " return matches[0]"
+ "with test.raises(exc=ValueError, contains='Missing default_exp directive'):\n",
+ " get_default_export([nbformat.v4.new_code_cell('a=5')])"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "7310f069",
+ "id": "3da9dde8",
"metadata": {},
"outputs": [],
"source": [
- "assert get_default_export(read_nb('export.ipynb')['cells']) == 'export'\n",
- "\n",
- "try: get_default_export([mk_cell('a=5')]); assert False, \"Expected ValueError\"\n",
- "except ValueError: pass\n",
- "\n",
- "try:\n",
- " get_default_export([mk_cell('#| default_exp: hello'), mk_cell('#| default_exp: test')])\n",
- " assert False, \"Expected ValueError\"\n",
- "except ValueError: pass"
+ "with test.raises(exc=ValueError, contains='More than one default_exp directive'):\n",
+ " get_default_export([\n",
+ " nbformat.v4.new_code_cell('#| default_exp: hello'),\n",
+ " nbformat.v4.new_code_cell('#| default_exp: test'),\n",
+ " ])"
]
},
{
@@ -717,7 +345,7 @@
" nbformat.v4.new_code_cell('#| default_exp: mymodule'),\n",
" nbformat.v4.new_code_cell('#| export\\ndef hello(): return \"hi\"'),\n",
"]\n",
- "assert find_cells_to_export(nb) == ('mymodule', [{**nb.cells[1], 'source': 'def hello(): return \"hi\"'}])"
+ "test.equal(find_cells_to_export(nb), ('mymodule', [{**nb.cells[1], 'source': 'def hello(): return \"hi\"'}]))"
]
},
{
@@ -739,7 +367,7 @@
"with tempfile.TemporaryDirectory() as tmp:\n",
" p = tmp/Path('test.ipynb')\n",
" p.write_text(json.dumps(nb))\n",
- " assert find_cells_to_export(p) == ('mymodule', [])"
+ " test.equal(find_cells_to_export(p), ('mymodule', []))"
]
},
{
@@ -758,8 +386,7 @@
"outputs": [],
"source": [
"nb = nbformat.v4.new_notebook()\n",
- "try: find_cells_to_export(nb); assert False\n",
- "except ValueError: pass"
+ "with test.raises(ValueError): find_cells_to_export(nb)"
]
},
{
@@ -791,7 +418,8 @@
"metadata": {},
"outputs": [],
"source": [
- "assert fmt_export_cell(mk_cell('a=5', id=1))['source'] == '# cell: 1\\na=5\\n'"
+ "c = nbformat.v4.new_code_cell('a=5', id='1')\n",
+ "test.equal(fmt_export_cell(c)['source'], '# cell: 1\\na=5\\n')"
]
},
{
@@ -824,10 +452,8 @@
"metadata": {},
"outputs": [],
"source": [
- "with tempfile.TemporaryDirectory() as tmp:\n",
- " try: mk_py_mod_scaffold('', src_dir=tmp); assert False, \"Should raise ValueError\"\n",
- " except ValueError: pass\n",
- " assert os.listdir(tmp) == []"
+ "with tempfile.TemporaryDirectory() as tmp, test.raises(ValueError, contains='The module path cannot be empty'):\n",
+ " mk_py_mod_scaffold('', src_dir=tmp)"
]
},
{
@@ -839,7 +465,7 @@
"source": [
"with tempfile.TemporaryDirectory() as tmp:\n",
" mk_py_mod_scaffold('file', src_dir=tmp)\n",
- " assert os.listdir(tmp) == ['__init__.py', 'file.py']"
+ " test.equal(os.listdir(tmp), ['__init__.py', 'file.py'])"
]
},
{
@@ -851,8 +477,8 @@
"source": [
"with tempfile.TemporaryDirectory() as tmp:\n",
" mk_py_mod_scaffold('mod.file', src_dir=tmp)\n",
- " assert os.listdir(tmp) == ['__init__.py', 'mod']\n",
- " assert os.listdir(Path(tmp)/'mod') == ['__init__.py', 'file.py']"
+ " test.equal(os.listdir(tmp), ['__init__.py', 'mod'])\n",
+ " test.equal(os.listdir(Path(tmp)/'mod'), ['__init__.py', 'file.py'])"
]
},
{
@@ -918,15 +544,7 @@
"with tempfile.TemporaryDirectory() as tmp:\n",
" export_nb(nb, src_dir=tmp)\n",
" result = Path(tmp, 'testmod.py').read_text()\n",
- " assert result == src, result"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "e7c49602",
- "metadata": {},
- "source": [
- "And...we've successfully bootstrapped our notebook! Let's export it out."
+ " test.equal(result, src)"
]
},
{
diff --git a/nbs/test.ipynb b/nbs/test.ipynb
index 3d32563..5645626 100644
--- a/nbs/test.ipynb
+++ b/nbs/test.ipynb
@@ -703,8 +703,8 @@
" if negstr is None: negstr = f'not {cmp.__name__}'\n",
" args_src, _ = get_call_args(frames_up)\n",
" lines = [f\"{repr(a)} {negstr} {repr(b)}\"]\n",
- " if repr(a) != args_src[0]: lines.append(f\" where {repr(a)} = {args_src[0]}\")\n",
- " if repr(b) != args_src[1]: lines.append(f\" where {repr(b)} = {args_src[1]}\")\n",
+ " if len(args_src) > 0 and repr(a) != args_src[0]: lines.append(f\" where {repr(a)} = {args_src[0]}\")\n",
+ " if len(args_src) > 1 and repr(b) != args_src[1]: lines.append(f\" where {repr(b)} = {args_src[1]}\")\n",
" assert cmp(a, b), \"\\n\".join(lines)\n"
]
},
@@ -968,6 +968,82 @@
]
},
{
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "9523b71e",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "def is_true(a):\n",
+ " \"Assert that a is truthy.\"\n",
+ " expect(bool(a), True, operator.eq, negstr='is not truthy', frames_up=2)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "505710d7",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "is_true(True)\n",
+ "is_true(\"abc\")\n",
+ "is_true(123)\n",
+ "is_true(['a'])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "183b172b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "with raises(): is_true(False)\n",
+ "with raises(): is_true(\"\")\n",
+ "with raises(): is_true([])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f76c0add",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "def is_false(a):\n",
+ " \"Assert that a is falsy.\"\n",
+ " expect(bool(a), False, operator.eq, negstr='is not falsy', frames_up=2)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "cc6907f0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "is_false(False)\n",
+ "is_false(\"\")\n",
+ "is_false([])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "6c583e54",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "with raises(): is_false(True)\n",
+ "with raises(): is_false(\"abc\")\n",
+ "with raises(): is_false(123)\n",
+ "with raises(): is_false(['a'])"
+ ]
+ },
+ {
"cell_type": "markdown",
"id": "33be6990",
"metadata": {},