[Tutor] custom error classes: how many of them does one need?
eryksun
eryksun at gmail.com
Mon Jul 8 08:28:39 CEST 2013
On Sun, Jul 7, 2013 at 2:37 PM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
> # this is a global variable (perhaps in __init__.py)
> # if warningsAsErrors > 0, even warnings will raise an error.
> import operator
> warningsAsErrors = bool(os.getenv("warningsAsErrors"))
I'd use uppercase for the environment variable, with the application
name as a prefix, and only require that it's defined (i.e. check if
it's in os.environ).
For a library you can leave it for the user to decide how to handle warnings:
http://docs.python.org/2/library/warnings
>>> class MyWarning(UserWarning): pass
...
>>> warnings.warn('the sky is falling', MyWarning)
__main__:1: MyWarning: the sky is falling
>>> warnings.simplefilter('error', MyWarning)
>>> warnings.warn('the sky is REALLY falling', MyWarning)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.MyWarning: the sky is REALLY falling
>>> warnings.simplefilter('ignore', MyWarning)
>>> warnings.warn('Listen! The sky is falling!', MyWarning)
>>> # crickets
>> SpoonError
>> +-- TeaspoonError
>> +-- SoupSpoonError
>> +-- DesertSpoonError
>
> DesertSpoonError? LOL! ;-)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
SpellingError: No dessert for you!
More information about the Tutor
mailing list