Skip to content

Commit 02664ec

Browse files
committed
Hopefully fix constants
1 parent 61fc348 commit 02664ec

File tree

6 files changed

+30
-19
lines changed

6 files changed

+30
-19
lines changed

formulate/expression.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,7 @@ def args(self):
240240

241241
@add_logging(ignore_args=[1, 2])
242242
def to_string(self, config, constants):
243-
if isinstance(self.args[0], SingleComponent):
244-
assert len(self.args) == 1, self.args
245-
# return str(constants[self.args[0]])
246-
return self.args[0].to_string(config, constants)
247-
else:
248-
return config[self.id].to_string(self, config, constants)
243+
return config[self.id].to_string(self, config, constants)
249244

250245

251246
class Variable(SingleComponent):
@@ -273,7 +268,7 @@ def __init__(self, id):
273268
self._id = id
274269

275270
def __repr__(self):
276-
return '{class_name}({name})'.format(
271+
return '{class_name}({id})'.format(
277272
class_name=self.__class__.__name__, id=self.id)
278273

279274
def __str__(self):

formulate/logging.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717

1818
def get_identifier():
19+
"""Generate an identifier for keeping track of return values when logging"""
1920
return''.join(random.choice(string.ascii_uppercase + string.digits)
2021
for i in range(5))
2122

formulate/parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def __init__(self, id, value):
4444
self._id = id
4545
self._value = value
4646

47-
def __call__(self):
47+
@add_logging
48+
def __call__(self, string, location, result):
4849
return Constant(self.id)
4950

5051
@property
@@ -280,7 +281,7 @@ def create_parser(config, constants):
280281

281282
VARIABLE = Word(pyparsing.alphas+'_', pyparsing.alphanums+'_-')
282283
VARIABLE.setName('Variable')
283-
VARIABLE.setParseAction(lambda x: Variable(x[0]))
284+
VARIABLE.setParseAction(add_logging(lambda string, location, result: Variable(result[0])))
284285

285286
NUMBER = pyparsing.Or([
286287
pyparsing_common.number,

tests/backends/test_backends.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def test():
1919
return test
2020

2121

22-
test_001 = do_checks('true', 'True')
23-
test_002 = do_checks('false', 'False')
22+
test_001 = do_checks('True', 'true')
23+
test_002 = do_checks('False', 'false')
2424
test_003 = do_checks('sqrt(2)', 'sqrt(2)')
2525
test_004 = do_checks('sqrt(2)', 'TMath::Sqrt(2)')
2626
test_005 = do_checks('sqrt(abs(-4))', 'TMath::Sqrt(TMath::Abs(-4))')

tests/backends/test_constants.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from formulate import to_numexpr
8+
from formulate import from_numexpr, to_numexpr
99
from formulate import from_root, to_root
1010

1111
numexpr = pytest.importorskip("numexpr")
@@ -34,16 +34,27 @@ def numexpr_eval(string, **kwargs):
3434
return numexpr.evaluate(string, local_dict=kwargs)
3535

3636

37-
def create_constant_test(root_string):
37+
def create_constant_test(input_string, input_backend='root'):
38+
assert input_backend in ('root', 'numexpr'), 'Unrecognised backend specified'
39+
input_from_method = {
40+
'root': from_root,
41+
'numexpr': from_numexpr,
42+
}[input_backend]
43+
3844
def test_constant():
39-
expression = from_root(root_string)
45+
expression = input_from_method(input_string)
4046
root_result = to_root(expression)
4147
numexpr_result = to_numexpr(expression)
42-
assert pytest.approx(root_eval(root_string), root_eval(root_result))
48+
assert pytest.approx(root_eval(input_string), root_eval(root_result))
4349
assert pytest.approx(root_eval(root_result), numexpr_eval(numexpr_result))
4450
return test_constant
4551

4652

53+
# Test basic numexpr constants
54+
test_numexpr_true = create_constant_test('True', input_backend='numexpr')
55+
test_numexpr_false = create_constant_test('False', input_backend='numexpr')
56+
57+
# Test basic ROOT constants
4758
test_true = create_constant_test('true')
4859
test_false = create_constant_test('false')
4960
test_sqrt2_1 = create_constant_test('sqrt2')

tests/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from formulate import Component, Expression, Variable
6+
from formulate import ExpressionComponent, Expression, Variable, Constant
77

88

99
def make_check_result(from_func, to_func):
@@ -23,9 +23,12 @@ def check_result(input_string, expected_expression, expected_string=None, **kwar
2323

2424

2525
def assert_equal_expressions(lhs, rhs):
26-
assert isinstance(lhs, Component)
27-
assert isinstance(rhs, Component)
28-
if isinstance(lhs, Variable):
26+
assert isinstance(lhs, ExpressionComponent)
27+
assert isinstance(rhs, ExpressionComponent)
28+
if isinstance(lhs, Constant):
29+
assert isinstance(rhs, Constant)
30+
assert lhs.id == rhs.id
31+
elif isinstance(lhs, Variable):
2932
assert isinstance(rhs, Variable)
3033
assert lhs.name == rhs.name
3134
else:

0 commit comments

Comments
 (0)