https://github.com/virgesmith/pybind11-examples
https://github.com/virgesmith/pybind11-examples
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/virgesmith/pybind11-examples
- Owner: virgesmith
- License: mit
- Created: 2024-03-02T12:51:04.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-19T08:45:01.000Z (over 1 year ago)
- Last Synced: 2025-08-21T02:47:06.433Z (10 months ago)
- Language: Python
- Size: 72.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# pybind11-examples
A python extension module written in C++ with pybind11 bindings, containing implementations of various constructs, including:
- [X] generators, including `send`, `close`, and `throw` methods
- [X] `__init_subclass__` - using it to register subclasses
- [X] decorators, simple and parameterised
- [X] a context manager
- [X] some prime number stuff, for performance comparison aginst equivalent python and rust implementations.
- [X] polymorphism across C++ and python (subclassing C++ classes in python, calling python overrides through a C++ interface)
- [X] ~~dynamic immutable values wrapped in a `Constants` singleton that can be trivially reassigned, so it's pointless.~~
- [X] C and C++ enumerations
- [X] vectorised functions with a performance comparison against python and vectorised numpy
- [X] type annotations for C++ implementations
Most of the code originated in [this](http://github.com/virgesmith/poetry-pybind11-integration) project which was originally intended to demonstate how to make pybind11 work with poetry. The project evolved and became more about:
- how to implement certain python constructs in C++
- performance comparison between python modules implemented in python, [rust](https://github.com/virgesmith/poetry-rust-integration/) and C++
# usage
Build and install the package:
```sh
pip install -e .[dev]
```
Test:
```sh
pytest
```
Performance (vectorisation comparison):
```sh
python test/test_vectorised.py
```
Use, e.g.:
```py
from pybind11_examples import FibGenerator, Collatz
fg = FibGenerator()
print([next(fg) for _ in range(10)])
print(list(Collatz(19)))
```
## Type annotations
Type stubs are generated based on the signatures and docstrings in [module.cpp](src/module.cpp), using the `pybind11-stubgen` package directly on the C++ shared object, like so:
```
pybind11-stubgen _pybind11_examples
```