def test_parse_expression_unit(): Var = namedtuple('Var', ['dim', 'dtype']) variables = {'a': Var(dim=(volt*amp).dim, dtype=np.float64), 'b': Var(dim=volt.dim, dtype=np.float64), 'c': Var(dim=amp.dim, dtype=np.float64)} group = SimpleGroup(namespace={}, variables=variables) EE = [ (volt*amp, 'a+b*c'), (DimensionMismatchError, 'a+b'), (DimensionMismatchError, 'a<b'), (1, 'a<b*c'), (1, 'a or b'), (1, 'not (a >= b*c)'), (DimensionMismatchError, 'a or b<c'), (1, 'a/(b*c)<1'), (1, 'a/(a-a)'), (1, 'a<mV*mA'), (volt**2, 'b**2'), (volt*amp, 'a%(b*c)'), (volt, '-b'), (1, '(a/a)**(a/a)'), # Expressions involving functions (volt, 'rand()*b'), (volt**0.5, 'sqrt(b)'), (volt, 'ceil(b)'), (volt, 'sqrt(randn()*b**2)'), (1, 'sin(b/b)'), (DimensionMismatchError, 'sin(b)'), (DimensionMismatchError, 'sqrt(b) + b'), (SyntaxError, 'sqrt(b, b)'), (SyntaxError, 'sqrt()'), (SyntaxError, 'int(1, 2)'), ] for expect, expr in EE: all_variables = {} for name in get_identifiers(expr): if name in variables: all_variables[name] = variables[name] else: all_variables[name] = group._resolve(name, {}) if isinstance(expect, type) and issubclass(expect, Exception): assert_raises(expect, parse_expression_dimensions, expr, all_variables) else: u = parse_expression_dimensions(expr, all_variables) assert have_same_dimensions(u, expect) wrong_expressions = ['a**b', 'a << b', 'int(True' # typo ] for expr in wrong_expressions: all_variables = {} for name in get_identifiers(expr): if name in variables: all_variables[name] = variables[name] else: all_variables[name] = group._resolve(name, {}) assert_raises(SyntaxError, parse_expression_dimensions, expr, all_variables)
def test_parse_expression_unit(): Var = namedtuple('Var', ['unit', 'dtype']) variables = { 'a': Var(unit=volt * amp, dtype=np.float64), 'b': Var(unit=volt, dtype=np.float64), 'c': Var(unit=amp, dtype=np.float64) } group = SimpleGroup(namespace={}, variables=variables) EE = [ (volt * amp, 'a+b*c'), (DimensionMismatchError, 'a+b'), (DimensionMismatchError, 'a<b'), (1, 'a<b*c'), (1, 'a or b'), (1, 'not (a >= b*c)'), (DimensionMismatchError, 'a or b<c'), (1, 'a/(b*c)<1'), (1, 'a/(a-a)'), (1, 'a<mV*mA'), (volt**2, 'b**2'), (volt * amp, 'a%(b*c)'), (volt, '-b'), (1, '(a/a)**(a/a)'), # Expressions involving functions (volt, 'rand()*b'), (volt**0.5, 'sqrt(b)'), (volt, 'ceil(b)'), (volt, 'sqrt(randn()*b**2)'), (1, 'sin(b/b)'), (DimensionMismatchError, 'sin(b)'), (DimensionMismatchError, 'sqrt(b) + b') ] for expect, expr in EE: all_variables = {} for name in get_identifiers(expr): if name in variables: all_variables[name] = variables[name] else: all_variables[name] = group.resolve(name) if expect is DimensionMismatchError: assert_raises(DimensionMismatchError, parse_expression_unit, expr, all_variables) else: u = parse_expression_unit(expr, all_variables) assert have_same_dimensions(u, expect) wrong_expressions = [ 'a**b', 'a << b', 'int(True' # typo ] for expr in wrong_expressions: all_variables = {} for name in get_identifiers(expr): if name in variables: all_variables[name] = variables[name] else: all_variables[name] = group.resolve(name) assert_raises(SyntaxError, parse_expression_unit, expr, all_variables)
def test_parse_expression_unit(): default_namespace = create_namespace({}) varunits = dict(default_namespace) varunits.update({'a': volt * amp, 'b': volt, 'c': amp}) EE = [ (volt * amp, 'a+b*c'), (DimensionMismatchError, 'a+b'), (DimensionMismatchError, 'a<b'), (1, 'a<b*c'), (1, 'a or b'), (1, 'not (a >= b*c)'), (DimensionMismatchError, 'a or b<c'), (1, 'a/(b*c)<1'), (1, 'a/(a-a)'), (1, 'a<mV*mA'), (volt**2, 'b**2'), (volt * amp, 'a%(b*c)'), (volt, '-b'), (1, '(a/a)**(a/a)'), # Expressions involving functions (volt, 'rand()*b'), (volt**0.5, 'sqrt(b)'), (volt, 'ceil(b)'), (volt, 'sqrt(randn()*b**2)'), (1, 'sin(b/b)'), (DimensionMismatchError, 'sin(b)'), (DimensionMismatchError, 'sqrt(b) + b') ] for expect, expr in EE: if expect is DimensionMismatchError: assert_raises(DimensionMismatchError, parse_expression_unit, expr, varunits, {}) else: u = parse_expression_unit(expr, varunits, {}) assert have_same_dimensions(u, expect) wrong_expressions = [ 'a**b', 'a << b', 'ot True' # typo ] for expr in wrong_expressions: assert_raises(SyntaxError, parse_expression_unit, expr, varunits, {})
def test_parse_expression_unit(): default_namespace = create_namespace({}) varunits = dict(default_namespace) varunits.update({'a': volt*amp, 'b': volt, 'c': amp}) EE = [ (volt*amp, 'a+b*c'), (DimensionMismatchError, 'a+b'), (DimensionMismatchError, 'a<b'), (1, 'a<b*c'), (1, 'a or b'), (1, 'not (a >= b*c)'), (DimensionMismatchError, 'a or b<c'), (1, 'a/(b*c)<1'), (1, 'a/(a-a)'), (1, 'a<mV*mA'), (volt**2, 'b**2'), (volt*amp, 'a%(b*c)'), (volt, '-b'), (1, '(a/a)**(a/a)'), # Expressions involving functions (volt, 'rand()*b'), (volt**0.5, 'sqrt(b)'), (volt, 'ceil(b)'), (volt, 'sqrt(randn()*b**2)'), (1, 'sin(b/b)'), (DimensionMismatchError, 'sin(b)'), (DimensionMismatchError, 'sqrt(b) + b') ] for expect, expr in EE: if expect is DimensionMismatchError: assert_raises(DimensionMismatchError, parse_expression_unit, expr, varunits, {}) else: u = parse_expression_unit(expr, varunits, {}) assert have_same_dimensions(u, expect) wrong_expressions = ['a**b', 'a << b', 'ot True' # typo ] for expr in wrong_expressions: assert_raises(SyntaxError, parse_expression_unit, expr, varunits, {})
def test_parse_expression_unit(expect, expr): Var = namedtuple('Var', ['dim', 'dtype']) variables = { 'a': Var(dim=(volt * amp).dim, dtype=np.float64), 'b': Var(dim=volt.dim, dtype=np.float64), 'c': Var(dim=amp.dim, dtype=np.float64) } group = SimpleGroup(namespace={}, variables=variables) all_variables = {} for name in get_identifiers(expr): if name in variables: all_variables[name] = variables[name] else: all_variables[name] = group._resolve(name, {}) if isinstance(expect, type) and issubclass(expect, Exception): with pytest.raises(expect): parse_expression_dimensions(expr, all_variables) else: u = parse_expression_dimensions(expr, all_variables) assert have_same_dimensions(u, expect)
def test_parse_expression_unit(): Var = namedtuple('Var', ['dim', 'dtype']) variables = { 'a': Var(dim=(volt * amp).dim, dtype=np.float64), 'b': Var(dim=volt.dim, dtype=np.float64), 'c': Var(dim=amp.dim, dtype=np.float64) } group = SimpleGroup(namespace={}, variables=variables) EE = [ (volt * amp, 'a+b*c'), (DimensionMismatchError, 'a+b'), (DimensionMismatchError, 'a<b'), (1, 'a<b*c'), (1, 'a or b'), (1, 'not (a >= b*c)'), (DimensionMismatchError, 'a or b<c'), (1, 'a/(b*c)<1'), (1, 'a/(a-a)'), (1, 'a<mV*mA'), (volt**2, 'b**2'), (volt * amp, 'a%(b*c)'), (volt, '-b'), (1, '(a/a)**(a/a)'), # Expressions involving functions (volt, 'rand()*b'), (volt**0.5, 'sqrt(b)'), (volt, 'ceil(b)'), (volt, 'sqrt(randn()*b**2)'), (1, 'sin(b/b)'), (DimensionMismatchError, 'sin(b)'), (DimensionMismatchError, 'sqrt(b) + b'), (SyntaxError, 'sqrt(b, b)'), (SyntaxError, 'sqrt()'), (SyntaxError, 'int(1, 2)'), ] for expect, expr in EE: all_variables = {} for name in get_identifiers(expr): if name in variables: all_variables[name] = variables[name] else: all_variables[name] = group._resolve(name, {}) if isinstance(expect, type) and issubclass(expect, Exception): with pytest.raises(expect): parse_expression_dimensions(expr, all_variables) else: u = parse_expression_dimensions(expr, all_variables) assert have_same_dimensions(u, expect) wrong_expressions = [ 'a**b', 'a << b', 'int(True' # typo ] for expr in wrong_expressions: all_variables = {} for name in get_identifiers(expr): if name in variables: all_variables[name] = variables[name] else: all_variables[name] = group._resolve(name, {}) with pytest.raises(SyntaxError): parse_expression_dimensions(expr, all_variables)