From c314b55376332b3666a4c9cd5d13aeb3a91f38eb Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Thu, 10 Mar 2016 16:40:52 +0300 Subject: [PATCH] Step init from a Renderable. Step in Expression --- pymath/calculus/expression.py | 4 ++-- pymath/calculus/step.py | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pymath/calculus/expression.py b/pymath/calculus/expression.py index 324bb5d..a4d12c9 100644 --- a/pymath/calculus/expression.py +++ b/pymath/calculus/expression.py @@ -98,7 +98,7 @@ class Expression(Explicable): else: self.simplified = self.child.simplify() - self.simplified.steps = self.child.steps + self.simplified.steps + self.simplified.this_append_before(self.child.steps) return self.simplified def compute_exp(self): @@ -106,7 +106,7 @@ class Expression(Explicable): if len(self.postfix_tokens) == 1: raise ComputeError("Nothing to compute in {}".format(self.postfix_tokens)) else: - ini_step = Expression(self.postfix_tokens) + ini_step = Step(self.postfix_tokens) tokenList = self.postfix_tokens.copy() tmpTokenList = [] diff --git a/pymath/calculus/step.py b/pymath/calculus/step.py index 5b48418..b66172a 100644 --- a/pymath/calculus/step.py +++ b/pymath/calculus/step.py @@ -41,7 +41,7 @@ class Step(Renderable): """ return super(Step, cls).tmp_render(Step) - def __init__(self, pstf_tokens): + def __init__(self, exp): """Initiate the renderable objet :param pstf_tokens: the postfix list of tokens @@ -54,12 +54,20 @@ class Step(Renderable): < Step [2, 3, '+', 5, '*']> """ - self.postfix_tokens = [] - for t in pstf_tokens: - try: - self.postfix_tokens += t.postfix_tokens - except AttributeError: - self.postfix_tokens.append(t) + if isinstance(exp, Renderable): + self.postfix_tokens = exp.postfix_tokens + elif isinstance(exp, list): + self.postfix_tokens = [] + for t in exp: + try: + self.postfix_tokens += t.postfix_tokens + except AttributeError: + self.postfix_tokens.append(t) + else: + raise ValueError( + "Can't initiate Step with {}".format( + exp + )) # -----------------------------