Skip to main content

extra-context

Sometimes, when you get warnings and exeptions, backtraces are just not enough. For example, you have a loop with 10.000 iterations. In the 1000th iteration there is an exception in a function you called. So you would like to have more context for the error message.

There are 2 options

  • give more context at the site the exception occured

    • Only possible if you control the exception/warning generation
    • Sometimes, there is not enough context to begin with (i.e. you are calling some common utility function)
  • Provide additional context as the exception/warning is passed up the call stack

    • Can be "opt-in" in user-code, no changes in libraries required.
    • Also works for warnings (which normally don't provide backtrace information)

Examples

The main example that inspired writing this library:

to_process = { "file1": {...}, "file2": {...}, ...}

for k, v in to_process.items():
    cleaned = clean_data(v)
    write_output(k, cleaned)

If there is an exception or warning somewhere in clean_data(v), the "context" i.e. from which file the data came is lost. The backtrace could look something like:

  File "clean.py", line 35, in clean_data
ZeroDivisionError: division by zero

Now, with this library you can give extra context to your error messages

from extra_context import provide_extra_context

to_process = { "file1": {...}, "file2": {...}, ...}

for k, v in to_process.items():
    with provide_extra_context(filename=k):
        cleaned = clean_data(v)
        write_output(k, cleaned)

This would report the same error as:

  File "clean.py", line 35, in clean_data
ZeroDivisionError: division by zero
  |- In context: filename='file4'

Decorator

There is also a decorator that reports the function arguments in case of an error

import warnings
from extra_context import provide_call_context

@provide_call_context
def w(x):
    warnings.warn("Oops")

w(42)

This reports:

UserWarning: Oops
  |- In context: w(42)
  warnings.warn("Oops")

License

Copyright 2018 Moritz Wanzenböck

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Metadata

Release files for extra-context 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for extra-context 1.0.0
File Size Uploaded
extra-context-1.0.0.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for extra-context 1.0.0
File Interpreter ABI Platform
extra_context-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.0 kB

Release files / extra-context-1.0.0.tar.gz

Download URL extra-context-1.0.0.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
58efaf35bc1179593f5d2e03fe31e8d1646284417963bf30c9023298b8932314
BLAKE2b-256 checksum
How to use checksums
c6eaf41e829743b564715e891c2a9749cc628eb16d952af0a197947e83fac2e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

Release files / extra_context-1.0.0-py3-none-any.whl

Download URL extra_context-1.0.0-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8ee44bc2b6bfaf35815f24319b4cb40ac4cf004c0e6cb861cfe0c1d52dec21f6
BLAKE2b-256 checksum
How to use checksums
b0d89f3d7e5ce680769faadb88349f3a39d22c374ebae5a0b167802773c20556
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page