Skip to content

Commit

Permalink
Add runtime wait generator
Browse files Browse the repository at this point in the history
This renames `from_value` from the original PR.
  • Loading branch information
Bob Green committed Aug 19, 2021
1 parent 56a4217 commit ed932f7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions backoff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
"""
from backoff._decorator import on_predicate, on_exception
from backoff._jitter import full_jitter, random_jitter
from backoff._wait_gen import constant, expo, fibo, from_value
from backoff._wait_gen import constant, expo, fibo, runtime

__all__ = [
'on_predicate',
'on_exception',
'constant',
'expo',
'fibo',
'from_value',
'runtime',
'full_jitter',
'random_jitter',
]
Expand Down
29 changes: 24 additions & 5 deletions backoff/_wait_gen.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# coding:utf-8

import itertools
from typing import Generator, Iterable, Optional, Union
from typing import Any, Callable, Generator, Iterable, Optional, Union


def expo(
base: int = 2,
factor: int = 1,
max_value: Optional[int] = None
) -> Generator[int, None, None]:
) -> Generator[int, Any, None]:

"""Generator for exponential decay.
Expand All @@ -19,7 +19,8 @@ def expo(
true exponential sequence exceeds this, the value
of max_value will forever after be yielded.
"""
yield # Advance past initial .send() call
# Advance past initial .send() call
yield # type: ignore[misc]
n = 0
while True:
a = factor * base ** n
Expand All @@ -38,7 +39,9 @@ def fibo(max_value: Optional[int] = None) -> Generator[int, None, None]:
true fibonacci sequence exceeds this, the value
of max_value will forever after be yielded.
"""
yield # Advance past initial .send() call
# Advance past initial .send() call
yield # type: ignore[misc]

a = 1
b = 1
while True:
Expand All @@ -57,11 +60,27 @@ def constant(
Args:
interval: A constant value to yield or an iterable of such values.
"""
yield # Advance past initial .send() call
# Advance past initial .send() call
yield # type: ignore[misc]

try:
itr = iter(interval) # type: ignore
except TypeError:
itr = itertools.repeat(interval) # type: ignore

for val in itr:
yield val


def runtime(*, value: Callable[[Any], int]) -> Generator[int, None, None]:
"""Generator that is based on parsing the return value or thrown
exception of the decorated method
Args:
value: a callable which takes as input the decorated
function's return value or thrown exception and
determines how long to wait
"""
ret_or_exc = yield # type: ignore[misc]
while True:
ret_or_exc = yield value(ret_or_exc)
4 changes: 2 additions & 2 deletions tests/test_wait_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def test_constant():
assert 3 == next(gen)


def test_from_value():
gen = backoff.from_value(lambda x: x)
def test_runtime():
gen = backoff.runtime(value=lambda x: x)
gen.send(None)
for i in range(20):
assert i == gen.send(i)

0 comments on commit ed932f7

Please sign in to comment.