Merge_history (from Explicable) and develop_steps (from Expression)

work together
This commit is contained in:
Benjamin Bertrand 2016-03-07 13:05:12 +03:00
parent bfef87424d
commit 976cbbdf03
2 changed files with 47 additions and 24 deletions

View File

@ -19,12 +19,12 @@ class Explicable(Renderable):
def __init__(self, *args, **kwargs):
super(Explicable, self).__init__()
self.steps = []
self.explainator = 0
self.explained = 0
def explain(self):
r""" Generate and render steps which leed to itself
After beening explained, the Explicable become amnesiac.
After beening explained, the Explicable become amnesiac. It will return itself in a list.
See 'history_generator' to explain it once again.
>>> action = Explicable()
@ -39,12 +39,19 @@ class Explicable(Renderable):
>>> # Now action is amnesiac
>>> for i in action.explain():
... print(i)
42
>>> action = Explicable()
>>> action.postfix_tokens = Expression('42')
>>> for i in action.explain():
... print(i)
42
"""
if not self.explainator:
self.explainator = self.history_generator()
return self.explainator
if self.explained:
return [self.STR_RENDER(self.postfix_tokens)]
else:
self.explained = 1
return self.history_generator()
def history_generator(self):
r""" Generator for rendered steps which leed to itself
@ -145,9 +152,14 @@ class Explicable(Renderable):
['2 + 10 \\times 4', '2 \\times 12']
['2 + 40', '24']
['42', '24']
>>> # Now they are amnesiac
>>> for i in action1.explain():
... print(i)
42
>>> m_history = Explicable().merge_history([action1, action2])
>>> for i in m_history:
... print(i)
['42', '24']
>>> action1 = Explicable()
>>> action1.postfix_tokens = Expression('42')
>>> action1.this_append_before([Expression('2+10*4'), Expression('2+40')])
@ -174,6 +186,10 @@ class Explicable(Renderable):
['2 + 10 \\times 4', '2 \\times 12', '3 - 3']
['2 + 40', '24', '0']
['42', '24', '0']
>>> m_history = Explicable().merge_history([1,2,3])
>>> for i in m_history:
... print(i)
[1, 2, 3]
"""
steps = []
for expl in explicables:

View File

@ -252,26 +252,33 @@ class Expression(Explicable):
@classmethod
def develop_steps(cls, tokenList):
""" From a list of tokens, it develops steps of each tokens """
tmp_steps = []
for t in tokenList:
try:
with Expression.tmp_render():
steps = [i for i in t.explain()]
if steps:
tmp_steps.append(steps)
else:
tmp_steps.append([t])
except AttributeError:
tmp_steps.append([t])
"""
From a list of tokens, it develops steps of each tokens and transpose it into steps respecting the stucture of the tokenList.
if max([len(i) for i in tmp_steps]) > 1:
tmp_steps = expand_list(tmp_steps)[:-1]
It try to use 'explain' method for every tokens. After using this methode, tokens becom amnesiac.
>>> e = Expression('1+2')
>>> e1 = e.simplify()
>>> f = Expression('3*4+5')
>>> f1 = f.simplify()
>>> Expression.develop_steps([e1, f1, op.add])
[< <class 'pymath.calculus.expression.Expression'> [1, 2, +, 3, 4, *, 5, +, +] >, < <class 'pymath.calculus.expression.Expression'> [3, 12, 5, +, +] >]
>>> e = Expression('1+2')
>>> e1 = e.simplify()
>>> f = Expression('3*4+5')
>>> f1 = f.simplify()
>>> g = Expression('6+7')
>>> g1 = g.simplify()
>>> Expression.develop_steps([e1, f1, op.add, g1, op.mul])
[< <class 'pymath.calculus.expression.Expression'> [1, 2, +, 3, 4, *, 5, +, +, 6, 7, +, *] >, < <class 'pymath.calculus.expression.Expression'> [3, 12, 5, +, +, 13, *] >]
"""
with Expression.tmp_render():
tmp_steps = list(Explicable().merge_history(tokenList))
tmp_steps = list(tmp_steps)[:-1]
steps = [Expression(s) for s in tmp_steps]
return steps
return []
@classmethod
def isExpression(cls, other):
try: