Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49363 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2013-06-28 10:57 +0200 |
| Last post | 2013-06-28 10:57 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: warnings filters for varying message Peter Otten <__peter__@web.de> - 2013-06-28 10:57 +0200
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-06-28 10:57 +0200 |
| Subject | Re: warnings filters for varying message |
| Message-ID | <mailman.3952.1372409846.3114.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web