solve unittest bugs and undetected develop_steps bug

This commit is contained in:
Benjamin Bertrand 2016-03-07 11:43:33 +03:00
parent 190b247fe4
commit 142eaa9c32
5 changed files with 49 additions and 43 deletions

View File

@ -55,6 +55,9 @@ class Explicable(Renderable):
>>> action = Explicable()
>>> from .expression import Expression
>>> action.postfix_tokens = Expression('42')
>>> for i in action.history_generator():
... print(i)
42
>>> action.this_append_before([Expression('2+10*4'), Expression('2+40')])
>>> for i in action.history_generator():
... print(i)

View File

@ -256,13 +256,19 @@ class Expression(Explicable):
for t in tokenList:
try:
with Expression.tmp_render():
tmp_steps.append([i for i in t.explain()])
steps = [i for i in t.explain()]
if steps:
tmp_steps.append(steps)
else:
tmp_steps.append([t])
except AttributeError:
tmp_steps.append([t])
if max([len(i) for i in tmp_steps]) == 1:
# Cas où rien n'a dû être expliqué.
return []
elif min([len(i) for i in tmp_steps]) == 0:
raise ValueError('tmp_steps: {} \ntokenList: {}'.format(tmp_steps, tokenList))
else:
tmp_steps = expand_list(tmp_steps)[:-1]
steps = [Expression(s) for s in tmp_steps]

View File

@ -134,32 +134,6 @@ def last_elem(ll):
return ll
def expand_list(list_list):
"""Expand list of list
>>> expand_list([1,2,[3,4],5,[6,7,8]])
[[1, 2, 3, 5, 6], [1, 2, 4, 5, 7], [1, 2, 4, 5, 8]]
>>> expand_list([1,2,4,5,6,7,8])
[[1, 2, 4, 5, 6, 7, 8]]
"""
list_in_list = [i for i in list_list if isinstance(i, list)].copy()
try:
nbr_ans_list = max([len(i) for i in list_in_list])
ans = [list_list.copy() for i in range(nbr_ans_list)]
for (i, l) in enumerate(ans):
for (j, e) in enumerate(l):
if isinstance(e, list):
ans[i][j] = e[min(i, len(e) - 1)]
# S'il n'y a pas de liste dans la liste (2e exemple)
except ValueError:
ans = [list_list]
return ans
def add_in_dict(dict1, dict2):
"""Merge dictionary keys and add the content from dict1 and dict2
@ -267,6 +241,24 @@ def spe_zip(l1, l2):
return ans
def expand_list(list_list):
"""Expand list of list
>>> expand_list([1,2,[3,4],5,[6,7,8]])
[[1, 2, 3, 5, 6], [1, 2, 4, 5, 7], [1, 2, 4, 5, 8]]
>>> expand_list([1,2,4,5,6,7,8])
[[1, 2, 4, 5, 6, 7, 8]]
"""
list_in_list = [
i if isinstance(i,list)
else [i]
for i in list_list
]
return list(transpose_fill(list_in_list))
def transpose_fill(list_lists):
"""Transpose a list of list and if inside list have not the same length, fill with last token

View File

@ -14,7 +14,9 @@ __all__ = ["Polynom"]
def polynom_factory(func):
""" Decorator which specify the type of polynom that the function returns """
"""
Decorator which specify the type of polynom that the function returns
"""
@wraps(func)
def wrapper(*args, **kwrds):
P = func(*args, **kwrds)
@ -146,7 +148,10 @@ class Polynom(AbstractPolynom):
>>> R = P(Q)
"""
postfix_exp = [
Expression(value) if i == self._letter else i for i in self.postfix_tokens]
Expression(value) if i == self._letter
else i
for i in self.postfix_tokens
]
return Expression(postfix_exp).simplify()

View File

@ -114,26 +114,26 @@ class Polynom_deg2(Polynom):
@property
def beta(self):
""" Compute beta the extremum of self
r""" Compute beta the extremum of self
>>> P = Polynom_deg2([1,2,3])
>>> P.beta
< Fraction 2 / 3>
>>> for i in P.beta.explain(): # Ça serait bien que l'on puisse enlever des étapes maintenant...
... print(i)
3 \\times ( \\frac{ -1 }{ 3 } )^{ 2 } + 2 \\times \\frac{ -1 }{ 3 } + 1
3 \\times ( \\frac{ -1 }{ 3 } )^{ 2 } + \\frac{ -1 }{ 3 } \\times 2 + 1
3 \\times \\frac{ ( -1 )^{ 2 } }{ 3^{ 2 } } + \\frac{ -1 \\times 2 }{ 3 } + 1
3 \\times \\frac{ 1 }{ 9 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 }{ 9 } \\times 3 + \\frac{ -1 }{ 3 } \\times 2 + 1
\\frac{ 1 \\times 3 }{ 3 \\times 3 } + \\frac{ -1 \\times 2 }{ 3 } + 1
\\frac{ 1 }{ 3 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 - 2 }{ 3 } + 1
\\frac{ -1 }{ 3 } + 1
\\frac{ -1 \\times 1 }{ 3 \\times 1 } + \\frac{ 1 \\times 3 }{ 1 \\times 3 }
\\frac{ -1 }{ 3 } + \\frac{ 3 }{ 3 }
\\frac{ -1 + 3 }{ 3 }
\\frac{ 2 }{ 3 }
3 \times ( \frac{ -1 }{ 3 } )^{ 2 } + 2 \times \frac{ -1 }{ 3 } + 1
3 \times ( \frac{ -1 }{ 3 } )^{ 2 } + \frac{ -1 }{ 3 } \times 2 + 1
3 \times \frac{ ( -1 )^{ 2 } }{ 3^{ 2 } } + \frac{ -1 \times 2 }{ 3 } + 1
3 \times \frac{ 1 }{ 9 } + \frac{ -2 }{ 3 } + 1
\frac{ 1 }{ 9 } \times 3 + \frac{ -2 }{ 3 } + 1
\frac{ 1 \times 3 }{ 3 \times 3 } + \frac{ -2 }{ 3 } + 1
\frac{ 1 }{ 3 } + \frac{ -2 }{ 3 } + 1
\frac{ 1 - 2 }{ 3 } + 1
\frac{ -1 }{ 3 } + 1
\frac{ -1 \times 1 }{ 3 \times 1 } + \frac{ 1 \times 3 }{ 1 \times 3 }
\frac{ -1 }{ 3 } + \frac{ 3 }{ 3 }
\frac{ -1 + 3 }{ 3 }
\frac{ 2 }{ 3 }
"""
return self(self.alpha)