cs.ratelimit
This provides a threadsafe configurable rate limiter to a Python callable.
It's easy to create a rate limited function
>>> from datetime import datetime, timedelta
>>> from cs import ratelimit
>>> @ratelimit.ratelimited(max_count=1, interval=timedelta(seconds=1), block=False)
... def my_func():
... pass
>>> my_func()
>>> try:
... my_func()
... except ratelimit.RateLimitExceeded:
... print(u"Too fast!")
Too fast!
We can just as easily make it a blocking rate limiter
>>> @ratelimit.ratelimited(max_count=1, interval=timedelta(seconds=1), block=True)
... def my_func():
... pass
>>> my_func()
>>> my_func() # blocks, doesn't raise
It's also easy to create a class-level rate limited method
>>> class MyClass1:
... @ratelimit.ratelimitedmethod(max_count=1, interval=timedelta(seconds=1), block=False)
... def my_method(self):
... pass
>>> instance1 = MyClass1()
>>> instance2 = MyClass1()
>>> instance1.my_method()
>>> try:
... instance2.my_method()
... except ratelimit.RateLimitExceeded:
... print(u"Too fast!")
Too fast!
A more advanced use case is per-instance limiters
>>> from operator import attrgetter
>>> class MyClass2:
... def __init__(self):
... self.rl = ratelimit.RateLimitProperties(max_count=1,
... interval=timedelta(seconds=1),
... block=False)
... @ratelimit.ratelimitedmethod(attrgetter('rl'))
... def my_method(self):
... pass
>>> instance1 = MyClass2()
>>> instance2 = MyClass2()
The rate limiters in these instances are independent
>>> instance1.my_method()
>>> instance2.my_method()
>>> try:
... instance1.my_method()
... except ratelimit.RateLimitExceeded:
... print(u"Too fast!")
Too fast!
They can also be updated at any time
>>> with instance2.rl.rlock: #needed in threaded environments
... instance2.rl.max_count = 2
>>> instance2.my_method()
WE STOP BREACHES
Metadata
Release files for cs.ratelimit 1.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| cs.ratelimit-1.3.1.tar.gz | 17.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cs.ratelimit-1.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.7 kB
Release files / cs.ratelimit-1.3.1.tar.gz
| Download URL | cs.ratelimit-1.3.1.tar.gz |
|---|---|
| Size | 17.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4403909c5d0ae4ef458f2865f5699f89b05d6a6e253008356f6975e0149a69f0
|
|
BLAKE2b-256 checksum How to use checksums |
1b4b855dcecdbc10fce1a9e8c4f9f393f8265167c719aad70a647712581f47c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / cs.ratelimit-1.3.1-py3-none-any.whl
| Download URL | cs.ratelimit-1.3.1-py3-none-any.whl |
|---|---|
| Size | 12.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6f021744ee9bcc8a57f61b77808d0752d1f1622f657fc0adab422d64625a5ae4
|
|
BLAKE2b-256 checksum How to use checksums |
16954ed2bdec7ff63af529d61f1d6b1126f0fa3bb46730bc1569314734428d05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Security for cs.ratelimit 1.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.