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
File details
Details for the file exception-template-1.0.0.tar.gz
.
File metadata
- Download URL: exception-template-1.0.0.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a927f01540ae223805d4eb20aad0022185257d773dfbea9989bed457790165ef |
|
MD5 | 1e4a9baf9604fc3716b4be8eea4b7e64 |
|
BLAKE2b-256 | 53f9aa7483675cd2cf59fd194a1e76d99423be41e69a4cdbb42a5ef6e4782f9a |
File details
Details for the file exception_template-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: exception_template-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 284169005e9c50aff3c3cce5f48e31eda1f545c85042473d12435328b65d811a |
|
MD5 | fa534ef5be748aaa0ec107bc4e3fece9 |
|
BLAKE2b-256 | 640e162d31a5515b546964c4e30d0efbc54d7ce2857258e81b5dd8a82d0a60f2 |