Dynamically print the Executing Line of Code?

905
4
Jump to solution
01-27-2014 10:39 AM
JohnDye
Occasional Contributor III
Kind of an odd question and it may have implications that I'm not seeing, but I'm wondering if anyone knows of a little method to instruct python to print the executing line of code, either before or after it executes instead of inserting print statements, you know like...everywhere.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
Isn't that what a debugger is for?

You could do this, but it's a terrible idea and will slow your program down by orders of magnitude:

import inspect import sys  def tracefunction(frame, event, arg):     if event == "line":         info = inspect.getframeinfo(frame)         fname, lineno, fn = info.filename, info.lineno, info.function         with open(fname, 'rb') as f:             line = [line.rstrip() for line in f][lineno - 1]             print "Function: {} (in file {}:{}) | {}".format(fname, fn, lineno, line)     return tracefunction  def registertracefunction():     sys.settrace(tracefunction)  registertracefunction()  def mainfunction():     for x in xrange(10):         print x * 5  if __name__ == "__main__":     mainfunction()


That is, paste the code up through registertracefunction() line to the top of your script and it will print out every line it's on.

View solution in original post

0 Kudos
4 Replies
JasonScheirer
Occasional Contributor III
Isn't that what a debugger is for?

You could do this, but it's a terrible idea and will slow your program down by orders of magnitude:

import inspect import sys  def tracefunction(frame, event, arg):     if event == "line":         info = inspect.getframeinfo(frame)         fname, lineno, fn = info.filename, info.lineno, info.function         with open(fname, 'rb') as f:             line = [line.rstrip() for line in f][lineno - 1]             print "Function: {} (in file {}:{}) | {}".format(fname, fn, lineno, line)     return tracefunction  def registertracefunction():     sys.settrace(tracefunction)  registertracefunction()  def mainfunction():     for x in xrange(10):         print x * 5  if __name__ == "__main__":     mainfunction()


That is, paste the code up through registertracefunction() line to the top of your script and it will print out every line it's on.
0 Kudos
MathewCoyle
Frequent Contributor
The best option I know of is using trace. It will can create a file of the code executed. Here is an example.

http://pymotw.com/2/trace/

Profile also has some good options.

http://docs.python.org/2/library/profile.html
0 Kudos
JohnDye
Occasional Contributor III
Isn't that what a debugger is for?

You could do this, but it's a terrible idea and will slow your program down by orders of magnitude:

import inspect
import sys

def tracefunction(frame, event, arg):
    if event == "line":
        info = inspect.getframeinfo(frame)
        fname, lineno, fn = info.filename, info.lineno, info.function
        with open(fname, 'rb') as f:
            line = [line.rstrip() for line in f][lineno - 1]
            print "Function: {} (in file {}:{}) | {}".format(fname, fn, lineno, line)
    return tracefunction

def registertracefunction():
    sys.settrace(tracefunction)

registertracefunction()

def mainfunction():
    for x in xrange(10):
        print x * 5

if __name__ == "__main__":
    mainfunction()


That is, paste the code up through registertracefunction() line to the top of your script and it will print out every line it's on.


Thanks Jason,
I figured that something like this would degrade performance. Debugging would be a good option except Python Addins don't lend themselves to debugging quite as well as something like a Script tool and I was really hoping to not have to resort to inserting print statements on every other line.
0 Kudos
TimBarnes
Occasional Contributor III
This may be completely obvious to you, apologies if it is, but when you are trying to debug a python addin, having the Geoprocessing> Python command line open in ArcMap while running the tool/button etc will show any error messages there.
0 Kudos