Custom exception and warning formatter 
Exceptions
Define exception format by subclassing CustomTraceback
class MyTracebackFormatter(CustomTraceback):
_EXC_FORMAT = '| {traceback}>> {type}: {exception}.\n'
_TB_FORMAT = '[{file}::{func}]@{line} "{code}" | '
_EXC_HOOK_HEAD_TEXT = 'Error:'
1. _EXC_FORMAT used for formatting single exception in exceptions chain:
_EXC_FORMAT should be python formatted string literal with values in {} brackets.
Available values for _EXC_FORMAT formatted string literal:
- traceback: formatted traceback goes here (see below)
- type: exception class name
- exception: exception message
2. _TB_FORMAT used for formatting single traceback frame in traceback chain:
_TB_FORMAT should be python formatted string literal with values in {} brackets.
Available values for _TB_FORMAT formatted string literal:
- file: python .py file stem where error occurred
- func: function or module where error occurred
- line: number of the line of code where error occurred
- code: line of code itself
3. _EXC_HOOK_HEAD_TEXT used as header for exceptions
_EXC_HOOK_HEAD_TEXT should be python string
4. Additional variables to define:
- _EXC_OUT: output for exceptions, sys.stderr by default
- _EXC_CHAIN: True - chain exceptions, False - only last exception, by default True
- _EXC_REVERSE: order of chained exceptions, True - show like default python exception hook, by default False
Usage:
- Definition
class MyTracebackFormatter(CustomTraceback):
_TB_FORMAT = '[{file}::{func}]@{line} "{code}" | '
_EXC_FORMAT = '| {traceback}>> {type}: {exception}.\n'
_EXC_HOOK_HEAD_TEXT = 'Error:'
_EXC_OUT = sys.stderr
_EXC_CHAIN = True
_EXC_REVERSE = False
- Get formatted exception string
exc_str = MyTracebackFormatter(exc)
- Print formatted exception
MyTracebackFormatter.print_exception(exc)
- Use actvate() method to alter way of presenting tracebacks for system, modules(i.e. logging) and apps:
MyTracebackFormatter.activate()
It is equivalent of executing these statements:
sys.excepthook = MyTracebackFormatter.exception_hook
traceback.print_exception = MyTracebackFormatter.traceback_print_exception_hook
traceback.format_exception = MyTracebackFormatter.traceback_format_exception_hook
Predefined traceback formatters
- compact_tb
from mwk_traceback import compact_tb
compact_tb.activate()
main() # ! check test_main.py for code !
test_tb_pr_exc() # ! check test_traceback_print_exception.py for code !
Output:
! Error(s):
| Error in [test_main.py] in [<module>] at line (27) while executing "main()"
| Error in [test_main.py] in [main] at line (22) while executing "func()"
| Error in [test_main.py] in [func] at line (17) while executing "raise"
>> NameError: error in func.
| Error in [test_main.py] in [func] at line (15) while executing "func_func()"
| Error in [test_main.py] in [func_func] at line (10) while executing "raise"
>> AttributeError: error in func_func.
| Error in [test_main.py] in [func_func] at line (8) while executing "func_func_func()"
| Error in [test_main.py] in [func_func_func] at line (3) while executing "x = 1 / 0"
>> ZeroDivisionError: division by zero.
----------------------------------------------------------------------------------------------------------
ERROR:root:logging error
ERROR:root:logging exception
| Error in [test_traceback_print_exception.py] in [test_tb_pr_exc] at line (13) while executing "x = 1 / 0"
>> ZeroDivisionError: division by zero.
ERROR:root:There was no exception
No exception is being handled.
- super_compact_tb
from mwk_traceback import super_compact_tb
super_compact_tb.activate()
main() # ! check test_main.py for code !
test_tb_pr_exc() # ! check test_traceback_print_exception.py for code !
Output:
! Error:
| [test_main::<module>]@31 "main()" | [test_main::main]@22 "func()" | [test_main::func]@17 "raise" | >> NameError: error in func.
| [test_main::func]@15 "func_func()" | [test_main::func_func]@10 "raise" | >> AttributeError: error in func_func.
| [test_main::func_func]@8 "func_func_func()" | [test_main::func_func_func]@3 "x = 1 / 0" | >> ZeroDivisionError: division by zero.
----------------------------------------------------------------------------------------------------------
ERROR:root:logging error
ERROR:root:logging exception
| [test_traceback_print_exception::test_tb_pr_exc]@14 "x = 1 / 0" | >> ZeroDivisionError: division by zero.
ERROR:root:There was no exception
No exception is being handled
Warnings
Define warning format by subclassing CustomWarningFormatter:
class MyWarningFormatter(CustomWarningFormatter):
_WARN_FORMAT = '[{file}]@{line} >> {type}: {message}\n'
_WARN_FORMAT should be python formatted string literal with values in {} brackets.
Available values for _WARN_FORMAT formatted string literal:
- message: warning message
- type: warning class name
- file: python .py file stem where warning occurred
- line: number of the line of code where warning occurred
Usage:
import warnings
warnings.formatwarning = MyWarningFormatter
warnings.warn('This is warning', UserWarning)
Predefined warning formatters (classes)
- compact_warn
import warnings
from mwk_traceback import compact_warn
warnings.formatwarning = compact_warn
warnings.warn('This is warning', RuntimeWarning)
Output:
Warning in [test_warn.py] at line (9)
>> RuntimeWarning: This is warning
- super_compact_warn
import warnings
from mwk_traceback import super_compact_warn
warnings.formatwarning = super_compact_warn
warnings.warn('This is another warning', UserWarning)
Output:
[test_warn]@13 >> UserWarning: This is another warning
Metadata
Release files for mwk-traceback 2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mwk_traceback-2.1.tar.gz | 7.8 kB | Without provenance | Details for mwk_traceback-2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mwk_traceback-2.1-py3-none-any.whl | Python 3 | none | any | Details for mwk_traceback-2.1-py3-none-any.whl |
Total release size: 14.6 kB
Release files / mwk_traceback-2.1.tar.gz
| Download URL | mwk_traceback-2.1.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
63a5b6cd9dc4b5d8e5d1536cdb17420ebe43994e0cc026785c6e7a0d6938116c
|
|
BLAKE2b-256 checksum How to use checksums |
2fd70c3f62733e70e896e9526074c397924bca950993d029985cdbb24bb95a23
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Release files / mwk_traceback-2.1-py3-none-any.whl
| Download URL | mwk_traceback-2.1-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
70dc2d08af3f6d749ed960531d64c445bc34104ccce5eed5c967ddab20c7cd11
|
|
BLAKE2b-256 checksum How to use checksums |
416e49e1bc822480967850d6207038ea0c71c0a6e51c1bcffbcb52619ab26ff7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Security for mwk-traceback 2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.