Production deployment patterns
Graceful shutdown, prod config, rollouts, readiness probes, and ship-with-confidence checks.
How to Mock a CI Pipeline with Build, Test, and Deploy Stages in Python
Simulate a three-stage CI pipeline (build, test, deploy) in Python with random pass/fail logic, early exit on failure, and measured stage durations.
import time
import random
from dataclasses import dataclass
@dataclass
class StageResult:
name: str
status: str
duration: float
def run_stage(name: str, success_chance: float = 0.9) -> StageResult:
"""Simulate a pipeline stage with random success/failure."""
start = time.time()
time.sleep(r…
How to Mock a Dockerfile Multi-Stage Build in Python
Simulate a Dockerfile multi-stage build process in Python using dataclasses to validate stage ordering and file availability before you write the real Dockerfile.
from dataclasses import dataclass
from pathlib import Path
@dataclass
class BuildStage:
name: str
base_image: str
files: list[str]
commands: list[str]
def run_build(stage: BuildStage, context_dir: Path):
print(f"=== Stage: {stage.name} (base: {stage.base_image}) ===")
for file in stage.file…
How to Mock a Feature Flag Rollout Percentage in Python
Simulate a percentage-based feature flag rollout by hashing a user ID to deterministically enable features for a subset of users.
import random
from dataclasses import dataclass
@dataclass
class FeatureFlag:
name: str
rollout_percentage: int
def is_feature_enabled(feature_flag: FeatureFlag, user_id: str) -> bool:
hashed_id = hash(user_id) % 100
return hashed_id < feature_flag.rollout_percentage
if __name__ == "__main__":
…
How to Mock a GitHub Actions Workflow in Python
Build a dataclass-based model of a GitHub Actions workflow and simulate its execution to validate steps and outputs before deployment.
import json
from dataclasses import dataclass, asdict
from typing import List, Dict, Any
@dataclass
class Step:
name: str
run: str
@dataclass
class Job:
name: str
steps: List[Step]
runs_on: str = "ubuntu-latest"
@dataclass
class Workflow:
name: str
jobs: List[Job]
def to_github_a…
How to Replace Fields in an Immutable Dataclass in Python
Create a new copy of a frozen dataclass with selected fields changed, leaving the original unchanged.
from dataclasses import dataclass, replace
@dataclass(frozen=True)
class ServerConfig:
name: str
cpu: int = 2
ram: int = 4096
tags: tuple = ()
original = ServerConfig("web-01", cpu=4, tags=("env:prod",))
updated = replace(original, ram=8192, tags=("env:prod", "region:us-east"))
print("Original:", …
Browse by section
Each section groups closely related Python snippets.
Production deployment patterns — Python code examples
What you will find here
This page collects production deployment patterns snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.