Python Forum
wanted: regex or code to find valide def statements in a line
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wanted: regex or code to find valide def statements in a line
#11
does the python engine parser move to the lines that follow before checking the first line for errors? counting continuations as one line ... must the line that begins with "def" (followed by white space) have the ":" on that line? must that line end with ":" (not counting comments)? i can't think of a legal "def" that does not end with anything other than ":" optionally followed by a comment. what does the python engine need to know from the lines that follow to correctly determine the validity of the first line?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#12
SyntaxError is different than all other error,it will fail upon the first syntax error.
This will shut down parser at a early stage and parsing to first stage(byte code) never happens.

So here is there two SyntaxError and a NameError,but as mention will it stop at first error SyntaxError and shut down.
So no more action is possible before fix this error.
def foo(:
    a = bar # NameError
    ages = {
    'pam': 24,
    'jim': 24 # SyntaxError
    'michael': 43
    }
    print(ages)

foo()
Output:
λ python error_test.py File "error_test.py", line 1 def foo(: ^ SyntaxError: invalid syntax
If fix the first line SyntaxError.
If will now not find the NameError first,as SyntaxError trump all other error and as mention shout down the parser.
All other errors need the parser to be finish before they are reported.
So now it jump to second SyntaxError.
def foo():
    a = bar # NameError
    ages = {
    'pam': 24,
    'jim': 24 # SyntaxError
    'michael': 43
    }
    print(ages)

foo()
Output:
λ python error_test.py File "error_test.py", line 6 'michael': 43 ^ SyntaxError: invalid syntax
An as often answered here when people post that they have SyntaxError,look at line before.

If fix SyntaxError now will the NameError get reported.
def foo():
    a = bar # NameError
    ages = {
    'pam': 24,
    'jim': 24, # Fixed
    'michael': 43
    }
    print(ages)

foo()
Output:
λ python error_test.py Traceback (most recent call last): File "error_test.py", line 10, in <module> foo() File "error_test.py", line 2, in foo a = bar # NameError NameError: name 'bar' is not defined
Reply
#13
Notice the following, which is syntactically correct but the : at the end of the first line is not what you think
>>> def foo(ages= { 'pam':
...     24,
...     'jim': 24,
...     }, a = 
...     print('spam')
...     ): pass
... 
spam
>>> 
Reply
#14
(Mar-15-2020, 10:51 PM)Gribouillis Wrote:
Skaperen Wrote:is this valid?…why not?
This is not valid because there is a syntax definition that forbids it. Have a look at the railroad diagram of python 3.8's syntax that I uploaded recently. You will clearly see that this construct is not possible. You will also see that you can not handle all the cases with a regex because the following is valid for example
def spam(ham=[n for n in range(1, dividend+1) if dividend % n == 0]):
   ...
One way to check that it is valid would be to add a line of function body with a simple 'pass' statement and call the 'compile' function to see if it can build an AST tree with this code.

so, would it be possible to check all possible cases for validity with parser code? yeah, silly question. i guess that one way to do this is add on a dummy body (maybe a solo return) and see what a call to something that parses python code (exec(), maybe) does with in.

(Mar-20-2020, 03:09 PM)Gribouillis Wrote: Notice the following, which is syntactically correct but the : at the end of the first line is not what you think
>>> def foo(ages= { 'pam':
...     24,
...     'jim': 24,
...     }, a = 
...     print('spam')
...     ): pass
... 
spam
>>> 
cases like this are what i was worried about.

i will only have the "first line". but a dictionary in the arg list can let the "first line" be on more than one text line. i need to thing out this more in terms of what code i am getting.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python code wanted: grep IP address Skaperen 7 6,110 Jul-09-2018, 05:25 AM
Last Post: Skaperen
  code wanted, but don't expect me to do it Skaperen 0 2,086 Jul-07-2018, 10:50 PM
Last Post: Skaperen
  command line progam wanted: clock Skaperen 2 2,694 Apr-18-2018, 06:54 AM
Last Post: Gribouillis
  code wanted: file splicing Skaperen 10 6,383 Mar-28-2018, 12:13 AM
Last Post: Skaperen
  looking 4 py code: line up columns Skaperen 8 7,793 Jan-09-2017, 05:15 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020