From e34215a9a98c40ee41c5cf59c4f8d546ec09a559 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Wed, 9 Mar 2016 10:48:29 +0300 Subject: [PATCH] new initiat for Renderable and Explicable - need to fix other --- pymath/calculus/explicable.py | 48 ++++++++++++++--------------------- pymath/calculus/render.py | 8 ++++++ 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/pymath/calculus/explicable.py b/pymath/calculus/explicable.py index 2e2a47b..14977cd 100644 --- a/pymath/calculus/explicable.py +++ b/pymath/calculus/explicable.py @@ -16,8 +16,8 @@ class Explicable(Renderable): """ - def __init__(self, *args, **kwargs): - super(Explicable, self).__init__() + def __init__(self, pstf_tokens): + super(Explicable, self).__init__(pstf_tokens) self.steps = [] self.explained = 0 @@ -27,9 +27,8 @@ class Explicable(Renderable): After beening explained, the Explicable become amnesiac. It will return itself in a list. See 'history_generator' to explain it once again. - >>> action = Explicable() + >>> action = Explicable(['42']) >>> from .expression import Expression - >>> action.postfix_tokens = Expression('42') >>> action.this_append_before([Expression('2+10*4'), Expression('2+40')]) >>> for i in action.explain(): ... print(i) @@ -40,8 +39,7 @@ class Explicable(Renderable): >>> for i in action.explain(): ... print(i) 42 - >>> action = Explicable() - >>> action.postfix_tokens = Expression('42') + >>> action = Explicable(['42']) >>> for i in action.explain(): ... print(i) 42 @@ -59,9 +57,8 @@ class Explicable(Renderable): This is the called method in explain which create the generator. It create a new generator at each call. - >>> action = Explicable() + >>> action = Explicable(['42']) >>> from .expression import Expression - >>> action.postfix_tokens = Expression('42') >>> for i in action.history_generator(): ... print(i) 42 @@ -116,7 +113,7 @@ class Explicable(Renderable): :param steps: list of steps that append before - >>> e = Explicable() + >>> e = Explicable(['Actions']) >>> s = ['eat', 'sleep'] >>> e.this_append_before(s) >>> print(e.steps) @@ -139,9 +136,8 @@ class Explicable(Renderable): except AttributeError: pass - - @staticmethod - def merge_history(explicables): + @classmethod + def merge_history(cls, explicables): r""" Take a list of Explicable objects, explain where they are from and merge their history This method try to use 'explain' method from Explicable. This means that after using this method, those objects will become amnesiac. @@ -150,13 +146,11 @@ class Explicable(Renderable): :returns: the list of steps >>> from .expression import Expression - >>> action1 = Explicable() - >>> action1.postfix_tokens = Expression('42') + >>> action1 = Explicable(['42']) >>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')]) - >>> action2 = Explicable() - >>> action2.postfix_tokens = Expression('24') + >>> action2 = Explicable(['24']) >>> action2.this_append_before([Expression('2*12')]) - >>> m_history = Explicable().merge_history([action1, action2]) + >>> m_history = Explicable.merge_history([action1, action2]) >>> m_history >>> for i in m_history: @@ -168,14 +162,13 @@ class Explicable(Renderable): >>> for i in action1.explain(): ... print(i) 42 - >>> m_history = Explicable().merge_history([action1, action2]) + >>> m_history = Explicable.merge_history([action1, action2]) >>> for i in m_history: ... print(i) ['42', '24'] - >>> action1 = Explicable() - >>> action1.postfix_tokens = Expression('42') + >>> action1 = Explicable(['42']) >>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')]) - >>> m_history = Explicable().merge_history([action1, 12]) + >>> m_history = Explicable.merge_history([action1, 12]) >>> m_history >>> for i in m_history: @@ -183,22 +176,19 @@ class Explicable(Renderable): ['2 + 10 \\times 4', 12] ['2 + 40', 12] ['42', 12] - >>> action1 = Explicable() - >>> action1.postfix_tokens = Expression('42') + >>> action1 = Explicable(['42']) >>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')]) - >>> action2 = Explicable() - >>> action2.postfix_tokens = Expression('24') + >>> action2 = Explicable(['24']) >>> action2.this_append_before([Expression('2*12')]) - >>> action3 = Explicable() - >>> action3.postfix_tokens = Expression('0') + >>> action3 = Explicable(['0']) >>> action3.this_append_before([Expression('3-3')]) - >>> m_history = Explicable().merge_history([action1, action2, action3]) + >>> m_history = Explicable.merge_history([action1, action2, action3]) >>> for i in m_history: ... print(i) ['2 + 10 \\times 4', '2 \\times 12', '3 - 3'] ['2 + 40', '24', '0'] ['42', '24', '0'] - >>> m_history = Explicable().merge_history([1,2,3]) + >>> m_history = Explicable.merge_history([1,2,3]) >>> for i in m_history: ... print(i) [1, 2, 3] diff --git a/pymath/calculus/render.py b/pymath/calculus/render.py index e87560e..765cc1a 100644 --- a/pymath/calculus/render.py +++ b/pymath/calculus/render.py @@ -139,6 +139,14 @@ class Renderable(object): Renderable.set_render(self.old_render) return TmpRenderEnv() + def __init__(self, pstf_tokens): + """Initiate the renderable objet + + :param pstf_tokens: the postfix list of tokens + + """ + self.postfix_tokens = pstf_tokens + def __repr__(self): return "< {cls} {pstf_tokens}>".format( cls = str(self.__class__).split('.')[-1][:-2],