Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python programming parsing function. b) Write a function called parse_functions that takes as parameter a string representing the name of a .py file. The function

Python programming parsing function.

image text in transcribed

image text in transcribed

b) Write a function called parse_functions that takes as parameter a string representing the name of a .py file. The function reads and parses the Python file and returns a tuple of tuples where each tuple has its element 0 a function name, element 1 the line number, and element 2 the function code as a string (signature and body), with all comments removed. The top-level tuple returned must be ordered alphabetically by the function name. Function parse_functions must have a proper docstring and annotations. Use try/except and in case of an error print a user-friendly message to the terminal and re-raise the exception. Example: suppose file funs.py has this content: # File with sample functions. def sum(x, y): # sums up two numbers """ Adds two numbers. Returns the sum." return x + y # Returns the product. def mul(x, y): # multiplies two numbers # z is a local variable z=x*y return z def print_pretty(a): print("The result is {:.3f}.".format(a) # test these functions: print_pretty(mul(10, sum(3,5)) A call to parse_functions("funs.py") returns tuple: (("mul", 9, 'def mul(x, y): \tz = x *y \treturn z '), ("print_pretty", 14, 'def print_pretty(a): \tprint("The result is (:.3f}.".format(a)) '), ("sum", 3, 'def sum(x, y): ""'"Adds two numbers. In Returns the sum."'"' \yreturn a + b '))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

ISBN: 1489982175, 978-1489982179

More Books

Students also viewed these Databases questions

Question

2. How should this be dealt with by the organisation?

Answered: 1 week ago

Question

explain what is meant by the term fair dismissal

Answered: 1 week ago