From 3b5583bd6168e6af41468a27dd3945182d80f5f6 Mon Sep 17 00:00:00 2001 From: Lafrite Date: Fri, 21 Nov 2014 15:49:43 +0100 Subject: [PATCH] Nice way for setting render <3 --- docs/tutorial.mdwn | 18 +++--------------- pymath/expression.py | 25 ++++++++++++++++++++----- pymath/polynom.py | 4 +++- pymath/random_expression.py | 4 ++++ test/test_expression.py | 10 ++++++---- 5 files changed, 36 insertions(+), 25 deletions(-) diff --git a/docs/tutorial.mdwn b/docs/tutorial.mdwn index 45874c1..e1e20e2 100644 --- a/docs/tutorial.mdwn +++ b/docs/tutorial.mdwn @@ -34,28 +34,16 @@ On peut ensuite afficher l'expression avec un *print*. Et si l'on souhaite un rendu plus adapté à la console: >>> from pymath.render import txt - >>> exp.render(render = txt) + >>> Expression.set_render(txt) + >>> exp.render() 1 + 2 * 3 -Ou encore - - >>> from pymath.renders import txt - >>> Expression.STR_RENDER = txt - >>> print(exp) - 1 + 2 * 3 - -/!\ Il ne faut pas faire - - >>> exp.STR_RENDER = txt - -car le changement ne se propagera pas à toute la classe. Ce qui posera problème quand on utilisera la méthode *simplify*(les sous expressions auront l'ancien STR_RENDER). - ### Simplification des expressions Une fois les expressions créées, elles peuvent se réduire en expliquant les étapes et en respectant les règles de priorités. Les exemples suivants seront données avec un rendu texte. >>> from pymath.expression import Expression >>> from pymath.renders import txt - >>> Expression.STR_RENDER = txt + >>> Expression.set_render(txt) >>> exp = Expression("1 + 2 * 3") >>> for i in exp.simplify(): diff --git a/pymath/expression.py b/pymath/expression.py index 6005307..eae21fe 100644 --- a/pymath/expression.py +++ b/pymath/expression.py @@ -12,6 +12,15 @@ class Expression(object): """A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown""" STR_RENDER = tex + DEFAULT_RENDER = tex + + @classmethod + def set_render(cls, render): + cls.STR_RENDER = render + + @classmethod + def set_df_render(cls): + cls.set_render(cls.DEFAULT_RENDER) def __init__(self, exp): """ Initiate the expression @@ -47,20 +56,20 @@ class Expression(object): ## --------------------- ## Mechanism functions - def simplify(self, render=STR_RENDER): + def simplify(self): """ Generator which return steps for computing the expression """ if not self.can_go_further(): - yield render(self.postfix_tokens) + yield self.STR_RENDER(self.postfix_tokens) else: self.compute_exp() old_s = '' for s in self.steps: - new_s = render(s) + new_s = self.STR_RENDER(s) # Astuce pour éviter d'avoir deux fois la même étape (par exemple pour la transfo d'une division en fraction) if new_s != old_s: old_s = new_s yield new_s - for s in self.child.simplify(render = render): + for s in self.child.simplify(): if old_s != s: yield s @@ -191,14 +200,20 @@ def test(exp): print("\n") if __name__ == '__main__': - Expression.STR_RENDER = txt + Expression.set_render(txt) exp1 = "2 ^ 3 * 5" test(exp1) + Expression.set_render(tex) + + test(exp1) + from pymath.operator import op exp = [2, 3, op.pw, 5, op.mul] test(exp) + Expression.set_render(txt) + test([Expression(exp1), Expression(exp), op.add]) exp = "1 + 3 * 5" diff --git a/pymath/polynom.py b/pymath/polynom.py index b2eee8f..2241b6b 100644 --- a/pymath/polynom.py +++ b/pymath/polynom.py @@ -176,7 +176,9 @@ class Polynom(object): coef_exp = coef # On fait réduire l'expression puis on ajoute dans steps - coef_steps = list(coef_exp.simplify(render = lambda x:Expression(x))) + Expression.set_render(lambda _,x:Expression(x)) + coef_steps = list(coef_exp.simplify()) + Expression.set_df_render() # On ajoute toutes ces étapes coefs_steps.append(coef_steps) diff --git a/pymath/random_expression.py b/pymath/random_expression.py index 4be4fac..82f06a4 100644 --- a/pymath/random_expression.py +++ b/pymath/random_expression.py @@ -26,6 +26,10 @@ class RdExpression(object): FORM = "exp" + @classmethod + def set_form(cls, form): + cls.FORM = form + def __init__(self, form, conditions = []): """Initiate the generator diff --git a/test/test_expression.py b/test/test_expression.py index 1f324ac..3afceed 100644 --- a/test/test_expression.py +++ b/test/test_expression.py @@ -11,6 +11,8 @@ from pymath.generic import first_elem from pymath.render import txt, tex +Expression.set_render(txt) + class TestExpression(unittest.TestCase): """Testing functions from pymath.expression""" @@ -26,16 +28,16 @@ class TestExpression(unittest.TestCase): self.assertEqual(exp.postfix_tokens, [2, 3, "+"]) def test_simplify_frac(self): - render = lambda x : str(x) + render = lambda _,x : str(x) + Expression.set_render(render) exp = Expression("1/2 - 4") steps = ["[1, 2, '/', 4, '-']", \ "[< Fraction 1 / 2>, 4, '-']", \ "[1, 1, '*', 2, 1, '*', '/', 4, 2, '*', 1, 2, '*', '/', '-']", \ "[1, 8, '-', 2, '/']", \ '[< Fraction -7 / 2>]'] - self.assertEqual(steps, list(exp.simplify(render = render))) - - Expression.STR_RENDER = tex + self.assertEqual(steps, list(exp.simplify())) + Expression.set_render(txt) def test_add_exp(self): e = Expression("12- 4")