new initiat for Renderable and Explicable - need to fix other

This commit is contained in:
Benjamin Bertrand 2016-03-09 10:48:29 +03:00
parent 351a9002be
commit e34215a9a9
2 changed files with 27 additions and 29 deletions

View File

@ -16,8 +16,8 @@ class Explicable(Renderable):
""" """
def __init__(self, *args, **kwargs): def __init__(self, pstf_tokens):
super(Explicable, self).__init__() super(Explicable, self).__init__(pstf_tokens)
self.steps = [] self.steps = []
self.explained = 0 self.explained = 0
@ -27,9 +27,8 @@ class Explicable(Renderable):
After beening explained, the Explicable become amnesiac. It will return itself in a list. After beening explained, the Explicable become amnesiac. It will return itself in a list.
See 'history_generator' to explain it once again. See 'history_generator' to explain it once again.
>>> action = Explicable() >>> action = Explicable(['42'])
>>> from .expression import Expression >>> from .expression import Expression
>>> action.postfix_tokens = Expression('42')
>>> action.this_append_before([Expression('2+10*4'), Expression('2+40')]) >>> action.this_append_before([Expression('2+10*4'), Expression('2+40')])
>>> for i in action.explain(): >>> for i in action.explain():
... print(i) ... print(i)
@ -40,8 +39,7 @@ class Explicable(Renderable):
>>> for i in action.explain(): >>> for i in action.explain():
... print(i) ... print(i)
42 42
>>> action = Explicable() >>> action = Explicable(['42'])
>>> action.postfix_tokens = Expression('42')
>>> for i in action.explain(): >>> for i in action.explain():
... print(i) ... print(i)
42 42
@ -59,9 +57,8 @@ class Explicable(Renderable):
This is the called method in explain which create the generator. This is the called method in explain which create the generator.
It create a new generator at each call. It create a new generator at each call.
>>> action = Explicable() >>> action = Explicable(['42'])
>>> from .expression import Expression >>> from .expression import Expression
>>> action.postfix_tokens = Expression('42')
>>> for i in action.history_generator(): >>> for i in action.history_generator():
... print(i) ... print(i)
42 42
@ -116,7 +113,7 @@ class Explicable(Renderable):
:param steps: list of steps that append before :param steps: list of steps that append before
>>> e = Explicable() >>> e = Explicable(['Actions'])
>>> s = ['eat', 'sleep'] >>> s = ['eat', 'sleep']
>>> e.this_append_before(s) >>> e.this_append_before(s)
>>> print(e.steps) >>> print(e.steps)
@ -139,9 +136,8 @@ class Explicable(Renderable):
except AttributeError: except AttributeError:
pass pass
@classmethod
@staticmethod def merge_history(cls, explicables):
def merge_history(explicables):
r""" Take a list of Explicable objects, explain where they are from and merge their history 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. 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 :returns: the list of steps
>>> from .expression import Expression >>> from .expression import Expression
>>> action1 = Explicable() >>> action1 = Explicable(['42'])
>>> action1.postfix_tokens = Expression('42')
>>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')]) >>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')])
>>> action2 = Explicable() >>> action2 = Explicable(['24'])
>>> action2.postfix_tokens = Expression('24')
>>> action2.this_append_before([Expression('2*12')]) >>> action2.this_append_before([Expression('2*12')])
>>> m_history = Explicable().merge_history([action1, action2]) >>> m_history = Explicable.merge_history([action1, action2])
>>> m_history >>> m_history
<generator object transpose_fill ...> <generator object transpose_fill ...>
>>> for i in m_history: >>> for i in m_history:
@ -168,14 +162,13 @@ class Explicable(Renderable):
>>> for i in action1.explain(): >>> for i in action1.explain():
... print(i) ... print(i)
42 42
>>> m_history = Explicable().merge_history([action1, action2]) >>> m_history = Explicable.merge_history([action1, action2])
>>> for i in m_history: >>> for i in m_history:
... print(i) ... print(i)
['42', '24'] ['42', '24']
>>> action1 = Explicable() >>> action1 = Explicable(['42'])
>>> action1.postfix_tokens = Expression('42')
>>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')]) >>> 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 >>> m_history
<generator object transpose_fill ...> <generator object transpose_fill ...>
>>> for i in m_history: >>> for i in m_history:
@ -183,22 +176,19 @@ class Explicable(Renderable):
['2 + 10 \\times 4', 12] ['2 + 10 \\times 4', 12]
['2 + 40', 12] ['2 + 40', 12]
['42', 12] ['42', 12]
>>> action1 = Explicable() >>> action1 = Explicable(['42'])
>>> action1.postfix_tokens = Expression('42')
>>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')]) >>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')])
>>> action2 = Explicable() >>> action2 = Explicable(['24'])
>>> action2.postfix_tokens = Expression('24')
>>> action2.this_append_before([Expression('2*12')]) >>> action2.this_append_before([Expression('2*12')])
>>> action3 = Explicable() >>> action3 = Explicable(['0'])
>>> action3.postfix_tokens = Expression('0')
>>> action3.this_append_before([Expression('3-3')]) >>> 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: >>> for i in m_history:
... print(i) ... print(i)
['2 + 10 \\times 4', '2 \\times 12', '3 - 3'] ['2 + 10 \\times 4', '2 \\times 12', '3 - 3']
['2 + 40', '24', '0'] ['2 + 40', '24', '0']
['42', '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: >>> for i in m_history:
... print(i) ... print(i)
[1, 2, 3] [1, 2, 3]

View File

@ -139,6 +139,14 @@ class Renderable(object):
Renderable.set_render(self.old_render) Renderable.set_render(self.old_render)
return TmpRenderEnv() 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): def __repr__(self):
return "< {cls} {pstf_tokens}>".format( return "< {cls} {pstf_tokens}>".format(
cls = str(self.__class__).split('.')[-1][:-2], cls = str(self.__class__).split('.')[-1][:-2],