Skip to content

Commit e7f894d

Browse files
committed
test(python): add examples and tests for Python bindings
1 parent 44edbff commit e7f894d

13 files changed

+3669
-2
lines changed

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ This project serves as both a learning resource and a reference implementation f
5454
- [Build the project](#build-the-project)
5555
- [Run individual examples](#run-individual-examples)
5656
- [Run tests](#run-tests)
57+
- [Build Python bindings (optional)](#build-python-bindings-optional)
5758
- [CMake Presets](#cmake-presets)
5859
- [Configure Presets](#configure-presets)
5960
- [Build \& Test Presets](#build--test-presets)
@@ -66,11 +67,16 @@ This project serves as both a learning resource and a reference implementation f
6667
- [Run on all files (optional)](#run-on-all-files-optional)
6768
- [What the hooks do](#what-the-hooks-do)
6869
- [🎯 Usage](#-usage)
70+
- [Python Usage](#python-usage)
6971
- [📁 Project Structure](#-project-structure)
7072
- [🔧 Components Overview](#-components-overview)
7173
- [💻 Development Notes](#-development-notes)
7274
- [Code Style](#code-style)
7375
- [Pre-commit Configuration](#pre-commit-configuration)
76+
- [Python Development](#python-development)
77+
- [Testing](#testing)
78+
- [Examples and Documentation](#examples-and-documentation)
79+
- [Code Quality](#code-quality)
7480
- [📄 License](#-license)
7581

7682
## 🎓 What You'll Learn
@@ -141,6 +147,14 @@ ctest --preset release
141147
```bash
142148
cmake --preset release -DBUILD_PYTHON_BINDINGS=ON
143149
cmake --build --preset release
150+
151+
# Test Python bindings (optional)
152+
cd python_tests
153+
python -m pytest --verbose
154+
155+
# Run Python examples (optional)
156+
python python_examples/basic_usage.py
157+
python python_examples/advanced_usage.py
144158
```
145159

146160
### CMake Presets
@@ -351,8 +365,29 @@ cpp-demo-project/
351365
├── src/ # Source implementation files
352366
│ ├── CMakeLists.txt # Components configuration
353367
│ └── [mirrors include structure]
354-
├── examples/ # Usage examples and demonstrations
355-
├── tests/ # Test suite using Catch2 v3
368+
├── binding/ # pybind11 C++ binding files
369+
│ ├── CMakeLists.txt # Python bindings configuration
370+
│ ├── cpp_features.cpp # Main pybind11 module
371+
│ └── [module]_binding.cpp # Individual module bindings
372+
├── python/ # Python wrapper modules
373+
│ ├── __init__.py # Package initialization
374+
│ ├── shapes.py # Enhanced shape functionality
375+
│ ├── containers.py # Pythonic container wrappers
376+
│ ├── algorithms.py # Functional programming utilities
377+
│ ├── exceptions.py # Result types and error handling
378+
│ ├── memory.py # RAII and resource management
379+
│ ├── timing.py # High-resolution timing utilities
380+
│ └── random.py # Enhanced random number generation
381+
├── python_tests/ # Python test suite using pytest
382+
│ ├── __init__.py # Test package initialization
383+
│ ├── conftest.py # pytest configuration and fixtures
384+
│ └── test_[module].py # Comprehensive module tests
385+
├── python_examples/ # Python usage examples
386+
│ ├── README.md # Examples documentation
387+
│ ├── basic_usage.py # Fundamental usage patterns
388+
│ └── advanced_usage.py # Advanced and real-world examples
389+
├── examples/ # C++ usage examples and demonstrations
390+
├── tests/ # C++ test suite using Catch2 v3
356391
├── .clang-format # clang-format configuration (for C++ code formatting)
357392
├── .clang-tidy # clang-tidy configuration (for static analysis)
358393
├── .clangd # clangd configuration (for code completion)
@@ -433,6 +468,31 @@ repos:
433468
- Enforced coding standards for all contributors
434469
- Integration with modern formatting tools
435470
471+
### Python Development
472+
473+
The project includes comprehensive Python bindings and tooling:
474+
475+
#### Testing
476+
477+
- **pytest-based test suite**: Located in `python_tests/` with comprehensive coverage
478+
- **C++ test pattern adoption**: Python tests mirror C++ test structure and patterns
479+
- **Fixtures and configuration**: Shared test utilities in `conftest.py`
480+
- **Type safety testing**: Verifies type annotations and generic behavior
481+
482+
#### Examples and Documentation
483+
484+
- **Basic examples**: `python_examples/basic_usage.py` demonstrates fundamental usage
485+
- **Advanced examples**: `python_examples/advanced_usage.py` shows real-world patterns
486+
- **Comprehensive documentation**: Numpy-style docstrings throughout
487+
- **Modern Python features**: Extensive use of Python 3.13 features like pattern matching
488+
489+
#### Code Quality
490+
491+
- **Black formatting**: Consistent Python code style
492+
- **Type annotations**: Full type hint coverage with modern typing
493+
- **Error handling**: Result types for functional error management
494+
- **Performance integration**: Built-in timing and benchmarking utilities
495+
436496
## 📄 License
437497

438498
This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details.

python_examples/README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Python Examples
2+
3+
This directory contains comprehensive examples demonstrating the Python bindings for the C++ features library.
4+
5+
## Examples Overview
6+
7+
### `basic_usage.py`
8+
9+
Demonstrates fundamental usage of each module:
10+
11+
- **Shapes**: Creating circles, rectangles, and squares with analysis
12+
- **Containers**: Type-safe containers with filtering and transformation
13+
- **Algorithms**: Sorting, counting, transforming, and functional chains
14+
- **Memory**: RAII-style resource management with context managers
15+
- **Exceptions**: Result types for safe error handling
16+
- **Timing**: High-resolution timing and benchmarking
17+
- **Random**: Type-safe random number generation with distributions
18+
- **Integration**: Cross-module usage patterns
19+
20+
### `advanced_usage.py`
21+
22+
Shows advanced patterns and real-world use cases:
23+
24+
- **ShapeAnalyzer**: Performance-monitored shape collection analysis
25+
- **DataProcessor**: Complex data processing pipelines with error handling
26+
- **PerformanceBenchmarkSuite**: Comprehensive performance testing
27+
- **Monte Carlo π Estimation**: Statistical computation example
28+
- **Functional Programming**: Advanced chain operations and pipelines
29+
- **Real-World Simulation**: Virtual environment analysis
30+
31+
## Running the Examples
32+
33+
### Prerequisites
34+
35+
1. Build the C++ library with Python bindings enabled:
36+
37+
```bash
38+
cmake -DBUILD_PYTHON_BINDINGS=ON -B build
39+
cmake --build build
40+
```
41+
42+
2. Ensure the built Python extension is available (typically in `build/binding/`)
43+
44+
3. Install pytest for running tests:
45+
46+
```bash
47+
pip install pytest
48+
```
49+
50+
### Running Examples
51+
52+
```bash
53+
# Run basic usage examples
54+
python python_examples/basic_usage.py
55+
56+
# Run advanced usage examples
57+
python python_examples/advanced_usage.py
58+
```
59+
60+
## Key Features Demonstrated
61+
62+
### Python 3.13 Features
63+
64+
- **Pattern Matching**: Used in factory functions and type dispatch
65+
- **Enhanced Type Hints**: Modern typing with generics and unions
66+
- **Dataclasses**: Immutable data structures for metrics
67+
- **Context Managers**: RAII-like resource management
68+
- **Enum Classes**: Type-safe enumerations
69+
70+
### Modern Python Patterns
71+
72+
- **Functional Programming**: Method chaining and pipeline composition
73+
- **Error Handling**: Result types instead of exceptions
74+
- **Performance Monitoring**: Integrated timing and benchmarking
75+
- **Type Safety**: Generic containers and proper type annotations
76+
- **Resource Management**: Automatic cleanup with context managers
77+
78+
### C++ Integration
79+
80+
- **Zero-Copy Operations**: Direct C++ container access where possible
81+
- **Exception Translation**: C++ exceptions to Python Result types
82+
- **Memory Safety**: RAII patterns adapted to Python
83+
- **Performance**: Leveraging C++ performance for computationally intensive operations
84+
85+
## Code Style
86+
87+
The examples follow modern Python conventions:
88+
89+
- **Black formatting**: Consistent code style
90+
- **Single quotes**: For string literals
91+
- **Numpy-style docstrings**: Comprehensive documentation
92+
- **Type hints**: Full type annotation coverage
93+
- **Error handling**: Graceful error management with Result types
94+
95+
## Example Output
96+
97+
When run successfully, the examples demonstrate:
98+
99+
- Shape creation and analysis with geometric calculations
100+
- Container operations with filtering and transformation
101+
- Algorithm performance on different data sets
102+
- Memory management with automatic cleanup
103+
- Error handling with safe operations
104+
- High-resolution timing measurements
105+
- Random number generation with statistical properties
106+
- Cross-module integration patterns
107+
108+
The advanced examples additionally show:
109+
110+
- Performance benchmarking across different scenarios
111+
- Real-world simulation with complex data processing
112+
- Statistical computation (Monte Carlo methods)
113+
- Advanced functional programming patterns
114+
- Comprehensive error handling strategies

0 commit comments

Comments
 (0)