Skip to main content

swap-exceptions

PyPI PyPI - Python Version PyPI License Code Style: black

Python utility decorator and context manager for swapping exceptions.

Basic Usage

As a decorator:

from swap_exceptions import swap_exceptions

@swap_exceptions({KeyError: ValueError("Incorrect value")})
def get_value(key: str):
    d = {'a': 1, 'b': 2}
    return d[key]

get_value('c')  # ValueError: Incorrect value

Or as a context manager:

from swap_exceptions import swap_exceptions

def get_value(key: str):
    d = {'a': 1, 'b': 2}
    with swap_exceptions({KeyError: ValueError("Incorrect value")}):
        return d[key]

get_value('c')  # ValueError: Incorrect value

Advanced Usage

Mapping key can also be a tuple:

from swap_exceptions import swap_exceptions

@swap_exceptions({(KeyError, TypeError): ValueError("Incorrect value")})
def get_value(key: str):
    d = {'a': 1, 'b': 2, 'c': 'not a number'}
    return d[key] + 10

get_value('c')  # ValueError: Incorrect value

Mapping value can also be a factory that generates the exception:

from swap_exceptions import swap_exceptions

@swap_exceptions({KeyError: lambda e: ValueError(f"Incorrect value {e.args[0]}")})
def get_value(key: str):
    d = {'a': 1, 'b': 2}
    return d[key]

get_value('c')  # ValueError: Incorrect value c

Metadata

Release files for swap-exceptions 1.0.2

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

Source distribution (sdist)

Source distribution for swap-exceptions 1.0.2
File Size Uploaded
swap-exceptions-1.0.2.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for swap-exceptions 1.0.2
File Interpreter ABI Platform
swap_exceptions-1.0.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 6.9 kB

Release files / swap-exceptions-1.0.2.tar.gz

Download URL swap-exceptions-1.0.2.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f0b805900ea6ab06c556122f5a6e6ceddcba8b3775d0c421f4c5b71f7623c1e1
BLAKE2b-256 checksum
How to use checksums
873b06b86077ec743473a6a2048a9fb68db29ee651631c4aaceaecf6fcb8cff4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.5 Linux/5.3.0-1034-azure

Release files / swap_exceptions-1.0.2-py2.py3-none-any.whl

Download URL swap_exceptions-1.0.2-py2.py3-none-any.whl
Size 3.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
36e708836de4bb5e4c211a4cf502aa6c91f7d0e0e3b0f0fcdbcd08d11aba409b
BLAKE2b-256 checksum
How to use checksums
7d3255eb9e4df53bcf24c933a81a616f328c54258b816a389a80857d761cc3e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.5 Linux/5.3.0-1034-azure

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

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