Skip to content
This repository was archived by the owner on Aug 8, 2025. It is now read-only.

Commit ed932f7

Browse files
author
Bob Green
committed
Add runtime wait generator
This renames `from_value` from the original PR.
1 parent 56a4217 commit ed932f7

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

backoff/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
"""
1515
from backoff._decorator import on_predicate, on_exception
1616
from backoff._jitter import full_jitter, random_jitter
17-
from backoff._wait_gen import constant, expo, fibo, from_value
17+
from backoff._wait_gen import constant, expo, fibo, runtime
1818

1919
__all__ = [
2020
'on_predicate',
2121
'on_exception',
2222
'constant',
2323
'expo',
2424
'fibo',
25-
'from_value',
25+
'runtime',
2626
'full_jitter',
2727
'random_jitter',
2828
]

backoff/_wait_gen.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# coding:utf-8
22

33
import itertools
4-
from typing import Generator, Iterable, Optional, Union
4+
from typing import Any, Callable, Generator, Iterable, Optional, Union
55

66

77
def expo(
88
base: int = 2,
99
factor: int = 1,
1010
max_value: Optional[int] = None
11-
) -> Generator[int, None, None]:
11+
) -> Generator[int, Any, None]:
1212

1313
"""Generator for exponential decay.
1414
@@ -19,7 +19,8 @@ def expo(
1919
true exponential sequence exceeds this, the value
2020
of max_value will forever after be yielded.
2121
"""
22-
yield # Advance past initial .send() call
22+
# Advance past initial .send() call
23+
yield # type: ignore[misc]
2324
n = 0
2425
while True:
2526
a = factor * base ** n
@@ -38,7 +39,9 @@ def fibo(max_value: Optional[int] = None) -> Generator[int, None, None]:
3839
true fibonacci sequence exceeds this, the value
3940
of max_value will forever after be yielded.
4041
"""
41-
yield # Advance past initial .send() call
42+
# Advance past initial .send() call
43+
yield # type: ignore[misc]
44+
4245
a = 1
4346
b = 1
4447
while True:
@@ -57,11 +60,27 @@ def constant(
5760
Args:
5861
interval: A constant value to yield or an iterable of such values.
5962
"""
60-
yield # Advance past initial .send() call
63+
# Advance past initial .send() call
64+
yield # type: ignore[misc]
65+
6166
try:
6267
itr = iter(interval) # type: ignore
6368
except TypeError:
6469
itr = itertools.repeat(interval) # type: ignore
6570

6671
for val in itr:
6772
yield val
73+
74+
75+
def runtime(*, value: Callable[[Any], int]) -> Generator[int, None, None]:
76+
"""Generator that is based on parsing the return value or thrown
77+
exception of the decorated method
78+
79+
Args:
80+
value: a callable which takes as input the decorated
81+
function's return value or thrown exception and
82+
determines how long to wait
83+
"""
84+
ret_or_exc = yield # type: ignore[misc]
85+
while True:
86+
ret_or_exc = yield value(ret_or_exc)

tests/test_wait_gen.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def test_constant():
6161
assert 3 == next(gen)
6262

6363

64-
def test_from_value():
65-
gen = backoff.from_value(lambda x: x)
64+
def test_runtime():
65+
gen = backoff.runtime(value=lambda x: x)
6666
gen.send(None)
6767
for i in range(20):
6868
assert i == gen.send(i)

0 commit comments

Comments
 (0)