Skip to main content

Package for customizing exceptions and warnings messages.

Project description

Custom exception and warning formatter PyPI


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:

  1. 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
  1. Get formatted exception string
exc_str = MyTracebackFormatter(exc)
  1. Print formatted exception
MyTracebackFormatter.print_exception(exc)
  1. 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

  1. 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.
  1. 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)

  1. 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
  1. 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mwk_traceback-2.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

mwk_traceback-2.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file mwk_traceback-2.1.tar.gz.

File metadata

  • Download URL: mwk_traceback-2.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for mwk_traceback-2.1.tar.gz
Algorithm Hash digest
SHA256 63a5b6cd9dc4b5d8e5d1536cdb17420ebe43994e0cc026785c6e7a0d6938116c
MD5 da911e519ce89adc0de756db2cd05b0c
BLAKE2b-256 2fd70c3f62733e70e896e9526074c397924bca950993d029985cdbb24bb95a23

See more details on using hashes here.

Provenance

File details

Details for the file mwk_traceback-2.1-py3-none-any.whl.

File metadata

  • Download URL: mwk_traceback-2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for mwk_traceback-2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 70dc2d08af3f6d749ed960531d64c445bc34104ccce5eed5c967ddab20c7cd11
MD5 5a4e87d8b9010fd8a90b613d4657ce3a
BLAKE2b-256 416e49e1bc822480967850d6207038ea0c71c0a6e51c1bcffbcb52619ab26ff7

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page