Metadata-Version: 2.1
Name: swap-exceptions
Version: 1.0.2
Summary: Python utility decorator and context manager for swapping exceptions
Home-page: https://github.com/tomgrin10/swap-exceptions
License: MIT
Keywords: swap,exceptions,switch,replace,utility
Author: Tom Gringauz
Author-email: tomgrin10@gmail.com
Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Topic :: Utilities
Classifier: Typing :: Typed
Requires-Dist: contextlib2 (>=0.6.0,<0.7.0); python_version >= "2.7" and python_version < "3.0"
Requires-Dist: six (>=1.15.0,<2.0.0)
Requires-Dist: typing (>=3.7.4,<4.0.0); python_version >= "2.7" and python_version < "3.0"
Project-URL: Repository, https://github.com/tomgrin10/swap-exceptions
Description-Content-Type: text/markdown

# swap-exceptions

[![PyPI](https://img.shields.io/pypi/v/swap-exceptions)](https://pypi.org/project/swap-exceptions/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/swap-exceptions)](https://pypi.org/project/swap-exceptions/)
[![PyPI License](https://img.shields.io/pypi/l/swap-exceptions)](https://pypi.org/project/swap-exceptions/)
[![Code Style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black/)

Python utility decorator and context manager for swapping exceptions.

### Basic Usage

As a decorator:
```python
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:
```python
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:
```python
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:
```python
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
```

