Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pyexpr remove evaluate many #314

Merged
merged 1 commit into from
Feb 10, 2025
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
45 changes: 2 additions & 43 deletions bindings/python/src/expression.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use crate::variable::PyVariable;
use anyhow::{anyhow, Context};
use either::Either;
use pyo3::types::{PyDict, PyList};
use pyo3::{
pyclass, pyfunction, pymethods, Bound, IntoPyObject, IntoPyObjectExt, Py, PyAny, PyErr,
PyResult, Python,
};
use pyo3::types::PyDict;
use pyo3::{pyclass, pyfunction, pymethods, Bound, IntoPyObjectExt, Py, PyAny, PyResult, Python};
use pythonize::depythonize;
use zen_expression::expression::{Standard, Unary};
use zen_expression::vm::VM;
use zen_expression::{Expression, Variable};

#[pyfunction]
Expand Down Expand Up @@ -100,41 +96,4 @@ impl PyExpression {

PyVariable(result).into_py_any(py)
}

pub fn evaluate_many(&self, py: Python, ctx: &Bound<'_, PyList>) -> PyResult<Py<PyAny>> {
let contexts: Vec<Variable> = depythonize(ctx).context("Failed to convert contexts")?;

let mut vm = VM::new();
let results: Vec<_> = contexts
.into_iter()
.map(|context| {
let result = match &self.expression {
Either::Left(standard) => standard.evaluate_with(context, &mut vm),
Either::Right(unary) => {
unary.evaluate_with(context, &mut vm).map(Variable::Bool)
}
};

match result {
Ok(ok) => Either::Left(PyVariable(ok)),
Err(err) => Either::Right(PyExpressionError(err.to_string())),
}
})
.collect();

results.into_py_any(py)
}
}

struct PyExpressionError(String);

impl<'py> IntoPyObject<'py> for PyExpressionError {
type Target = PyAny;
type Output = Bound<'py, PyAny>;
type Error = PyErr;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let err = pyo3::exceptions::PyException::new_err(self.0);
err.into_bound_py_any(py)
}
}
9 changes: 0 additions & 9 deletions bindings/python/zen.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,5 @@ def compile_expression(expression: str) -> Expression: ...

def compile_unary_expression(expression: str) -> Expression: ...


class ExpressionResult(TypedDict):
success: bool
result: Optional[Any]
error: Optional[str]


class Expression:
def evaluate(self, ctx: Optional[dict] = None) -> Any: ...

def evaluate_many(self, ctxs: list[dict]) -> list[ExpressionResult]: ...