Create pstf_factory and fix unittest! :D:D

This commit is contained in:
Benjamin Bertrand 2016-02-14 20:22:04 +03:00
parent c6ee2f2140
commit deb5be0955
2 changed files with 52 additions and 18 deletions

View File

@ -13,6 +13,53 @@ from .random_expression import RdExpression
__all__ = ['Expression']
def pstf_factory(pstf_tokens):
"""Factory which tranform postfix tokens list into an Expression or the simpliest object type ready to be rendered
:param pstf_tokens: a postfix tokens list
:returns: the object
>>> from .operator import op
>>> pstf_t = [2, 3, op.add]
>>> pstf_factory(pstf_t)
< <class 'pymath.calculus.expression.Expression'> [2, 3, '+'] >
>>> pstf_factory([2])
2
>>> type(pstf_factory([2]))
<class 'pymath.calculus.explicable.Explicable_int'>
>>> pstf_factory([2.45])
2.45
>>> type(pstf_factory([2.45]))
<class 'pymath.calculus.explicable.Explicable_float'>
>>> from .fraction import Fraction
>>> f = Fraction(1,2)
>>> pstf_factory([f])
< Fraction 1 / 2>
"""
try:
l_pstf_token = len(pstf_tokens)
except TypeError:
if isinstance(pstf_tokens[0], int):
return Explicable_int(pstf_tokens[0])
elif isinstance(pstf_tokens[0], float):
return Explicable_float(pstf_tokens[0])
elif hasattr(pstf_tokens[0], 'STR_RENDER'):
return pstf_tokens[0]
else:
return Expression(self)
else:
if l_pstf_token == 1:
if isinstance(pstf_tokens[0], int):
return Explicable_int(pstf_tokens[0])
elif isinstance(pstf_tokens[0], float):
return Explicable_float(pstf_tokens[0])
elif hasattr(pstf_tokens[0], 'STR_RENDER'):
return pstf_tokens[0]
else:
return Expression(self)
else:
return Expression(pstf_tokens)
class Expression(Explicable):
"""A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown"""
@ -31,7 +78,8 @@ class Expression(Explicable):
return Expression(random_generator(val_min, val_max))
@classmethod
def tmp_render(cls, render=lambda _, x: Expression(x)):
def tmp_render(cls, render=lambda _, x: pstf_factory(x)):
# def tmp_render(cls, render=lambda _, x: Expression(x)):
""" Same ad tmp_render for Renderable but default render is Expression
>>> exp = Expression("2*3/5")
@ -48,7 +96,7 @@ class Expression(Explicable):
... i
< <class 'pymath.calculus.expression.Expression'> [2, 3, 5, '/', '*'] >
< <class 'pymath.calculus.expression.Expression'> [2, < Fraction 3 / 5>, '*'] >
< <class 'pymath.calculus.expression.Expression'> [3, 5, '/', 2, '*'] >
< <class 'pymath.calculus.expression.Expression'> [< Fraction 3 / 5>, 2, '*'] >
< <class 'pymath.calculus.expression.Expression'> [3, 2, '*', 5, '/'] >
< <class 'pymath.calculus.expression.Expression'> [6, 5, '/'] >
>>> from .render import txt
@ -380,24 +428,13 @@ class Expression(Explicable):
def __neg__(self):
return Expression(self.postfix_tokens + [op.sub1])
class ExpressionError(Exception):
pass
class ComputeError(Exception):
pass
def untest(exp):
a = Expression(exp)
b = a.simplify()
for i in b.explain():
# print(type(i))
print(i)
#print(type(a.simplified()), ":", a.simplified())
print("\n")
if __name__ == '__main__':
print('\n')
A = Expression("( -8 x + 8 ) ( -8 - ( -6 x ) )")

View File

@ -126,10 +126,7 @@ class Polynom_deg2(Polynom):
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 1 \\times 3 }{ 3 \\times 3 } + \\frac{ -1 \\times 2 }{ 3 } + 1
\\frac{ 1 \\times 3 }{ 9 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 3 }{ 9 } + \\frac{ -2 }{ 3 } + 1
\\frac{ 1 \\times 3 }{ 3 \\times 3 } + \\frac{ -2 }{ 3 } + 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