Clear class and static class in order to be used in render

This commit is contained in:
lafrite 2013-11-17 08:53:05 +01:00
parent 25dc4e4845
commit db50fdb9ec

View File

@ -160,7 +160,7 @@ class Expression(object):
return self._infix_tokens return self._infix_tokens
elif self._postfix_tokens: elif self._postfix_tokens:
self.post2in_fix() self._infix_tokens = self.post2in_fix(self._postfix_tokens)
return self._infix_tokens return self._infix_tokens
else: else:
@ -180,7 +180,7 @@ class Expression(object):
return self._postfix_tokens return self._postfix_tokens
elif self._infix_tokens: elif self._infix_tokens:
self.in2post_fix() self._postfix_tokens = self.in2post_fix(self._infix_tokens)
return self._postfix_tokens return self._postfix_tokens
else: else:
@ -193,24 +193,16 @@ class Expression(object):
# ---------------------- # ----------------------
# "fix" tranformations # "fix" tranformations
#@classmethod @classmethod
def in2post_fix(self, infix_tokens = None): def in2post_fix(self, infix_tokens):
""" From the self.infix_tokens list compute the corresponding self.postfix_tokens list """ From the infix_tokens list compute the corresponding postfix_tokens list
@param infix_tokens: the infix list of tokens to transform into postfix form. If nothing is set, it takes the value self.infix_tokens @param infix_tokens: the infix list of tokens to transform into postfix form.
@return: the corresponding postfix list of tokens if infix_tokens is set. nothing otherwise but stock it in self.postfix_tokens @return: the corresponding postfix list of tokens.
>>> Expression.in2post_fix(['(', 2, '+', 5, '-', 1, ')', '/', '(', 3, '*', 4, ')']) >>> Expression.in2post_fix(['(', 2, '+', 5, '-', 1, ')', '/', '(', 3, '*', 4, ')'])
[2, 5, '+', 1, '-', 3, 4, '*', '/'] [2, 5, '+', 1, '-', 3, 4, '*', '/']
""" """
arg = 1
if infix_tokens == None:
arg = 0
infix_tokens = self._infix_tokens
# TODO: Trouver une autre méthode pour ne pas à avoir à utiliser cet attribu privé |dim. nov. 3 07:24:31 CET 2013
opStack = Stack() opStack = Stack()
postfixList = [] postfixList = []
@ -233,26 +225,18 @@ class Expression(object):
while not opStack.isEmpty(): while not opStack.isEmpty():
postfixList.append(opStack.pop()) postfixList.append(opStack.pop())
if not arg: return postfixList
self.postfix_tokens = postfixList
else:
return postfixList
#@classmethod @classmethod
def post2in_fix(self, postfix_tokens = None): def post2in_fix(self, postfix_tokens):
""" From the self.postfix_tokens list compute the corresponding self.infix_tokens list """ From the postfix_tokens list compute the corresponding infix_tokens list
@param postfix_tokens: the postfix list of tokens to transform into infix form. If nothing is set, it takes the value self.postfix_tokens @param postfix_tokens: the postfix list of tokens to transform into infix form.
@return: the corresponding infix list of tokens if postfix_tokens is set. nothing otherwise but stock it in self.infix_tokens @return: the corresponding infix list of tokens if postfix_tokens.
>>> Expression.post2in_fix([2, 5, '+', 1, '-', 3, 4, '*', '/']) >>> Expression.post2in_fix([2, 5, '+', 1, '-', 3, 4, '*', '/'])
['( ', 2, '+', 5, '-', 1, ' )', '/', '( ', 3, '*', 4, ' )'] ['( ', 2, '+', 5, '-', 1, ' )', '/', '( ', 3, '*', 4, ' )']
""" """
arg = 1
if postfix_tokens == None:
arg = 0
postfix_tokens = self._postfix_tokens
operandeStack = Stack() operandeStack = Stack()
for token in postfix_tokens: for token in postfix_tokens:
@ -272,10 +256,7 @@ class Expression(object):
infix_tokens = flatten_list(operandeStack.pop()) infix_tokens = flatten_list(operandeStack.pop())
if not arg: return infix_tokens
self.infix_tokens = infix_tokens
else:
return infix_tokens
# --------------------- # ---------------------
# Tools for placing parenthesis in infix notation # Tools for placing parenthesis in infix notation
@ -333,8 +314,8 @@ class Expression(object):
## --------------------- ## ---------------------
## Computing the expression ## Computing the expression
@classmethod @staticmethod
def doMath(self, op, op1, op2): def doMath(op, op1, op2):
"""Compute "op1 op op2" or create a fraction """Compute "op1 op op2" or create a fraction
:param op: operator :param op: operator
@ -354,8 +335,8 @@ class Expression(object):
## --------------------- ## ---------------------
## Recognize numbers and operators ## Recognize numbers and operators
@classmethod @staticmethod
def isNumber(self, exp): def isNumber(exp):
"""Check if the expression can be a number """Check if the expression can be a number
:param exp: an expression :param exp: an expression
@ -364,8 +345,8 @@ class Expression(object):
""" """
return type(exp) == int or type(exp) == Fraction return type(exp) == int or type(exp) == Fraction
@classmethod @staticmethod
def isOperator(self, exp): def isOperator(exp):
"""Check if the expression is an opération in "+-*/" """Check if the expression is an opération in "+-*/"
:param exp: an expression :param exp: an expression
@ -413,23 +394,26 @@ if __name__ == '__main__':
#exp = "2 + 5 * ( 3 - 4 )" #exp = "2 + 5 * ( 3 - 4 )"
#test(exp) #test(exp)
exp = "( 2 + 5 ) * ( 3 - 4 )" #exp = "( 2 + 5 ) * ( 3 - 4 )"
test(exp) #test(exp)
exp = "( 2 + 5 ) * ( 3 * 4 )" #exp = "( 2 + 5 ) * ( 3 * 4 )"
test(exp) #test(exp)
exp = "( 2 + 5 - 1 ) / ( 3 * 4 )" #exp = "( 2 + 5 - 1 ) / ( 3 * 4 )"
test(exp) #test(exp)
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12" #exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12"
test(exp) #test(exp)
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 2" #exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 2"
test(exp) #test(exp)
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12 + 5 * 5" #exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12 + 5 * 5"
test(exp) #test(exp)
import doctest
doctest.testmod()
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'