Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.19)
project(bonacci_bindings LANGUAGES CXX)

find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)

execute_process(
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_ROOT
COMMAND_ERROR_IS_FATAL ANY)

find_package(nanobind CONFIG REQUIRED)

nanobind_add_module(sequence src/bonacci_bindings/sequence.cpp)
install(TARGETS sequence DESTINATION bonacci_bindings)
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Python Starter

A minimalist template for starting a new [Python](https://www.python.org/) project.
A minimalist template for starting a new [Python](https://www.python.org/) project with C++ bindings.

This template provides a basic Python project containing an example package with built-in support for formatting, linting, testing, and continuous integration.

## Key Features

- Uses [uv](https://docs.astral.sh/uv/) as the package manager.
- Supports C++ bindings using [nanobind](https://nanobind.readthedocs.io/).
- Supports formatting and linting with [dprint](https://dprint.dev/) and [Ruff](https://github.com/astral-sh/ruff).
- Supports testing and coverage checks with [Pytest](https://docs.pytest.org/en/stable/).
- Fixes formatting and linting issues during pre-commit hooks using [Lefthook](https://lefthook.dev/).
Expand Down Expand Up @@ -60,6 +61,11 @@ Modify the source files under the [`src`](./src) directory to start writing the

You can replace the [`src/bonacci`](./src/bonacci) directory with your package name. You can also add as many packages as you want to the `src` directory. Just make sure to update the contents of the [`pyproject.toml`](./pyproject.toml) file according to your package information.

#### Writing C++ Extensions

This template includes C++ bindings using [nanobind](https://nanobind.readthedocs.io/).
Replace the [`src/bonacci_bindings`](./src/bonacci_bindings) with your C++ extension package name. When adding new C++ source files, make sure to update the [`CMakeLists.txt`](./CMakeLists.txt) file. For more information on nanobind, refer to [this documentation](https://nanobind.readthedocs.io/en/latest/).

#### Testing the Package

Test files in this template are named `test_*.py` and located in the [`tests`](./tests) directory. Write the necessary tests for your package and run them with:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ repository = "https://github.com/leonardo/bonacci.git"
issues = "https://github.com/leonardo/bonacci/issues"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
requires = ["scikit-build-core>=0.11.6", "nanobind>=2.9.2"]
build-backend = "scikit_build_core.build"

[dependency-groups]
dev = [
Expand Down
2 changes: 1 addition & 1 deletion src/bonacci/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Example Python package for generating Fibonacci sequences."""

from .sequence import fibonacci_sequence
from bonacci_bindings.sequence import fibonacci_sequence

__all__ = ["fibonacci_sequence"]
18 changes: 0 additions & 18 deletions src/bonacci/sequence.py

This file was deleted.

23 changes: 23 additions & 0 deletions src/bonacci_bindings/sequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <nanobind/nanobind.h>
#include <nanobind/stl/vector.h>

#include <algorithm>
#include <vector>

std::vector<int> fibonacci_sequence(int n) {
std::vector<int> sequence(std::max(n, 0), 1);
for (int i{2}; i < n; ++i) {
sequence[i] = sequence[i - 2] + sequence[i - 1];
}
return sequence;
}

namespace nb = nanobind;

NB_MODULE(sequence, m) {
m.doc() = "Example functions for generating Fibonacci sequences.";

m.def(
"fibonacci_sequence", &fibonacci_sequence,
"Generate a Fibonacci sequence up to the given number of terms");
}