Python bindings for the Modern C++23 Features Demo Project, providing Pythonic access to advanced C++ features
through pybind11. This package demonstrates best practices for creating high-performance Python libraries backed by
modern C++ code, with comprehensive type hints, testing, and development tools.
- Selected C++23 Features: Python API for key C++ components with practical Python applications
- pybind11 Integration: Seamless C++ / Python interoperability with automatic conversions
- Modern Python 3.12: Leverages latest Python features and performance improvements
- Pythonic Design: Intuitive Python APIs that follow PEP conventions
- Type Safety: Comprehensive type hints and runtime type checking
- Iterator Support: Python-native iteration over C++ containers and ranges
- Context Managers: RAII patterns exposed as Python context managers
- Error Handling: Proper Python exception mapping from C++ exceptions
- Automatic Formatting: Pre-commit hooks with
ruff format - Linting: Pre-commit hooks with
ruff checkandbanditfor code style and security checks - Type Checking: Full
mypysupport with strict type checking - Poetry: Modern dependency management and packaging
- Test Coverage: 100% test coverage with HTML and XML reports
- Documentation: Comprehensive numpy-style docstrings
- CI/CD Automation: Automatic build and test workflows
- 🚀 Features
- 📋 Table of Contents
- 🛠️ Installation
- 🎯 Usage
- 📁 Project Structure
- 🔧 Components Overview
- 💻 Development Notes
- Python 3.12+
- Poetry (for dependency management)
cd .. # From root directory
cmake --preset release-python
cmake --build --preset release-pythoncd python
# Install all dependencies
poetry install
# Activate the virtual environment
eval $(poetry env activate)# Build the package
python3 scripts/build.py
# Install the package
pip3 install dist/demo_python-1.1.0-py3-none-any.whlpython3 examples/algorithms_example.py
python3 examples/containers_example.py
python3 examples/exceptions_example.py
python3 examples/random_example.py
python3 examples/shapes_example.py
python3 examples/timing_example.pypoetry run pytestfrom demo.algorithms import count_if, sort
from demo.containers import Container
from demo.shapes import Circle
# Exported C++ container
numbers = Container(int, [42, 17, 89, 3, 56])
print(f'Original: {numbers}')
# Sort the container in place
sort(numbers)
print(f'Sorted: {numbers}')
# Count elements matching a condition
even_count = count_if(numbers, lambda n: n % 2 == 0)
print(f'Even numbers: {even_count}')
# Exported C++ shape
circle = Circle(5.0)
area = circle.get_area()
perimeter = circle.get_perimeter()
print(f'Area: {area:.2f}, Perimeter: {perimeter:.2f}')More examples can be found in the examples directory.
python/
├── .venv/ # Virtual environment (generated by Poetry)
├── dist/ # Build output (generated by scripts/build.py)
├── src/
│ └── demo/ # Main Python package
│ ├── __init__.py # Package initialization
│ ├── algorithms.py # Python wrapper for the algorithms module
│ ├── containers.py # Python wrapper for the containers module
│ ├── exceptions.py # Python wrapper for the exceptions module
│ ├── random.py # Python wrapper for the random module
│ ├── shapes.py # Python wrapper for the shapes module
│ └── timing.py # Python wrapper for the timing module
├── examples/ # Usage examples and demonstrations
│ └── [module]_example.py # Usage examples for the module
├── tests/ # Test suite using pytest
│ └── test_[module].py # Unit tests for the module
├── scripts/ # Development scripts
│ └── build.py # Package build automation
├── pyproject.toml # Project configuration and dependencies
├── poetry.lock # Locked dependency versions
└── README.md # This file
Each Python module provides a clean, Pythonic interface to the corresponding C++ component:
| Component | Python Module |
|---|---|
| Algorithms | demo.algorithms |
| Containers | demo.containers |
| Exceptions | demo.exceptions |
| Random | demo.random |
| Shapes | demo.shapes |
| Timing | demo.timing |
The project uses a comprehensive set of tools to maintain code quality:
# All-in-one command to run all quality checks
poetry run pre-commit run --all-files
# Lint with ruff
poetry run ruff check src
# Format with ruff
poetry run ruff format src
# Check types with mypy
poetry run mypy .
# Security analysis with bandit
poetry run bandit -r src