refactor: modernize code quality tooling with Ruff
- Replace black, isort, and flake8 with Ruff (all-in-one linter and formatter) - Add comprehensive pyproject.toml configuration - Simplify Makefile code quality targets - Configure exclusions for venv/.venv in pyproject.toml - Auto-fix 1,359 linting issues across codebase Benefits: - Much faster builds (Ruff is written in Rust) - Single tool replaces multiple tools - More comprehensive rule set (UP, B, C4, SIM, PIE, RET, Q) - All configuration centralized in pyproject.toml - Better import sorting and formatting consistency 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -9,8 +9,7 @@ This module provides classes and functions for:
|
||||
|
||||
import logging
|
||||
from collections import defaultdict, deque
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Dict
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -21,9 +20,9 @@ class RateLimiter:
|
||||
def __init__(self):
|
||||
"""Class constructor."""
|
||||
# Dictionary to store request timestamps for each client
|
||||
self.clients: Dict[str, deque] = defaultdict(lambda: deque())
|
||||
self.clients: dict[str, deque] = defaultdict(lambda: deque())
|
||||
self.cleanup_interval = 3600 # Clean up old entries every hour
|
||||
self.last_cleanup = datetime.now(timezone.utc)
|
||||
self.last_cleanup = datetime.now(UTC)
|
||||
|
||||
def allow_request(
|
||||
self, client_id: str, max_requests: int, window_seconds: int
|
||||
@@ -33,7 +32,7 @@ class RateLimiter:
|
||||
|
||||
Uses sliding window algorithm
|
||||
"""
|
||||
now = datetime.now(timezone.utc)
|
||||
now = datetime.now(UTC)
|
||||
window_start = now - timedelta(seconds=window_seconds)
|
||||
|
||||
# Clean up old entries periodically
|
||||
@@ -60,7 +59,7 @@ class RateLimiter:
|
||||
|
||||
def _cleanup_old_entries(self):
|
||||
"""Clean up old entries to prevent memory leaks."""
|
||||
cutoff_time = datetime.now(timezone.utc) - timedelta(hours=24)
|
||||
cutoff_time = datetime.now(UTC) - timedelta(hours=24)
|
||||
|
||||
clients_to_remove = []
|
||||
for client_id, requests in self.clients.items():
|
||||
@@ -80,11 +79,11 @@ class RateLimiter:
|
||||
f"Rate limiter cleanup completed. Removed {len(clients_to_remove)} inactive clients"
|
||||
)
|
||||
|
||||
def get_client_stats(self, client_id: str) -> Dict[str, int]:
|
||||
def get_client_stats(self, client_id: str) -> dict[str, int]:
|
||||
"""Get statistics for a specific client."""
|
||||
client_requests = self.clients.get(client_id, deque())
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
now = datetime.now(UTC)
|
||||
hour_ago = now - timedelta(hours=1)
|
||||
day_ago = now - timedelta(days=1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user