Debounce and throttle decorators for Python functions.
Project description
philiprehberger-debounce
Debounce and throttle decorators for Python functions.
Installation
pip install philiprehberger-debounce
Usage
from philiprehberger_debounce import debounce, throttle
Debounce
Delay execution until a quiet period has passed. Each new call resets the timer.
@debounce(0.5)
def on_resize(width, height):
print(f"Resized to {width}x{height}")
on_resize(800, 600)
on_resize(1024, 768) # cancels the previous call
# Only the last call executes after 0.5s
Throttle
Limit a function to a fixed number of calls within a time window. Excess calls are silently dropped.
@throttle(calls=3, per=1.0)
def send_request(data):
print(f"Sending {data}")
for i in range(10):
send_request(i) # only the first 3 calls within 1s execute
API
| Decorator | Description |
|---|---|
debounce(seconds) |
Delays execution until seconds have elapsed since the last call. Cancels any pending invocation on each new call. |
throttle(calls, per) |
Limits the function to calls invocations per per seconds. Uses a sliding window. |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_debounce-0.1.2.tar.gz.
File metadata
- Download URL: philiprehberger_debounce-0.1.2.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
413193397375400e8e88ca74042765ccd8c8a9239fb8779ad741ca00e128dc93
|
|
| MD5 |
558b52358dd230336b4e644eccee1dd3
|
|
| BLAKE2b-256 |
8a9a2a1bd3d017c330b860474c8123acef447995938a39adb63dbccee8cd6517
|
File details
Details for the file philiprehberger_debounce-0.1.2-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_debounce-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22df81b19d6c9fca32d1419f011134bb7216ebca3969f9a70b383f87c35b1880
|
|
| MD5 |
d6b62a3cdfd8407311bf2010e7cb834e
|
|
| BLAKE2b-256 |
20b1eb424808152f7e167e7733e3553a6c0056aec9d2257f237c20a07c24b924
|