Skip to content

Release 0.0.2 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
python-version: [ '3.6', '3.7', '3.8', '3.9', '3.10']
python-version: [ '3.6', '3.7', '3.8', '3.9', '3.10', '3.11']

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Python Retry
:target: https://badge.fury.io/py/python-retry/
:alt: Badge PyPi

.. image:: https://coveralls.io/repos/github/pyprogrammerblog/python-retry/badge.svg?branch=master
:target: https://coveralls.io/github/pyprogrammerblog/python-retry?branch=master
:alt: Coverage


Features
----------
Expand Down
14 changes: 9 additions & 5 deletions python_retry/retry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from python_retry.retry_patterns import InnmediateRetryPattern
from python_retry.retry_patterns import RetryPatterns

import functools
import logging
import time
Expand All @@ -8,7 +11,7 @@

def retry(
max_retries: int = 3,
backoff_factor: int = 1,
pattern: RetryPatterns = InnmediateRetryPattern(),
retry_on: (Exception,) = None,
supress_exception: bool = False,
retry_logger: logging.Logger = None,
Expand All @@ -17,7 +20,7 @@ def retry(
Retry decorator

:param max_retries: int. Defaults to 3.
:param backoff_factor: int. Defaults to 1.
:param pattern: RetryPatterns. Defaults to InnmediateRetryPattern.
:param retry_on: tuple. A tuple of exceptions.
When no argument is passed all exceptions are catched.
Defaults to None.
Expand All @@ -42,9 +45,8 @@ def retry(
>>> div(1, 0)
>>>
>>> @retry(
... retry_on=(ZeroDivisionError,),
... retry_on=(ZeroDivisionError,),
... max_retries=2,
... backoff_factor=1,
... supress_exception=True,
... retry_logger=LOGGER
... )
Expand All @@ -53,6 +55,7 @@ def retry(
>>>
>>> div(1, 0)
"""
retry_mechanism = pattern.retry_mechanism()

def decorator(func):
@functools.wraps(func)
Expand All @@ -69,8 +72,9 @@ def wrapper(*args, **kwargs):
if supress_exception:
return
raise e

# time sleep
seconds = backoff_factor * (2 ** (n - 1))
seconds = retry_mechanism(n)
if retry_logger is not None:
log_str = "%s, retrying in %s seconds..."
retry_logger.warning(log_str, e, seconds)
Expand Down
57 changes: 57 additions & 0 deletions python_retry/retry_patterns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from typing import Union

import abc


class BasePattern:
def __int__(self, max_retries: int = 3):
self.max_retries: int = max_retries
self.current_retry: int = 0

@abc.abstractmethod
def next_retry(self):
pass


class InnmediateRetryPattern(BasePattern):
def __int__(self, max_retries: int = 3):
self.max_retries: int = max_retries
self.current_retry: int = 0

def next_retry(self):
pass


class IncrementalIntervalsPattern(BasePattern):
""""""

def __int__(self, max_retries: int = 3, interval: int = 3):
self.max_retries: int = max_retries
self._n_retries: int = 0
self.interval = interval

def next_retry(self):
return self.interval * self._n_retries


class ExponentialBackoffPattern(BasePattern):
""""""

def __int__(self, backoff_factor: int = 2):
self.backoff_factor = backoff_factor
self._n_retries = 0

def next_retry(self):
return self.backoff_factor * (2 ** (self._n_retries - 1))


class JitterPattern(BasePattern):
pass


RetryPatterns = Union[
JitterPattern,
ExponentialBackoffPattern,
InnmediateRetryPattern,
IncrementalIntervalsPattern,
]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os


version = "0.0.1"
version = "0.0.2"

long_description = "\n\n".join([open("README.rst").read(), open("CHANGES.rst").read()])

Expand Down