From bcd5643202f31905cc7dd0c32a5b4dfb32d1ab20 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Sat, 12 Mar 2016 04:51:38 +0300 Subject: [PATCH] move __str__ from expression to renderable --- pymath/calculus/abstract_polynom.py | 2 +- pymath/calculus/expression.py | 18 ++++++++---------- pymath/calculus/renderable.py | 8 ++++++++ pymath/calculus/step.py | 2 +- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pymath/calculus/abstract_polynom.py b/pymath/calculus/abstract_polynom.py index 6751692..a582985 100644 --- a/pymath/calculus/abstract_polynom.py +++ b/pymath/calculus/abstract_polynom.py @@ -427,7 +427,7 @@ class AbstractPolynom(Explicable): return ans def replace_letter(self, letter): - """ Replace the letter in the expression + r""" Replace the letter in the expression :param letter: the new letter. :returns: The expression with the new letter. diff --git a/pymath/calculus/expression.py b/pymath/calculus/expression.py index fb5206e..cc8a44c 100644 --- a/pymath/calculus/expression.py +++ b/pymath/calculus/expression.py @@ -73,14 +73,6 @@ class Expression(Explicable): self.steal_history(exp) self._isExpression = 1 - def __str__(self): - """ - Overload str - - If you want to changer render use Expression.set_render(...) or use tmp_render context manager. - """ - return self.STR_RENDER(self.postfix_tokens) - def simplify(self): """ Compute entirely the expression and return the result with .steps attribute """ try: @@ -177,7 +169,7 @@ class Expression(Explicable): @classmethod def develop_steps(cls, tokenList): - """ + r""" From a list of tokens, it develops steps of each tokens and transpose it into steps respecting the stucture of the tokenList. It try to use 'explain' method for every tokens. After using this methode, tokens becom amnesiac. @@ -186,8 +178,14 @@ class Expression(Explicable): >>> e1 = e.simplify() >>> f = Expression('3*4+5') >>> f1 = f.simplify() - >>> Expression.develop_steps([e1, f1, op.add]) + >>> dev_steps = Expression.develop_steps([e1, f1, op.add]) + >>> dev_steps [< Step [1, 2, +, 3, 4, *, 5, +, +]>, < Step [3, 12, 5, +, +]>, < Step [3, 17, +]>] + >>> for i in dev_steps: + ... print(i) + 1 + 2 + 3 \times 4 + 5 + 3 + 12 + 5 + 3 + 17 >>> e = Expression('1+2') >>> e1 = e.simplify() >>> f = Expression('3*4+5') diff --git a/pymath/calculus/renderable.py b/pymath/calculus/renderable.py index ba1c688..4583aa6 100644 --- a/pymath/calculus/renderable.py +++ b/pymath/calculus/renderable.py @@ -76,6 +76,14 @@ class Renderable(object): """ self.postfix_tokens = pstf_tokens + def __str__(self): + """ + Overload str + + If you want to changer render use Expression.set_render(...) or use tmp_render context manager. + """ + return self.STR_RENDER(self.postfix_tokens) + def __repr__(self): return "< {cls} {pstf_tokens}>".format( cls = str(self.__class__).split('.')[-1][:-2], diff --git a/pymath/calculus/step.py b/pymath/calculus/step.py index b66172a..da97089 100644 --- a/pymath/calculus/step.py +++ b/pymath/calculus/step.py @@ -26,7 +26,7 @@ class Step(Renderable): \\frac{ 6 }{ 5 } >>> with Step.tmp_render(): ... for i in exp.simplify().explain(): - ... print(i) + ... print(repr(i)) < Step [2, 3, 5, /, *]> < Step [3, 5, /, 2, *]> < Step [3, 2, *, 5, /]>