Reference library

Modern tooling

uv, ruff, pyproject.toml, packaging, and current Python project workflows.

5 matches
Modern tooling easy

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.

subprocess command-runner recipes
Python
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…
13 0 Open
Modern tooling medium

How to Mock CLI Output in Typer with unittest.mock

Mock and capture Typer CLI output using unittest.mock.patch and io.StringIO for testing command-line applications.

typer cli testing
Python
import typer
from unittest.mock import patch
import io

app = typer.Typer()

@app.command()
def greet(name: str, age: int = 18, uppercase: bool = False):
    """Greet a person with optional formatting."""
    message = f"Hello {name}, age {age}"
    if uppercase:
        message = message.upper()
    typer.echo(messag…
11 0 Open
Modern tooling easy

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.

pipx cli mocking
Python
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…
15 0 Open
Modern tooling easy

How to Mock isort Output to Test Import Sorting in Python

Uses isort with check mode and a unittest mock to verify whether a Python source string has correctly sorted imports.

isort import-sorting mock
Python
import isort
from unittest.mock import patch

code = """
import os
import sys
import json
import pathlib
"""

def check_imports_sorted(code_str):
    with patch("isort.api.output") as mock_output:
        isort.code(code_str, check=True, show_diff=True)
        return mock_output.called

if __name__ == "__main__":
   …
10 0 Open
Modern tooling easy

pytest mark slow skip integration

Uses pytest markers to select fast tests, skip unfinished ones, and run integration checks with verbose output.

pytest markers testing
Python
import pytest

def test_fast():
    assert 1 + 1 == 2

@pytest.mark.slow
def test_slow():
    import time
    time.sleep(1)
    assert 5 * 5 == 25

@pytest.mark.skip(reason="Not ready for production")
def test_skipped():
    assert 2 + 2 == 5

@pytest.mark.integration
def test_integration():
    database = {"users": […
17 0 Open

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.