|
31 | 31 | from numpy import shape, allclose, array_equal, ravel, isnan, isinf
|
32 | 32 |
|
33 | 33 | import numexpr
|
34 |
| -from numexpr import E, NumExpr, evaluate, re_evaluate, disassemble, use_vml |
| 34 | +from numexpr import E, NumExpr, evaluate, re_evaluate, validate, disassemble, use_vml |
35 | 35 | from numexpr.expressions import ConstantNode
|
36 | 36 |
|
37 | 37 | import unittest
|
@@ -370,10 +370,38 @@ def test_re_evaluate(self):
|
370 | 370 | assert_array_equal(x, array([86., 124., 168.]))
|
371 | 371 |
|
372 | 372 | def test_re_evaluate_dict(self):
|
| 373 | + a1 = array([1., 2., 3.]) |
| 374 | + b1 = array([4., 5., 6.]) |
| 375 | + c1 = array([7., 8., 9.]) |
| 376 | + x = evaluate("2*a + 3*b*c", local_dict={'a': a1, 'b': b1, 'c': c1}) |
| 377 | + x = re_evaluate() |
| 378 | + assert_array_equal(x, array([86., 124., 168.])) |
| 379 | + |
| 380 | + def test_validate(self): |
373 | 381 | a = array([1., 2., 3.])
|
374 | 382 | b = array([4., 5., 6.])
|
375 | 383 | c = array([7., 8., 9.])
|
376 |
| - x = evaluate("2*a + 3*b*c", local_dict={'a': a, 'b': b, 'c': c}) |
| 384 | + retval = validate("2*a + 3*b*c") |
| 385 | + assert(retval is None) |
| 386 | + x = re_evaluate() |
| 387 | + assert_array_equal(x, array([86., 124., 168.])) |
| 388 | + |
| 389 | + def test_validate_missing_var(self): |
| 390 | + a = array([1., 2., 3.]) |
| 391 | + b = array([4., 5., 6.]) |
| 392 | + retval = validate("2*a + 3*b*c") |
| 393 | + assert(isinstance(retval, KeyError)) |
| 394 | + |
| 395 | + def test_validate_syntax(self): |
| 396 | + retval = validate("2+") |
| 397 | + assert(isinstance(retval, SyntaxError)) |
| 398 | + |
| 399 | + def test_validate_dict(self): |
| 400 | + a1 = array([1., 2., 3.]) |
| 401 | + b1 = array([4., 5., 6.]) |
| 402 | + c1 = array([7., 8., 9.]) |
| 403 | + retval = validate("2*a + 3*b*c", local_dict={'a': a1, 'b': b1, 'c': c1}) |
| 404 | + assert(retval is None) |
377 | 405 | x = re_evaluate()
|
378 | 406 | assert_array_equal(x, array([86., 124., 168.]))
|
379 | 407 |
|
|
0 commit comments