Recipes for trace statements inside python programs?
bieffe62 at gmail.com
Thu Jun 25 10:33:08 CEST 2009
as many - I think - python programmers, I find muself debugging my
scripts by placing print statements in strategic places rather than
using the python debugger, and commenting/uncommenting them according
to myy deugging needs. After a time, these prints staements start to
evolving in some ad-hoc half-baked framework ... so I wonder if there
is somewhere there is a full-baked trace statement support framework
which I can use. I'm aware of the logging module, but for me it its
more geared toward application logging rather than toward trace for
Having googlet and found nothing (or too much but nothing relefìvant),
I'm now asking The List.
Here is what I have in mind:
Each module, function, class and method should have an attribute, say
trace_flag, which can be set to true or false value.
there should be a function TRACE which does something like this:
if __debug__ :
if trace_enabled(): print "TRACE(%s) : %s " % ( context(), "
".join( str(x) for x in args ) )
where trace_enabled() should return the value of the innermost
trace_flag (checking current function/method then current class (if
any) then current module) and context() shoud return a string like
"module.function" or "module.class.method" ).
At this point I could in my test code enable the trace in the
function/class that gives me trouble and disable it after I fixed it,
without having to touch the actual code under test.
I guess it should not be too hard do using python introspection
modules, but of couse I first would like to know if something like
this already exists.
I'm aware that this imposes a performance penalty, but my scripts are
not operformance-critical. And if I put an if __debug__ switch
More information about the Python-list