66import jupytext
77
88
9+ WRITE_YOUR_CODE_COMMENT = "# Write your code here."
10+
11+
912def replace_simple_text (input_py_str ):
1013 result = input_py_str .replace ("📃 Solution for" , "📝" )
1114 return result
@@ -44,7 +47,24 @@ def remove_solution(input_py_str):
4447 ]
4548
4649 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
4868
4969 # TODO: we could potentially try to avoid changing the input file jupytext
5070 # header since this info is rarely useful. Let's keep it simple for now.
@@ -53,6 +73,7 @@ def remove_solution(input_py_str):
5373
5474
5575def write_exercise (solution_path , exercise_path ):
76+ print (f"Writing exercise to { exercise_path } from solution { solution_path } " )
5677 input_str = solution_path .read_text ()
5778
5879 output_str = input_str
@@ -67,7 +88,9 @@ def write_all_exercises(python_scripts_folder):
6788 for solution_path in solution_paths :
6889 exercise_path = Path (str (solution_path ).replace ("_sol_" , "_ex_" ))
6990 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+ )
7194
7295 write_exercise (solution_path , exercise_path )
7396
0 commit comments