Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Q&A How to add a percent_jitter strategy to backoff Python package and pass extra args in the decorator?

One option is to write a (parameterised) decorator that transforms your jitter function with a given percentage value: from typing import Callable def percent_jitter_dec(percentage: float = 0.2...

posted 11mo ago by mr Tsjolder‭

Answer
#1: Initial revision by user avatar mr Tsjolder‭ · 2025-11-05T21:41:27Z (11 months ago)
One option is to write a (parameterised) [decorator](https://docs.python.org/3/glossary.html#term-decorator) that transforms your jitter function with a given `percentage` value:

```python
from typing import Callable

def percent_jitter_dec(percentage: float = 0.2) -> Callable[[float], float]:
    def _jitter(value: float) -> float:
        max_jitter = value * percentage
        return value + random.uniform(-max_jitter, max_jitter)

    return _jitter
```

Now, you should be able to use `jitter=percent_jitter_dec(percentage=0.5)`.

In this simple case, you actually do not need to write the wrapper yourself. The `functools` package (normally included with any standard Python installation) provides the [`partial`](https://docs.python.org/3/library/functools.html#functools.partial) function that implements exactly this kind of transformations. To use it, you would do something as follows:

```python
from functools import partial

@backoff.on_exception(jitter=partial(percent_jitter, percentage=0.5))
def func(): ...
```
Note that we are using the plain `percent_jitter` function you provided in your question, here.