Skip to content
StackPractices

Tag: python

Browse 151 practical software engineering resources tagged with "python". Discover code recipes, design patterns, documentation templates, and in-depth guides to help you build, deploy, and maintain production-ready solutions involving python.

Build Stateful AI Agents with LangGraph State Machines

Create multi-step AI agents with LangGraph using state machines, conditional edges, tool calling, and human-in-the-loop checkpoints for production workflows

Fine-Tune and Deploy Text Classifiers with Hugging Face

Fine-tune a pre-trained transformer model for text classification using Hugging Face Trainer, tokenize datasets, evaluate metrics, and deploy for inference

Compose LCEL Chains in LangChain for Multi-Step LLM

Build composable LLM pipelines with LangChain Expression Language (LCEL) using pipes, parallel execution, and custom runnable components

Evaluate RAG Quality with RAGAS Metrics

Measure RAG pipeline quality using RAGAS framework metrics — faithfulness, answer relevancy, context precision, and context recall for objective evaluation

Stream LLM Output with Server-Sent Events (SSE)

Stream LLM responses to clients in real-time using Server-Sent Events with FastAPI, OpenAI streaming, and async generators for token-by-token output

Run LLMs Locally with Ollama for Private Inference

Install and use Ollama to run open-source LLMs locally with Python, including streaming, embeddings, function calling, and model management without API costs

Compare Text Semantic Similarity with OpenAI Embeddings

Generate text embeddings with OpenAI and compute cosine similarity to measure semantic similarity between texts for search, dedup, and clustering

Structured JSON Output from OpenAI Function Calling

Use OpenAI function calling and structured outputs to get reliable JSON from LLMs with Pydantic validation and error handling

a Local RAG Pipeline with ChromaDB and Sentence Transformers

Implement retrieval-augmented generation locally with ChromaDB, sentence-transformers embeddings, and LLM generation without external API dependencies

Sentiment Analysis with Python and NLTK

Score text sentiment using NLTK VADER and custom lexicons in Python.

Store and Query Embeddings in Pinecone Vector Database

Use Pinecone to store, query, and filter vector embeddings for semantic search with metadata filtering and namespace isolation

Python API Rate Limiting with Token Bucket

Implement token bucket rate limiting in Flask and FastAPI with Redis support

Dependency Injection

Implement dependency injection to write testable, decoupled code across languages and frameworks.

Cache Database Query Results with Redis and Python

Cache expensive database query results in Redis with cache-aside pattern, TTL management, and invalidation on writes for Python applications.

Cache Database Queries with Django Cache Framework

Use Django's built-in cache framework with per-view caching, template fragment caching, and low-level cache API for database query optimization.

Cache HTTP Responses with httpx and CacheControl in Python

Cache HTTP responses in Python using httpx with CacheControl for HTTP-compliant caching, ETag handling, and conditional requests.

Store User Sessions in Memcached with Python

Use Memcached as a distributed session store in Python web applications with pymemcache, TTL management, and failover handling.

Cache Function Results with Redis and TTL in Python

Build a Python decorator that caches function return values in Redis with configurable TTL, key generation, and cache invalidation

Make Concurrent HTTP Requests with Python and aiohttp

Fetch multiple APIs concurrently using asyncio and aiohttp. Covers connection pooling, rate limiting, retries, and batch processing.

Concurrent Async Tasks with asyncio.gather and Task Groups

Execute multiple async operations concurrently in Python using asyncio.gather, asyncio.TaskGroup, error handling with return_exceptions, timeouts, and semaphores for rate limiting.

Rate Limit Async Operations with asyncio.Semaphore

Control concurrency in async Python using asyncio.Semaphore for rate limiting API calls, database connections, and resource access with bounded parallelism patterns.

Schedule Periodic Tasks in Python with APScheduler

Run cron-like jobs in Python using APScheduler. Covers interval, cron, and date triggers, job stores, and background scheduling.

Parallelize CPU and I/O Work with ThreadPoolExecutor

Use Python's ThreadPoolExecutor for parallel I/O operations, thread-safe result collection, Future callbacks, error handling, and mixing threads with asyncio for blocking work.

Convert CSV to JSON

How to convert CSV data to JSON format in Python, Java, and JavaScript.

Convert JSON to CSV

How to convert JSON data to CSV format in Python, Java, and JavaScript.

Diff JSON Objects

How to compare two JSON objects and find differences in Python, Java, and JavaScript.

Format Phone Numbers

How to format and validate phone numbers in Python, Java, and JavaScript.

Generate PDF Reports with Python

Create styled PDF documents from data using ReportLab and fpdf2 in Python.

Generate URL Slugs

How to generate clean, URL-friendly slugs from strings in multiple programming languages.

Merge JSON Files

How to merge multiple JSON files into a single object or array in Python, Java, and JavaScript.

Parse Command Line Arguments

How to parse command line arguments in Python, Java, and Node.js CLI applications.

Parse CSV Files

How to parse CSV files in Python, Java, and JavaScript with practical code examples.

Parse CSV Files with Python and Pandas

How to read, filter, and transform large CSV files efficiently using Python pandas and the csv module.

Parse Excel Files

How to read and write Excel (.xlsx) files in Python, Java, and JavaScript.

Parse Log Files

How to parse and analyze server log files using Python, Java, and JavaScript.

Parse Markdown Files

How to parse Markdown to HTML and extract structured data in Python, Java, and JavaScript.

Parse PDF Files

How to extract text and metadata from PDF files in Python, Java, and JavaScript.

Parse TOML Files

How to parse TOML configuration files in Python, Java, and JavaScript.

Parse XML Files

How to parse XML documents in Python, Java, and JavaScript with practical code examples.

Parse YAML Files

How to parse YAML configuration files in Python, Java, and JavaScript.

Schedule and Monitor DAGs with Apache Airflow

How to define, schedule, and monitor Directed Acyclic Graphs in Apache Airflow with operators, sensors, XCom, and task dependencies.

Parallel DataFrame Operations with Dask

How to use Dask for parallel DataFrame operations on datasets larger than memory, covering lazy evaluation, partitions, custom computations, and distributed scheduling.

Validate DataFrame Schemas with Pandera

How to validate pandas and Polars DataFrame schemas with Pandera, covering column types, constraints, custom checks, hypothesis testing, and schema inheritance.

Transform Data in the Warehouse with dbt

How to use dbt for SQL-based data transformations with models, tests, materializations, macros, and incremental loading in a data warehouse.

Read and Write Excel Files with Python

How to read, write, and format Excel spreadsheets using openpyxl and pandas in Python.

Generate QR Codes with Python

Create QR codes for URLs, text, and contact cards using the qrcode library in Python.

Build an ETL Pipeline with pandas and Parquet

How to build an extract-transform-load pipeline using pandas for data processing and Parquet for columnar storage with type coercion and validation.

High-Performance DataFrame Operations with Polars

How to use Polars for fast DataFrame operations with lazy evaluation, expression API, streaming, and interop with pandas for large datasets.

Large-Scale Aggregation with PySpark

How to perform group-by aggregations on large datasets with PySpark, covering window functions, UDFs, broadcast joins, and performance tuning.

Extract Data from HTML Pages with Python and BeautifulSoup

Parse HTML and extract data using BeautifulSoup. Covers CSS selectors, navigation, tables, pagination, and respectful scraping with rate limiting.

Serialize and Deserialize Data

How to serialize and deserialize data in JSON, XML, and YAML across Python, Java, and JavaScript.

Truncate Text

How to truncate text with ellipsis and word boundaries in Python, Java, and JavaScript.

Validate JSON Schema

How to validate JSON data against schemas in Python, Java, and JavaScript.

Connect to MySQL

How to connect to MySQL databases in Python, JavaScript, and Java.

Connect to PostgreSQL

How to connect to PostgreSQL databases in Python, JavaScript, and Java.

Connect to Redis

How to connect to Redis and perform basic operations in Python, JavaScript, and Java.

Execute Raw SQL

How to execute raw SQL queries safely with parameterized statements.

Use ORM for CRUD

How to perform CRUD operations using ORMs in Python, JavaScript, and Java.

Expose Custom Application Metrics with Python and Prometheus

Build a custom Prometheus metrics exporter in Python using prometheus_client for counters, gauges, histograms, and summaries.

a Custom Terraform Provider with Python and

Extend Terraform with a custom provider using Python and the terraform-plugin-framework to manage external resources.

Compress and Decompress Files

How to handle ZIP, GZIP, and TAR archives programmatically.

Copy and Move Files

How to copy and move files across platforms safely and efficiently.

Generate Temporary Files

How to create temporary files and directories safely with automatic cleanup across Python, Node.js, Java, and Bash.

Batch Resize Images with Python

How to bulk resize and optimize images using Pillow and Python for web delivery.

Safely Extract Zip Files with Python

How to extract and validate zip archives securely using Python zipfile and shutil.

Read Large Files

How to read large files efficiently without running out of memory.

Rotate Log Files

How to implement log rotation by size, date, and count to prevent disk exhaustion across Python, Node.js, Java, and Linux systems.

Process Large Files with Streams

How to read, transform, and write large files efficiently using streams without loading entire files into memory in Python, Node.js, and Java.

Watch File Changes

How to monitor file system changes in real time.

Write Large Files

How to write large files efficiently using buffered and streaming output.

Implement Event Sourcing with CQRS in Python

Build an event-sourced system with CQRS separation using Python, event store persistence, projection rebuilds, snapshots, and idempotent event handlers for audit-ready architectures.

Kafka Consumer Groups with Python for Scalable Streaming

Create Kafka consumer groups in Python with partition assignment, offset management, commit strategies, rebalance handling, and exactly-once semantics for scalable stream processing.

Implement the Transactional Outbox Pattern for Reliable

Use the transactional outbox pattern to reliably publish domain events alongside database changes, with a relay processor, polling strategies, and exactly-once delivery guarantees.

Distribute Background Tasks with Python Celery and Redis

Set up Celery with Redis broker for distributed task processing including task chaining, groups, chords, retry strategies, scheduled tasks with Celery Beat, and result backends.

Build a RabbitMQ Consumer with Python and Pika

Create a RabbitMQ consumer and producer in Python using pika with durable queues, work dispatching, acknowledgments, dead-letter exchanges, and prefetch tuning.

Implement Redis Pub/Sub Messaging in Python

Build real-time pub/sub messaging with Redis and Python including pattern subscriptions, message serialization, connection pooling, and broadcast patterns for microservices.

Distributed Tracing with OpenTelemetry

How to implement distributed tracing in Python with OpenTelemetry SDK, including spans, context propagation, auto-instrumentation, and Jaeger export.

Expose Business Metrics with Prometheus

How to expose custom business metrics in Python using prometheus_client, including counters, gauges, histograms, summaries, and Flask integration.

Structured JSON Logging with structlog

How to emit structured JSON logs in Python using structlog, including context binding, log levels, processors, and integration with standard logging.

Escape HTML Entities

How to escape HTML entities to prevent XSS attacks in Python, Java, and JavaScript.

Concurrent HTTP Requests with asyncio.gather and aiohttp

Fetch multiple HTTP endpoints concurrently using asyncio.gather and aiohttp with error handling, rate limiting, timeouts, and connection pooling

Find Security Issues in Python Code with Bandit

How to use Bandit to scan Python code for common security vulnerabilities, configure ignore lists, integrate with CI/CD, and interpret results.

Encrypt and Decrypt Data with AES-GCM in Python

Encrypt sensitive data using AES-GCM with the cryptography library. Covers key derivation, nonce generation, authenticated encryption, and file encryption.

Secure JWT Refresh Token Rotation with Python

Implement secure JWT access and refresh token rotation in Python with blacklist, reuse detection, and automatic access token renewal for stateless auth

Strict Type Checking in Python with mypy

How to configure mypy strict mode for Python projects, handle common type errors, use Protocol and TypeGuard, and integrate with CI/CD.

Scan Python Packages for Known CVEs with pip-audit

How to use pip-audit to scan Python dependencies for known vulnerabilities, configure ignore lists, integrate with CI/CD, and remediate findings.

Distributed Rate Limiting with FastAPI and Redis

Implement distributed rate limiting in FastAPI using Redis sliding window and token bucket algorithms with per-user, per-IP, and per-endpoint limits

Manage Application Secrets with HashiCorp Vault and Python

Store, retrieve, and rotate application secrets securely using HashiCorp Vault with Python hvac client, dynamic secrets, and automatic lease renewal

Prevent SQL Injection with SQLAlchemy Parameterized Queries

Protect Python applications from SQL injection using SQLAlchemy parameterized queries, ORM models, input validation, and query inspection to ensure safe database access

Sanitize User Input

How to sanitize and validate user input in Python, Java, and JavaScript to prevent injection attacks.

Package Python Dependencies for AWS Lambda with Layers

Package Python dependencies for AWS Lambda using Lambda Layers, Docker builds for native extensions, and SAM/Serverless Framework integration.

Build HTTP-Triggered Azure Functions with Python

Create HTTP-triggered Azure Functions in Python with binding configuration, async handlers, dependency injection, and deployment via Azure CLI.

Generate Test Data

How to generate realistic, deterministic test data with Faker, factory-boy, and type-aware generators for reliable test suites in Python, JavaScript, and Java.

Measure Test Coverage with pytest-cov

How to measure and enforce Python test coverage thresholds with pytest-cov, including branch coverage, HTML reports, exclusions, and CI integration.

Property-Based Testing with Hypothesis

How to use Hypothesis for property-based testing in Python, generating hundreds of test cases automatically from strategies instead of writing them by hand.

Mock External APIs with responses Library

How to mock HTTP API calls in Python tests using the responses library, including status codes, headers, JSON bodies, and error simulation.

Pytest Fixtures and Parametrize

How to use pytest fixtures and @pytest.mark.parametrize to write data-driven tests with reusable setup logic across Python projects.

Multi-Tenant Data Isolation Pattern

Isolate tenant data in shared infrastructure using row-level security, schema-per-tenant, or database-per-tenant strategies. A pattern for SaaS applications.

Pipes and Filters Pattern

Chain processing steps with independent filters connected by pipes. A pattern for data transformation pipelines where each step is reusable and composable.

Federated Identity Pattern

Delegate authentication to external identity providers. A pattern for integrating OAuth2, OIDC, SAML, and SSO across multiple services and organizations.

Voucher Pattern

Validate claims and delegate access using signed vouchers without exposing sensitive data. A security pattern for token-based authorization between services.

Abstract Factory Pattern

Create families of related objects without specifying concrete classes. A creational design pattern for consistent object families.

Adapter Pattern

Convert the interface of a class into another interface clients expect. A structural design pattern for interface compatibility.

Bridge Pattern

Decouple an abstraction from its implementation so both can vary independently. A structural design pattern for platform independence.

Builder Pattern

Construct complex objects step by step. A creational design pattern for readable, configurable object construction.

Cache-Aside Pattern

Load data into the cache on demand from the backing store. A caching pattern that gives the application full control over what and when to cache.

Cache Invalidation Pattern

Strategies for keeping cached data fresh: TTL expiration, explicit invalidation, write-through, and event-driven cache eviction.

Cache Stampede Prevention Pattern

Prevent thundering herd cache misses with locks, single-flight, and early refresh strategies to protect the database from concurrent reloads.

Chain of Responsibility Pattern

Pass requests along a chain of handlers until one handles it. A behavioral design pattern for decoupling senders and receivers.

Circuit Breaker Pattern

Prevent cascading failures by stopping requests to failing services. An architectural pattern for resilient distributed systems.

Command Pattern

Encapsulate a request as an object, letting you parameterize clients with queues, logs, and undoable operations. A behavioral design pattern.

Composite Pattern

Compose objects into tree structures to represent part-whole hierarchies. A structural design pattern for treating individual objects and compositions uniformly.

CQRS Pattern

Separate read and write operations into different models, optimizing each for their specific workload. A data pattern for scalable systems.

Decorator Pattern

Add new functionality to objects dynamically by wrapping them. A structural design pattern for flexible behavior extension.

Dependency Injection Pattern

Supply dependencies from outside rather than creating them internally. An architectural pattern for decoupled, testable code.

Event Sourcing Pattern

Store the state of an application as a sequence of events rather than storing only the current state. An architectural pattern for audit-friendly systems.

Factory Pattern

Create objects without specifying the exact class to instantiate. A creational design pattern for flexible object creation.

Flyweight Pattern

Share objects to support large numbers of fine-grained objects efficiently. A structural design pattern for memory optimization.

Interpreter Pattern

Define a representation for a language's grammar along with an interpreter that uses the representation to interpret sentences. A behavioral design pattern for mini-languages.

Iterator Pattern

Provide a way to access elements of a collection sequentially without exposing its underlying representation. A behavioral design pattern for traversal.

Mediator Pattern

Define an object that encapsulates how a set of objects interact. A behavioral design pattern for reducing chaotic dependencies.

Memento Pattern

Capture and restore an object's internal state without violating encapsulation. A behavioral design pattern for undo/redo.

MVC Pattern

Separate application into Model, View, and Controller components. An architectural design pattern for organized, maintainable code.

Observer Pattern

Define a subscription mechanism to notify multiple objects about events. A behavioral design pattern for event-driven communication.

Prototype Pattern

Create new objects by copying existing ones. A creational design pattern for cloning and object duplication.

Proxy Pattern

Provide a surrogate or placeholder for another object to control access to it. A structural design pattern for access control, lazy loading, and logging.

Read-Through Cache Pattern

A transparent cache layer that intercepts read requests, fetches from the data source on miss, and populates the cache automatically.

Refresh-Ahead Cache Pattern

Proactively refresh cache entries before they expire to eliminate cache misses on hot keys and maintain consistent read latency.

Repository Pattern

Abstract data access logic behind a clean interface. An architectural design pattern for testable, maintainable data layers.

Retry Pattern

Retry an operation that has failed with transient errors, using configurable strategies like fixed delay, exponential backoff, or circuit breaker integration.

Saga Pattern

Manage distributed transactions across multiple services by chaining local transactions with compensating actions for rollbacks. A microservices pattern.

Serverless Event Sourcing Pattern

Store function state as an append-only event log so workflows can be replayed, audited, and recovered without a persistent database.

Serverless Fanout Pattern

Broadcast a single event to multiple independent consumers via SNS, EventBridge, or SQS so each consumer processes the event without coupling.

Serverless Function Composition Pattern

Chain serverless functions via Step Functions or orchestration layers to build multi-step workflows with retries, branching, and state management.

Serverless Throttling Pattern

Handle backpressure in serverless by using SQS, token buckets, and concurrency limits to protect downstream services from burst traffic.

Serverless Warm Pool Pattern

Keep Lambda functions warm by sending periodic ping events to reduce cold start latency for latency-sensitive workloads.

Singleton Pattern

Ensure a class has only one instance and provide global access to it. A creational design pattern for controlled object creation.

State Pattern

Allow an object to alter its behavior when its internal state changes. A behavioral design pattern for finite state machines.

Strategy Pattern

Define a family of algorithms, encapsulate each one, and make them interchangeable. A behavioral design pattern for flexible behavior selection.

Template Method Pattern

Define the skeleton of an algorithm in a base class, letting subclasses override specific steps without changing the algorithm's structure. A behavioral design pattern.

Timeout Pattern

Prevent operations from hanging indefinitely by enforcing a maximum execution time. A resilience pattern for predictable response times.

Two-Level Cache Pattern

Combine an L1 in-memory cache with an L2 distributed cache to reduce latency for hot keys while maintaining cache consistency across instances.

Visitor Pattern

Represent an operation to be performed on elements of an object structure without changing the classes of the elements. A behavioral design pattern.

Write-Behind Cache Pattern

Write to cache synchronously and persist to the database asynchronously for high-throughput write workloads with eventual consistency.

Write-Through Cache Pattern

Synchronously write to both cache and backing store so the cache always has the latest data without TTL-based invalidation.

Thread Pool Sizing Template

Template for documenting thread pool configuration per service: pool type selection, sizing formulas, CPU vs I/O bound tuning, queue strategies, rejection policies, monitoring metrics, and tuning examples for Java, Python, Go, and Node.js.

Complete Guide to Python Asyncio

Master asynchronous Python programming with asyncio. Covers coroutines, tasks, event loops, async/await, gather, semaphores, queues, HTTP clients, websockets, and debugging async code.

Complete Guide to Python Asyncio in Production

Run Python asyncio in production with confidence. Covers event loops, task management, debugging, cancellation, timeouts, backpressure, and patterns for high-concurrency async applications.

Property-Based Testing Guide

Master property-based testing with Hypothesis (Python), fast-check (TypeScript), and QuickCheck principles. Generate test cases automatically, find edge cases, and shrink failures.

Pytest in Production Guide

Master pytest for production codebases: advanced fixtures, plugins, custom markers, parametrized tests, parallel execution with pytest-xdist, and CI integration.

Testcontainers: Real Dependencies in Integration Tests

Master Testcontainers for integration testing with real databases, message brokers, and APIs. Covers Java, Python, and Node.js with Docker-based test fixtures.