Question: Write a program called tracer.py that may be used to assist with debugging Python programs. - Given the name of a Python program as input,

Write a program called tracer.py that may be used to assist with debugging Python programs.

- Given the name of a Python program as input, tracer.py will insert a trace statement at the beginning of each function definition.

- Given the name of a program that already contains trace statements, tracer.py will remove them.

Say, for example, we have the following program, rfunction.py:

# reverse a string.

def reverse_string (sentence):

new_sent = ""

for i in range (len (sentence)-1,-1,-1):

new_sent = new_sent + sentence[i]

return new_sent

def main ():

sent = input ("Enter a sentence: ")

print (reverse_string (sent))

print (reverse_string (sent+sent))

main()

Heres a sample of its behaviour:

Enter a sentence: how now brown cow

woc nworb won woh

woc nworb won wohwoc nworb won who

Given the file namerfunction.py as input, tracer.py will produce the following transformation:

"""DEBUG"""

# Reverse a string.

def reverse_string (sentence):

"""DEBUG""";print('reverse_string')

new_sent = ""

for i in range (len (sentence)-1,-1,-1):

new_sent = new_sent + sentence[i]

return new_sent

def main ():

"""DEBUG""";print('main')

sent = input ("Enter a sentence: ")

print (reverse_string (sent))

print (reverse_string (sent+sent))

The program inserts the docstring """DEBUG""" at the start of the file. After the signature line of each function it inserts the same docstring followed by a semicolon, followed by a statement that prints the name of the function.

Running the transformed rfunction.py (with the same input as before) produces this transcript:

main

Enter a sentence: how now brown cow

reverse_string

woc nworb won woh

reverse_string

woc nworb won wohwoc nworb won who

The transcript now shows what function was executed at which point. Given rfunction.py as input for a second time, trace.py will return the text to its original form.

To complete the example, here is the expected user interaction for each execution of trace.py:

***** Program Trace Utility *****

Enter the name of the program file: rfunction.py

Inserting...Done

***** Program Trace Utility *****

Enter the name of the program file: rfunction.py

Program contains trace statements

Removing...Done

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!