Modern tooling
uv, ruff, pyproject.toml, packaging, and current Python project workflows.
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 Export a Conda Environment YAML File in Python
Generate a mock conda environment YAML export with a reusable Python function and the PyYAML library.
import yaml
def conda_env_mock(name="demo_env", channels=None, packages=None):
channels = channels or ["defaults"]
packages = packages or [
"python=3.11",
"pip",
"numpy=1.24.3",
"pandas=2.0.3",
]
env_dict = {
"name": name,
"channels": channels,
…
How to Generate a Mock Rollbar Error Report in Python
Create a realistic fake Rollbar error report with random timestamps, levels, messages, and counts for testing and demos.
import json
import random
import time
from datetime import datetime, timedelta
def mock_rollbar_report(n_errors=5):
messages = [
"TypeError: unsupported operand type(s) for +: 'int' and 'str'",
"KeyError: 'user_id'",
"ValueError: invalid literal for int() with base 10: 'abc'",
"At…
How to Generate a Mock devcontainer.json Config in Python
Build a reproducible devcontainer.json file with Python, composing name, image, extensions, forwarded ports, and a post-create command as a dict.
import json
from pathlib import Path
def create_devcontainer_config(
image: str = "mcr.microsoft.com/devcontainers/python:3.11",
name: str = "python-dev-container",
ports: list[int] | None = None,
post_create: str | None = None,
) -> dict:
config = {
"name": name,
"image": image,
…
How to Run Coverage Report and Generate HTML in Python
Use the coverage module to measure test coverage, save the report, and generate an HTML report in Python.
import coverage
import unittest
def add(a, b):
return a + b
class TestAdd(unittest.TestCase):
def test_add_positive(self):
self.assertEqual(add(2, 3), 5)
if __name__ == "__main__":
cov = coverage.Coverage(source=["__main__"])
cov.start()
suite = unittest.defaultTestLoader.loadTestsFro…
How to configure ruff linter rules in pyproject.toml with Python
This Python script generates a pyproject.toml file with ruff linter rules, including selected and ignored rules, per-file ignores, and complexity limits.
from pathlib import Path
def configure_ruff_rules(project_dir: str = "my_project") -> None:
"""Create a pyproject.toml with ruff linter rules for mock usage."""
pyproject_path = Path(project_dir) / "pyproject.toml"
pyproject_path.parent.mkdir(parents=True, exist_ok=True)
config = """[tool.ruff]
line-…
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.