Recipes for trace statements inside python programs?

  • Thread starter Francesco Bochicchio
  • Start date
F

Francesco Bochicchio

Hi all,

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
debugging purpose.

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__ :
def TRACE(*args):
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
 
F

Francesco Bochicchio

Sorry, hit the send button by mistake ...
The definition of the trace function should be like:

if __debug__ :
def TRACE(*args):
if trace_enabled(): print "TRACE(%s) : %s " % ( context(),
" ".join( str(x) for x in args ) )
else : # optimazed code, only a function call performance penalty
def TRACE(*args): pass


If I don't find anything suitable, maybe I will bake my own again,
this
time aiming to something that I can reuse ...

Ciao and thanks for any tip/suggestion
 
K

koranthala

Sorry, hit the send button by mistake ...
The definition of the trace function should be like:

if __debug__ :
   def TRACE(*args):
     if  trace_enabled(): print "TRACE(%s) : %s " % ( context(),
                              " ".join( str(x) for x in args ) )
else : # optimazed code, only a function call performance penalty
   def TRACE(*args): pass

If I don't find anything suitable, maybe I will bake my own again,
this
time aiming to something that I can reuse ...

Ciao and thanks for any tip/suggestion

Is assert what you are looking for?
 
S

samwyse

I use an @trace decorator. This (http://wordaligned.org/articles/
echo) will get you started but there are lots of others available. My
personal preference is a decorator that catches, displays and re-
raises exceptions as well as displaying both calling parameters and
returned values.

btw, here's a cool Python3K (or 2.6 if you turn on print functions)
trick that's sort-of on topic since it relates to log files and such:
import functools, sys
warn = functools.partial(print, file=sys.stderr)
logfile = open(...)
log = functools.partial(print, file=logfile)
# etc.
 
F

Francesco Bochicchio

Is assert what you are looking for?
No. Assert raises exception if some condition is met. I just want to
be able to enable/disable the
printout of intermediate data, on a per function/method/class/module
basis, without altering the
execution flow.

Ciao
 
C

cassiope

Hi all,

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
debugging purpose.

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__ :
def TRACE(*args):
     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

What are you trying to achieve that could not be accomplished with
the logging module?

http://docs.python.org/library/logging.html#module-logging

-f
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top