cahnge Fake_int to Explicable_int

This commit is contained in:
Benjamin Bertrand 2016-02-13 10:30:19 +03:00
parent 47d43849d0
commit a5d84e89e3

View File

@ -14,17 +14,17 @@ from .random_expression import RdExpression
__all__ = ['Expression'] __all__ = ['Expression']
class Fake_int(int, Explicable): class Explicable_int(int, Explicable):
isNumber = True isNumber = True
def __init__(self, val): def __init__(self, val):
super(Fake_int, self).__init__(val) super(Explicable_int, self).__init__(val)
self._val = val self._val = val
self.postfix_tokens = [self] self.postfix_tokens = [self]
self.steps = [] self.steps = []
def simplify(self): def simplify(self):
return Fake_int(self._val) return Explicable_int(self._val)
class Expression(Explicable): class Expression(Explicable):
@ -94,12 +94,14 @@ class Expression(Explicable):
expression.postfix_tokens = str2tokens(exp) expression.postfix_tokens = str2tokens(exp)
elif isinstance(exp, list): elif isinstance(exp, list):
# Ici on ne peut convertir les "+" en opérateur que s'ils sont # Ici on ne peut convertir les "+-*/..." en opérateur que s'ils sont
# d'arité 2. # d'arité 2.
exp_mod_op = [ exp_mod_op = [
op.get_op(i) if op.can_be_operator(i) else i for i in exp] op.get_op(i) if op.can_be_operator(i) else i for i in exp
]
expression.postfix_tokens = flatten_list( expression.postfix_tokens = flatten_list(
[tok.postfix_tokens if Expression.isExpression(tok) else tok for tok in exp_mod_op]) [tok.postfix_tokens if Expression.isExpression(tok) else tok for tok in exp_mod_op]
)
elif isinstance(exp, Expression): elif isinstance(exp, Expression):
return exp return exp
@ -110,31 +112,23 @@ class Expression(Explicable):
else: else:
raise ValueError( raise ValueError(
"Can't build Expression with {} object".format( "Can't build Expression with {} object".format(
type(exp))) type(exp)
)
)
if len(expression.postfix_tokens) == 1: if len(expression.postfix_tokens) == 1:
token = expression.postfix_tokens[0] token = expression.postfix_tokens[0]
if isinstance(token, Fake_int) or isinstance(token, int): if isinstance(token, Explicable_int) or isinstance(token, int):
return Fake_int(token) return Explicable_int(token)
elif hasattr(token, 'simplify') and hasattr(token, 'explain'): elif hasattr(token, 'simplify') and hasattr(token, 'explain'):
ans = expression.postfix_tokens[0] ans = expression.postfix_tokens[0]
return ans return ans
elif isinstance(token, str): elif isinstance(token, str):
# TODO: Pourquoi ne pas créer directement un polynom ici? |jeu. févr. 26 18:59:24 CET 2015 from .polynom import Polynom
# On crée un faux str en ajoutant la méthode simplify et return Polynom([0,1], letter = token)
# simplified et la caractérisique isNumber
simplify = lambda x: [x]
is_polynom = True
methods_attr = {
'simplify': simplify,
'_isPolynom': is_polynom,
'postfix_tokens': [token]}
fake_token = type(
'fake_str', (str, Explicable, ), methods_attr)(token)
return fake_token
else: else:
raise ValueError( raise ValueError(