feat: add python bindings for argus
This commit is contained in:
parent
2b447409a1
commit
f8ec8857d4
5 changed files with 416 additions and 6 deletions
181
.gitignore
vendored
181
.gitignore
vendored
|
|
@ -1,11 +1,10 @@
|
|||
# Created by https://www.toptal.com/developers/gitignore/api/rust
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=rust
|
||||
|
||||
# Created by https://www.toptal.com/developers/gitignore/api/rust,python
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=rust,python
|
||||
### Rust ###
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
/target
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
|
|
@ -17,4 +16,176 @@ Cargo.lock
|
|||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/rust
|
||||
### Python ###
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
.pdm.toml
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
### Python Patch ###
|
||||
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
|
||||
poetry.toml
|
||||
|
||||
# ruff
|
||||
.ruff_cache/
|
||||
|
||||
# LSP config files
|
||||
pyrightconfig.json
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/rust,python
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
[workspace]
|
||||
members = ["argus", "argus-core", "argus-semantics"]
|
||||
members = ["argus", "argus-core", "argus-semantics", "pyargus"]
|
||||
|
|
|
|||
13
pyargus/Cargo.toml
Normal file
13
pyargus/Cargo.toml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "pyargus"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[lib]
|
||||
name = "pyargus"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
argus-core = { version = "0.1.0", path = "../argus-core" }
|
||||
pyo3 = "0.18.1"
|
||||
16
pyargus/pyproject.toml
Normal file
16
pyargus/pyproject.toml
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
[build-system]
|
||||
requires = ["maturin>=0.14,<0.15"]
|
||||
build-backend = "maturin"
|
||||
|
||||
[project]
|
||||
name = "pyargus"
|
||||
requires-python = ">=3.7"
|
||||
classifiers = [
|
||||
"Programming Language :: Rust",
|
||||
"Programming Language :: Python :: Implementation :: CPython",
|
||||
"Programming Language :: Python :: Implementation :: PyPy",
|
||||
]
|
||||
|
||||
|
||||
[tool.maturin]
|
||||
features = ["pyo3/extension-module"]
|
||||
210
pyargus/src/lib.rs
Normal file
210
pyargus/src/lib.rs
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
use argus_core::expr::{BoolExpr, NumExpr};
|
||||
use argus_core::prelude::*;
|
||||
use pyo3::prelude::*;
|
||||
|
||||
#[pyclass(name = "NumExpr", subclass)]
|
||||
#[derive(Clone)]
|
||||
struct PyNumExpr(Box<NumExpr>);
|
||||
|
||||
#[pymethods]
|
||||
impl PyNumExpr {
|
||||
fn __repr__(&self) -> String {
|
||||
format!("{:?}", &self.0)
|
||||
}
|
||||
|
||||
fn __neg__(&self) -> PyResult<Py<Negate>> {
|
||||
Python::with_gil(|py| Py::new(py, Negate::new(self.clone())))
|
||||
}
|
||||
|
||||
fn __add__(&self, other: &Self) -> PyResult<Py<Add>> {
|
||||
Python::with_gil(|py| Py::new(py, Add::new(vec![self.clone(), other.clone()])))
|
||||
}
|
||||
|
||||
fn __mul__(&self, other: &Self) -> PyResult<Py<Mul>> {
|
||||
Python::with_gil(|py| Py::new(py, Mul::new(vec![self.clone(), other.clone()])))
|
||||
}
|
||||
|
||||
fn __truediv__(&self, other: &Self) -> PyResult<Py<Div>> {
|
||||
Python::with_gil(|py| Py::new(py, Div::new(self.clone(), other.clone())))
|
||||
}
|
||||
|
||||
fn __abs__(&self) -> PyResult<Py<Abs>> {
|
||||
Python::with_gil(|py| Py::new(py, Abs::new(self.clone())))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct ConstInt;
|
||||
|
||||
#[pymethods]
|
||||
impl ConstInt {
|
||||
#[new]
|
||||
fn new(val: i64) -> (Self, PyNumExpr) {
|
||||
(Self, PyNumExpr(Box::new(NumExpr::IntLit(val))))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct ConstUInt;
|
||||
|
||||
#[pymethods]
|
||||
impl ConstUInt {
|
||||
#[new]
|
||||
fn new(val: u64) -> (Self, PyNumExpr) {
|
||||
(Self, PyNumExpr(Box::new(NumExpr::UIntLit(val))))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct ConstFloat;
|
||||
|
||||
#[pymethods]
|
||||
impl ConstFloat {
|
||||
#[new]
|
||||
fn new(val: f64) -> (Self, PyNumExpr) {
|
||||
(Self, PyNumExpr(Box::new(NumExpr::FloatLit(val))))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct VarInt;
|
||||
|
||||
#[pymethods]
|
||||
impl VarInt {
|
||||
#[new]
|
||||
fn new(name: String) -> (Self, PyNumExpr) {
|
||||
(Self, PyNumExpr(Box::new(NumExpr::IntVar { name })))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct VarUInt;
|
||||
|
||||
#[pymethods]
|
||||
impl VarUInt {
|
||||
#[new]
|
||||
fn new(name: String) -> (Self, PyNumExpr) {
|
||||
(Self, PyNumExpr(Box::new(NumExpr::UIntVar { name })))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct VarFloat;
|
||||
|
||||
#[pymethods]
|
||||
impl VarFloat {
|
||||
#[new]
|
||||
fn new(name: String) -> (Self, PyNumExpr) {
|
||||
(Self, PyNumExpr(Box::new(NumExpr::FloatVar { name })))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct Negate;
|
||||
|
||||
#[pymethods]
|
||||
impl Negate {
|
||||
#[new]
|
||||
fn new(arg: PyNumExpr) -> (Self, PyNumExpr) {
|
||||
let arg = arg.0;
|
||||
(Self, PyNumExpr(Box::new(NumExpr::Neg { arg })))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct Add;
|
||||
|
||||
#[pymethods]
|
||||
impl Add {
|
||||
#[new]
|
||||
fn new(args: Vec<PyNumExpr>) -> (Self, PyNumExpr) {
|
||||
let args: Vec<NumExpr> = args.into_iter().map(|arg| *arg.0).collect();
|
||||
(Self, PyNumExpr(Box::new(NumExpr::Add { args })))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct Sub;
|
||||
|
||||
#[pymethods]
|
||||
impl Sub {
|
||||
#[new]
|
||||
fn new(lhs: PyNumExpr, rhs: PyNumExpr) -> (Self, PyNumExpr) {
|
||||
let lhs = lhs.0;
|
||||
let rhs = rhs.0;
|
||||
(Self, PyNumExpr(Box::new(NumExpr::Sub { lhs, rhs })))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct Mul;
|
||||
|
||||
#[pymethods]
|
||||
impl Mul {
|
||||
#[new]
|
||||
fn new(args: Vec<PyNumExpr>) -> (Self, PyNumExpr) {
|
||||
let args: Vec<NumExpr> = args.into_iter().map(|arg| *arg.0).collect();
|
||||
(Self, PyNumExpr(Box::new(NumExpr::Mul { args })))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct Div;
|
||||
|
||||
#[pymethods]
|
||||
impl Div {
|
||||
#[new]
|
||||
fn new(dividend: PyNumExpr, divisor: PyNumExpr) -> (Self, PyNumExpr) {
|
||||
let dividend = dividend.0;
|
||||
let divisor = divisor.0;
|
||||
(Self, PyNumExpr(Box::new(NumExpr::Div { dividend, divisor })))
|
||||
}
|
||||
}
|
||||
|
||||
// #[pyclass(extends=PyNumExpr)]
|
||||
// struct Pow;
|
||||
//
|
||||
// #[pymethods]
|
||||
// impl Pow {
|
||||
// #[new]
|
||||
// fn new(base: PyNumExpr, exponent: PyNumExpr) -> (Self, PyNumExpr) {
|
||||
// let base = base.0;
|
||||
// let exponent = exponent.0;
|
||||
// (Self, PyNumExpr(Box::new(NumExpr:: { base, exponent })))
|
||||
// }
|
||||
// }
|
||||
|
||||
#[pyclass(extends=PyNumExpr)]
|
||||
struct Abs;
|
||||
|
||||
#[pymethods]
|
||||
impl Abs {
|
||||
#[new]
|
||||
fn new(arg: PyNumExpr) -> (Self, PyNumExpr) {
|
||||
let arg = arg.0;
|
||||
(Self, PyNumExpr(Box::new(NumExpr::Abs { arg })))
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass(name = "BoolExpr")]
|
||||
struct PyBoolExpr(BoolExpr);
|
||||
|
||||
#[pymodule]
|
||||
fn pyargus(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<PyNumExpr>()?;
|
||||
m.add_class::<ConstInt>()?;
|
||||
m.add_class::<ConstUInt>()?;
|
||||
m.add_class::<ConstFloat>()?;
|
||||
m.add_class::<VarInt>()?;
|
||||
m.add_class::<VarUInt>()?;
|
||||
m.add_class::<VarFloat>()?;
|
||||
m.add_class::<Negate>()?;
|
||||
m.add_class::<Add>()?;
|
||||
m.add_class::<Mul>()?;
|
||||
m.add_class::<Div>()?;
|
||||
// m.add_class::<Pow>()?;
|
||||
|
||||
m.add_class::<PyBoolExpr>()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue