Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 line
_
number 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 try
/
except and in case of an error print
a user
-
friendly message to the terminal and re
-
raise the exception.
Example: suppose file test.py has this content:
import math
y
=
math.sqrt
(
2
)
print
(
y
)
Function call line
_
number
(
test
.
py
,
test
.
py
.
txt
)
creates a new file named test.py
.
txt with content:
1
.
import math
2
.
y
=
math.sqrt
(
2
)
3
.
print
(
y
)
Write a main function that tests function line
_
number on the problem
1
Python source file itself. Make
sure you don
t overwrite your program by mistake.
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
the line number of the function definition, element
1
the function name, element
2
the
formal argument list as a string, and element
3
the function code as a string
(
signature and body
)
,
with
all empty lines and 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.
Write in the main function code that calls parse
_
functions on the problem
1
Python file and displays
the returned tuple.
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
{
:
.
3
f
}
.
"
.
format
(
a
)
)
# test these functions:
print
_
pretty
(
mul
(
1
0
,
sum
(
3
,
5
)
)
)
A call to parse
_
functions
(
"
funs
.
py
"
)
returns tuple:
(
(
9
,
"mul",
x
,
y
,
'def mul
(
x
,
y
)
:
\
tz
=
x
*
y
\
treturn z
'
)
,
(
1
4
,
"print
_
pretty",
a
,
'def print
_
pretty
(
a
)
:
\
tprint
(
"
The result is
{
:
.
3
f
}
.
"
.
format
(
a
)
)
'
)
,
(
3
,
"sum",
x
,
y
,
'def sum
(
x
,
y
)
:
"
"
"
Adds two numbers.
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

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions

Question

Explain The Characteristics of Venture Capital (About 2 paragraphs)

Answered: 1 week ago