I feel like I have solve the bug! Need to adapt doctest and tests now

This commit is contained in:
Lafrite 2015-04-21 08:37:25 +02:00
parent f81f2b47ce
commit d95b520ba0
2 changed files with 8 additions and 49 deletions

View File

@ -10,8 +10,6 @@ from .random_expression import RdExpression
__all__ = ['Expression'] __all__ = ['Expression']
class Expression(Explicable): class Expression(Explicable):
"""A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown""" """A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown"""
@ -125,27 +123,10 @@ class Expression(Explicable):
def simplify(self): def simplify(self):
""" Compute entirely the expression and return the result with .steps attribute """ """ Compute entirely the expression and return the result with .steps attribute """
#from .render import txt
#with Expression.tmp_render(txt):
# print("self -> ", self, " --> ", len(self.steps))
self.compute_exp() self.compute_exp()
#with Expression.tmp_render(txt):
# print("|-> self.child -> ", self.child, " --> ", len(self.child.steps))
self.simplified = self.child.simplify() self.simplified = self.child.simplify()
#print('|-- len(self.simplified.steps)=', len(self.child.steps) , " + ", len(self.simplified.steps))
self.simplified.steps = self.child.steps + self.simplified.steps self.simplified.steps = self.child.steps + self.simplified.steps
#if self.simplified != self.child:
# try:
# self.simplified.steps = self.child.steps + self.simplified.steps
# except AttributeError as e:
# #print(e)
# pass
#with Expression.tmp_render(txt):
# print("End self.simplified -> ", self.simplified, " --> ", len(self.simplified.steps))
return self.simplified return self.simplified
def compute_exp(self): def compute_exp(self):
@ -208,37 +189,16 @@ class Expression(Explicable):
tmpTokenList += tokenList tmpTokenList += tokenList
self.child = Expression(tmpTokenList) self.child = Expression(tmpTokenList)
#from .render import txt
#with Expression.tmp_render(txt):
# print('------------')
# print("self -> ", self)
# print("self.child -> ", self.child)
# print("self.child.steps -> ", [str(i) for i in self.child.steps], ' -- ', len(self.child.steps))
steps = self.develop_steps(tmpTokenList) steps = self.develop_steps(tmpTokenList)
#with Expression.tmp_render(txt):
# print('************')
# print("steps -> ", [str(i) for i in steps], ' -- ', len(steps))
# print('************')
#if steps !=[] and ini_step != steps[0]:
# self.child.steps = [ini_step] + steps
#else:
# self.child.steps = steps
if self.child.postfix_tokens == ini_step.postfix_tokens: if self.child.postfix_tokens == ini_step.postfix_tokens:
self.child.steps = steps self.child.steps = steps
else: else:
self.child.steps = [ini_step] + steps self.child.steps = [ini_step] + steps
#with Expression.tmp_render(txt):
# print('++++')
# print("self.child.steps -> ", [str(i) for i in self.child.steps], ' -- ', len(self.child.steps))
def develop_steps(self, tokenList): def develop_steps(self, tokenList):
""" From a list of tokens, it develops steps of each tokens """ """ From a list of tokens, it develops steps of each tokens """
# TODO: Attention les étapes sont dans le mauvais sens |lun. avril 20 10:06:03 CEST 2015 # TODO: Attention les étapes sont dans le mauvais sens |lun. avril 20 10:06:03 CEST 2015
print("---- develop_steps ------")
print("tokenList -> ", tokenList)
tmp_steps = [] tmp_steps = []
with Expression.tmp_render(): with Expression.tmp_render():
for t in tokenList: for t in tokenList:
@ -246,14 +206,12 @@ class Expression(Explicable):
tmp_steps.append([i for i in t.explain()]) tmp_steps.append([i for i in t.explain()])
else: else:
tmp_steps.append([t]) tmp_steps.append([t])
print("tmp_steps -> ", tmp_steps)
if max([len(i) for i in tmp_steps]) == 1: if max([len(i) for i in tmp_steps]) == 1:
# Cas où rien n'a dû être expliqué. # Cas où rien n'a dû être expliqué.
return [] return []
else: else:
tmp_steps = expand_list(tmp_steps)[:-1] tmp_steps = expand_list(tmp_steps)[:-1]
steps = [Expression(s) for s in tmp_steps] steps = [Expression(s) for s in tmp_steps]
print("steps -> ", steps)
return steps return steps
@classmethod @classmethod
@ -389,11 +347,11 @@ def untest(exp):
if __name__ == '__main__': if __name__ == '__main__':
#print('\n') #print('\n')
#A = Expression.random("( -8 x + 8 ) ( -8 - ( -6 x ) )") A = Expression.random("( -8 x + 8 ) ( -8 - ( -6 x ) )")
#Ar = A.simplify() Ar = A.simplify()
#print("Ar.steps -> ", Ar.steps) #print("Ar.steps -> ", Ar.steps)
#for i in Ar.steps: for i in Ar.steps:
# print(i) print(i)
#print("------------") #print("------------")
#for i in Ar.explain(): #for i in Ar.explain():
# print(i) # print(i)
@ -408,8 +366,8 @@ if __name__ == '__main__':
#for i in Ar.steps: #for i in Ar.steps:
# print(i) # print(i)
#print("------------") #print("------------")
#for i in Ar.explain(): for i in Ar.explain():
# print(i) print(i)
#print(type(Ar)) #print(type(Ar))

View File

@ -78,7 +78,7 @@ class Fraction(Explicable):
return n_frac return n_frac
else: else:
return self return copy(self)
@property @property
def postfix_tokens(self): def postfix_tokens(self):
@ -188,6 +188,7 @@ class Fraction(Explicable):
ans = exp.simplify() ans = exp.simplify()
ini_step = Expression(self.postfix_tokens + number.postfix_tokens + [op.add]) ini_step = Expression(self.postfix_tokens + number.postfix_tokens + [op.add])
ans.steps = [ini_step] + ans.steps ans.steps = [ini_step] + ans.steps
#print("\t\tIn add ans.steps -> ", ans.steps)
return ans return ans
def __radd__(self, other): def __radd__(self, other):