Welcome, guest | Sign In | My Account | Store | Cart

While debugging, if you don't want a section of code to be executed temporarily, replace the expression after an 'if' or 'while' with '0' and comment out the old expression, leaving it in place.

Python, 9 lines
1
2
3
4
5
6
7
8
9
Before:

if i < 1:
   doSomething()

After:

if 0: # i< 1
   doSomething()

6 comments

Dinu Gherman 23 years, 1 month ago  # | flag

Not Python-specific. This is as much true for other languages like C or Perl... as it is for Python.

Dinu Gherman

srinivas b 23 years, 1 month ago  # | flag

Not very flexible. THis approach is not very flexible imo. If you were to have a whole bunch of such statements all over the code, then you keep changing back and forth between the debug and non-debug modes of operation. Easy for error prone and too much manual work. I would usually prefer to have a 'debug' variable defined some where in there and change the value to "0" or "1" and use that to control the program flow. That is how I usually do my debug. Love to hear from others. Srinivas B.

Michael Chermside 23 years, 1 month ago  # | flag

Play it safe. I would recommend against ever writing this:

if 0:  # i < 1:
    doSomething()

in favor of this construct:

if 0:  # i < 1:   FIXME -- restore the condition
    doSomething()

because it's quite dangerous to leave the incorrect code there... you might forget to put it back when you finish testing.

I'm not sure I would recommend the entire approach... if your editor provides any support you can write this:

## FIXME: Restore this statement after testing
##    if i < 1:
##        doSomething()

with only a couple of keystrokes. This has the advantage that the ## on the left-hand-edge of the screen really shows up, and it's clear that you're really only testing.

-- Michael Chermside Michael Chermside

Anders Schneiderman 23 years, 1 month ago  # | flag

Not very flexible, part II. I'd echo what Srinivas said. Rather than changing code, using an "if debug:" approach makes much more sense. This is particularly true if you're debugging a large application, where you want the ability to turn on and off debugging code in different objects/modules w/o having to worry about side-effects.

Another method that might be worth exploring is an object-oriented logging approach. Most of the time, I use debugging sections to print info. In that case, using a "log" object makes more sense. In other words, rather than using

if debug: print counter

use

log.out(counter)

This way, not only is the code shorter, but it also allows you to change formatting of your debugging output depending on what you're doing. If you have an object that you use both for command-line apps and web apps, you can switch between the two by simply changing which version of log you use (e.g., one class of log, weblog, might replace every "\n" with a "<br>\n" so you can more clearly read the output). Anders Schneiderman

Abhi Khanal 11 years, 3 months ago  # | flag

I am just illustrating a combination of Srinivas and Anders, since it is exactly what I use for any platform that I am developing on. Markdown is a good option for logging debug data as well. Here is essentially what I do.

#!/usr/bin/python
from sys import argv #for commandline arguments

# default mode is non-debugging
DEBUG = False

# if second commandline arguement os '-d', since the first commandline argument is the path/filename 
# as typed on the shell to execute it, it switches on the debugging mode by turning DEBUG on.
args = len(argv)

# if more than one commandline parameters
if args > 1:
    if argv[1] == '-d':
        DEBUG = True

# a very simplistic logger
def log(data):
    string_data = str(data)
    logfile = open('logfile.txt', 'a')
    logfile.write('%s\n' % data)
    logfile.close()

# for completeness
def do_something():
    pass


# some programming logic goes here
i = -1
if i < 0 and not DEBUG:
    do_something()

# somewhere else
if DEBUG:
    log(i)

I usually use multiple commandline arguments for different modes since it means I do not have to keep on toggling the DEBUG value in the source code. I also have created an entire logger module with a log class that uses different methods like:

# for html links    
log.html.href(text, link)
# for md lines
log.md.line()

It has worked pretty well thus far.

Abhi Khanal 11 years, 3 months ago  # | flag

Wow, I feel stupid. Did not see the '10 years' in 10 year 11 months ago.

Created by Chris McDonough on Sun, 4 Mar 2001 (PSF)
Python recipes (4591)
Chris McDonough's recipes (4)

Required Modules

  • (none specified)

Other Information and Tasks