def test_parse_expr(): a, b = symbols('a, b') # tests issue_16393 assert parse_expr('a + b', {}) == a + b raises(SympifyError, lambda: parse_expr('a + ', {})) # tests Transform.visit_Num assert parse_expr('1 + 2', {}) == S(3) assert parse_expr('1 + 2.0', {}) == S(3.0) # tests Transform.visit_Name assert parse_expr('Rational(1, 2)', {}) == S(1)/2 assert parse_expr('a', {'a': a}) == a # tests issue_23092 with warnings.catch_warnings(): warnings.simplefilter('error') assert parse_expr('6 * 7', {}) == S(42) # Note: The test below can be removed when support for Python 3.7 is # dropped. This test exists to ensure that the visit_Num function # exists for Python 3.7, because in 3.7, Python didn't use the # visit_Constant to create AST Nodes yet. test_expr = parse_expr('1 / 3', {}) assert test_expr == S(1)/3 # sanity check assert isinstance(test_expr, Rational)
def test_parse_expr(): a, b = symbols('a, b') # tests issue_16393 assert parse_expr('a + b', {}) == a + b raises(SympifyError, lambda: parse_expr('a + ', {})) # tests Transform.visit_Num assert parse_expr('1 + 2', {}) == S(3) assert parse_expr('1 + 2.0', {}) == S(3.0) # tests Transform.visit_Name assert parse_expr('Rational(1, 2)', {}) == S(1) / 2 assert parse_expr('a', {'a': a}) == a
def test_parse_expr(): a, b = symbols('a, b') # tests issue_16393 assert parse_expr('a + b', {}) == a + b raises(SympifyError, lambda: parse_expr('a + ', {})) # tests Transform.visit_Constant assert parse_expr('1 + 2', {}) == S(3) assert parse_expr('1 + 2.0', {}) == S(3.0) # tests Transform.visit_Name assert parse_expr('Rational(1, 2)', {}) == S(1)/2 assert parse_expr('a', {'a': a}) == a # tests issue_23092 with warnings.catch_warnings(): warnings.simplefilter('error') assert parse_expr('6 * 7', {}) == S(42)
def _map_gene_to_rxn(self, reaction, gene_values, by): """ Map gene data to reactions (using GPR associations). Parameters ---------- reaction: cobra.Reaction The reaction to map gene data to. gene_values: dict The dict of gene IDs as keys and expression values as values. by: str The method to use for mapping the gene data. "or2max_and2min": if more than one gene contribute as a complex, the min of the gene values is taken and if the genes act as isozymes, the max is taken. "or2sum_and2min": if more than one gene contribute as a complex, the min of the gene values is taken and if the genes act as isozymes, the sum is taken. Returns ------- float """ local_dict = {gene.id: Symbol(gene.id) for gene in reaction.genes} # print("_map_gene_to_rxn") rule = reaction.gene_reaction_rule.replace("and", "*").replace("or", "+") try: expression = parse_expr(rule, local_dict) if by == "or2max_and2min": expression = expression.replace(Mul, Min).replace(Add, Max) elif by == "or2sum_and2min": expression = expression.replace(Mul, Min) return expression.subs(gene_values).evalf() except: pass
a = a.replace(sym, L, 1) reps.insert(a[:a.find(L)].count(M), rep) a = a.replace(L, M) a = a.split(M) anew = [] for i, m in enumerate(reps): anew.append(a[i]) anew.append(reps[i]) anew.append(a[-1]) a = ''.join(anew) except TokenError: raise SympifyError('%r had a tokenizing problem' % a) from sympy.parsing.ast_parser import parse_expr return parse_expr(a, locals or {}) def _sympify(a): """Short version of sympify for internal usage for __add__ and __eq__ methods where it is ok to allow some things (like Python integers and floats) in the expression. This excludes things (like strings) that are unwise to allow into such an expression. >>> from sympy import Integer >>> Integer(1) == 1 True >>> Integer(1) == '1' False >>> from sympy import Symbol
def __gene_to_expression__(reaction): local_dict = {g.id: Symbol(g.id) for g in reaction.genes} rule = reaction._gene_reaction_rule.replace("and", "+").replace("or", "*") return parse_expr(rule, local_dict)
def test_parse_expr(): a, b = symbols("a, b") # tests issue_16393 parse_expr("a + b", {}) == a + b raises(SympifyError, lambda: parse_expr("a + ", {})) # tests Transform.visit_Num parse_expr("1 + 2", {}) == S(3) parse_expr("1 + 2.0", {}) == S(3.0) # tests Transform.visit_Name parse_expr("Rational(1, 2)", {}) == S(1) / 2 parse_expr("a", {"a": a}) == a