Add list of operators in operator.py (may need to change the form

This commit is contained in:
lafrite 2014-11-11 09:44:39 +01:00
parent 633e6b4e2c
commit 9c868b4e79
2 changed files with 27 additions and 3 deletions

View File

@ -18,16 +18,28 @@ class Expression(object):
: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.
""" """
if type(exp) == str: if type(exp) == str:
self._exp = exp #self._exp = exp
print("\t type(exp) :" + str(type(exp)))
self.postfix_tokens = str2tokens(exp) # les tokens seront alors stockés dans self.tokens temporairement self.postfix_tokens = str2tokens(exp) # les tokens seront alors stockés dans self.tokens temporairement
elif type(exp) == list: elif type(exp) == list:
print("\t type(exp) :" + str(type(exp)))
self.postfix_tokens = exp self.postfix_tokens = exp
print("\t self.postfix_tokens :" + str(self.postfix_tokens))
def __str__(self): def __str__(self):
""" """
Overload str Overload str
If you want to changer render set Expression.RENDER If you want to changer render set Expression.RENDER
""" """
print("\t self.STR_RENDER :" + str(self.STR_RENDER))
print("\t self.postfix_tokens :" + str(self.postfix_tokens))
print("\t self.STR_RENDER(self.postfix_tokens) :" + str(self.STR_RENDER(self.postfix_tokens)))
return self.STR_RENDER(self.postfix_tokens) return self.STR_RENDER(self.postfix_tokens)
def render(self, render = lambda x:str(x)): def render(self, render = lambda x:str(x)):
@ -122,8 +134,9 @@ class Expression(object):
def test(exp): def test(exp):
a = Expression(exp) a = Expression(exp)
for i in a.simplify(): print(a)
print(i) #for i in a.simplify():
# print(i)
print("\n") print("\n")
@ -132,6 +145,10 @@ if __name__ == '__main__':
exp = "2 ^ 3 * 5" exp = "2 ^ 3 * 5"
test(exp) test(exp)
from pymath.operator import add, pw, mul
exp = [2, 3, pw, 5, mul]
test(exp)
exp = "1 + 3 * 5" exp = "1 + 3 * 5"
test(exp) test(exp)

View File

@ -183,6 +183,13 @@ class Operator(str):
return flatten_list([op]) return flatten_list([op])
add = Operator("+")
sub = Operator("-")
mul = Operator("*")
div = Operator("/")
pw = Operator("^")
sub1 = Operator("-", 1)
par = Operator("(")
def save_mainOp(obj, mainOp): def save_mainOp(obj, mainOp):
"""Create a temporary class build over built-in type to stock the main operation of a calculus """Create a temporary class build over built-in type to stock the main operation of a calculus