def sanitize(self, equation): """Transform string to a SymPy equality or a tuple of them""" equation = parse(equation) if equation.func == sympy.Tuple: return equation.func(*[self.sanitize(eq) for eq in equation.args]) if equation.func != sympy.Eq: equation = sympy.Eq(equation, 0, evaluate=False) return equation
def sanitize(self, expr): """Check it is an appropriate range""" if isinstance(expr, (tuple, list, set)): return {parse(i) if isinstance(i, int) else i for i in expr} try: numbers = [int(n) for n in str(expr).split(",")] assert len(numbers) > 0 if len(numbers) > 1: assert numbers[1] >= numbers[0] assert len(numbers) == 2 else: numbers.append(numbers[0] + 1) except (AssertionError, ValueError): raise ValueError( f"{repr(expr)} is not a valid expression for {self.name}") return self.sanitize(list(range(*numbers)))
def sanitize(self, expr): if isinstance(expr, str): return [parse(e) for e in expr.split(",")] return parse(expr)
def sanitize(self, expr): """Transform expr into a real mathematical expression""" return parse(expr)
def sanitize(self, expr): """Transform expr into a real mathematical expression""" expr = parse(expr) if getattr(self, "numeric", False): expr = float(expr.evalf()) return expr
def test_parse(): x = Symbol("x") with evaluate(False): assert parse("5 x + 3") == 5 * x + 3 assert parse("sqrt x") == sqrt(x) assert parse("sqrt 2x") == sqrt(2 * x) assert parse("4 + 3") == Add(4, 3, evaluate=False) with pytest.raises(TypeError): parse({}) with pytest.raises(ValueError): parse("sin (") with pytest.raises(ValueError): parse("3 *") with pytest.raises(ValueError): parse("5 =")