5
5
from jupytext .myst import myst_to_notebook
6
6
import jupytext
7
7
8
+
8
9
def replace_simple_text (input_py_str ):
9
10
result = input_py_str .replace ("📃 Solution for" , "📝" )
10
11
return result
@@ -19,28 +20,35 @@ def remove_solution(input_py_str):
19
20
before this comment and add "# Write your code here." at the end of the
20
21
cell.
21
22
"""
22
- nb = jupytext .reads (input_py_str , fmt = ' py:percent' )
23
+ nb = jupytext .reads (input_py_str , fmt = " py:percent" )
23
24
24
- cell_tags_list = [c ['metadata' ].get ('tags' ) for c in nb .cells ]
25
- is_solution_list = [tags is not None and 'solution' in tags
26
- for tags in cell_tags_list ]
25
+ cell_tags_list = [c ["metadata" ].get ("tags" ) for c in nb .cells ]
26
+ is_solution_list = [
27
+ tags is not None and "solution" in tags for tags in cell_tags_list
28
+ ]
27
29
# Completely remove cells with "solution" tags
28
- nb .cells = [cell for cell , is_solution in zip (nb .cells , is_solution_list )
29
- if not is_solution ]
30
+ nb .cells = [
31
+ cell
32
+ for cell , is_solution in zip (nb .cells , is_solution_list )
33
+ if not is_solution
34
+ ]
30
35
31
36
# Partial cell removal based on "# solution" comment
32
37
marker = "# solution"
33
- pattern = re .compile (f"^{ marker } .*" , flags = re .MULTILINE | re .DOTALL )
38
+ pattern = re .compile (f"^{ marker } .*" , flags = re .MULTILINE | re .DOTALL )
34
39
35
- cells_to_modify = [c for c in nb .cells if c ["cell_type" ] == "code" and
36
- marker in c ["source" ]]
40
+ cells_to_modify = [
41
+ c
42
+ for c in nb .cells
43
+ if c ["cell_type" ] == "code" and marker in c ["source" ]
44
+ ]
37
45
38
46
for c in cells_to_modify :
39
47
c ["source" ] = pattern .sub ("# Write your code here." , c ["source" ])
40
48
41
49
# TODO: we could potentially try to avoid changing the input file jupytext
42
50
# header since this info is rarely useful. Let's keep it simple for now.
43
- py_nb_str = jupytext .writes (nb , fmt = ' py:percent' )
51
+ py_nb_str = jupytext .writes (nb , fmt = " py:percent" )
44
52
return py_nb_str
45
53
46
54
@@ -49,7 +57,7 @@ def write_exercise(solution_path, exercise_path):
49
57
50
58
output_str = input_str
51
59
for replace_func in [replace_simple_text , remove_solution ]:
52
- output_str = replace_func (output_str )
60
+ output_str = replace_func (output_str )
53
61
exercise_path .write_text (output_str )
54
62
55
63
@@ -70,12 +78,14 @@ def write_all_exercises(python_scripts_folder):
70
78
if path .is_dir ():
71
79
write_all_exercises (path )
72
80
else :
73
- if ' _ex_' not in str (path ):
81
+ if " _ex_" not in str (path ):
74
82
raise ValueError (
75
- f'Path argument should be an exercise file. Path was { path } ' )
83
+ f"Path argument should be an exercise file. Path was { path } "
84
+ )
76
85
solution_path = Path (str (path ).replace ("_ex_" , "_sol_" ))
77
86
if not solution_path .exists ():
78
87
raise ValueError (
79
- f"{ solution_path } does not exist, check argument path { path } " )
88
+ f"{ solution_path } does not exist, check argument path { path } "
89
+ )
80
90
81
91
write_exercise (solution_path , path )
0 commit comments