Modern tooling
uv, ruff, pyproject.toml, packaging, and current Python project workflows.
Build a Recipe Runner Mock in Python
A Python script that mocks a command runner recipe system: maps recipe names to shell commands, executes them with subprocess, and prints the output and exit code.
import subprocess
import sys
def run_recipe(recipe: str) -> None:
"""Simulate a command runner recipe by printing the command and exit code."""
print(f"Running recipe: {recipe}")
result = subprocess.run(recipe, shell=True, capture_output=True, text=True)
print(f"Exit code: {result.returncode}")
i…
How to Create a Mock Virtualenv with an Activation Script in Python
Create a mock virtualenv directory with a generated bash activation script using Python's standard library.
import os
import subprocess
import sys
from pathlib import Path
def mock_virtualenv(name: str = "myenv") -> Path:
"""Create a mock virtualenv directory and activation script."""
env_dir = Path(name)
env_dir.mkdir(exist_ok=True)
(env_dir / "bin").mkdir(exist_ok=True)
activate_script = f"""#!/bin/…
How to Mock Commitizen Version Bump in Python
Simulate commitizen's version bump logic and mock the subprocess call to avoid real execution in tests.
import subprocess
from unittest.mock import patch, MagicMock
def bump_version(current_version: str, increment: str = "patch") -> str:
"""Simulate commitizen's version bump logic."""
major, minor, patch = map(int, current_version.split("."))
if increment == "major":
major += 1
minor = 0
…
How to Mock a Fast uv pip sync in Python
Simulate a fast uv pip sync by mocking file operations and subprocess calls to test dependency installation workflows.
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
def uv_pip_sync_fast_install_mock(requirements_text: str) -> dict:
"""Simulate a fast uv pip sync by mocking file operations and subprocess calls."""
mock_dir = Path(tempfile.mkdtemp(prefix="uv_mock_"))
req_lines…
How to Mock a pipx Install Command in Python
Simulate a pipx install step by validating tool names and printing the exact command output a real pipx run would produce.
import subprocess
import sys
def install_with_pipx(tool_name: str) -> str:
"""
Mock a pipx install step by validating the tool name and
simulating the installation command output.
"""
allowed_tools = {"black", "flake8", "mypy", "ruff"}
if tool_name not in allowed_tools:
raise ValueErr…
Makefile Targets for lint, test, and build in Python
This Python script defines common Makefile targets (lint, test, build) as subprocess commands, printing each target's command and executing them with error checking.
import subprocess
TARGETS = {
"lint": ["ruff", "check", "."],
"test": ["pytest", "-q"],
"build": ["python", "-m", "build"],
}
def run(target: str) -> None:
if target not in TARGETS:
raise ValueError(f"Unknown target: {target}")
print(f"Running {target}...")
subprocess.run(TARGETS[tar…
Browse by section
Each section groups closely related Python snippets.
Modern tooling — Python code examples
What you will find here
This page collects modern tooling 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.