From 976cbbdf03cd714f109a8310bd86a5208f4ad47e Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Mon, 7 Mar 2016 13:05:12 +0300 Subject: [PATCH] Merge_history (from Explicable) and develop_steps (from Expression) work together --- pymath/calculus/explicable.py | 30 +++++++++++++++++++------ pymath/calculus/expression.py | 41 ++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/pymath/calculus/explicable.py b/pymath/calculus/explicable.py index bcb8558..9ac3dad 100644 --- a/pymath/calculus/explicable.py +++ b/pymath/calculus/explicable.py @@ -19,12 +19,12 @@ class Explicable(Renderable): def __init__(self, *args, **kwargs): super(Explicable, self).__init__() self.steps = [] - self.explainator = 0 + self.explained = 0 def explain(self): r""" Generate and render steps which leed to itself - After beening explained, the Explicable become amnesiac. + After beening explained, the Explicable become amnesiac. It will return itself in a list. See 'history_generator' to explain it once again. >>> action = Explicable() @@ -39,12 +39,19 @@ class Explicable(Renderable): >>> # Now action is amnesiac >>> for i in action.explain(): ... print(i) + 42 + >>> action = Explicable() + >>> action.postfix_tokens = Expression('42') + >>> for i in action.explain(): + ... print(i) + 42 """ - if not self.explainator: - self.explainator = self.history_generator() - - return self.explainator + if self.explained: + return [self.STR_RENDER(self.postfix_tokens)] + else: + self.explained = 1 + return self.history_generator() def history_generator(self): r""" Generator for rendered steps which leed to itself @@ -145,9 +152,14 @@ class Explicable(Renderable): ['2 + 10 \\times 4', '2 \\times 12'] ['2 + 40', '24'] ['42', '24'] + >>> # Now they are amnesiac >>> for i in action1.explain(): ... print(i) - + 42 + >>> m_history = Explicable().merge_history([action1, action2]) + >>> for i in m_history: + ... print(i) + ['42', '24'] >>> action1 = Explicable() >>> action1.postfix_tokens = Expression('42') >>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')]) @@ -174,6 +186,10 @@ class Explicable(Renderable): ['2 + 10 \\times 4', '2 \\times 12', '3 - 3'] ['2 + 40', '24', '0'] ['42', '24', '0'] + >>> m_history = Explicable().merge_history([1,2,3]) + >>> for i in m_history: + ... print(i) + [1, 2, 3] """ steps = [] for expl in explicables: diff --git a/pymath/calculus/expression.py b/pymath/calculus/expression.py index 79e8ace..863156e 100644 --- a/pymath/calculus/expression.py +++ b/pymath/calculus/expression.py @@ -252,25 +252,32 @@ class Expression(Explicable): @classmethod def develop_steps(cls, tokenList): - """ From a list of tokens, it develops steps of each tokens """ - tmp_steps = [] - for t in tokenList: - try: - with Expression.tmp_render(): - steps = [i for i in t.explain()] - if steps: - tmp_steps.append(steps) - else: - tmp_steps.append([t]) - except AttributeError: - tmp_steps.append([t]) + """ + From a list of tokens, it develops steps of each tokens and transpose it into steps respecting the stucture of the tokenList. - if max([len(i) for i in tmp_steps]) > 1: - tmp_steps = expand_list(tmp_steps)[:-1] - steps = [Expression(s) for s in tmp_steps] - return steps + It try to use 'explain' method for every tokens. After using this methode, tokens becom amnesiac. + + >>> e = Expression('1+2') + >>> e1 = e.simplify() + >>> f = Expression('3*4+5') + >>> f1 = f.simplify() + >>> Expression.develop_steps([e1, f1, op.add]) + [< [1, 2, +, 3, 4, *, 5, +, +] >, < [3, 12, 5, +, +] >] + >>> e = Expression('1+2') + >>> e1 = e.simplify() + >>> f = Expression('3*4+5') + >>> f1 = f.simplify() + >>> g = Expression('6+7') + >>> g1 = g.simplify() + >>> Expression.develop_steps([e1, f1, op.add, g1, op.mul]) + [< [1, 2, +, 3, 4, *, 5, +, +, 6, 7, +, *] >, < [3, 12, 5, +, +, 13, *] >] + """ + with Expression.tmp_render(): + tmp_steps = list(Explicable().merge_history(tokenList)) - return [] + tmp_steps = list(tmp_steps)[:-1] + steps = [Expression(s) for s in tmp_steps] + return steps @classmethod def isExpression(cls, other):