@@ -865,6 +865,31 @@ def get_init_code(leaf, language):
865
865
return ""
866
866
867
867
868
+ def fix_line_spacing (code ):
869
+ """
870
+ Removing beginning/trailing blank lines and allow max of two blank lines.
871
+
872
+ :param code:
873
+ :return:
874
+ """
875
+ code_lines = code .splitlines ()
876
+
877
+ # remove beginning blank lines
878
+ while not code_lines [0 ]:
879
+ code_lines .pop (0 )
880
+
881
+ # remove ending blank lines
882
+ while not code_lines [- 1 ]:
883
+ code_lines .pop ()
884
+
885
+ # allow max of two consecutive blank lines
886
+ code = '\n ' .join (code_lines )
887
+ while '\n \n \n ' in code :
888
+ code = code .replace ('\n \n \n ' , '\n \n ' )
889
+
890
+ return code
891
+
892
+
868
893
def save_code (code , leaf , language , mode , newline = True ):
869
894
if mode == 'documentation' :
870
895
leaf_folder = os .path .join (dirs ['run' ],
@@ -881,12 +906,7 @@ def save_code(code, leaf, language, mode, newline=True):
881
906
code_path = os .path .join (code_folder , filename .replace ("-" , "_" ))
882
907
else :
883
908
raise Exception ("mode: 'execution' | 'documentation' | 'exception'" )
884
- code_lines = code .splitlines ()
885
- while not code_lines [0 ]:
886
- code_lines .pop (0 )
887
- while not code_lines [- 1 ]:
888
- code_lines .pop ()
889
- code = '\n ' .join (code_lines )
909
+ code = fix_line_spacing (code )
890
910
if not os .path .exists (code_folder ):
891
911
os .makedirs (code_folder )
892
912
with open (code_path , 'w' ) as f :
0 commit comments