Question: code in python a ) Write a function called line _ number that takes as parameters two strings representing file names. Assume these are text
code in python a Write a function called linenumber that takes as parameters two strings representing file names.
Assume these are text files. The function reads the file indicated by the first parameter and writes its
lines prefixed by the line number to the file represented by the second parameter.
The function must have a proper docstring and annotations. Use tryexcept and in case of an error print
a userfriendly message to the terminal and reraise the exception.
Example: suppose file test.py has this content:
import math
y math.sqrt
printy
Function call linenumbertestpytestpytxt creates a new file named test.pytxt with content:
import math
y math.sqrt
printy
Write a main function that tests function linenumber on the problem Python source file itself. Make
sure you dont overwrite your program by mistake.
b Write a function called parsefunctions 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 the line number of the function definition, element the function name, element the
formal argument list as a string, and element the function code as a string signature and body with
all empty lines and comments removed.
The toplevel tuple returned must be ordered alphabetically by the function name.
Function parsefunctions must have a proper docstring and annotations. Use tryexcept and in case of
an error print a userfriendly message to the terminal and reraise the exception.
Write in the main function code that calls parsefunctions on the problem Python file and displays
the returned tuple.
Example: suppose file funs.py has this content:
# File with sample functions.
def sumx y: # sums up two numbers
Adds two numbers.
Returns the sum."""
return x y
# Returns the product.
def mulx y: # multiplies two numbers
# z is a local variable
z x y
return z
def printprettya:
printThe result is :fformata
# test these functions:
printprettymul sum
A call to parsefunctionsfunspy returns tuple:
"mul", x y 'def mulx y:
tz x y
treturn z
"printpretty", a 'def printprettya:
tprintThe result is :fformata
"sum", xy 'def sumx y:
Adds two numbers.
Returns the sum."""
yreturn a b
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
