6
6
import jupytext
7
7
8
8
9
+ WRITE_YOUR_CODE_COMMENT = "# Write your code here."
10
+
11
+
9
12
def replace_simple_text (input_py_str ):
10
13
result = input_py_str .replace ("📃 Solution for" , "📝" )
11
14
return result
@@ -44,7 +47,24 @@ def remove_solution(input_py_str):
44
47
]
45
48
46
49
for c in cells_to_modify :
47
- c ["source" ] = pattern .sub ("# Write your code here." , c ["source" ])
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
48
68
49
69
# TODO: we could potentially try to avoid changing the input file jupytext
50
70
# header since this info is rarely useful. Let's keep it simple for now.
@@ -53,6 +73,7 @@ def remove_solution(input_py_str):
53
73
54
74
55
75
def write_exercise (solution_path , exercise_path ):
76
+ print (f"Writing exercise to { exercise_path } from solution { solution_path } " )
56
77
input_str = solution_path .read_text ()
57
78
58
79
output_str = input_str
@@ -67,7 +88,9 @@ def write_all_exercises(python_scripts_folder):
67
88
for solution_path in solution_paths :
68
89
exercise_path = Path (str (solution_path ).replace ("_sol_" , "_ex_" ))
69
90
if not exercise_path .exists ():
70
- print (f"{ exercise_path } does not exist" )
91
+ print (
92
+ f"{ exercise_path } does not exist, generating it from solution."
93
+ )
71
94
72
95
write_exercise (solution_path , exercise_path )
73
96
0 commit comments