I put the readable version into the gist file -> link
Please ignore the print
and comment
in the following code.
I often have to write this sort of logic: init the conditions, and deep into loop condition, get the variables in each iteration and break when the condition meets.
As you can see, I get the first line in a region and then loop until the region ends.
The exit condition is while True and (beginning_spaces - start_beginning_spaces)
.
Are there any better practices in my case?
def get_exception_region(self, except_region, max_lines_in_exception=100):
'''
# get current line number
row, _ = self.view.rowcol(except_region.a)
# get next line's starting point
next_row_starting = self.view.text_point(row + 1, 0)
# get the whole next line
next_row_region = self.view.full_line(next_row_starting)
print(self.view.substr(next_row_region))
'''
# Init first setting
try:
row, _ = self.view.rowcol(except_region.a)
next_line_text, next_line_region = self.get_next_line__text_and_region(row)
prev_line_text, prev_line_region = next_line_text, next_line_region
beginning_spaces = self.count_the_beginning_spaces(next_line_text)
start_row, start_beginning_spaces = row, beginning_spaces
print "=================Start================="
print row, beginning_spaces, next_line_text.strip()
while True and (beginning_spaces - start_beginning_spaces) < max_lines_in_exception :
row = row + 1
next_line_text , next_line_region = self.get_next_line__text_and_region(row)
beginning_spaces = self.count_the_beginning_spaces(next_line_text)
print row, beginning_spaces, next_line_text.strip()
if beginning_spaces > 1 and beginning_spaces != start_beginning_spaces:
print "=================Exit================="
break
prev_line_text, prev_line_region = next_line_text, next_line_region
return row, prev_line_text, prev_line_region
pass
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print("[Error]\n", exc_type, fname, exc_tb.tb_lineno)
raise e
sys.exit(e)
#EndOfExcept
while True and somecondition
same aswhile somecondition
? Am I missing something obvious here? \$\endgroup\$