Performance-oriented caching utilities with timing and monitoring capabilities.Module
This directory contains cross-cutting utilities that are used across multiple modules in the voodoo-box. These utilities provide common functionality for caching, logging, error handling, and other shared concerns.
Performance-oriented caching utilities with timing and monitoring capabilities.
Key Features:
TimedLRUCache: LRU cache with performance timingsimple_cache: Easy-to-use caching decoratorCacheStats: Cache performance analysis
Usage Example:
from voodoo_box.utils.caching import TimedLRUCache, simple_cache
@TimedLRUCache(maxsize=64)
def expensive_api_call(player_id):
# Expensive operation
return api_response
@simple_cache(maxsize=32)
def calculate_stats(data):
return processed_dataComprehensive logging setup with colored console output and JSON file logging.
Key Features:
LoggerSetup: Centralized logger configurationColoredFormatter: Console output with colorsJSONFileHandler: Structured JSON log filesPerformanceLogger: Context manager for timing operations
Usage Example:
from voodoo_box.utils.logging import setup_project_logging, PerformanceLogger
# Set up project loggers
loggers = setup_project_logging("my_project", debug=True)
logger = loggers['main']
# Use performance logging
with PerformanceLogger(loggers['performance'], "data_processing"):
# Your operation here
process_data()Robust error handling with retry logic, circuit breakers, and validation.
Key Features:
retry_with_backoff: Automatic retry with exponential backoffCircuitBreaker: Circuit breaker pattern for external servicesErrorHandler: Centralized error handling and statisticsvalidate_input: Input validation decorators
Usage Example:
from voodoo_box.utils.error_handling import (
retry_with_backoff, CircuitBreaker, RetryConfig, ErrorHandler
)
# Retry configuration
@retry_with_backoff(RetryConfig(max_attempts=5))
def api_call():
# Code that might fail
pass
# Circuit breaker for external services
@CircuitBreaker(failure_threshold=3)
def external_service_call():
# External service interaction
pass
# Error handling context
error_handler = ErrorHandler()
with ExceptionContext(error_handler, {"operation": "data_load"}):
load_data()# Example: Using multiple utilities together
from voodoo_box.utils.logging import setup_project_logging
from voodoo_box.utils.caching import TimedLRUCache
from voodoo_box.utils.error_handling import retry_with_backoff, RetryConfig
# Set up logging
loggers = setup_project_logging("my_app")
logger = loggers['main']
# Create cached function with retry logic
@TimedLRUCache(maxsize=64)
@retry_with_backoff(RetryConfig(max_attempts=3))
def robust_api_call(endpoint):
logger.info(f"Calling API endpoint: {endpoint}")
# API call logic
return responsefrom voodoo_box.utils.error_handling import ErrorHandler
from voodoo_box.utils.logging import setup_project_logging
loggers = setup_project_logging("monitoring")
error_handler = ErrorHandler(loggers['main'])
# Monitor application errors
try:
risky_operation()
except Exception as e:
error_handler.handle_error(e, context={"user_id": "123"})
# Get error statistics
stats = error_handler.get_error_stats()
loggers['main'].info(f"Error statistics: {stats}")All utilities include example usage and can be tested individually:
# Test caching utilities
python -m voodoo_box.utils.caching
# Test logging setup
python -m voodoo_box.utils.logging
# Test error handling
python -m voodoo_box.utils.error_handling- Logging Strategy: Use structured logging with appropriate levels
- Cache Management: Monitor cache hit rates and clear when needed
- Error Handling: Use circuit breakers for external services
- Performance: Combine caching with performance logging for optimization
- Context: Always provide context when handling errors
- Standard library only (no external dependencies)
- Compatible with Python 3.7+
- Works with both Windows and Unix systems
- Async versions of utilities
- Metrics collection and monitoring
- Configuration file support
- Database logging handlers