From 9c868b4e7992343a5434c328cfa8ec0a78bdaf6a Mon Sep 17 00:00:00 2001 From: lafrite Date: Tue, 11 Nov 2014 09:44:39 +0100 Subject: [PATCH] Add list of operators in operator.py (may need to change the form --- pymath/expression.py | 23 ++++++++++++++++++++--- pymath/operator.py | 7 +++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pymath/expression.py b/pymath/expression.py index 42b9906..9264ffb 100644 --- a/pymath/expression.py +++ b/pymath/expression.py @@ -18,16 +18,28 @@ class Expression(object): :param exp: the expression. It can be a string or a list of postfix tokens. """ 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 elif type(exp) == list: + print("\t type(exp) :" + str(type(exp))) self.postfix_tokens = exp + print("\t self.postfix_tokens :" + str(self.postfix_tokens)) + + def __str__(self): """ Overload str 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) def render(self, render = lambda x:str(x)): @@ -122,8 +134,9 @@ class Expression(object): def test(exp): a = Expression(exp) - for i in a.simplify(): - print(i) + print(a) + #for i in a.simplify(): + # print(i) print("\n") @@ -132,6 +145,10 @@ if __name__ == '__main__': exp = "2 ^ 3 * 5" test(exp) + from pymath.operator import add, pw, mul + exp = [2, 3, pw, 5, mul] + test(exp) + exp = "1 + 3 * 5" test(exp) diff --git a/pymath/operator.py b/pymath/operator.py index 944b25a..4b03bb7 100644 --- a/pymath/operator.py +++ b/pymath/operator.py @@ -183,6 +183,13 @@ class Operator(str): return flatten_list([op]) +add = Operator("+") +sub = Operator("-") +mul = Operator("*") +div = Operator("/") +pw = Operator("^") +sub1 = Operator("-", 1) +par = Operator("(") def save_mainOp(obj, mainOp): """Create a temporary class build over built-in type to stock the main operation of a calculus