new init for Expression

This commit is contained in:
Benjamin Bertrand 2016-03-09 10:50:25 +03:00
parent f5c64ce9e4
commit 19069a41f7

View File

@ -107,16 +107,15 @@ class Expression(Explicable):
""" """
return super(Expression, cls).tmp_render(render) return super(Expression, cls).tmp_render(render)
def __new__(cls, exp): def __init__(self, exp):
"""Create Expression objects """Create Expression objects
:param exp: the expression. It can be a string or a list of postfix tokens. :param exp: the expression. It can be a string or a list of postfix tokens.
""" """
expression = object.__new__(cls)
if isinstance(exp, str): if isinstance(exp, str):
expression.postfix_tokens = str2tokens(exp) pstf_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
@ -124,15 +123,18 @@ class Expression(Explicable):
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( pstf_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 pstf_tokens = exp.postfix_tokens
elif isNumerand(exp): elif isNumerand(exp):
expression.postfix_tokens = [exp] pstf_tokens = [exp]
else: else:
raise ValueError( raise ValueError(
@ -141,8 +143,9 @@ class Expression(Explicable):
) )
) )
expression._isExpression = 1 super(Expression, self).__init__(pstf_tokens)
return expression self.steal_history(exp)
self._isExpression = 1
def __str__(self): def __str__(self):
""" """
@ -269,7 +272,7 @@ class Expression(Explicable):
[< Expression [1, 2, +, 3, 4, *, 5, +, +, 6, 7, +, *]>, < Expression [3, 12, 5, +, +, 13, *]>] [< Expression [1, 2, +, 3, 4, *, 5, +, +, 6, 7, +, *]>, < Expression [3, 12, 5, +, +, 13, *]>]
""" """
with Expression.tmp_render(): with Expression.tmp_render():
tmp_steps = list(Explicable().merge_history(tokenList)) tmp_steps = list(Explicable.merge_history(tokenList))
tmp_steps = list(tmp_steps)[:-1] tmp_steps = list(tmp_steps)[:-1]
steps = [Expression(s) for s in tmp_steps] steps = [Expression(s) for s in tmp_steps]