Re: Debugging Python ?

Discussion in 'Python' started by Michael Sparks, Jul 28, 2003.

  1. On Mon, 28 Jul 2003, Shane Hathaway wrote:

    > As a bonus, I find that print statements do not rightfully belong in
    > most of the software I write. Therefore I can happily add print
    > statements anywhere for debugging purposes, knowing that removing them
    > later is perfectly safe.


    As an extra bonus, if you do this you can remove them all automatically:

    def print(*args):
    for arg in args:
    print arg,
    return True


    assert print("Well, well, well", "said", "he")

    To erase all tracks of the debugging statements at runtime, just use the
    -O flag. I tend to use a variant of this for debugging systems that have
    "complex" concurrency to make it simpler to track "who" "said" what.

    I know you can do the same with just grep if you use print - but you have
    to ensure that your debugging statements don't span multiple lines.


    Michael.
    Michael Sparks, Jul 28, 2003
    #1
    1. Advertising

  2. Michael Sparks

    John Roth Guest

    "Michael Sparks" <> wrote in message
    news:...
    > On Mon, 28 Jul 2003, Shane Hathaway wrote:
    >
    > > As a bonus, I find that print statements do not rightfully belong in
    > > most of the software I write. Therefore I can happily add print
    > > statements anywhere for debugging purposes, knowing that removing them
    > > later is perfectly safe.


    Yup. AFIC, the print statement is only for debugging, and it's quite
    effective for that purpose.

    > As an extra bonus, if you do this you can remove them all automatically:
    >
    > def print(*args):
    > for arg in args:
    > print arg,
    > return True
    >
    >
    > assert print("Well, well, well", "said", "he")
    >
    > To erase all tracks of the debugging statements at runtime, just use the
    > -O flag. I tend to use a variant of this for debugging systems that have
    > "complex" concurrency to make it simpler to track "who" "said" what.
    >
    > I know you can do the same with just grep if you use print - but you have
    > to ensure that your debugging statements don't span multiple lines.


    Which mine do, quite frequently. By the time you get this far,
    you've pretty much got a logging system.

    Another interesting arguement for the print statement versus
    online debuggers is that, if you practice test driven development,
    you gradually lose your facility with the debugger - you use it
    so seldom.

    John Roth
    >
    >
    > Michael.
    >
    >
    John Roth, Jul 28, 2003
    #2
    1. Advertising

  3. Michael Sparks

    Keith Jones Guest

    On Mon, 28 Jul 2003 13:23:43 +0000, John Roth wrote:

    >
    > Yup. AFIC, the print statement is only for debugging, and it's quite
    > effective for that purpose.
    >


    I would guess it's pretty good for teaching programming, too. Seeing

    print "Hello world"

    is more welcoming than even:

    import sys
    sys.stdout.write("Hello world\n")
    Keith Jones, Jul 28, 2003
    #3
  4. I use:

    if __debug__: print "bla, bla, bla..."

    And I was just wondering if it's legal to define a "print" function...
    isn't print a keyword ?
    Pedro Werneck, Jul 28, 2003
    #4
  5. Pedro> And I was just wondering if it's legal to define a "print"
    Pedro> function... isn't print a keyword ?

    You're right. You'd need it to be Print or print_ or something similar.

    Skip
    Skip Montanaro, Jul 29, 2003
    #5
  6. (Pedro Werneck) wrote in message news:<>...
    > I use:
    >
    > if __debug__: print "bla, bla, bla..."
    >
    > And I was just wondering if it's legal to define a "print" function...
    > isn't print a keyword ?


    Instead of defining "print" you can assign a custom class to
    sys.stdout. Then all the print statements get automagically
    redirected.:

    if __debug__:
    class newStdout:
    def write(self,string):
    print string
    else:
    class newStdout:
    def write(self,string):
    pass

    import sys
    sys.stdout = newStdout()
    logistix at cathoderaymission.net, Jul 29, 2003
    #6
  7. Michael Sparks

    Anand Pillai Guest

    It would be nice if you could give a name
    to your email address, i.e if you are not
    that paranoid as it seems.

    ~Anand

    (logistix at cathoderaymission.net) wrote in message news:<>...
    > (Pedro Werneck) wrote in message news:<>...
    > > I use:
    > >
    > > if __debug__: print "bla, bla, bla..."
    > >
    > > And I was just wondering if it's legal to define a "print" function...
    > > isn't print a keyword ?

    >
    > Instead of defining "print" you can assign a custom class to
    > sys.stdout. Then all the print statements get automagically
    > redirected.:
    >
    > if __debug__:
    > class newStdout:
    > def write(self,string):
    > print string
    > else:
    > class newStdout:
    > def write(self,string):
    > pass
    >
    > import sys
    > sys.stdout = newStdout()
    Anand Pillai, Jul 29, 2003
    #7
  8. Michael Sparks

    Aahz Guest

    Casting stones (was Re: Debugging Python ?)

    In article <>,
    Anand Pillai <> wrote:
    >
    >It would be nice if you could give a name to your email address, i.e if
    >you are not that paranoid as it seems.


    Top-posting is more annoying IMO. I remember e-mail addresses better
    than names, anyway.
    --
    Aahz () <*> http://www.pythoncraft.com/

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
    Aahz, Jul 29, 2003
    #8
  9. That's a nice idea... I done something similar to redirect print
    statements to a text widget in a debug environment I am developing...
    but my point here is not to redirect output... I think a statement
    that simply does: if __debug__: print "..." would ease
    debugging... that: def _print(*args): for arg in args:
    print arg return True assert _print("bla", "bla bla") is very
    interesting but it would be nice to have a standard way to do that...
    a new statement, a modified assert statement or a new builtin... I am
    not that experienced on python, so I don't know... what do you Python
    gurus think ? Is this worth the effort ?
    Pedro Werneck, Jul 29, 2003
    #9
  10. Michael Sparks

    Alan Kennedy Guest

    Re: Casting stones (was Re: Debugging Python ?)

    Aahz wrote:

    > Top-posting is more annoying IMO.


    Aahz,

    I know that you champion the cause of stopping people from
    top-posting, and I agree that bottom-posting a good thing.

    However, there is a down side: it makes it impossible to get
    meaningful summaries of a posters posting history on Google Groups.
    For example, here is your own history of posts:-

    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&q=author:

    As you can see if you view that history, the summaries represented do
    not contain what you actually said or wrote: they contain what other
    people said, that you replied to. While this is generally not a
    problem, you might find occasionally that it will appear, at the
    summary level, that you said something that it is the precise opposite
    of what you actually think or feel.

    I don't have a solution, but I thought I would point out the problem.

    regards,

    --
    alan kennedy
    -----------------------------------------------------
    check http headers here: http://xhaus.com/headers
    email alan: http://xhaus.com/mailto/alan
    Alan Kennedy, Jul 29, 2003
    #10
  11. Michael Sparks

    Aahz Guest

    Re: Casting stones (was Re: Debugging Python ?)

    In article <>,
    Alan Kennedy <> wrote:
    >Aahz wrote:
    >>
    >> Top-posting is more annoying IMO.

    >
    >However, there is a down side: it makes it impossible to get
    >meaningful summaries of a posters posting history on Google Groups.


    To the extent that's an issue (and I don't necessarily agree that it is
    an issue), I consider that Google's fault for not writing software that
    accounts for quoting. If/when they fix their software, that solves the
    issue from your perspective; in the meantime, we get easier-to-read
    threads in realtime.
    --
    Aahz () <*> http://www.pythoncraft.com/

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
    Aahz, Jul 29, 2003
    #11
  12. Michael Sparks

    Alan Kennedy Guest

    Re: Casting stones (was Re: Debugging Python ?)

    [Aahz]
    >>> Top-posting is more annoying IMO.


    [Alan Kennedy]
    >>However, there is a down side: it makes it impossible to get
    >>meaningful summaries of a posters posting history on Google Groups.


    [Aahz]
    > To the extent that's an issue (and I don't necessarily agree that it is
    > an issue), I consider that Google's fault for not writing software that
    > accounts for quoting. If/when they fix their software, that solves the
    > issue from your perspective; in the meantime, we get easier-to-read
    > threads in realtime.


    I agree that Google could try harder, and also agree that we should
    not start top-posting, just to hack around Google.

    It is interesting to note that the Timbot's style of posting seems to
    address this problem, if not solve it.

    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&q=author:

    It uses a style I have copied in this message, which is

    [Original poster]
    > What the original poster said


    With only a single line break between the two lines. The Timbots
    "summary view" at least shows that the text one one is seeing in the
    summary are the words of someone else.

    My own posting style, which is

    Original Poster:

    > What the original poster said


    fails to achieve that.

    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&q=author:

    Let's see how this message turns up on Google Groups.

    regards,

    --
    alan kennedy
    -----------------------------------------------------
    check http headers here: http://xhaus.com/headers
    email alan: http://xhaus.com/mailto/alan
    Alan Kennedy, Jul 29, 2003
    #12
  13. (Pedro Werneck) wrote in message news:<>...
    > That's a nice idea... I done something similar to redirect print
    > statements to a text widget in a debug environment I am developing...
    > but my point here is not to redirect output... I think a statement
    > that simply does: if __debug__: print "..." would ease
    > debugging... that: def _print(*args): for arg in args:
    > print arg return True assert _print("bla", "bla bla") is very
    > interesting but it would be nice to have a standard way to do that...
    > a new statement, a modified assert statement or a new builtin... I am
    > not that experienced on python, so I don't know... what do you Python
    > gurus think ? Is this worth the effort ?


    The logging package in the soon-to-be-released 2.3 should take care of
    you, and might even be overkill ;)

    http://www.python.org/doc/2.3c2/whatsnew/node9.html
    logistix at cathoderaymission.net, Jul 29, 2003
    #13
  14. (Anand Pillai) writes:

    > It would be nice if you could give a name
    > to your email address, i.e if you are not
    > that paranoid as it seems.


    I know his name, and I bet a bit of work with google will find it for
    you too, if you care -- though I can't see why you would.

    Cheers,
    mwh

    --
    3. Syntactic sugar causes cancer of the semicolon.
    -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
    Michael Hudson, Jul 29, 2003
    #14
  15. Michael Sparks

    Trent Mick Guest

    ....
    > Which mine do, quite frequently. By the time you get this far,
    > you've pretty much got a logging system.

    ....

    Python's new logging system (standard in the coming Python 2.3 and
    available for Python versions back to 1.5.2 here
    http://www.red-dove.com/python_logging.html) is good for not
    re-inventing the wheel here. Granted there is a learning curve greater
    than:
    if __debug__: print "yada yada yada"
    but it is time well spent.

    Cheers,
    Trent

    --
    Trent Mick
    Trent Mick, Jul 29, 2003
    #15
    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. Johann Blake
    Replies:
    1
    Views:
    1,194
    Hermit Dave
    Jan 5, 2004
  2. Bill Loren

    Debugging Python ?

    Bill Loren, Jul 28, 2003, in forum: Python
    Replies:
    2
    Views:
    336
    Syver Enstad
    Aug 2, 2003
  3. Andrew Chalk

    Debugging a Python program from a browser

    Andrew Chalk, Aug 1, 2003, in forum: Python
    Replies:
    0
    Views:
    278
    Andrew Chalk
    Aug 1, 2003
  4. jacob navia

    Debugging C vs debugging C++

    jacob navia, Oct 26, 2006, in forum: C Programming
    Replies:
    11
    Views:
    586
    Ian Collins
    Oct 27, 2006
  5. AAaron123
    Replies:
    3
    Views:
    659
    AAaron123
    Jul 28, 2009
Loading...

Share This Page