-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- move python code from init into separate module - refactor tests - bump version to 0.0.10
- Loading branch information
Showing
6 changed files
with
47 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# here we import the contents of our compiled C++ module into this package: | ||
from ._pybind11_numpy_example import * | ||
"""An example of how to use pybind11 and numpy""" | ||
|
||
# here we import the contents of our compiled C++ module | ||
from ._pybind11_numpy_example import * | ||
|
||
# this allows us to also include pure python code in the same namespace: | ||
def pure_python_list(size: int): | ||
return list(range(size)) | ||
# we can also import from python modules as usual: | ||
from .python_code import pure_python_list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def pure_python_list(size: int): | ||
return list(range(size)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,29 @@ | ||
import pybind11_numpy_example as pne | ||
import numpy as np | ||
import pytest | ||
|
||
|
||
def test_pybind11_numpy_example(): | ||
assert pne.pure_python_list(0) == [] | ||
assert pne.pure_python_list(3) == [0, 1, 2] | ||
n_values = [0, 1, 2, 17, 159] | ||
|
||
assert pne.vector_as_list(0) == [] | ||
assert pne.vector_as_list(3) == [0, 1, 2] | ||
|
||
a0 = pne.vector_as_array(0) | ||
assert type(a0) == np.ndarray | ||
assert len(a0) == 0 | ||
assert a0.dtype == np.int16 | ||
a3 = pne.vector_as_array(3) | ||
assert type(a3) == np.ndarray | ||
assert len(a3) == 3 | ||
assert a3.dtype == np.int16 | ||
assert a3[0] == 0 | ||
assert a3[1] == 1 | ||
assert a3[2] == 2 | ||
@pytest.mark.parametrize("list_func", [pne.pure_python_list, pne.vector_as_list]) | ||
@pytest.mark.parametrize("n", n_values) | ||
def test_pybind11_numpy_example_list(list_func, n): | ||
l = list_func(n) | ||
assert isinstance(l, list) | ||
assert len(l) == n | ||
for i in range(n): | ||
assert l[i] == i | ||
|
||
a0 = pne.vector_as_array_nocopy(0) | ||
assert type(a0) == np.ndarray | ||
assert len(a0) == 0 | ||
assert a0.dtype == np.int16 | ||
a3 = pne.vector_as_array_nocopy(3) | ||
assert type(a3) == np.ndarray | ||
assert len(a3) == 3 | ||
assert a3.dtype == np.int16 | ||
assert a3[0] == 0 | ||
assert a3[1] == 1 | ||
assert a3[2] == 2 | ||
|
||
@pytest.mark.parametrize( | ||
"ndarray_func", [pne.vector_as_array, pne.vector_as_array_nocopy] | ||
) | ||
@pytest.mark.parametrize("n", n_values) | ||
def test_pybind11_numpy_example_ndarray(ndarray_func, n): | ||
a = ndarray_func(n) | ||
assert isinstance(a, np.ndarray) | ||
assert len(a) == n | ||
assert a.dtype == np.int16 | ||
for i in range(n): | ||
assert a[i] == i |