Re: Debug python programs

Discussion in 'Python' started by Jeremy Jones, Sep 11, 2003.

  1. Jeremy Jones

    Jeremy Jones Guest

    * York () wrote:
    > Hi, anybody,
    >
    > Would you please recommend some good softwares/methods for
    > tracking/debugging python codes.
    >
    > Thanks,
    > York


    You've got lots of options.

    1) Your standard insertion of "print" statements throughout the code. A
    lot of times, this is good enough to get the job done. I typically set an
    instance variable named DEBUG for each class that I create (in __init__,
    set self.DEBUG = 0 or 1). Then I have lots of
    if self.DEBUG:
    print "whatever" #insert debug statements here
    It's not the most elegant thing, but man, has it come in handy when you
    don't need a full-blown debugger. (Segue into number 2)

    2) Python comes with a pretty good debugger built in. Here are the docs:
    http://www.python.org/doc/current/lib/module-pdb.html

    3) pychecker. I know, I know, I know - it's not a debugger. But if you
    point it at your code, it'll inspect it and can help you uncover some
    mistakes that can bite you in the rear.

    4) trace.py. Never used it, but it's in the standard distribution now
    under Tools/scripts. (I'm including this line item since you mentioned
    "tracking". I guess this is what you mean?)

    HTH


    Jeremy Jones
     
    Jeremy Jones, Sep 11, 2003
    #1
    1. Advertising

  2. Jeremy Jones

    Peter Abel Guest

    Jeremy Jones <> wrote in message news:<>...
    > * York () wrote:
    > > Hi, anybody,
    > >
    > > Would you please recommend some good softwares/methods for
    > > tracking/debugging python codes.
    > >
    > > Thanks,
    > > York

    >
    > You've got lots of options.
    >
    > 1) Your standard insertion of "print" statements throughout the code. A
    > lot of times, this is good enough to get the job done. I typically set an
    > instance variable named DEBUG for each class that I create (in __init__,
    > set self.DEBUG = 0 or 1). Then I have lots of
    > if self.DEBUG:
    > print "whatever" #insert debug statements here
    > It's not the most elegant thing, but man, has it come in handy when you
    > don't need a full-blown debugger. (Segue into number 2)
    >
    > 2) Python comes with a pretty good debugger built in. Here are the docs:
    > http://www.python.org/doc/current/lib/module-pdb.html
    >
    > 3) pychecker. I know, I know, I know - it's not a debugger. But if you
    > point it at your code, it'll inspect it and can help you uncover some
    > mistakes that can bite you in the rear.
    >
    > 4) trace.py. Never used it, but it's in the standard distribution now
    > under Tools/scripts. (I'm including this line item since you mentioned
    > "tracking". I guess this is what you mean?)
    >
    > HTH
    >
    >
    > Jeremy Jones


    Try boa:
    http://boa-constructor.sourceforge.net/

    At the moment it is
    "Project of the month: Boa Constructor" at
    http://sourceforge.net/potm/potm-2003-08.php

    Regards
    Peter
     
    Peter Abel, Sep 11, 2003
    #2
    1. Advertising

  3. Jeremy Jones

    Guest

    I have found a combination of the logging and optparse modules to be a
    very effective alternative to #1 (Both come standard with 2.3 or you
    can download/install them yourself). That way I just hand my script
    --debug, sprinkle my code where I want with log.debug("message") and
    voila! I get debug messages and the code looks much cleaner.

    I can also do the same with whatever modules I create and import by
    calling getLogger() at the top of the module
    ie. log = logging.getLogger("auto_test")

    import logging
    from optparse import OptionParser

    p = OptionParser()
    p.add_option("", "--debug", action="store_const", const=1, default=0,
    dest="debug", help="Print out debug information")

    log = logging.getLogger("auto_test")
    handler = logging.StreamHandler()

    if opts.debug == 1:
    log.setLevel(logging.DEBUG)
    format = logging.Formatter("%(asctime)s %(levelname)-3s
    [%(module)s:%" \
    "(lineno)d] %(message)s")
    else:
    log.setLevel(logging.INFO)
    format = logging.Formatter("%(asctime)s %(levelname)-3s
    %(message)s")

    handler.setFormatter(format)
    log.addHandler(handler)

    Gotta love the snake!

    --ERick


    Jeremy Jones <> wrote in message news:<>...
    > * York () wrote:
    > > Hi, anybody,
    > >
    > > Would you please recommend some good softwares/methods for
    > > tracking/debugging python codes.
    > >
    > > Thanks,
    > > York

    >
    > You've got lots of options.
    >
    > 1) Your standard insertion of "print" statements throughout the code. A
    > lot of times, this is good enough to get the job done. I typically set an
    > instance variable named DEBUG for each class that I create (in __init__,
    > set self.DEBUG = 0 or 1). Then I have lots of
    > if self.DEBUG:
    > print "whatever" #insert debug statements here
    > It's not the most elegant thing, but man, has it come in handy when you
    > don't need a full-blown debugger. (Segue into number 2)
    >
    > 2) Python comes with a pretty good debugger built in. Here are the docs:
    > http://www.python.org/doc/current/lib/module-pdb.html
    >
    > 3) pychecker. I know, I know, I know - it's not a debugger. But if you
    > point it at your code, it'll inspect it and can help you uncover some
    > mistakes that can bite you in the rear.
    >
    > 4) trace.py. Never used it, but it's in the standard distribution now
    > under Tools/scripts. (I'm including this line item since you mentioned
    > "tracking". I guess this is what you mean?)
    >
    > HTH
    >
    >
    > Jeremy Jones
     
    , Sep 12, 2003
    #3
    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. Replies:
    12
    Views:
    1,702
    Dave Thompson
    Jan 10, 2005
  2. Mike C. Fletcher
    Replies:
    3
    Views:
    1,018
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Oct 12, 2003
  3. Coca
    Replies:
    7
    Views:
    777
    Aidan Grey
    Aug 24, 2004
  4. Replies:
    18
    Views:
    661
    Dave Thompson
    Jan 10, 2005
  5. lone_eagle
    Replies:
    3
    Views:
    670
    psykeedelik
    May 26, 2009
Loading...

Share This Page