From 9ebfbd52e21e396f69225217af39ea8ae64ee08e Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Wed, 28 Feb 2024 16:42:27 +0100 Subject: [PATCH] add python interface crate #57 --- .python-version | 1 + members/libpt-py/.gitignore | 72 +++++++++++++++++++++++++++++++++ members/libpt-py/Cargo.toml | 18 +++------ members/libpt-py/pyproject.toml | 16 ++++++++ members/libpt-py/src/lib.rs | 15 +++++++ 5 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 .python-version create mode 100644 members/libpt-py/.gitignore create mode 100644 members/libpt-py/pyproject.toml diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..871732e --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +rust diff --git a/members/libpt-py/.gitignore b/members/libpt-py/.gitignore new file mode 100644 index 0000000..af3ca5e --- /dev/null +++ b/members/libpt-py/.gitignore @@ -0,0 +1,72 @@ +/target + +# Byte-compiled / optimized / DLL files +__pycache__/ +.pytest_cache/ +*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +.venv/ +env/ +bin/ +build/ +develop-eggs/ +dist/ +eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +include/ +man/ +venv/ +*.egg-info/ +.installed.cfg +*.egg + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt +pip-selfcheck.json + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.cache +nosetests.xml +coverage.xml + +# Translations +*.mo + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject + +# Rope +.ropeproject + +# Django stuff: +*.log +*.pot + +.DS_Store + +# Sphinx documentation +docs/_build/ + +# PyCharm +.idea/ + +# VSCode +.vscode/ + +# Pyenv +.python-version \ No newline at end of file diff --git a/members/libpt-py/Cargo.toml b/members/libpt-py/Cargo.toml index a924a26..2ebdf45 100644 --- a/members/libpt-py/Cargo.toml +++ b/members/libpt-py/Cargo.toml @@ -1,18 +1,12 @@ [package] name = "libpt-py" -publish.workspace = true -version.workspace = true -edition.workspace = true -authors.workspace = true -license.workspace = true -description.workspace = true -readme.workspace = true -homepage.workspace = true -repository.workspace = true -keywords.workspace = true -categories.workspace = true +version = "0.3.11" +edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "libpt" +crate-type = ["cdylib"] [dependencies] -pyo3 = { version = "0.20.3", features = ["full"] } +pyo3 = { version = "0.19.0", features = ["full", "extension-module"] } diff --git a/members/libpt-py/pyproject.toml b/members/libpt-py/pyproject.toml new file mode 100644 index 0000000..7d42dc0 --- /dev/null +++ b/members/libpt-py/pyproject.toml @@ -0,0 +1,16 @@ +[build-system] +requires = ["maturin>=1.1,<2.0"] +build-backend = "maturin" + +[project] +name = "libpt" +requires-python = ">=3.7" +classifiers = [ + "Programming Language :: Rust", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", +] + + +[tool.maturin] +features = ["pyo3/extension-module"] diff --git a/members/libpt-py/src/lib.rs b/members/libpt-py/src/lib.rs index e69de29..b226085 100644 --- a/members/libpt-py/src/lib.rs +++ b/members/libpt-py/src/lib.rs @@ -0,0 +1,15 @@ +use pyo3::prelude::*; + +/// Formats the sum of two numbers as string. +#[pyfunction] +fn sum_as_string(a: usize, b: usize) -> PyResult { + Ok((a + b).to_string()) +} + +/// A Python module implemented in Rust. +#[pymodule] +#[pyo3(name="libpt")] +fn libpt_py(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; + Ok(()) +}