mirror of
https://github.com/gorules/zen.git
synced 2026-10-04 00:02:18 +00:00
feat: pyexpr remove evaluate many (#314)
This commit is contained in:
@@ -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<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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]: ...
|
||||
|
||||
Reference in New Issue
Block a user