views:

96

answers:

3

I'm issuing lots of warnings in a validator, and I'd like to suppress everything in stdout except the message that is supplied to warnings.warn().

I.e., now I see this:

./file.py:123: UserWarning: My looong warning message
some Python code

I'd like to see this:

My looong warning message

Edit 2: Overriding warnings.showwarning() turned out to work:

def _warning(
    message,
    category = UserWarning,
    filename = '',
    lineno = -1):
    print(message)
...
warnings.showwarning = _vcard_warning
warnings.warn('foo')
+4  A: 

Monkeypatch warnings.showwarning() with your own custom function.

Ignacio Vazquez-Abrams
Good idea, but I was hoping for something a little less drastic. See next edit of the question.
l0b0
Did you read the link? And I quote: "You may replace this function with an alternative implementation by assigning to warnings.showwarning.". The manual *itself* recommends that you monkeypatch the module.
Ignacio Vazquez-Abrams
don't worry, it's perfectly normal python procedure, not drastic at all.
CrazyJugglerDrummer
+1  A: 

There is always monkeypatching:

import warnings

def custom_formatwarning(msg, *a):
    # ignore everything except the message
    return str(msg) + '\n'

warnings.formatwarning = custom_formatwarning
warnings.warn("achtung")
Otto Allmendinger
+2  A: 

Use the logging module instead of warnings.

Geoff Reedy