Skip to main content

Python library for easy message broker handling using Pydantic

Project description

pikantic

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

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

pikantic-0.1.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

pikantic-0.1.1-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file pikantic-0.1.1.tar.gz.

File metadata

  • Download URL: pikantic-0.1.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.5 Windows/10

File hashes

Hashes for pikantic-0.1.1.tar.gz
Algorithm Hash digest
SHA256 007d73398399bddf67ca141f74a1f1a4edce54a2a8eaefaf35f60dc3605434ca
MD5 9f33a11b11e6ec0a46acbfc2144e47eb
BLAKE2b-256 851e4baf8847ec8607d0fd071a0b23337724e6a6d9353c55d1db975f8abc4189

See more details on using hashes here.

File details

Details for the file pikantic-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pikantic-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.5 Windows/10

File hashes

Hashes for pikantic-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e811248441082eb10e31d4c01c52ece9267cad03dbb8a71d4236aedacc426e7
MD5 25d3d6e6c5ab1694ea8716f306ac96b7
BLAKE2b-256 d155ec152e8898bc51c98bd42f0445a76a3481aa038715ff007d3c2119c3c648

See more details on using hashes here.

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