A library for buffering function calls
Project description
pyfuncbuffer
A simple to use decorator to buffer function calls. Supports python versions 3.6 and up. Works for both regular and async functions.
Install
$ pip install pyfuncbuffer
Example usage
Let's say you have a scraper, and don't want sites to timeout you.
You can use the @buffer()
wrapper to make your function calls buffered!
from pyfuncbuffer import buffer
# We specify scrape_links to always buffer at least 0.5 seconds
# and by a random delay of 0 to 0.5
@buffer(seconds=0.5, random_delay=0.5)
def scrape_links(url) -> []: ...
links = scrape_links("https://example.org")
while True:
link = links.pop(0)
links.append(scrape_links(link))
The @buffer()
wrapper works both for regular functions, and instance methods!
Parameters
seconds
: Required
Seconds to buffer. Can be an int or a float.
random_delay
: Optional
Seconds to define a random delay between 0 and random_delay.
Can be an int or a float. Alternativelly if a tuple is passed,
delay is chosen between random_delay[0]
and random_delay[1]
.
always_buffer
: Optional
Whether or not to always buffer. If specified, buffer_on_same_arguments
is ignored.
buffer_on_same_arguments
: Optional
Only buffer if the arguments on the buffered function are the same. False by default.
Testing
Testing is done using pytest and pytest-asyncio.
Run
$ python -m pytest tests/test_pyfuncbuffer.py
in the project root to run all the tests.
Project details
Release history Release notifications | RSS feed
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
Hashes for pyfuncbuffer-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e41f38b09a0a41d11d6949dfe0b4eb06ee5c2ab22f31027ad79a46a2ca8d6cf8 |
|
MD5 | 62838ced2b49bef6fd17dd56f103da78 |
|
BLAKE2b-256 | ef922978a26f14a43939a74b92be24fadc5d87199d9bc141c26f44fc1510be66 |