Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago