Skip to content

Commit

Permalink
add unparse
Browse files Browse the repository at this point in the history
  • Loading branch information
bertiqwerty committed Jul 26, 2024
1 parent 79bc9e4 commit 4199342
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions rormula-rs/tests/test_rormula.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,15 @@ fn test_restrict() {
assert!(false);
}
}

#[test]
fn test_binop() {
let s = "4/3 * a / b * (1.3 / 112.12 / ((21.0+x+y+z) / 2000))";
let exp = ExprArithmetic::<ColMajor>::parse(s).unwrap();
let a = exp.operator_reprs();
assert_eq!(a.len(), 3);
assert_eq!(a.as_ref(), &["*", "+", "/"]);
let b = exp.binary_reprs();
assert_eq!(b.len(), 3);
assert_eq!(b.as_ref(), &["*", "+", "/"]);
}
3 changes: 3 additions & 0 deletions rormula/rormula/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,8 @@ def eval_asdf(self, data: pd.DataFrame):
data = pd.DataFrame(data=resulting_data, columns=[self.name])
return data

def unparse(self) -> str:
return self.ror.unparse()

def has_row_change_op(self) -> bool:
return self.ror.has_row_change_op()
1 change: 1 addition & 0 deletions rormula/rormula/rormula.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def eval_wilkinson(

class Arithmetic:
def has_row_change_op(self) -> bool: ...
def unparse(self) -> str: ...

def parse_arithmetic(s: str) -> Arithmetic: ...
def eval_arithmetic(
Expand Down
3 changes: 3 additions & 0 deletions rormula/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ impl Arithmetic {
pub fn has_row_change_op(&self) -> PyResult<bool> {
Ok(has_row_change_op(&self.expr))
}
pub fn unparse(&self) -> PyResult<String> {
Ok(self.expr.unparse().to_string())
}
}

#[derive(Debug)]
Expand Down
1 change: 1 addition & 0 deletions rormula/test/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def eval_asdf():
res = rormula.eval_asdf(df)
assert np.allclose(res.to_numpy().item(), (5.0 - 2.5) / 4.0)
assert rormula.has_row_change_op()
assert rormula.unparse() == s

def test_unary(repr: str, np_func):
s = f"{repr}( ((first_var|{{second.var}}==5.0) - (first_var|{{second.var}}==2.5)) / 4.0)"
Expand Down

0 comments on commit 4199342

Please # to comment.