Question: 2. (40 points) Modify the file Func_headers.py, available on iLearn, so that the program shows function headers it detects in an input file. The most



2. (40 points) Modify the file Func_headers.py, available on iLearn, so that the program shows function headers it detects in an input file. The most important change you will need to make is to func_header_regex, so that in addition to matching function headers with single parameters: It allows that single parameter to take a default value of an integer, a string, or a variable. It captures the function header match in a group, excluding leading and trailing white space. 1 import re 2 3 4 5 D# '$' isn't allowed in Python variable names #Python variable names must start with a letter or a start_var_chars = '[a-zA-Z_]' #iniimbutmdigitsmaremalsomallowed intrailing.chacters trail_var_chars = '[a-zA-Z0-9_]' var_chars = start_var_chars + trail_var_chars + '*' 6 7 8 9 10 11 12 13 #Verbose regex definition #Somewnotes on Python.stringiteral syntax # 1. A triple-quoted string, delimited by 'n or, is allowed to span # multiple lines in the source file. # # 2. The 'r' string defintion prefix stands for 'raw', and means that the backslash characters are passed to the regular expression engine without interpretation. 14 15 16 # 17 # 18 # 19 20 # # 3. The f string defintion prefix allows for inserting variables into the string by enclosing them in curly braces. For example, {var_chars]|| 21 # 22 # 23 24 25 26 27 28 #FOR THE MIDTERM ENHANCE THIS REGULAR EXPRESSION ACCORDING TO THE INSTRUCTIONS func_header_regex = \ re.compile(fr' \s* # any and all blanks & tabs at start of string def\s+ # "def" then blanks & tabs {var_chars} # function name \s*\\s* # blanks & tabs.'(', blanks & tabs {var_chars} #parameter name *\s*\)\s*:\s*$ # blanks & tabs, ')', blanks & tabs, ':' # blanks & tabs, end of string ", re. VERBOSE) 29 30 31 32 33 35 36 def Match_func_header(text): #FOR THE MIDTERM, MODIFY THE RETURN SO THAT THIS FUNCTION ONLY RETURNS THE # FUNCTION HEADER, OMITTING ENCLOSING WHITE SPACE. return func_header_regex.search(text) 37 38 39 40 Dif 41 42 o 43 44 45 46 47 48 49 A 50 51 52 name, ==maingan 0 = open('func_headers.out'u'w') while True: filename = input('Enter file to check for function headers, or enter to quit: ') if filename == '';break try: f = open(filename, 'r') except: print(f'Could not open "{filename}" for reading') continue print(f 'Looking for function headers in "{filename}" ..', file=o) lineNum = 1 for line in f.readlines(): #THE PROCESSING BELOW DEPENDS ON Match_func_header RETURNING A MATCH #MOBIELTWHICH WILL NOT NECESSARILY BE TRUE AFTER YOU MAKE YOUR # MODIFICATIONS m = Match_func_header(line) #IN THE PROCESSING BELOW. THE "I:-11" AT THE END OF THE OUTPUT # STRING JUST REMOVES THE TRAILING NEWLINE CHAR. THAT MAY NO LONGER #BE.NEEDED AFTER YOU MAKE YOUR MODIFICATIONS if m: pfx = 'Python function header online! print(f'{pfx} {lineNum}: "{m.group()[:-1]}"', file=o) else: pfx = 'No Python function header on line print(f'{pfx} {lineNum}: "{line[:-1]}"', file=o) 53 54 55 A 56 57 58 59 60 61 62 a 63 64 65 66 67 A lineNum += 1 68 69 file=o) print(f'... "{filename}" function header search complete ', f.close 70 A PyCharm 2020
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
