Skip to content

Commit

Permalink
feat: pyexpr remove evaluate many (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-gorules authored Feb 10, 2025
1 parent 1036bd7 commit a16009b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 52 deletions.
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]: ...

0 comments on commit a16009b

Please sign in to comment.