Modern tooling
uv, ruff, pyproject.toml, packaging, and current Python project workflows.
How to Mock BugSnag Notify in Python
Use unittest.mock to simulate BugSnag notifications, verify calls, and test error handling without external dependencies.
import mock
bugsnag = mock.MagicMock()
def notify_error(message, severity="error"):
bugsnag.notify(message, severity=severity)
if __name__ == "__main__":
notify_error("Test error", severity="warning")
bugsnag.notify.assert_called_once_with("Test error", severity="warning")
print("Mocked BugSnag noti…
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.
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__":
…
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.
```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…
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…
Mock pdm build and publish in Python
Simulate pdm build and publish commands with unittest.mock to test packaging workflows without triggering real builds or uploads.
from unittest.mock import Mock, patch
import pdm
def build_package() -> str:
"""Simulate building a package with pdm."""
build_mock = Mock(return_value="dist/mypackage-0.1.0-py3-none-any.whl")
with patch.object(pdm, "build", build_mock):
result = pdm.build()
return result
def publish_packa…
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.