how to suppress the "source code echo" output by warnings.warn("x")?

Discussion in 'Python' started by funkyj, May 19, 2006.

  1. funkyj

    funkyj Guest

    I've been googling around trying to find the answer to this question
    but all I've managed to turn up is a 2 year old post of someone else
    asking the same question (no answer though).

    http://groups.google.com/group/comp...=python warnings echo&rnum=6#81e460a0ee8b03a5

    jh> In the following
    jh>
    jh> import warnings
    jh> warnings.warn('change me')
    jh>
    jh> The warning is issued:
    jh>
    jh> hunter:~/python/test> python test.py
    jh> test.py:3: UserWarning: change me
    jh> warnings.warn('change me')
    jh>
    jh> I want to supress the line echo. Eg, I just want
    jh>
    jh> hunter:~/python/test> python test.py
    jh> test.py:3: UserWarning: change me
    jh>
    jh> How do I configure warnings to do this?

    Perhaps this can't be done without rewriting the warning module?
     
    funkyj, May 19, 2006
    #1
    1. Advertising

  2. funkyj

    Peter Otten Guest

    funkyj wrote:

    > I've been googling around trying to find the answer to this question
    > but all I've managed to turn up is a 2 year old post of someone else
    > asking the same question (no answer though).


    > jh> In the following
    > jh>
    > jh> import warnings
    > jh> warnings.warn('change me')
    > jh>
    > jh> The warning is issued:
    > jh>
    > jh> hunter:~/python/test> python test.py
    > jh> test.py:3: UserWarning: change me
    > jh> warnings.warn('change me')
    > jh>
    > jh> I want to supress the line echo. Eg, I just want
    > jh>
    > jh> hunter:~/python/test> python test.py
    > jh> test.py:3: UserWarning: change me
    > jh>
    > jh> How do I configure warnings to do this?
    >
    > Perhaps this can't be done without rewriting the warning module?


    How about monkey-patching?

    import warnings

    def formatwarning(message, category, filename, lineno):
    return "%s:%s: %s: %s\n" % (filename, lineno,
    category.__name__, message)

    warnings.formatwarning = formatwarning

    warnings.warn("so what")

    Peter
     
    Peter Otten, May 19, 2006
    #2
    1. Advertising

  3. funkyj

    funkyj Guest

    Peter Otten wrote:
    > funkyj wrote:
    >
    > > I've been googling around trying to find the answer to this question
    > > but all I've managed to turn up is a 2 year old post of someone else
    > > asking the same question (no answer though).


    > How about monkey-patching?
    >
    > import warnings
    >
    > def formatwarning(message, category, filename, lineno):
    > return "%s:%s: %s: %s\n" % (filename, lineno,
    > category.__name__, message)
    >
    > warnings.formatwarning = formatwarning
    >
    > warnings.warn("so what")


    Thanks, that did the trick!
     
    funkyj, May 19, 2006
    #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. neo_anderson
    Replies:
    0
    Views:
    929
    neo_anderson
    Mar 13, 2008
  2. Replies:
    9
    Views:
    6,238
    John B. Matthews
    Apr 20, 2009
  3. Aaron Smith
    Replies:
    3
    Views:
    155
    Johannes Held
    Aug 26, 2007
  4. Ted Sung
    Replies:
    1
    Views:
    313
    Sherm Pendley
    Aug 30, 2004
  5. mathog
    Replies:
    47
    Views:
    422
    Tim Rentsch
    Jun 12, 2014
Loading...

Share This Page