Recipes for trace statements inside python programs?

Discussion in 'Python' started by Francesco Bochicchio, Jun 25, 2009.

  1. 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
    Francesco Bochicchio, Jun 25, 2009
    #1
    1. Advertising

  2. 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
    Francesco Bochicchio, Jun 25, 2009
    #2
    1. Advertising

  3. Francesco Bochicchio

    koranthala Guest

    On Jun 25, 1:40 pm, Francesco Bochicchio <> 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?
    koranthala, Jun 25, 2009
    #3
  4. Francesco Bochicchio

    samwyse Guest

    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 <> 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.
    samwyse, Jun 25, 2009
    #4
  5. On 25 Giu, 13:15, koranthala <> wrote:
    > On Jun 25, 1:40 pm, Francesco Bochicchio <> 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
    Francesco Bochicchio, Jun 25, 2009
    #5
  6. Francesco Bochicchio

    cassiope Guest

    On Jun 25, 1:33 am, Francesco Bochicchio <> 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/logging.html#module-logging

    -f
    cassiope, Jun 26, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rukmal Fernando
    Replies:
    4
    Views:
    838
    Richard Grimes [MVP]
    Oct 27, 2003
  2. Matthias S.
    Replies:
    1
    Views:
    635
    Daniel Fisher\(lennybacon\)
    Nov 30, 2005
  3. Jiho Han
    Replies:
    0
    Views:
    503
    Jiho Han
    Aug 22, 2006
  4. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Adding my messages to the trace.axd trace?

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Apr 6, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    580
    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
    Apr 6, 2007
  5. Lew
    Replies:
    1
    Views:
    1,311
    Frank Cisco
    Feb 21, 2009
Loading...

Share This Page