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