Python
Modern Python patterns including async, decorators, testing fixtures, and structured logging.
20 snippets
Showing 20 of 20 snippets
Async HTTP Client with httpx
Production-ready async HTTP client using httpx with retries, timeouts, and connection pooling.
Retry Decorator with Exponential Backoff
Generic retry decorator with configurable attempts, exponential backoff, and exception filtering.
Dataclass with Validation
Python dataclass with __post_init__ field validation, type coercion, and descriptive error messages.
Custom Context Manager
Context managers for resource management using both class-based and decorator approaches with error handling.
Generator Pipeline for Data Processing
Chain generators to build memory-efficient data processing pipelines for large files and streams.
LRU Cache with TTL Support
Extend functools.lru_cache with time-based expiration for caching expensive function calls with staleness control.
CLI Tool with argparse
Build a professional CLI tool with subcommands, typed arguments, environment variable fallbacks, and help text.
Type-Safe Settings with Pydantic
Load and validate environment variables into a typed settings object using pydantic-settings with defaults.
Pytest Fixtures and Parametrize
Reusable pytest fixtures with scope control, parametrize for data-driven tests, and temporary resources.
Structured Logging with structlog
Configure structlog for JSON-formatted structured logging with request context, timestamps, and log levels.
FastAPI Dependency Injection
Use FastAPI's dependency injection for database sessions, auth checks, and shared service logic.
Pydantic v2 Model Patterns
Define and validate data models with Pydantic v2 using field validators, computed fields, and serialization.
asyncio.gather Concurrent Tasks
Run multiple async operations concurrently with asyncio.gather and proper error handling.
Tenacity Retry with Backoff
Add robust retry logic with exponential backoff, jitter, and conditional retry using the tenacity library.
Rich Progress Bar for CLI
Display beautiful progress bars and status spinners in CLI applications using the Rich library.
Pathlib File Operations
Modern file system operations using pathlib for reading, writing, globbing, and path manipulation.
Dataclasses with Post-Init Processing
Use Python dataclasses with __post_init__ for computed fields, validation, and default factories.
functools.cache and lru_cache
Memoize expensive function calls with functools.cache and lru_cache for automatic result caching.
Click CLI Command Group
Build professional CLI tools with Click using command groups, options, arguments, and help text.
HTTPX Async Client with Retry
Make async HTTP requests with HTTPX featuring timeout config, retry logic, and response streaming.