Skip to content

Commit

Permalink
Implement timedelta total_seconds() for python 2.6
Browse files Browse the repository at this point in the history
Python 2.6's timedelta doesn't have total_seconds() so we implement
our own here.

It keeps being just easy enough to keep python 2.6 support...
  • Loading branch information
Bob Green committed Mar 30, 2018
1 parent 3b82e87 commit 9903416
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions backoff/_async.py
Expand Up @@ -6,7 +6,7 @@

from backoff._common import (_handlers, _init_wait_gen,
_log_backoff, _log_giveup, _maybe_call,
_next_wait)
_next_wait, _total_seconds)


def _ensure_coroutine(coro_or_func):
Expand Down Expand Up @@ -61,7 +61,7 @@ def retry(*args, **kwargs):
wait = _init_wait_gen(wait_gen, wait_gen_kwargs)
while True:
tries += 1
elapsed = (datetime.datetime.now() - start).total_seconds()
elapsed = _total_seconds(datetime.datetime.now() - start)
details = (target, args, kwargs, tries, elapsed)

ret = yield from target(*args, **kwargs)
Expand Down Expand Up @@ -125,7 +125,7 @@ def retry(*args, **kwargs):
wait = _init_wait_gen(wait_gen, wait_gen_kwargs)
while True:
tries += 1
elapsed = (datetime.datetime.now() - start).total_seconds()
elapsed = _total_seconds(datetime.datetime.now() - start)
details = (target, args, kwargs, tries, elapsed)

try:
Expand Down
8 changes: 8 additions & 0 deletions backoff/_common.py
Expand Up @@ -91,3 +91,11 @@ def _log_giveup(details):
msg = "{0} ({1})".format(msg, details['value'])

logger.error(msg)


# Python 2.6 datetime.timedelta does not have total_seconds()
# so we do our own implementation here.
def _total_seconds(timedelta):
return (
(timedelta.microseconds + 0.0 +
(timedelta.seconds + timedelta.days * 24 * 3600) * 10**6) / 10**6)
7 changes: 4 additions & 3 deletions backoff/_sync.py
Expand Up @@ -4,7 +4,8 @@
import time

from backoff._common import (_handlers, _init_wait_gen, _log_backoff,
_log_giveup, _maybe_call, _next_wait)
_log_giveup, _maybe_call, _next_wait,
_total_seconds)


def _call_handlers(hdlrs, target, args, kwargs, tries, elapsed, **extra):
Expand Down Expand Up @@ -41,7 +42,7 @@ def retry(*args, **kwargs):
wait = _init_wait_gen(wait_gen, wait_gen_kwargs)
while True:
tries += 1
elapsed = (datetime.datetime.now() - start).total_seconds()
elapsed = _total_seconds(datetime.datetime.now() - start)
details = (target, args, kwargs, tries, elapsed)

ret = target(*args, **kwargs)
Expand Down Expand Up @@ -91,7 +92,7 @@ def retry(*args, **kwargs):
wait = _init_wait_gen(wait_gen, wait_gen_kwargs)
while True:
tries += 1
elapsed = (datetime.datetime.now() - start).total_seconds()
elapsed = _total_seconds(datetime.datetime.now() - start)
details = (target, args, kwargs, tries, elapsed)

try:
Expand Down

0 comments on commit 9903416

Please sign in to comment.