Re: How do I create user-defined warnings?

2008-06-18 Thread Andrii V. Mishkovskyi
2008/6/18 Clay Hobbs [EMAIL PROTECTED]:
 I already know how to make user-defined exceptions, like this one:

class MyException(Exception):
pass

 But for a module I'm making, I would like to make a warning (so it just
 prints the warning to stderr and doesn't crash the program).  I have
 tried this:

class MyWarning(Warning):
pass

 And it behaves like a normal error.  Please help me, I can't figure out
 what I'm doing wrong.

Use 'warnings' module.
http://docs.python.org/lib/module-warnings.html


 --
 Ratfink

 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
Wbr, Andrii Mishkovskyi.

He's got a heart of a little child, and he keeps it in a jar on his desk.
--
http://mail.python.org/mailman/listinfo/python-list


How do I create user-defined warnings?

2008-06-17 Thread Clay Hobbs
I already know how to make user-defined exceptions, like this one:

class MyException(Exception):
pass

But for a module I'm making, I would like to make a warning (so it just
prints the warning to stderr and doesn't crash the program).  I have
tried this:

class MyWarning(Warning):
pass

And it behaves like a normal error.  Please help me, I can't figure out
what I'm doing wrong.

-- 
Ratfink

--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I create user-defined warnings?

2008-06-17 Thread Hans Nowak

Clay Hobbs wrote:

I already know how to make user-defined exceptions, like this one:

class MyException(Exception):
pass

But for a module I'm making, I would like to make a warning (so it just
prints the warning to stderr and doesn't crash the program).  I have
tried this:

class MyWarning(Warning):
pass

And it behaves like a normal error.  Please help me, I can't figure out
what I'm doing wrong.


Are you using the warning with 'raise'?  Don't do that, use warnings.warn 
instead:

In [1]: import warnings

In [2]: class MyWarning(Warning): pass
   ...:

In [3]: warnings.warn(MyWarning(bah humbug))
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/IPython/FakeModule.py:1: 
MyWarning: bah humbug

  # -*- coding: utf-8 -*-

--
Hans Nowak (zephyrfalcon at gmail dot com)
http://4.flowsnake.org/
--
http://mail.python.org/mailman/listinfo/python-list