Office-M > SoftwareDevelopment > Tips > Python

Trace-Function |

Python
======

I wanted a simple trace-function under simple control:

def TraceBeg ():
    """Define Trace-Entry."""
    if DEBUG > 1:
        print ">>>>>", sys._getframe(1).f_code.co_name

def TraceEnd ():
    """Define Trace-Exit."""
    if DEBUG > 1:
        print "<<<<<", sys._getframe(1).f_code.co_name

The heart is "sys._getframe(1).f_code.co_name".
To trace a function you simply have to write "TraceBeg ()" at the beginning and "TraceEnd ()" at the end of the function.

Example:

#!/usr/bin/env python
# Demonstrate usage of small trace-functions.

import sys

DEBUG = 2

def TraceBeg ():
    """Define Trace-Entry."""
    if DEBUG > 1:
        print ">>>>>", sys._getframe(1).f_code.co_name

def TraceEnd ():
    """Define Trace-Exit."""
    if DEBUG > 1:
        print "<<<<<", sys._getframe(1).f_code.co_name

def TestSub ():
    TraceBeg ()
    print "Hello Space!"
    TraceEnd ()

print "With trace..."
TestSub ()

print

DEBUG = 0
print "Without trace..."
TestSub ()


^Top


Questions / tips?
markus AT office-m.at

Last Update: 23.07.2004 11:13:04 Uhr