{"id":19860350,"url":"https://github.com/mwksolution/mwk-traceback","last_synced_at":"2025-09-09T22:35:16.455Z","repository":{"id":207629334,"uuid":"719720007","full_name":"MWKSolution/mwk-traceback","owner":"MWKSolution","description":"Package for customizing exceptions and warnings messages","archived":false,"fork":false,"pushed_at":"2024-01-27T14:36:42.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-01T00:12:14.361Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MWKSolution.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-16T18:58:24.000Z","updated_at":"2023-11-16T19:46:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"c6727566-7788-4672-8fa1-7f3e6d5ce564","html_url":"https://github.com/MWKSolution/mwk-traceback","commit_stats":{"total_commits":6,"total_committers":1,"mean_commits":6.0,"dds":0.0,"last_synced_commit":"b3be5e5c1298ad05a7063da5aa02eaf553d2b895"},"previous_names":["mwksolution/mwk-traceback"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/MWKSolution/mwk-traceback","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MWKSolution%2Fmwk-traceback","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MWKSolution%2Fmwk-traceback/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MWKSolution%2Fmwk-traceback/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MWKSolution%2Fmwk-traceback/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MWKSolution","download_url":"https://codeload.github.com/MWKSolution/mwk-traceback/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MWKSolution%2Fmwk-traceback/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271888099,"owners_count":24839139,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-24T02:00:11.135Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T15:04:35.166Z","updated_at":"2025-08-24T14:37:37.119Z","avatar_url":"https://github.com/MWKSolution.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Custom exception and warning formatter [![PyPI](https://img.shields.io/pypi/v/mwk-traceback)](https://pypi.org/project/mwk-traceback/) \n\n---\n\n## Exceptions\n### Define exception format by subclassing **CustomTraceback**\n```python\nclass MyTracebackFormatter(CustomTraceback):\n    _EXC_FORMAT = '| {traceback}\u003e\u003e {type}: {exception}.\\n'\n    _TB_FORMAT = '[{file}::{func}]@{line} \"{code}\" | '\n    _EXC_HOOK_HEAD_TEXT = 'Error:'\n```\n*1. **_EXC_FORMAT** used for formatting single exception in exceptions chain:*  \n**_EXC_FORMAT** should be *python formatted string literal* with values in **{}** brackets.\nAvailable values for **_EXC_FORMAT** *formatted string literal*:\n- **traceback**: formatted traceback goes here (see below)\n- **type**: exception class name\n- **exception**: exception message  \n\n*2. **_TB_FORMAT** used for formatting single traceback frame in traceback chain:*  \n**_TB_FORMAT** should be *python formatted string literal* with values in **{}** brackets.\nAvailable values for **_TB_FORMAT** *formatted string literal*:\n- **file**: python .py file stem where error occurred\n- **func**: function or module where error occurred\n- **line**: number of the line of code where error occurred  \n- **code**: line of code itself\n\n*3. **_EXC_HOOK_HEAD_TEXT** used as header for exceptions*  \n**_EXC_HOOK_HEAD_TEXT** should be python string\n\n*4. Additional variables to define:*\n- **_EXC_OUT**: output for exceptions, **sys.stderr**  by default\n- **_EXC_CHAIN**: **True** - chain exceptions, **False** - only last exception, by default **True**\n- **_EXC_REVERSE**: order of chained exceptions, **True** - show like default python exception hook, by default **False**\n### Usage:\n1. Definition\n```python\nclass MyTracebackFormatter(CustomTraceback):\n    _TB_FORMAT = '[{file}::{func}]@{line} \"{code}\" | '\n    _EXC_FORMAT = '| {traceback}\u003e\u003e {type}: {exception}.\\n'\n    _EXC_HOOK_HEAD_TEXT = 'Error:'\n    _EXC_OUT = sys.stderr\n    _EXC_CHAIN = True\n    _EXC_REVERSE = False\n```\n2. Get formatted exception string\n```python\nexc_str = MyTracebackFormatter(exc)\n```\n3. Print formatted exception\n```python\nMyTracebackFormatter.print_exception(exc)\n```\n4. Use **actvate()** method to alter way of presenting tracebacks for system, modules(i.e. logging) and apps:\n```python\nMyTracebackFormatter.activate()\n```\nIt is equivalent of executing these statements:\n```python\nsys.excepthook = MyTracebackFormatter.exception_hook\ntraceback.print_exception = MyTracebackFormatter.traceback_print_exception_hook\ntraceback.format_exception = MyTracebackFormatter.traceback_format_exception_hook\n```\n### Predefined traceback formatters\n1. **compact_tb**\n```python\nfrom mwk_traceback import compact_tb\n\ncompact_tb.activate()\n\nmain()  # ! check test_main.py for code !\ntest_tb_pr_exc()  # ! check test_traceback_print_exception.py for code !\n```\nOutput:\n```commandline\n! Error(s):\n| Error in [test_main.py] in [\u003cmodule\u003e] at line (27) while executing \"main()\"\n| Error in [test_main.py] in [main] at line (22) while executing \"func()\"\n| Error in [test_main.py] in [func] at line (17) while executing \"raise\"\n   \u003e\u003e NameError: error in func.\n| Error in [test_main.py] in [func] at line (15) while executing \"func_func()\"\n| Error in [test_main.py] in [func_func] at line (10) while executing \"raise\"\n   \u003e\u003e AttributeError: error in func_func.\n| Error in [test_main.py] in [func_func] at line (8) while executing \"func_func_func()\"\n| Error in [test_main.py] in [func_func_func] at line (3) while executing \"x = 1 / 0\"\n   \u003e\u003e ZeroDivisionError: division by zero.\n ----------------------------------------------------------------------------------------------------------  \nERROR:root:logging error\nERROR:root:logging exception\n| Error in [test_traceback_print_exception.py] in [test_tb_pr_exc] at line (13) while executing \"x = 1 / 0\"\n   \u003e\u003e ZeroDivisionError: division by zero.\n\nERROR:root:There was no exception\nNo exception is being handled.\n```\n2. **super_compact_tb**\n```python\nfrom mwk_traceback import super_compact_tb\n\nsuper_compact_tb.activate()\n\nmain()  # ! check test_main.py for code !\ntest_tb_pr_exc()  # ! check test_traceback_print_exception.py for code !\n```\nOutput:\n```commandline\n! Error:\n| [test_main::\u003cmodule\u003e]@31 \"main()\" | [test_main::main]@22 \"func()\" | [test_main::func]@17 \"raise\" | \u003e\u003e NameError: error in func.\n| [test_main::func]@15 \"func_func()\" | [test_main::func_func]@10 \"raise\" | \u003e\u003e AttributeError: error in func_func.\n| [test_main::func_func]@8 \"func_func_func()\" | [test_main::func_func_func]@3 \"x = 1 / 0\" | \u003e\u003e ZeroDivisionError: division by zero.\n ----------------------------------------------------------------------------------------------------------\nERROR:root:logging error\nERROR:root:logging exception\n| [test_traceback_print_exception::test_tb_pr_exc]@14 \"x = 1 / 0\" | \u003e\u003e ZeroDivisionError: division by zero.\n\nERROR:root:There was no exception\nNo exception is being handled\n```\n## Warnings\n### Define warning format by subclassing **CustomWarningFormatter**:\n```python\nclass MyWarningFormatter(CustomWarningFormatter):\n    _WARN_FORMAT = '[{file}]@{line} \u003e\u003e {type}: {message}\\n'\n```\n**_WARN_FORMAT** should be *python formatted string literal* with values in **{}** brackets.  \nAvailable values for **_WARN_FORMAT** *formatted string literal*:\n- **message**: warning message\n- **type**: warning class name\n- **file**: python .py file stem where warning occurred\n- **line**: number of the line of code where warning occurred\n### Usage:\n```python\nimport warnings\nwarnings.formatwarning = MyWarningFormatter\n\nwarnings.warn('This is warning', UserWarning)\n```\n### Predefined warning formatters (classes)\n1. **compact_warn**\n```python\nimport warnings\nfrom mwk_traceback import compact_warn\nwarnings.formatwarning = compact_warn\n\nwarnings.warn('This is warning', RuntimeWarning)\n```\nOutput:\n```commandline\nWarning in [test_warn.py] at line (9)\n   \u003e\u003e RuntimeWarning: This is warning\n```\n2. **super_compact_warn**\n```python\nimport warnings\nfrom mwk_traceback import super_compact_warn\nwarnings.formatwarning = super_compact_warn\n\nwarnings.warn('This is another warning', UserWarning)\n```\nOutput:\n```commandline\n[test_warn]@13 \u003e\u003e UserWarning: This is another warning\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmwksolution%2Fmwk-traceback","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmwksolution%2Fmwk-traceback","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmwksolution%2Fmwk-traceback/lists"}