diff --git a/pymath/calculus/explicable.py b/pymath/calculus/explicable.py index ad3ec61..bcb8558 100644 --- a/pymath/calculus/explicable.py +++ b/pymath/calculus/explicable.py @@ -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 >>> 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 >>> 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):