Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49363
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'warnings': 0.04; 'true,': 0.05; 'matched': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'determines': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'str)': 0.16; 'url:html)': 0.16; 'wrote:': 0.18; 'module': 0.19; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'script': 0.25; 'second': 0.26; 'header:X-Complaints-To:1': 0.27; 'prints': 0.31; 'class': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'url:org': 0.36; 'should': 0.36; 'filter': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'john': 0.61; 'simple': 0.61; 'first': 0.61; 'skip:w 30': 0.69; 'glance': 0.84; 'reid': 0.84; 'warning.': 0.84; 'defeat': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: warnings filters for varying message |
| Date | Fri, 28 Jun 2013 10:57:24 +0200 |
| Organization | None |
| References | <kqj9k5$kj9$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084b9a0.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3952.1372409846.3114.python-list@python.org> (permalink) |
| Lines | 57 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1372409846 news.xs4all.nl 15879 [2001:888:2000:d::a6]:59555 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:49363 |
Show key headers only | View raw
John Reid wrote:
> Looking at the docs for warnings.simplefilter
> (http://docs.python.org/2/library/warnings.html) I think the following
> script should only produce one warning at each line as any message is
> matched by the simple filter
>
> import warnings
> warnings.simplefilter('default')
> for i in xrange(2):
> warnings.warn('Warning message') # This prints 1 warning
> warnings.warn("Warning %d" % i) # This prints 2 warnings
>
> What do I need to do to get the warnings module just to print one
> warning for the second warnings.warn line?
$ cat warn_once_orig.py
import warnings
for i in xrange(2):
warnings.warn('Warning message')
warnings.warn("Warning %d" % i)
$ python -i warn_once_orig.py
warn_once_orig.py:4: UserWarning: Warning message
warnings.warn('Warning message')
warn_once_orig.py:5: UserWarning: Warning 0
warnings.warn("Warning %d" % i)
warn_once_orig.py:5: UserWarning: Warning 1
warnings.warn("Warning %d" % i)
>>> __warningregistry__
{('Warning message', <type 'exceptions.UserWarning'>, 4): True, ('Warning
0', <type 'exceptions.UserWarning'>, 5): True, ('Warning 1', <type
'exceptions.UserWarning'>, 5): True}
As you can see the message is part of the key that determines the identity
of a warning. At first glance I see no official way to defeat that, so
here's a quick hack:
$ cat warn_once.py
import warnings
class WarningMessage(str):
def __hash__(self):
return 0
def __eq__(self, other):
return isinstance(other, str)
for i in xrange(2):
warnings.warn('Warning message')
warnings.warn(WarningMessage("Warning %d" % i))
$ python warn_once.py
warn_once.py:10: UserWarning: Warning message
warnings.warn('Warning message')
warn_once.py:11: UserWarning: Warning 0
warnings.warn(WarningMessage("Warning %d" % i))
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: warnings filters for varying message Peter Otten <__peter__@web.de> - 2013-06-28 10:57 +0200
csiph-web