Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if given lines of code are complete and proper Python code

Tags:

python

Sorry for my English. This problem is complicated, and my English isn't thats good to describe everything clearly.

Ok, I have this example code. This is only a small code, in real environment i operate on much larger code than that.

01: multi_line_string = """Abc
02: Dfg
03: Hjk"""
04: # Comment
05: do_somethingA()
06: do_somethingB()
07: do_somethingC()
08: string_with_linebrak = "Abc \
09: Dfg \
10: Hjk"

This code, each line as list element i get as argument in my function with one line number. In result i need to return list of 2 lines around given line number. For example, with this code i get line number 5. This is do_somethingA() line.

My function must return list with 5 elements - 2 lines before line 5, line 5 and 2 lines after line 5. That is:

03: Hjk"""
04: # Comment
05: do_somethingA()
06: do_somethingB()
07: do_somethingB()

As you see in return i have broken Python code. Unfortunately, I must get complete code. So, I need somehow detect that line 3 are not complete line of Python code and i need to detect where this broken line are started or ended and get all lines along the way. So, what i need is:

01: multi_line_string = """Abc
02: Dfg
03: Hjk"""
04: # Comment
05: do_somethingA()
06: do_somethingB()
07: do_somethingC()

Lexing complete code is not an option, I need most CPU efficient way to do this.

like image 305
Galmi Avatar asked Jan 22 '26 09:01

Galmi


1 Answers

I would suggest looking at pylint.

like image 194
murgatroid99 Avatar answered Jan 24 '26 23:01

murgatroid99



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!