From a16009be00b3ca33adf5fa43bde1e941dfcdce98 Mon Sep 17 00:00:00 2001 From: stefan-gorules <127550877+stefan-gorules@users.noreply.github.com> Date: Mon, 10 Feb 2025 13:01:34 +0100 Subject: [PATCH] feat: pyexpr remove evaluate many (#314) --- bindings/python/src/expression.rs | 45 ++----------------------------- bindings/python/zen.pyi | 9 ------- 2 files changed, 2 insertions(+), 52 deletions(-) diff --git a/bindings/python/src/expression.rs b/bindings/python/src/expression.rs index 602368b2..1e2f982a 100644 --- a/bindings/python/src/expression.rs +++ b/bindings/python/src/expression.rs @@ -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] @@ -100,41 +96,4 @@ impl PyExpression { PyVariable(result).into_py_any(py) } - - pub fn evaluate_many(&self, py: Python, ctx: &Bound<'_, PyList>) -> PyResult> { - let contexts: Vec = 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 { - let err = pyo3::exceptions::PyException::new_err(self.0); - err.into_bound_py_any(py) - } } diff --git a/bindings/python/zen.pyi b/bindings/python/zen.pyi index e18dcf52..b1791e4d 100644 --- a/bindings/python/zen.pyi +++ b/bindings/python/zen.pyi @@ -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]: ...