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
#9
To also check if all parameter in a Python function is valid then it's like the famous regex problem from email.
So this the is fully RFC 822 compliant regex,i have never needed to use that regex to eg extract email from text.
So with the regex i posted it will find both last two examples.
import re

text = '''\
def my_game():
    print('Game running')

def 123

def foo(arg):
    pass

def Bar(args*, kwargs**):
    pass

hello def is nice
def () wrong

def spam(ham=[n for n in range(1, dividend+1) if dividend % n == 0]):
    pass

def foobar( **abc, *def ):
    pass'''

# Make list of match
def_name = re.findall(r"def\s(\w+)\(.*", text)
def_line = re.findall(r"def\s\w+\(.*", text)

# Iterate over matches group() or group(1)
matches = re.finditer(r"def\s(\w+)\(.*", text)
for match in matches:
    print(match.group(1))
Output:
my_game foo Bar spam foobar
>>> def_line
['def my_game():',
 'def foo(arg):',
 'def Bar(args*, kwargs**):',
 'def spam(ham=[n for n in range(1, dividend+1) if dividend % n == 0]):',
 'def foobar( **abc, *def ):']
I would not try to write a regex for all cases,could as @Gribouillis mention step it up with adding pass and try to run the function.
With the not valid one will get SyntaxError ,valid one will return None or other error like NameError.
So if i test this it look like this:
>>> check = def_line[-1]
>>> check
'def foobar( **abc, *def ):'
>>> check = check.replace(':', ':pass')
>>> exec(check)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "<string>", line 1
    def foobar( **abc, *def ):pass
                       ^
SyntaxError: invalid syntax
So with the valid ones:
>>> check = def_line[1]
>>> check = check.replace(':', ':pass')
>>> check
'def foo(arg):pass'
>>> repr(exec(check))
'None'
>>> 
>>> check = def_line[-2]
>>> check = check.replace(':', ':pass')
>>> check
'def spam(ham=[n for n in range(1, dividend+1) if dividend % n == 0]):pass'
>>> exec(check)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'dividend' is not defined
Reply


Messages In This Thread
RE: wanted: regex or code to find valide def statements in a line - by snippsat - Mar-16-2020, 11:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python code wanted: grep IP address Skaperen 7 6,127 Jul-09-2018, 05:25 AM
Last Post: Skaperen
  code wanted, but don't expect me to do it Skaperen 0 2,095 Jul-07-2018, 10:50 PM
Last Post: Skaperen
  command line progam wanted: clock Skaperen 2 2,706 Apr-18-2018, 06:54 AM
Last Post: Gribouillis
  code wanted: file splicing Skaperen 10 6,425 Mar-28-2018, 12:13 AM
Last Post: Skaperen
  looking 4 py code: line up columns Skaperen 8 7,831 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