Best way to work in debug code?

Michael P. Reilly arcege at shore.net
Wed Jan 19 08:47:59 EST 2000


rdudfield at my-deja.com wrote:
: Just wondering how you people write debug code in your functions
: /methods?

: I personally write a "if (DEBUG_LEVEL >= 5):" around the code.  However
: this reduces readability.

: To fix the readability problem I usually delete debug code all the
: time.  Only having to rewrite it again ( and debug the debug code ).

: I sometimes use cvs to find that old debug code, but that's time
: consumming.

About 90-95% of the debugging I want to do is printing data, so most of
the statements would be just print's after the conditional.  Also,
"good debugging code" can sometimes add to the readability, just like
assert statements.

I have a module that contains a singleton class, which installs a
callable instance into the __builtin__ module.  The reason it is a
class instance and a module (which for this is a little redundant) is
because I used to write the short class into the code itself.  The class
looks something like:

import __builtin__
class Debug:
  from sys import stderr
  from string import join
  state, file = 0, stderr
  del stderr
  def on(self): self.state = 1
  def off(self): self.state = 0
  def __nonzero__(self): return self.state
  if __DEBUG__:
    def __call__(self, *args):
      if self.state:
        self.file.write(self.join(map(str, args))+'\n')
  else:
    __call__ = lambda *args: None
__builtin__.debug = Debug()
del __builtin__, Debug      # cleanup

Adding debug levels is as simple as changing the state and modifying
the __call__ method to handle that.

For my own usability, I also add:
import os
if os.path.exists('.debug'): debug.on()

If the file exists, I get debugging output - no change in code.

: The way that would be perfect ( for me ) would be for the editor I use(
: jed ) to be able to toggle on and off the viewing of debug code, at
: different levels of course.  Then have the python interpreter skip over
: the debug code, or optimise it out.  One way to do this would be to do
: this:

:   for x in range(0,100):
:     doNonDebugstuff()
:   debug 5:
:     print len(listOfObjects)
:     writeGraphOfObjectsToImage(listOfObjects,"graph.png")
:   doMoreNonDebugStuff()

: Where debug 5 means "if (DEBUG_LEVEL <= 5):".

: You could then call the python interpreter with a debug flag or maybe
: call a builtin function to change the debug level.

: It would be good to be able to choose to debug at level 4 and below, or
: just level 4.

I used to use debug levels, but found that it is easier to get all the
output.  The more complex the project, the more bugs can creep into other
parts of the code, debug levels can sometimes hide that creep.

: Additions to this might include using strings as debug tags. Like:

: debug "functionName":
:   doDebugStuff()

: Or both.

: debug "functionName" 4:
:   doDebugStuff()

: Being able to tell the interpreter to run all debug code in a particular
: function/method/object would be good too.

Sounds like you could easily handle some of this with a preprocessor.
For readability, I would not use "statement" syntax, but would use
"expression" syntax.  But that's just me. :)

  -Arcege




More information about the Python-list mailing list