Skip to content

Commit

Permalink
Add sdist to pypi upload
Browse files Browse the repository at this point in the history
- move python code from init into separate module
- refactor tests
- bump version to 0.0.10
  • Loading branch information
lkeegan committed Jan 12, 2024
1 parent f30312c commit a38566d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 36 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,23 @@ jobs:

- uses: actions/upload-artifact@v4
with:
name: wheels-${{ matrix.os }}
name: cibw-wheels-${{ matrix.os }}
path: ./wheelhouse/*.whl

build-sdist:
name: Build source distribution
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Build sdist
run: pipx run build --sdist

- uses: actions/upload-artifact@v4
with:
name: cibw-sdist
path: dist/*.tar.gz

upload_pypi:
needs: [build-wheels]
runs-on: ubuntu-latest
Expand All @@ -30,7 +44,7 @@ jobs:
steps:
- uses: actions/download-artifact@v4
with:
pattern: wheels-*
pattern: cibw-*
merge-multiple: true
path: dist

Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.15..3.26)
# Set a name and a version number for your project:
project(
pybind11-numpy-example
VERSION 0.0.9
VERSION 0.0.10
LANGUAGES CXX)

# Initialize some default paths
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"

[project]
name = "pybind11-numpy-example"
version = "0.0.9"
version = "0.0.10"
description = "An example of using numpy with pybind11"
readme = "README.md"
license = {text = "MIT"}
Expand Down
10 changes: 5 additions & 5 deletions src/pybind11_numpy_example/__init__.py
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
2 changes: 2 additions & 0 deletions src/pybind11_numpy_example/python_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def pure_python_list(size: int):
return list(range(size))
49 changes: 22 additions & 27 deletions tests/python/test_pybind11_numpy_example.py
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

0 comments on commit a38566d

Please sign in to comment.