From d26f1ff083b132fde9e3572ca09e46fa4c8cb8bf Mon Sep 17 00:00:00 2001 From: nimrodmilo Date: Thu, 19 Jan 2023 22:04:39 +0200 Subject: [PATCH 1/4] added exponential decay --- backoff/__init__.py | 3 ++- backoff/_wait_gen.py | 28 ++++++++++++++++++++++++++++ tests/test_wait_gen.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/backoff/__init__.py b/backoff/__init__.py index 196274d..eb7bfa6 100644 --- a/backoff/__init__.py +++ b/backoff/__init__.py @@ -14,13 +14,14 @@ """ from backoff._decorator import on_exception, on_predicate from backoff._jitter import full_jitter, random_jitter -from backoff._wait_gen import constant, expo, fibo, runtime +from backoff._wait_gen import constant, expo, fibo, runtime, decay __all__ = [ 'on_predicate', 'on_exception', 'constant', 'expo', + 'decay', 'fibo', 'runtime', 'full_jitter', diff --git a/backoff/_wait_gen.py b/backoff/_wait_gen.py index cc9c885..9b882a9 100644 --- a/backoff/_wait_gen.py +++ b/backoff/_wait_gen.py @@ -1,6 +1,7 @@ # coding:utf-8 import itertools +import math from typing import Any, Callable, Generator, Iterable, Optional, Union @@ -31,6 +32,33 @@ def expo( yield max_value +def decay( + initial_value: float = 1, + decay_factor: float = 1, + min_value: Optional[float] = None +) -> Generator[float, Any, None]: + + """Generator for exponential decay. + + Args: + base: The mathematical base of the exponentiation operation + factor: Factor to multiply the exponentiation by. + max_value: The maximum value to yield. Once the value in the + true exponential sequence exceeds this, the value + of max_value will forever after be yielded. + """ + # Advance past initial .send() call + yield # type: ignore[misc] + t = 0 + while True: + a = initial_value * math.e ** (-t*decay_factor) + if min_value is None or a > min_value: + yield a + t += 1 + else: + yield min_value + + def fibo(max_value: Optional[int] = None) -> Generator[int, None, None]: """Generator for fibonaccial decay. diff --git a/tests/test_wait_gen.py b/tests/test_wait_gen.py index b72482d..70ba5c2 100644 --- a/tests/test_wait_gen.py +++ b/tests/test_wait_gen.py @@ -1,7 +1,35 @@ # coding:utf-8 import backoff +import math +def test_decay(): + gen = backoff.decay() + gen.send(None) + for i in range(10): + assert math.e ** -i == next(gen) + + +def test_decay_init100(): + gen = backoff.decay(initial_value=100) + gen.send(None) + for i in range(10): + assert 100 * math.e ** -i == next(gen) + + +def test_decay_init100_decay3(): + gen = backoff.decay(initial_value=100, decay_factor=3) + gen.send(None) + for i in range(10): + assert 100 * math.e ** (-i*3) == next(gen) + + +def test_decay_init100_decay3_min5(): + gen = backoff.decay(initial_value=100, decay_factor=3, min_value=5) + gen.send(None) + for i in range(10): + assert max(100 * math.e ** (-i*3), 5) == next(gen) + def test_expo(): gen = backoff.expo() gen.send(None) From 9fc1e307a1fa30f4f6dba6edf8ce44dec13269f6 Mon Sep 17 00:00:00 2001 From: nimrodmilo Date: Fri, 20 Jan 2023 13:24:10 +0200 Subject: [PATCH 2/4] updated docstring --- backoff/_wait_gen.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/backoff/_wait_gen.py b/backoff/_wait_gen.py index 9b882a9..319eaf7 100644 --- a/backoff/_wait_gen.py +++ b/backoff/_wait_gen.py @@ -41,11 +41,11 @@ def decay( """Generator for exponential decay. Args: - base: The mathematical base of the exponentiation operation - factor: Factor to multiply the exponentiation by. - max_value: The maximum value to yield. Once the value in the - true exponential sequence exceeds this, the value - of max_value will forever after be yielded. + initial_value: initial quantity + decay_factor: exponential decay constant. + min_value: The minimum value to yield. Once the value in the + true exponential sequence is lower than this, the value + of min_value will forever after be yielded. """ # Advance past initial .send() call yield # type: ignore[misc] From 0352fc0f751e9d0739785c3f48f0972300a52a35 Mon Sep 17 00:00:00 2001 From: nimrodmilo Date: Fri, 20 Jan 2023 20:20:04 +0200 Subject: [PATCH 3/4] fixed flake8 --- backoff/_wait_gen.py | 6 ++++-- tests/test_wait_gen.py | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/backoff/_wait_gen.py b/backoff/_wait_gen.py index 319eaf7..33b37b8 100644 --- a/backoff/_wait_gen.py +++ b/backoff/_wait_gen.py @@ -38,7 +38,7 @@ def decay( min_value: Optional[float] = None ) -> Generator[float, Any, None]: - """Generator for exponential decay. + """Generator for exponential decay[1]: Args: initial_value: initial quantity @@ -46,12 +46,14 @@ def decay( min_value: The minimum value to yield. Once the value in the true exponential sequence is lower than this, the value of min_value will forever after be yielded. + + [1] https://en.wikipedia.org/wiki/Exponential_decay """ # Advance past initial .send() call yield # type: ignore[misc] t = 0 while True: - a = initial_value * math.e ** (-t*decay_factor) + a = initial_value * math.e ** (-t * decay_factor) if min_value is None or a > min_value: yield a t += 1 diff --git a/tests/test_wait_gen.py b/tests/test_wait_gen.py index 70ba5c2..a658712 100644 --- a/tests/test_wait_gen.py +++ b/tests/test_wait_gen.py @@ -21,14 +21,15 @@ def test_decay_init100_decay3(): gen = backoff.decay(initial_value=100, decay_factor=3) gen.send(None) for i in range(10): - assert 100 * math.e ** (-i*3) == next(gen) + assert 100 * math.e ** (-i * 3) == next(gen) def test_decay_init100_decay3_min5(): gen = backoff.decay(initial_value=100, decay_factor=3, min_value=5) gen.send(None) for i in range(10): - assert max(100 * math.e ** (-i*3), 5) == next(gen) + assert max(100 * math.e ** (-i * 3), 5) == next(gen) + def test_expo(): gen = backoff.expo() From c46f89e516ed59c23627f110ee9a411702e17abd Mon Sep 17 00:00:00 2001 From: nimrodmilo Date: Sun, 22 Jan 2023 09:41:53 +0200 Subject: [PATCH 4/4] added CONTRIBUTING.md file --- CONTRIBUTING.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..a2aead1 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,58 @@ +# Contributing to backoff + +Thank you for considering contributing to backoff! We appreciate any help you can offer to make this project better. + +## Introduction + +backoff is an open source project that aims to provide Function decorations for backoff and retry. We welcome any contributions to the project, whether it's fixing bugs, adding new features, or improving documentation. + +Please read this guide carefully before submitting any contributions. By participating in this project, you agree to abide by our [Code of Conduct](CODE_OF_CONDUCT.md). + +## How to contribute + +### Submitting contributions + +We accept contributions in the form of pull requests on GitHub. Please make sure that you have read and understood the following guidelines before submitting a pull request: + +- backoff follows the [PEP 8 style guide](https://www.python.org/dev/peps/pep-0008/). Please make sure that your code follows these guidelines. +- Make sure that your code is well-documented and that you have added tests to cover any new functionality you've added. +- Make sure that your code passes our tests by running `make check` before submitting your pull request. +- Please create your pull request against the `master` branch. + +### Issues + +If you find a bug or have an idea for a new feature, please open an issue on GitHub. Please make sure that you have read and understood the following guidelines before opening an issue: + +- Check if the issue you're reporting has already been reported. +- Make sure that you provide as much information as possible about the problem you're reporting, including error messages and steps to reproduce the problem. +- If you're requesting a new feature, please explain why you think it would be a valuable addition to the project. + +## Getting started + +To set up the development environment for backoff, you will need to have [Python](https://www.python.org/) and [poetry](https://python-poetry.org/docs/#installation) installed on your machine. + +To install the dependencies for backoff, run the following command in the root directory of the project: +```poetry install``` + + +## Where to ask for help + +If you have any questions or need help with contributing to backoff, you can reach the maintainers of the project by emailing [email address]. + +We also encourage you to read through the [documentation](README.rst) and take a look at the [existing issues](https://github.com/litl/backoff/issues) to get a better understanding of the project and where you can help. + +Thank you for your interest in contributing to backoff! + +## The development workflow + +- Fork the repository +- Clone the repository to your local machine +- Create a new branch for your changes (optional) +- Make your changes +- Commit your changes +- Push your changes to your fork +- Submit a pull request to the `master` branch + +## Coding standards + +backoff follows the [PEP 8 style guide](https://www.python.org/dev/peps/pep-0008/). Please make sure that your code follows these guidelines.