Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Python Bindings for Modern C++ Features Demo Project

Build & Test Python Poetry pybind11 Coverage Type Hints License

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.

🚀 Features

Core Features

  • 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

Code Quality & Development

  • Automatic Formatting: Pre-commit hooks with ruff format
  • Linting: Pre-commit hooks with ruff check and bandit for code style and security checks
  • Type Checking: Full mypy support 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

📋 Table of Contents

🛠️ Installation

Prerequisites

  • Python 3.12+
  • Poetry (for dependency management)

Quick Start

Build the C++ extensions

cd ..  # From root directory
cmake --preset release-python
cmake --build --preset release-python

Prepare the environment

cd python

# Install all dependencies
poetry install

# Activate the virtual environment
eval $(poetry env activate)

Build and install the package

# Build the package
python3 scripts/build.py

# Install the package
pip3 install dist/demo_python-1.1.0-py3-none-any.whl

Run examples

python3 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.py

Run tests

poetry run pytest

🎯 Usage

from 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.

📁 Project Structure

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

🔧 Components Overview

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

💻 Development Notes

Code Quality

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