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

@@ -252,25 +252,32 @@ 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]
steps = [Expression(s) for s in tmp_steps]
return steps
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))
return []
tmp_steps = list(tmp_steps)[:-1]
steps = [Expression(s) for s in tmp_steps]
return steps
@classmethod
def isExpression(cls, other):