def evaluate_weight(self, src_value: float) -> float: """ Evaluate weight of relation. :param src_value: float-value of source attribute :return: evaluated weight of relation """ return float(safe_eval(self.evaluation.replace('x', str(src_value))))
def test_full(): d = """[ '1', b'b', (1 + 2, 1 / 5, 3 ** 8), { 'help': {'1', 'x', 1 + 2}, 1 : 'yelp' } ]""" assert seval.safe_eval(d) == eval(d)
def test_unsafe(): with pytest.raises(ValueError): seval.safe_eval('pow(2,3)')
def test_sequence(seq): for t in (list, set, tuple): assert seval.safe_eval(repr(t(seq))) == eval(repr(t(seq)))
def test_dict(): d = {'a': 1, 'a b c': 2, 1: 'a'} assert seval.safe_eval(repr(d)) == d
def test_literal(literal): assert seval.safe_eval(literal) == eval(literal)
def test_expression(exp): assert seval.safe_eval(exp) == eval(exp)
def test_binary(op): l = _get_random_digit() r = _get_random_digit(non_zero=True) exp = _get_binary(op, l, r) assert seval.safe_eval(exp) == eval(exp)
def test_unary(op): d = _get_random_digit() exp = _get_unary(op, d) assert seval.safe_eval(exp) == eval(exp)
def test_num(number): assert seval.safe_eval(str(number)) == eval(number)