Skip to content

Commit 80f7acf

Browse files
committed
Add example for NumPy and FindPython.
1 parent 8fdbc8a commit 80f7acf

File tree

8 files changed

+118
-0
lines changed

8 files changed

+118
-0
lines changed

projects/hello-numpy/CMakeLists.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.18...3.22)
2+
3+
project(hello-numpy VERSION "0.1")
4+
5+
# Define CMAKE_INSTALL_xxx: LIBDIR, INCLUDEDIR
6+
include(GNUInstallDirs)
7+
include(FetchContent)
8+
9+
find_package(Python COMPONENTS Development NumPy)
10+
11+
set(python_module_name _hello)
12+
pybind11_add_module(${python_module_name} MODULE
13+
src/hello/hello_py.cpp
14+
)
15+
16+
install(TARGETS ${python_module_name} DESTINATION .)
17+
18+
# Quiet a warning, since this project is only valid with SKBUILD
19+
set(ignoreMe "${SKBUILD}")

projects/hello-numpy/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# PyBind11 + Scikit Build example
2+
3+
4+
## Building
5+
6+
To build, you must have pip 10 or greater, *or* you need to manually install
7+
`scikit-build` and `cmake`. Once you create a wheel, that wheel can be used in
8+
earlier versions of pip.
9+
10+
Example build and install sequence:
11+
12+
```bash
13+
pip install .
14+
python -c "import hello; hello.hello()"
15+
```
16+
17+
This should print "Hello, World!".
18+
19+
## Testing
20+
21+
Testing is managed by tox. This will build the package in a temp directory and runs the tests in the test dir.
22+
23+
```shell
24+
tox
25+
```

projects/hello-numpy/pyproject.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[build-system]
2+
requires = [
3+
"setuptools",
4+
"scikit-build>=0.15",
5+
"cmake",
6+
"ninja",
7+
"numpy>=1.21",
8+
]
9+
build-backend = "setuptools.build_meta"
10+
11+
[tool.pytest.ini_options]
12+
testpaths = ["tests"]

projects/hello-numpy/setup.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from skbuild import setup
2+
3+
setup(
4+
name="hello-numpy",
5+
version="1.2.3",
6+
description="a minimal example package (with pybind11 and NumPy)",
7+
author='Hameer Abbasi',
8+
license="MIT",
9+
packages=['hello'],
10+
package_dir={'': 'src'},
11+
cmake_install_dir='src/hello',
12+
python_requires='>=3.7',
13+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from ._hello import hello, return_two
2+
3+
4+
__all__ = ("hello", "return_two")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <pybind11/pybind11.h>
3+
#include <numpy/arrayobject.h>
4+
5+
namespace py = pybind11;
6+
7+
void hello() {
8+
std::cout << "Hello, World!" << std::endl;
9+
}
10+
11+
py::object zeros2x2() {
12+
return py::reinterpret_steal<py::object>(
13+
PyArray_ZEROS(2, {2, 2}, NPY_FLOAT64, 0);
14+
);
15+
}
16+
17+
PYBIND11_MODULE(_hello, m) {
18+
m.doc() = "_hello";
19+
m.def("hello", &hello, "Prints \"Hello, World!\"");
20+
m.def("zeros2x2", &zeros2x2, "Returns a 2x2 array of zeros.");
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This kind of import is automatically done when importing hello from outside
2+
import hello
3+
import unittest
4+
import numpy as np
5+
6+
7+
class TestHello(unittest.TestCase):
8+
def test_hello(self):
9+
hello.hello()
10+
11+
def test_return_two(self):
12+
expected = np.zeros((2, 2), dtype=np.float64)
13+
actual = hello.zeros2x2()
14+
self.assertEqual(expected.dtype, actual.dtype)
15+
self.assert_(np.all(expected == actual))
16+
17+
18+
if __name__ == "__main__":
19+
unittest.main()
20+
# You can run all python test with:
21+
# ctest -R python -V
22+
# from the build folder

projects/hello-numpy/tox.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[testenv]
2+
commands = python -m unittest discover -s tests/

0 commit comments

Comments
 (0)