Skip to content

Commit 53961f4

Browse files
format code
1 parent a9feef6 commit 53961f4

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

src/eo_datascience/clean_nb.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,55 @@
33
from pathlib import Path
44
import re
55

6-
def clean_up_frontmatter(dir = './notebooks', save=True):
6+
7+
def clean_up_frontmatter(dir="./notebooks", save=True):
78
# Define the path to the notebooks
89
nb_paths = find_ipynb(dir)
910

1011
# Iterate over the notebooks
1112
for nb_path in nb_paths:
1213
# Load the notebook
1314
nb = nbformat.read(nb_path, as_version=4)
14-
if nb.cells[0].source.startswith('---'):
15-
#Load frontmatter
16-
fm = nb.cells[0].source.split('\n')
15+
if nb.cells[0].source.startswith("---"):
16+
# Load frontmatter
17+
fm = nb.cells[0].source.split("\n")
1718

1819
# Extract the title and the subtitle and convert
1920
i = 1
2021
line = fm[i]
2122
new_text = []
2223
while not line.startswith("---"):
23-
if line.startswith('title'):
24+
if line.startswith("title"):
2425
new_text.append(f"# {line.split(': ')[1]}")
25-
if line.startswith('subtitle'):
26+
if line.startswith("subtitle"):
2627
new_text.append(f"**{line.split(': ')[1]}**")
2728

2829
i += 1
2930
line = fm[i]
3031

31-
new_text += fm[i+1:]
32+
new_text += fm[i + 1 :]
3233
nb.cells[0].source = "\n".join(new_text) + "\n"
3334
# Save notebook
3435
if save:
3536
nbformat.write(nb, nb_path)
3637
else:
3738
return nb
3839

40+
3941
def convert_bibliography(nb_path="./notebooks/references.ipynb", save=True):
4042
nb = nbformat.read(nb_path, as_version=4)
4143
nb.cells[0].source = """# References
4244
4345
```{bibliography}
4446
```
4547
"""
46-
# Save the notebook
48+
# Save the notebook
4749
if save:
4850
nbformat.write(nb, nb_path)
4951
else:
5052
return nb
5153

54+
5255
def convert_callout_notes(dir="./notebooks", save=True):
5356
nb_paths = find_ipynb(dir)
5457

@@ -57,27 +60,32 @@ def convert_callout_notes(dir="./notebooks", save=True):
5760
# Load the notebook
5861
nb = nbformat.read(nb_path, as_version=4)
5962
for i in range(len(nb.cells)):
60-
6163
if nb.cells[i]["cell_type"] == "markdown":
6264
nb.cells[i].source = quarto_note_replace(nb.cells[i].source)
6365

64-
# Save the notebook
66+
# Save the notebook
6567
if save:
6668
nbformat.write(nb, nb_path)
6769
else:
6870
return nb
69-
71+
72+
7073
def quarto_note_replace(quarto):
7174
note_rst_start = r":::{note}"
7275
note_rst_end = r":::"
7376
nts = re.findall(r"(?<=:::\s\{\.callout\-note\})[^:::]+", quarto)
7477
for i in nts:
75-
quarto = re.sub(r":::\s\{\.callout\-note\}" + re.escape(i) + r":::", note_rst_start + i + note_rst_end, quarto)
78+
quarto = re.sub(
79+
r":::\s\{\.callout\-note\}" + re.escape(i) + r":::",
80+
note_rst_start + i + note_rst_end,
81+
quarto,
82+
)
7683
return quarto
7784

85+
7886
def convert_refs(dir="./notebooks", save=True):
7987
nb_paths = find_ipynb(dir)
80-
88+
8189
# Iterate over the notebooks
8290
for nb_path in nb_paths:
8391
# Load the notebook
@@ -95,35 +103,40 @@ def convert_refs(dir="./notebooks", save=True):
95103
else:
96104
return nb
97105

106+
98107
def quarto_ref_figure_replace(quarto):
99108
bibs = re.findall(r"(?<=\(\@)[^\)]+", quarto)
100109
for i in bibs:
101110
quarto = re.sub(r"\(\@" + i + "\)", r"", quarto)
102111
return quarto
103112

113+
104114
def quarto_ref_person_replace(quarto):
105115
bibs = re.findall(r"(?<=\[\@)[^\]]+", quarto)
106116
for i in bibs:
107117
quarto = re.sub(r"\[\@" + i + "\]", r"{cite:p}`" + i + "`", quarto)
108118
return quarto
109119

120+
110121
def quarto_ref_time_replace(quarto):
111122
bibs = re.findall(r"(?<=\@)[^\s]+", quarto)
112123
for i in bibs:
113124
quarto = re.sub(r"\@" + i, r"{cite:t}`" + i + "`", quarto)
114125
return quarto
115126

127+
116128
def find_ipynb(dir):
117129
root = Path(dir).resolve()
118-
nb_paths = [root / file for file in os.listdir(root) if file.endswith('.ipynb')]
130+
nb_paths = [root / file for file in os.listdir(root) if file.endswith(".ipynb")]
119131
return nb_paths
120132

133+
121134
def main():
122135
clean_up_frontmatter()
123136
convert_callout_notes()
124137
convert_refs()
125138
convert_bibliography()
126139

127140

128-
if __name__ == '__main__':
129-
main()
141+
if __name__ == "__main__":
142+
main()

tests/test_quarto_nb_conversions.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
11
import nbformat
22
from pathlib import Path
33
import pytest
4-
from eo_datascience.clean_nb import clean_up_frontmatter, convert_refs, quarto_ref_person_replace, quarto_ref_time_replace, \
5-
convert_callout_notes, quarto_note_replace
4+
from eo_datascience.clean_nb import (
5+
clean_up_frontmatter,
6+
convert_refs,
7+
quarto_ref_person_replace,
8+
quarto_ref_time_replace,
9+
convert_callout_notes,
10+
quarto_note_replace,
11+
find_ipynb,
12+
)
13+
614

715
def test_remove_front_matter():
8-
assert clean_up_frontmatter("./tests", False)["cells"][0]["source"] == '# This a mock Jupyter file\n**We use it for testing**\n\nSome other text, which should not be deleted!\n'
16+
assert (
17+
clean_up_frontmatter("./tests", False)["cells"][0]["source"]
18+
== "# This a mock Jupyter file\n**We use it for testing**\n\nSome other text, which should not be deleted!\n"
19+
)
20+
21+
22+
def test_find_ipynb():
23+
assert find_ipynb("tests")[0].stem == "mock"
24+
925

1026
def test_conversion_of_refs():
11-
quarto = [r"lorem ipsum [@anon2024] and [@anon2025]", r"lorem ipsum @anon2024 and @anon2025"]
27+
quarto = [
28+
r"lorem ipsum [@anon2024] and [@anon2025]",
29+
r"lorem ipsum @anon2024 and @anon2025",
30+
]
1231
quarto[0] = quarto_ref_person_replace(quarto[0])
1332
quarto[1] = quarto_ref_time_replace(quarto[1])
14-
assert quarto == [r"lorem ipsum {cite:p}`anon2024` and {cite:p}`anon2025`", r"lorem ipsum {cite:t}`anon2024` and {cite:t}`anon2025`"]
15-
assert convert_refs("./tests", False)["cells"][2]["source"] == r"lorem ipsum {cite:p}`anon2024` and {cite:p}`anon2025` and lorem ipsum {cite:t}`anon2024` and {cite:t}`anon2025`"
33+
assert quarto == [
34+
r"lorem ipsum {cite:p}`anon2024` and {cite:p}`anon2025`",
35+
r"lorem ipsum {cite:t}`anon2024` and {cite:t}`anon2025`",
36+
]
37+
assert (
38+
convert_refs("./tests", False)["cells"][2]["source"]
39+
== r"lorem ipsum {cite:p}`anon2024` and {cite:p}`anon2025` and lorem ipsum {cite:t}`anon2024` and {cite:t}`anon2025`"
40+
)
41+
1642

1743
def test_conversion_of_callout_notes():
18-
rst = ':::{note}\nThis a callout note.\n:::'
44+
rst = ":::{note}\nThis a callout note.\n:::"
1945
assert quarto_note_replace(r"::: {.callout-note}\nThis a callout note.\n:::") == rst
20-
assert convert_callout_notes("./tests", False)["cells"][1]["source"] == rst
46+
assert convert_callout_notes("./tests", False)["cells"][1]["source"] == rst

0 commit comments

Comments
 (0)