Skip to main content

qaviton handlers

Project description

Qaviton Handlers

logo
version license open issues downloads code size

error handling utilities

Installation

pip install --upgrade qaviton_handlers  

Requirements

  • Python 3.6+

Features

  • retry decorator ✓
  • retry with context ✓
  • try functions ✓
  • catch errors ✓
  • simple Exception wrapper ✓

Usage

def retry(tries=3, delay=0, backoff=1, jitter=0, max_delay=None, exceptions=Exception, logger: Logger = log):
    """Retry function decorator \ try a context of actions until attempts run out
    :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
    :param tries: the maximum number of attempts: -1 (infinite).
    :param delay: initial delay between attempts. default: 0.
    :param max_delay: the maximum value of delay. default: None (no limit).
    :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
                    fixed if a number, random if a range tuple (min, max), functional if callable (function must receive **kwargs)
    :param jitter: extra seconds added to delay between attempts. default: 0.
                   fixed if a number, random if a range tuple (min, max), functional if callable (function must receive **kwargs)
    :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
                   default: retry.logging_logger. if None, logging is disabled."""

retry decorator

from qaviton_handlers.try_decorator import retry

@retry()
def foo():
    n = int('1'+input('select number:'))
    print(n)

foo()

retry with context

from qaviton_handlers.try_context import retry

with retry() as trying:
    while trying:
        with trying:
            print("Attempt #%d of %d" % (trying.attempt, trying.attempts))
            raise

using different try wrapper functions

from qaviton_handlers.try_functions import try_to, try_or_none, multi_try, multi_try_no_break

def foo(a=0):
    print(float(a+input("select number:")))

# simply try
try_to(foo, 1)
try_to(foo, 2)
try_to(foo, 3)
# get the error
error = try_to(foo, 4)
if error: print(error)

# if error occurred
if try_to(foo, 5):
    try_to(foo, 5)
# try with key arguments
r = try_to(lambda a,b,c:a*b*c, 1,kwargs={'b':2,'c':3})
print(r)
# try to get a number
number = try_or_none(lambda a:float(a+input("select number:")), 6)
if number: print(number)
# try many functions, return a list of results, or an error
# if an error occurred, the multi try stops
multi_try(
  lambda: foo(10), 
  lambda: foo(11), 
)

# specify errors to ignore
response = multi_try(
  lambda: foo(13),
  lambda: foo(14),
  exceptions=Exception,
)

# handle the error
response = multi_try(
  lambda: foo(13),
  lambda: foo(14),
)
if isinstance(response, Exception):
    ...
# try many functions, return a list of results, some may be errors
# if an error occurred, the multi try continues
multi_try_no_break(
    lambda: foo(8),
    lambda: foo(9),
    lambda: foo(0),
)

ignore errors now so you can handle them later

from qaviton_handlers.catch import Catch
from qaviton_handlers.utils.error import Error

catch = Catch(store=True)

# catch an error
try:
    1 + '1'
except Exception as e:
    catch(e)

# a cleaner syntax
with catch:
    1 + '1'
    2 + '2'

# ignore the error
with Catch():
    5 * 'e'

print(f"caught {catch.count} errors")
print(f"caught first {catch.first}")
print(f"caught last {catch.last}")

# make your own Catch
class MyCatch(Catch):
    def handler(self, e):
        self.stack.add(Error(e))
        if self.log:
            self.log.warning(f"I caught {e}")
        return self

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

qaviton_handlers-2019.9.29.7.56.30.312631.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qaviton_handlers-2019.9.29.7.56.30.312631-py2.py3-none-any.whl (12.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file qaviton_handlers-2019.9.29.7.56.30.312631.tar.gz.

File metadata

  • Download URL: qaviton_handlers-2019.9.29.7.56.30.312631.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7

File hashes

Hashes for qaviton_handlers-2019.9.29.7.56.30.312631.tar.gz
Algorithm Hash digest
SHA256 4153ade6d4082030b86a482bd909a4d91d8e663c14af43f3a21d20539011b05f
MD5 9972f6043f5feb95d91880c484702cd3
BLAKE2b-256 d8d7f935227de84c70ba48884bdf632346b1ddf0003e8a3e33cc902372fc5785

See more details on using hashes here.

File details

Details for the file qaviton_handlers-2019.9.29.7.56.30.312631-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for qaviton_handlers-2019.9.29.7.56.30.312631-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 80a8e5932caf57d09169c5d7bc508fd8ba5e4f1045df5d96288a35af1fd0d19f
MD5 885dee619a809556a724ec549fb2dc36
BLAKE2b-256 f3ff6aab968edcdb0cad84b784cbe30a95c1d6650aee11b8837bf4779573a4a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page