Modern tooling
uv, ruff, pyproject.toml, packaging, and current Python project workflows.
Configure ruff linter rules in pyproject.toml with Python
Reads an existing pyproject.toml and merges common ruff linter rules into the tool.ruff section using Python's tomllib.
import tomllib
from pathlib import Path
def configure_ruff_linter_rules(project_path: str = ".") -> dict:
"""Add common ruff linter rules to pyproject.toml if missing."""
pyproject_path = Path(project_path) / "pyproject.toml"
# Default config for ruff linter with practical rules
ruff_config = {
…
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 List Pre-commit Hooks from YAML Config in Python
Parse a .pre-commit-config.yaml file with PyYAML and print every hook ID paired with its source repository.
import yaml
pre_commit_config = """
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
"""
def list_hooks(c…
How to Mock Poetry pyproject.toml Dependencies Sections in Python
Parse and extract dependency lists from Poetry-style pyproject.toml text using Python's standard library.
from pathlib import Path
import re
def parse_pyproject_dependencies(text):
"""Extract dependencies from a pyproject.toml style text."""
lines = text.splitlines()
sections = {
"dependencies": [],
"dev": [],
"optional": [],
}
current_section = None
patterns = {
…
How to Parse Taskfile YAML in Python
Load a Taskfile.yaml with PyYAML and simulate task execution by returning each task's commands.
import yaml
from pathlib import Path
def load_taskfile(taskfile_path: str) -> dict:
"""Load and parse a Taskfile.yaml file into a dict."""
data = Path(taskfile_path).read_text()
return yaml.safe_load(data)
def run_task(taskfile: dict, task_name: str) -> dict:
"""Simulate running a task by returning i…
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.