Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to suppress the "source code echo" output by warnings.warn("x")?

Reply
Thread Tools

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

 
 
funkyj
Guest
Posts: n/a
 
      05-19-2006
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....e460a0ee8b03a5

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?

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      05-19-2006
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
 
Reply With Quote
 
 
 
 
funkyj
Guest
Posts: n/a
 
      05-19-2006

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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Suppress output to stdout/stderr in InteractiveInterpreter Dave W. Python 7 04-16-2010 04:46 PM
Logging: how to suppress default output when adding handlers? Chris Shenton Python 3 06-05-2007 10:13 PM
Suppress result output in irb? Tom Pollard Ruby 4 10-05-2006 01:25 PM
How do I suppress links to code names in rdoc documentation? billmcn Ruby 0 06-08-2006 07:24 PM



Advertisments