Add your description here
Project description
Rate Keeper: Used to limit function call frequency. It ensures your function is called evenly within the limit rather than being called intensively in a short time. Moreover, it can dynamically adjust the call frequency based on remaining calls and time.
Installation
pip install rate-keeper
Quick Start
from rate_keeper import RateKeeper
if __name__ == "__main__":
rate_keeper = RateKeeper(limit=3, period=1)
@rate_keeper.decorator
def request(url: str) -> str:
print(f"Requesting {url}, {rate_keeper}, {rate_keeper.recommend_delay:.2f}")
count = 0
while count < 6:
request(f"https://www.example.com/{count}")
count += 1
# Output:
# Requesting https://www.example.com/0, RateKeeper(limit=3, period=1, used=1, reset=55981.89), 0.50
# Requesting https://www.example.com/1, RateKeeper(limit=3, period=1, used=2, reset=55981.89), 0.50
# Requesting https://www.example.com/2, RateKeeper(limit=3, period=1, used=1, reset=55982.89), 0.50
# Requesting https://www.example.com/3, RateKeeper(limit=3, period=1, used=2, reset=55982.89), 0.50
# Requesting https://www.example.com/4, RateKeeper(limit=3, period=1, used=1, reset=55983.906), 0.50
# Requesting https://www.example.com/5, RateKeeper(limit=3, period=1, used=2, reset=55983.906), 0.50
Dynamic Adjust
from typing import Dict
import requests
from requests import Response
from datetime import datetime, timezone
from rate_keeper import RateKeeper
timestamp_clock = datetime.now(timezone.utc).timestamp
rate_keeper = RateKeeper(limit=5000, period=3600, clock=timestamp_clock)
@rate_keeper.decorator
def fetch(
method: str, url: str, headers: Dict[str, str] = {}, params: Dict[str, str] = {}
) -> Response:
# https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api#checking-the-status-of-your-rate-limit
response = requests.request(method, url, headers=headers, params=params)
headers_map = {
"x-ratelimit-limit": rate_keeper.update_limit,
"x-ratelimit-used": rate_keeper.update_used,
"x-ratelimit-reset": rate_keeper.update_reset,
}
for key, value in response.headers.items():
lower_key = key.lower()
if lower_key in headers_map:
headers_map[lower_key](int(value))
return response
def create_headers(token: str) -> Dict[str, str]:
return {
"Accept": "application/vnd.github.v3+json",
"User-Agent": "Follower Bot",
"Authorization": f"token {token}",
}
print(rate_keeper)
response = fetch("GET", "https://api.github.com/user", create_headers("github_token"))
print(response.json())
print(rate_keeper)
# Output:
# RateKeeper(limit=5000, period=3600, used=0, reset=1745863571.523727)
# {'message': 'Bad credentials', 'documentation_url': 'https://docs.github.com/rest', 'status': '401'}
# RateKeeper(limit=60, period=3600, used=7, reset=1745862671)
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
rate_keeper-0.1.0.tar.gz
(4.3 kB
view details)
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 rate_keeper-0.1.0.tar.gz.
File metadata
- Download URL: rate_keeper-0.1.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d119c63062336405e207b7303a510b75eabb58640d257b555cb803d0875bbd5
|
|
| MD5 |
01c5241e2fc913786ad90a20ccdd6264
|
|
| BLAKE2b-256 |
4778e7bbd51ca4f1313b5ac869650f7dd27187091f020eebeb9b45a9adb8947a
|
File details
Details for the file rate_keeper-0.1.0-py3-none-any.whl.
File metadata
- Download URL: rate_keeper-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48d02d9a2195ade7f9989f07cc5fed95a1f0327f9e93d82a40c25b64f8b9a6b1
|
|
| MD5 |
d1d572cac016f30dba5db00bf87449be
|
|
| BLAKE2b-256 |
f568c63ad6b2f5e7ac1a21a4f43a0186954baa88f5ddf98d9eaa0e0b0b9cee9f
|