Reference library

Modern tooling

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

6 matches
Modern tooling easy

Data Conversion Helper Functions in Python

A set of beginner-friendly helper functions to convert between JSON strings and Python data, parse dates, and read/write files using pathlib.

json datetime pathlib
Python
from datetime import datetime
from pathlib import Path
import json

def to_json(data, indent=2):
    """Convert Python data to pretty-printed JSON string."""
    return json.dumps(data, indent=indent, default=str)

def from_json(json_string):
    """Parse JSON string back into Python data."""
    return json.loads(jso…
13 0 Open
Modern tooling easy

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.

commitizen mock subprocess
Python
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
  …
15 0 Open
Modern tooling easy

How to Mock a pyenv Local Version File in Python

Read and write a mock .python-version file using the pathlib module and tempfile for isolated testing.

pyenv pathlib version-control
Python
import json
import tempfile
from pathlib import Path


def read_pyenv_local(directory: Path) -> str:
    """Read the .python-version file in the given directory."""
    version_file = directory / ".python-version"
    if not version_file.exists():
        return "no-version-file"
    return version_file.read_text().st…
12 0 Open
Modern tooling easy

How to Mock setuptools_scm get_version in Python

This code demonstrates how to mock setuptools_scm.get_version in Python using unittest.mock.patch to test version retrieval logic without installing or relying on the actual package.

setuptools-scm mock unittest
Python
```python
from unittest.mock import patch

def get_version_from_scm():
    try:
        import setuptools_scm
        return setuptools_scm.get_version()
    except (ImportError, LookupError):
        return None

if __name__ == "__main__":
    with patch("setuptools_scm.get_version", return_value="1.2.3"):
        pr…
14 0 Open
Modern tooling medium

Mock Python version with unittest.mock.patch

Use unittest.mock.patch to simulate a specific Python version and test version-dependent behavior.

unittest mock version
Python
import sys
import unittest
from unittest.mock import patch

class TestPythonVersion(unittest.TestCase):
    @patch("sys.version_info", (3, 9, 0, "final", 0))
    def test_python_version_pinned(self):
        self.assertEqual(sys.version_info[:2], (3, 9))
        print(f"Pinned version: {sys.version_info.major}.{sys.ve…
13 0 Open
Modern tooling easy

Mock pip-compile to Resolve Requirements in Python

A mock function that mimics pip-compile by converting a requirements.in file into pinned, locked package versions.

pip-tools requirements mock
Python
import subprocess
import tempfile
from pathlib import Path


def compile_requirements_mock(requirements_in: str) -> str:
    """Mock pip-compile: resolve a simple requirements.in into a locked format."""
    lines = [line.strip() for line in requirements_in.splitlines() if line.strip() and not line.startswith("#")]
  …
12 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.