Create exception classes from message templates.
Project description
Raise exceptions with consistent messages while avoiding the boiler plate.
Compare this:
>>> from exception_template import ExceptionTemplate >>> class MyException(ExceptionTemplate): ... message = 'Hello, {person}. Here is my {adjective} exception class.' >>> raise MyException(person='Ryan', adjective='fancy') Traceback (most recent call last): ... MyException: Hello, Ryan. Here is my fancy exception class.
To this:
>>> class MyException(Exception): ... def __init__(self, person: str, adjective: str) -> None: ... self.person = person ... self.adjective = adjective ... super().__init__('Hello, {person}. Here is my {adjective} exception class.' ... .format(person=person, adjective=adjective)) >>> raise MyException('Ryan', 'lame') Traceback (most recent call last): ... MyException: Hello, Ryan. Here is my lame exception class.
Or this:
>>> class MyException(Exception): ... pass >>> raise MyException('Hello, Ryan. Here is my exception class with a message I copy-pasted in 500 places.') Traceback (most recent call last): ... MyException: Hello, Ryan. Here is my exception class with a message I copy-pasted in 500 places.
Additionally, the ExceptionTemplate parameters are available as members on the exception instance:
>>> try: ... foo() ... except MyException as ex: ... print(ex.person) Ryan
Installing
pip install exception-template
No extra dependencies are needed!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for exception_template-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 284169005e9c50aff3c3cce5f48e31eda1f545c85042473d12435328b65d811a |
|
MD5 | fa534ef5be748aaa0ec107bc4e3fece9 |
|
BLAKE2b-256 | 640e162d31a5515b546964c4e30d0efbc54d7ce2857258e81b5dd8a82d0a60f2 |