Create merge_history method for Explicable

This commit is contained in:
Benjamin Bertrand 2016-03-07 09:58:22 +03:00
parent b241142976
commit 29d0234050

View File

@ -3,6 +3,7 @@
from .render import Renderable
from decimal import Decimal
from .generic import transpose_fill
class Explicable(Renderable):
@ -88,6 +89,40 @@ class Explicable(Renderable):
"""
self.steps = steps + self.steps
@staticmethod
def merge_history(expl1, expl2, operation):
""" Take two Explicable objects, explain where they are from and merge their history
/!\ Using this method make the two objects amnesiac
:param expl1: fist explicable
:param expl2: second explicable
:param operation: the operation that link them
: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
<generator object transpose_fill ...>
>>> for i in m_history:
... print(i)
['2 + 10 \\times 4', '2 \\times 12', +]
['2 + 40', '24', +]
['42', '24', +]
"""
steps1 = list(expl1.explain())
steps2 = list(expl2.explain())
return transpose_fill([steps1, steps2, [operation]])
class Explicable_int(int, Explicable):