🐍

Python

Modern Python patterns including async, decorators, testing fixtures, and structured logging.

20 snippets

Showing 20 of 20 snippets

pythonintermediate

Async HTTP Client with httpx

Production-ready async HTTP client using httpx with retries, timeouts, and connection pooling.

#httpx#async
pythonintermediate

Retry Decorator with Exponential Backoff

Generic retry decorator with configurable attempts, exponential backoff, and exception filtering.

#decorator#retry
pythonbeginner

Dataclass with Validation

Python dataclass with __post_init__ field validation, type coercion, and descriptive error messages.

#dataclass#validation
pythonintermediate

Custom Context Manager

Context managers for resource management using both class-based and decorator approaches with error handling.

#context-manager#resource-management
pythonintermediate

Generator Pipeline for Data Processing

Chain generators to build memory-efficient data processing pipelines for large files and streams.

#generator#pipeline
pythonintermediate

LRU Cache with TTL Support

Extend functools.lru_cache with time-based expiration for caching expensive function calls with staleness control.

#cache#lru
pythonbeginner

CLI Tool with argparse

Build a professional CLI tool with subcommands, typed arguments, environment variable fallbacks, and help text.

#cli#argparse
pythonbeginner

Type-Safe Settings with Pydantic

Load and validate environment variables into a typed settings object using pydantic-settings with defaults.

#pydantic#settings
pythonintermediate

Pytest Fixtures and Parametrize

Reusable pytest fixtures with scope control, parametrize for data-driven tests, and temporary resources.

#pytest#testing
pythonintermediate

Structured Logging with structlog

Configure structlog for JSON-formatted structured logging with request context, timestamps, and log levels.

#logging#structlog
pythonintermediate

FastAPI Dependency Injection

Use FastAPI's dependency injection for database sessions, auth checks, and shared service logic.

#fastapi#dependency-injection
pythonbeginner

Pydantic v2 Model Patterns

Define and validate data models with Pydantic v2 using field validators, computed fields, and serialization.

#pydantic#validation
pythonintermediate

asyncio.gather Concurrent Tasks

Run multiple async operations concurrently with asyncio.gather and proper error handling.

#asyncio#concurrency
pythonbeginner

Tenacity Retry with Backoff

Add robust retry logic with exponential backoff, jitter, and conditional retry using the tenacity library.

#retry#tenacity
pythonbeginner

Rich Progress Bar for CLI

Display beautiful progress bars and status spinners in CLI applications using the Rich library.

#cli#rich
pythonbeginner

Pathlib File Operations

Modern file system operations using pathlib for reading, writing, globbing, and path manipulation.

#pathlib#filesystem
pythonbeginner

Dataclasses with Post-Init Processing

Use Python dataclasses with __post_init__ for computed fields, validation, and default factories.

#dataclasses#python
pythonbeginner

functools.cache and lru_cache

Memoize expensive function calls with functools.cache and lru_cache for automatic result caching.

#caching#functools
pythonintermediate

Click CLI Command Group

Build professional CLI tools with Click using command groups, options, arguments, and help text.

#cli#click
pythonintermediate

HTTPX Async Client with Retry

Make async HTTP requests with HTTPX featuring timeout config, retry logic, and response streaming.

#httpx#async