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

View File

@ -122,57 +122,67 @@ class Explicable(Renderable):
self.steps = steps + self.steps self.steps = steps + self.steps
@staticmethod @staticmethod
def merge_history(expl1, expl2, operation): def merge_history(explicables):
r""" Take two 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 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 explicables: list
:param expl2: second explicable
:param operation: the operation that link them
:returns: the list of steps :returns: the list of steps
>>> from .expression import Expression >>> from .expression import Expression
>>> from .operator import op
>>> action1 = Explicable() >>> action1 = Explicable()
>>> action1.postfix_tokens = Expression('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()
>>> action2.postfix_tokens = Expression('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, op.add) >>> 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:
... print(i) ... print(i)
['2 + 10 \\times 4', '2 \\times 12', +] ['2 + 10 \\times 4', '2 \\times 12']
['2 + 40', '24', +] ['2 + 40', '24']
['42', '24', +] ['42', '24']
>>> for i in action1.explain(): >>> for i in action1.explain():
... print(i) ... print(i)
>>> action1 = Explicable() >>> action1 = Explicable()
>>> action1.postfix_tokens = Expression('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, op.add) >>> 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:
... print(i) ... print(i)
['2 + 10 \\times 4', 12, +] ['2 + 10 \\times 4', 12]
['2 + 40', 12, +] ['2 + 40', 12]
['42', 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: steps = []
steps1 = list(expl1.explain()) for expl in explicables:
except AttributeError: try:
steps1 = [expl1] steps.append(list(expl.explain()))
except AttributeError:
steps.append([expl])
try: return transpose_fill(steps)
steps2 = list(expl2.explain())
except AttributeError:
steps2 = [expl2]
return transpose_fill([steps1, steps2, [operation]])
class Explicable_int(int, Explicable): class Explicable_int(int, Explicable):