test2doc 0.0.4__tar.gz → 0.0.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {test2doc-0.0.4 → test2doc-0.0.5}/PKG-INFO +2 -2
- {test2doc-0.0.4 → test2doc-0.0.5}/pyproject.toml +1 -1
- {test2doc-0.0.4 → test2doc-0.0.5}/test2doc/notebook_utils.py +2 -1
- test2doc-0.0.5/test2doc/tests/test_utils_offline.py +104 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/.editorconfig +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/.github/workflows/ci.yml +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/.gitignore +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/LICENSE +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/README.md +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/docsrc/.gitignore +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/docsrc/Makefile +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/docsrc/conf.py +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/docsrc/index.rst +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/docsrc/make.bat +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/test2doc/__init__.py +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/test2doc/base.py +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/test2doc/doctest_utils.py +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/test2doc/misc.py +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/test2doc/tests/__init__.py +0 -0
- {test2doc-0.0.4 → test2doc-0.0.5}/test2doc/tests/base_test.py +0 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"""Utils for jupyter (ipython) notebooks."""
|
|
2
2
|
|
|
3
|
+
from itertools import islice
|
|
3
4
|
import json
|
|
4
5
|
from pathlib import Path
|
|
5
6
|
from typing import Union, List, Tuple
|
|
@@ -73,7 +74,7 @@ def cells_with_largest_output(notebook, n=5, cell_egress=_output_size_stats):
|
|
|
73
74
|
sorted_cells = sort_notebook_cells(
|
|
74
75
|
notebook, key=get_output_size, reverse=True, cell_egress=cell_egress
|
|
75
76
|
)
|
|
76
|
-
return
|
|
77
|
+
return list(islice(sorted_cells, n))
|
|
77
78
|
|
|
78
79
|
|
|
79
80
|
def clear_outputs_of_cell_ids(notebook, cell_ids):
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"""Tests for base conversion helpers, doctest_utils and notebook_utils."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from test2doc import doctest_utils as du
|
|
8
|
+
from test2doc import notebook_utils as nu
|
|
9
|
+
from test2doc.base import (
|
|
10
|
+
are_all_valid_python_code,
|
|
11
|
+
blocks_to_docs,
|
|
12
|
+
code_to_docs,
|
|
13
|
+
is_proper_partition,
|
|
14
|
+
is_valid_blocks_of_src_string,
|
|
15
|
+
is_valid_python_code,
|
|
16
|
+
src_string_to_blocks,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_validity_helpers():
|
|
21
|
+
assert is_valid_python_code("x = 1") and not is_valid_python_code("x =")
|
|
22
|
+
assert are_all_valid_python_code(["a = 1", "b = 2"])
|
|
23
|
+
assert not are_all_valid_python_code(["a = 1", "b ="])
|
|
24
|
+
assert is_proper_partition(["a = 1", "b = 2"], "a = 1\nb = 2")
|
|
25
|
+
assert not is_proper_partition(["a = 1"], "a = 1\nb = 2")
|
|
26
|
+
assert is_valid_blocks_of_src_string(["a = 1", "b = 2"], "a = 1\nb = 2")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_blocks_to_docs_alternates_text_and_code():
|
|
30
|
+
docs = blocks_to_docs(["# a comment", "x = 1", "y = 2", "# more"])
|
|
31
|
+
assert docs.startswith(" a comment\n")
|
|
32
|
+
assert "```python\nx = 1\ny = 2\n```" in docs
|
|
33
|
+
assert docs.rstrip().endswith("more")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_code_to_docs_from_string_and_function():
|
|
37
|
+
src = "# intro\nx = 1\n# next\nprint(x)"
|
|
38
|
+
docs = code_to_docs(src)
|
|
39
|
+
assert "intro" in docs and "```python\nx = 1\n```" in docs
|
|
40
|
+
assert src_string_to_blocks(src) # non-empty
|
|
41
|
+
|
|
42
|
+
def demo():
|
|
43
|
+
# say hi
|
|
44
|
+
greeting = "hi"
|
|
45
|
+
return greeting
|
|
46
|
+
|
|
47
|
+
assert "greeting" in code_to_docs(demo)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_doctest_utils():
|
|
51
|
+
assert du.remove_assertion("assert x == 1\nplain") == "x\nplain"
|
|
52
|
+
assert (
|
|
53
|
+
du.assertions_to_doctest(">>> assert f(1) == 2\nother")
|
|
54
|
+
== ">>> f(1)\n2\nother\n"
|
|
55
|
+
)
|
|
56
|
+
out = du.txt_to_doctest_txt("a = 1\n b = 2\n")
|
|
57
|
+
assert out.splitlines()[0] == ">>> a = 1"
|
|
58
|
+
assert out.splitlines()[1].startswith("...\t")
|
|
59
|
+
assert out.splitlines()[-1] == ">>> "
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
NB = {
|
|
63
|
+
"cells": [
|
|
64
|
+
{"id": "a", "outputs": [{"text": ["12345"]}]},
|
|
65
|
+
{"id": "b", "outputs": [{"data": {"text/plain": "123", "image/png": ["ab", "c"]}}]},
|
|
66
|
+
{"id": "c"},
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_notebook_dict_and_sizes(tmp_path):
|
|
72
|
+
assert nu.ensure_notebook_dict(NB) is NB
|
|
73
|
+
p = tmp_path / "n.ipynb"
|
|
74
|
+
p.write_text(json.dumps(NB))
|
|
75
|
+
assert nu.ensure_notebook_dict(str(p)) == NB
|
|
76
|
+
with pytest.raises(FileNotFoundError):
|
|
77
|
+
nu.ensure_notebook_dict(str(tmp_path / "missing.ipynb"))
|
|
78
|
+
assert [nu.get_output_size(c) for c in NB["cells"]] == [5, 6, 0]
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def test_sort_and_largest():
|
|
82
|
+
ids = [c["id"] for c in nu.sort_notebook_cells(NB, key=nu.get_output_size, reverse=True)]
|
|
83
|
+
assert ids == ["b", "a", "c"]
|
|
84
|
+
assert nu.cells_with_largest_output(NB, n=2) == [
|
|
85
|
+
{"id": "b", "output_size": 6},
|
|
86
|
+
{"id": "a", "output_size": 5},
|
|
87
|
+
]
|
|
88
|
+
with pytest.raises(ValueError, match="Invalid notebook"):
|
|
89
|
+
list(nu.sort_notebook_cells({"nope": 1}))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def test_largest_default_n_on_small_notebook():
|
|
93
|
+
assert len(nu.cells_with_largest_output(NB)) == 3
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_clear_outputs():
|
|
97
|
+
import copy
|
|
98
|
+
|
|
99
|
+
nb = copy.deepcopy(NB)
|
|
100
|
+
out = nu.clear_outputs_of_cell_ids(nb, {"a"})
|
|
101
|
+
assert out["cells"][0]["outputs"] == [] and out["cells"][1]["outputs"]
|
|
102
|
+
nb2 = copy.deepcopy(NB)
|
|
103
|
+
nu.clear_outputs_of_largest_output_cells(nb2, n=1)
|
|
104
|
+
assert nb2["cells"][1]["outputs"] == []
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|