Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Recipes for trace statements inside python programs?

Reply
Thread Tools

Recipes for trace statements inside python programs?

 
 
Francesco Bochicchio
Guest
Posts: n/a
 
      06-25-2009
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


 
Reply With Quote
 
 
 
 
Francesco Bochicchio
Guest
Posts: n/a
 
      06-25-2009

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
------
FB
 
Reply With Quote
 
 
 
 
koranthala
Guest
Posts: n/a
 
      06-25-2009
On Jun 25, 1:40*pm, Francesco Bochicchio <(E-Mail Removed)> wrote:
> 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
> ------
> FB


Is assert what you are looking for?
 
Reply With Quote
 
samwyse
Guest
Posts: n/a
 
      06-25-2009
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.

On Jun 25, 3:33*am, Francesco Bochicchio <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
Francesco Bochicchio
Guest
Posts: n/a
 
      06-25-2009
On 25 Giu, 13:15, koranthala <(E-Mail Removed)> wrote:
> On Jun 25, 1:40*pm, Francesco Bochicchio <(E-Mail Removed)> wrote:
>
>
> 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
---
FB
 
Reply With Quote
 
cassiope
Guest
Posts: n/a
 
      06-26-2009
On Jun 25, 1:33*am, Francesco Bochicchio <(E-Mail Removed)> wrote:
> 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/loggi...module-logging

-f
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statements and case statements questions John Crichton Ruby 6 07-12-2010 06:17 PM
Chicken Recipes - The Web's Largest Chicken Recipes Collection Lew Java 1 02-21-2009 02:44 AM
Prepare Statements VS Statements Vince Java 12 01-21-2008 01:18 PM
component statements within architecture statements Neil Zanella VHDL 8 10-20-2006 09:05 AM
if statements with or w/o else statements Harry George Python 6 02-23-2004 06:48 PM



Advertisments