Skip to content
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

Add two new parameters for more flexability #21

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
.installed.cfg
.Python
.tox/
.pytest_cache/
build/
coverage.xml
develop-eggs/
Expand Down
6 changes: 5 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Richard O'Dwyer <[email protected]>
Rémy <[email protected]>
Rémy <[email protected]>
Rémy Greinhofer <[email protected]>
invl <[email protected]>
invlpg <[email protected]>
Richard O'Dwyer <[email protected]>
[email protected] <[email protected]>
williara <[email protected]>
TreantBG <[email protected]>
45 changes: 35 additions & 10 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
CHANGES
=======

* Add default return param
* Add configuration param
0.9.4
-----

* Bump version
0.9.3
-----

* Trim setup.py
* Move NullHandler to .compat
* Expose retry.retry\_call
* Relative import
* Move tests out of package
* Bump version
* Fix badges

0.9.2
-----

* Update ChangeLog
* Update AUTHORS
* Don't pin pytest
* tox: Add py35
* Bump version
* Updating requirements.txt to allow for any decorators >=3.4.2

0.9.1
-----

* Fix dependency issues with other packages caused by explicit dep verions in requirements
* Updates setup.cfg version info
* Updates authors and changelog
* Adds version ranges to requirements, widening compatibility with other packages and their deps

0.9.0
-----

* Fix typo in classifier
* Add AUTHORS and ChangeLog files
* Packaging the application using PBR
* Update documentation
* Add retry_call function
* Add retry\_call function
* Update tox.ini
* Update requirements
* Move the tests to the appropriate package
Expand All @@ -35,7 +60,7 @@ CHANGES
* v0.8.0
* dos2unix
* Add argument jitter for retry()
* Add argument max_delay for retry()
* Add argument max\_delay for retry()
* Refactor retry()

0.7.0
Expand All @@ -46,8 +71,8 @@ CHANGES
* retry(): Update docstring
* retry(): Change default tries to -1
* Move decorator() to .compat
* Add test_tries_minus1()
* Add test_tries_inf()
* Add test\_tries\_minus1()
* Add test\_tries\_inf()
* Mock time.sleep in test case
* Refactor retry()

Expand All @@ -57,7 +82,7 @@ CHANGES
* v0.6.0
* Fix inaccurate attempt counter
* logger is now optional
* Extract logging_logger
* Extract logging\_logger
* Make decorator module optional

0.5.0
Expand All @@ -74,22 +99,22 @@ CHANGES
* Add tox.ini
* Extract retry/api.py
* Require pytest
* Add test_retry.py
* Add test\_retry.py
* Added tag 0.4.2 for changeset 315f5f1229f6

0.4.2
-----

* Version 0.4.2
* python2.6 support
* (untested) python2.6 support
* README.rst: Add installation
* Add classifiers
* Add LICENSE
* Add requirements.txt
* Fix rST h1 h2 for README.rst
* Add url
* Add README.rst
* Ignore *.egg-info
* Ignore \*.egg-info
* Ignore .env
* Ignore .ropeproject
* Ignore .git
Expand All @@ -100,7 +125,7 @@ CHANGES

* Version 0.4.1
* Add license
* Add long_description
* Add long\_description
* Add docstring for retry()
* Added tag 0.4.0 for changeset e053cae4b105

Expand Down
33 changes: 24 additions & 9 deletions retry/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

from functools import partial

from retry.retry_config import RetryConfig
from .compat import decorator


logging_logger = logging.getLogger(__name__)
RISE_EXCEPTION = 'rise_exception'


def __retry_internal(f, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0,
logger=logging_logger):
logger=logging_logger, default_return=RISE_EXCEPTION, configuration: RetryConfig = None):
"""
Executes a function and retries it if it failed.

Expand All @@ -25,16 +26,25 @@ def __retry_internal(f, exceptions=Exception, tries=-1, delay=0, max_delay=None,
fixed if a number, random if a range tuple (min, max)
:param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
default: retry.logging_logger. if None, logging is disabled.
:param default_return: insted of rising exception return default value
:param configuration: pass configuration that can be changed dinamically
:returns: the result of the f function.
"""
_tries, _delay = tries, delay

if configuration:
_tries, _delay = configuration.tries, configuration.delay
else:
_tries, _delay = tries, delay

while _tries:
try:
return f()
except exceptions as e:
_tries -= 1
if not _tries:
raise
if default_return is RISE_EXCEPTION:
raise
return default_return

if logger is not None:
logger.warning('%s, retrying in %s seconds...', e, _delay)
Expand All @@ -51,7 +61,8 @@ def __retry_internal(f, exceptions=Exception, tries=-1, delay=0, max_delay=None,
_delay = min(_delay, max_delay)


def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, logger=logging_logger):
def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, logger=logging_logger,
default_return=RISE_EXCEPTION, configuration=None):
"""Returns a retry decorator.

:param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
Expand All @@ -63,6 +74,8 @@ def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, ji
fixed if a number, random if a range tuple (min, max)
:param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
default: retry.logging_logger. if None, logging is disabled.
:param default_return: insted of rising exception return default value
:param configuration: pass configuration that can be changed dinamically
:returns: a retry decorator.
"""

Expand All @@ -71,14 +84,13 @@ def retry_decorator(f, *fargs, **fkwargs):
args = fargs if fargs else list()
kwargs = fkwargs if fkwargs else dict()
return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter,
logger)
logger, default_return, configuration)

return retry_decorator


def retry_call(f, fargs=None, fkwargs=None, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1,
jitter=0,
logger=logging_logger):
jitter=0, logger=logging_logger, default_return=RISE_EXCEPTION, configuration=None):
"""
Calls a function and re-executes it if it failed.

Expand All @@ -94,8 +106,11 @@ def retry_call(f, fargs=None, fkwargs=None, exceptions=Exception, tries=-1, dela
fixed if a number, random if a range tuple (min, max)
:param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
default: retry.logging_logger. if None, logging is disabled.
:param default_return: insted of rising exception return default value
:param configuration: pass configuration that can be changed dinamically
:returns: the result of the f function.
"""
args = fargs if fargs else list()
kwargs = fkwargs if fkwargs else dict()
return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter, logger)
return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter, logger,
default_return, configuration)
4 changes: 4 additions & 0 deletions retry/retry_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class RetryConfig:
def __init__(self):
self.tries = -1
self.delay = 0
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = retry
version = 0.9.3
version = 0.9.4
author = invl
author-email = [email protected]
summary = Easy to use retry decorator.
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# and then run "tox" from this directory.

[tox]
envlist = flake8, py26, py27, py34, py35, pypy
envlist = flake8, py26, py27, py34, py35, py37, pypy

[flake8]
max-line-length = 120
Expand Down