change the goal of merge_history

This commit is contained in:
Benjamin Bertrand 2016-03-07 12:32:51 +03:00
parent d1f074e8df
commit bfef87424d
1 changed files with 34 additions and 24 deletions

View File

@ -122,57 +122,67 @@ class Explicable(Renderable):
self.steps = steps + self.steps
@staticmethod
def merge_history(expl1, expl2, operation):
r""" Take two Explicable objects, explain where they are from and merge their history
def merge_history(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 expl1 and expl2. This means that after using this method, those two 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.
:param expl1: fist explicable
:param expl2: second explicable
:param operation: the operation that link them
:param explicables: list
:returns: the list of steps
>>> from .expression import Expression
>>> from .operator import op
>>> action1 = Explicable()
>>> action1.postfix_tokens = Expression('42')
>>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')])
>>> action2 = Explicable()
>>> action2.postfix_tokens = Expression('24')
>>> action2.this_append_before([Expression('2*12')])
>>> m_history = Explicable().merge_history(action1, action2, op.add)
>>> m_history = Explicable().merge_history([action1, action2])
>>> m_history
<generator object transpose_fill ...>
>>> for i in m_history:
... print(i)
['2 + 10 \\times 4', '2 \\times 12', +]
['2 + 40', '24', +]
['42', '24', +]
['2 + 10 \\times 4', '2 \\times 12']
['2 + 40', '24']
['42', '24']
>>> for i in action1.explain():
... print(i)
>>> action1 = Explicable()
>>> action1.postfix_tokens = Expression('42')
>>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')])
>>> m_history = Explicable().merge_history(action1, 12, op.add)
>>> m_history = Explicable().merge_history([action1, 12])
>>> m_history
<generator object transpose_fill ...>
>>> for i in m_history:
... print(i)
['2 + 10 \\times 4', 12, +]
['2 + 40', 12, +]
['42', 12, +]
['2 + 10 \\times 4', 12]
['2 + 40', 12]
['42', 12]
>>> action1 = Explicable()
>>> action1.postfix_tokens = Expression('42')
>>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')])
>>> action2 = Explicable()
>>> action2.postfix_tokens = Expression('24')
>>> action2.this_append_before([Expression('2*12')])
>>> action3 = Explicable()
>>> action3.postfix_tokens = Expression('0')
>>> action3.this_append_before([Expression('3-3')])
>>> 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']
"""
try:
steps1 = list(expl1.explain())
except AttributeError:
steps1 = [expl1]
steps = []
for expl in explicables:
try:
steps.append(list(expl.explain()))
except AttributeError:
steps.append([expl])
try:
steps2 = list(expl2.explain())
except AttributeError:
steps2 = [expl2]
return transpose_fill([steps1, steps2, [operation]])
return transpose_fill(steps)
class Explicable_int(int, Explicable):