0

I am trying to write a script in Python which will read any python script searching for function definitions and add a print statement inside the function for debugging purpose.

e.g.

def function_for_test:
    print "this line should be added for debugging purpose"
    more code here....
    more code here....

so far I have this code

import os
import re
import sys

match_class = re.compile(r'^[ \t]*(def|class)[ \t]+([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*[:\(]')


for root, dirs, files in  os.walk(sys.argv[1]):
        for fn in files:
                if fn.endswith(".py"):
                        with open(os.path.join(root, fn), "r+") as source:
                                        while True:
                                                line = source.readline()
                                                if line == "": break
                                                m = match_class.match(line.expandtabs())
                                                if m:
                                                        print m.groups()

I have run in to trouble because if I try to write text, the existing text is getting over written. Could anyone please suggest some way to overcome this. I don't want to create another file for this purpose and copy text from original to new file with modifications

2
  • Have you considered using the sys.settrace function. This can be used to execute a function every time one of your functions is executed, so might satisfy your requirement without the need for parsing and modifying the package source?> Commented Jan 12, 2014 at 13:04
  • I will try to read about settrace.... What I need is that whenever any function from the project gets executed, it prints output to the commandline or to a text file; this way I can traceout the entire flow of the project. I shouldn't have used the word debugging...I need to trace all the functions that are being called when I perform some action using the program. e.g. If I were to click on a GUI button, if all the functions in the source would go and print a line in a text file when they are called, this way I could know which functions are being called. Commented Jan 12, 2014 at 13:17

2 Answers 2

1

There is no way of adding content to a file without rewriting all the downstream content.

You can use the following logic:

with open(filename, 'r+') as f:
    lines = f.readlines()
    modified_lines = instrument_code(lines)

    f.seek(0)  # Go back to file start
    f.writelines(modified_lines)

    # Remove trailing content in case your file is shorter than original
    f.truncate()  

Where instrument_code is your code that modifies the source file.

Sign up to request clarification or add additional context in comments.

Comments

1

Settrace can potentially be used to meet this requirement. Be aware that there will be a performance overhead, and there may well be a better way.. But, this might be a quick (from a coding point of view) way to meet your requirement.

For example

import sys

def trace_f(frame, event, arg):
    if event == "call":
        print 'Execute {f} '.format(f=frame.f_code)

Now defining a trivial sample function to trace

def multiply_by_two(x):
    print x*2

And activate tracing

sys.settrace(trace_f)

multiply_by_two(12)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.