@@ -38,16 +38,14 @@ def decay(
3838 decay_factor : float = 1 ,
3939 min_value : float | None = None ,
4040) -> Generator [float , Any , None ]:
41- """Generator for exponential decay[1]:
41+ """Generator for [ exponential decay](https://en.wikipedia.org/wiki/Exponential_decay).
4242
4343 Args:
4444 initial_value: initial quantity
4545 decay_factor: exponential decay constant.
4646 min_value: The minimum value to yield. Once the value in the
4747 true exponential sequence is lower than this, the value
4848 of min_value will forever after be yielded.
49-
50- [1] https://en.wikipedia.org/wiki/Exponential_decay
5149 """
5250 # Advance past initial .send() call
5351 yield 0
@@ -106,23 +104,39 @@ def runtime(*, value: Callable[[Any], float]) -> Generator[float, Any, None]:
106104 Useful for honoring a server-specified retry delay, e.g. an HTTP
107105 `Retry-After` header, rather than a fixed wait sequence:
108106
109- # with on_predicate, `value` receives the return value
110- @backoff.on_predicate(
111- backoff.runtime,
112- predicate=lambda r: r.status_code == 429,
113- value=lambda r: int(r.headers.get("Retry-After", 1)),
114- )
115- def get_page():
116- return requests.get(url)
117-
118- # with on_exception, `value` receives the raised exception
119- @backoff.on_exception(
120- backoff.runtime,
121- RetryableError,
122- value=lambda e: e.wait_seconds,
107+ ```python
108+ # with on_predicate, `value` receives the return value
109+ @backoff.on_predicate(
110+ backoff.runtime,
111+ predicate=lambda r: (
112+ r.status_code
113+ == 429
114+ ),
115+ value=lambda r: (
116+ int(
117+ r.headers.get(
118+ "Retry-After",
119+ 1,
120+ )
121+ )
122+ ),
123+ )
124+ def get_page():
125+ return requests.get(
126+ url
123127 )
124- def get_page():
125- ...
128+
129+
130+ # with on_exception, `value` receives the raised exception
131+ @backoff.on_exception(
132+ backoff.runtime,
133+ RetryableError,
134+ value=lambda e: (
135+ e.wait_seconds
136+ ),
137+ )
138+ def get_page(): ...
139+ ```
126140
127141 Args:
128142 value: a callable which takes as input the decorated
0 commit comments