GithubHelp home page GithubHelp logo

byteskeptical / retry Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 50 KB

Retry decorator, for the stubborn programmer in all of us

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%
decorator exceptions exception-handling retry retryer error-handling logging

retry's Introduction

Retry

Exception type based retry decorator for all your problematic functions.

Inspired by: https://wiki.python.org/moin/PythonDecoratorLibrary#Retry

# Parmeters

Exceptions:
    description: Exception(s) to check. May be a tuple of exceptions to check.
    example: IOError or IOError(errno.ECOMM) or (IOError,) or (ValueError, IOError(errno.ECOMM)
    type: Exception type, exception instance, or tuple containing any number of both

Tries:
    description: Number of times to try (not retry) before giving up.
    example: 3
    type: integer

Delay:
    description: Initial delay between retries in seconds.
    example: 3
    type: integer

Backoff:
    description: Backoff multiplier
    example: Value of 2 will double the delay each retry
    type: integer

Silent:
    description: If set then no logging will be attempted.
    example: True
    type: Boolean

Logger:
    description: Logger to use. If None, print.
    example: log = getLogger(__name__)
    type: logging.Logger

Usage

from logging import basicConfig, getLogger, INFO
from retry import retry

basicConfig(level=INFO)
log = getLogger(__name__)

@retry((TestError, IOError), tries=6, delay=1, backoff=2, silent=False, logger=log)
def yourfunction(self, example):
    try:
        ...
    except:
        raise TestError
    finally:
        log.INFO('No Errors!')

Examples

Always Fail

from retry import retry

@retry(Exception, tries=4)
def test_fail(text):
    raise Exception('Fail')

test_fail('It Works!')

Success

from retry import retry

@retry(Exception, tries=4)
def test_success(text):
    print('Success: {0}'.format(text))

test_success('It Works!')

Random Fail

from retry import retry
from random import random

@retry(Exception, tries=4)
def test_random(text):
    x = random()
    if x < 0.5:
        raise Exception('Fail')
    else:
        print('Success: {0}'.format(text))

test_random('It Works!')

Handling Multiple Exceptions

from retry import retry
from random import random

@retry((NameError, IOError), tries=20, delay=1, backoff=1)
def test_multiple_exceptions():
    x = random()
    if x < 0.40:
        raise NameError('NameError')
    elif x < 0.80:
        raise IOError('IOError')
    else:
        raise KeyError('KeyError')

test_multiple_exceptions()

retry's People

Contributors

byteskeptical avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.