add_parenthesis to l_parenthesis and r_parenthesis
This commit is contained in:
parent
4841aef478
commit
c00c0a86cc
@ -49,9 +49,15 @@ class Operator(str):
|
||||
:returns: the string with operator and operands
|
||||
|
||||
"""
|
||||
replacement = {"op"+str(i+1): ' '.join(self.add_parenthesis(op)) for (i,op) in enumerate(args)}
|
||||
|
||||
ans = link.format(**replacement)
|
||||
if self.arity == 1:
|
||||
op1 = self.l_parenthesis(args[0], True)
|
||||
ans = link.format(op1 = op1)
|
||||
|
||||
elif self.arity == 2:
|
||||
op1 = self.r_parenthesis(args[0], True)
|
||||
op2 = self.l_parenthesis(args[1], True)
|
||||
ans = link.format(op1 = op1, op2 = op2)
|
||||
|
||||
ans = save_mainOp(ans, self)
|
||||
return ans
|
||||
|
||||
@ -127,24 +133,19 @@ class Operator(str):
|
||||
# TODO: Attention à gestion des fractions qui se comportent chelou avec les parenthèses |dim. nov. 9 09:21:52 CET 2014
|
||||
if self.arity == 1:
|
||||
# TODO: Marche juste avec -, il faudra voir quand il y aura d'autres operateurs unitaires |dim. nov. 9 09:24:53 CET 2014
|
||||
op1 = self.add_parenthesis(args[0])
|
||||
op1 = self.l_parenthesis(args[0])
|
||||
ans = flatten_list([self, op1])
|
||||
|
||||
elif self.arity == 2:
|
||||
op1 = self.add_parenthesis(args[0])
|
||||
op2 = self.add_parenthesis(args[1])
|
||||
op1 = self.l_parenthesis(args[0])
|
||||
op2 = self.r_parenthesis(args[1])
|
||||
ans = flatten_list([op1, self, op2])
|
||||
|
||||
ans = save_mainOp(ans, self)
|
||||
return ans
|
||||
|
||||
def add_parenthesis(self, op):
|
||||
""" Add parenthesis if necessary
|
||||
|
||||
>>> from pymath.polynom import Polynom
|
||||
>>> P = Polynom([1,2,3])
|
||||
|
||||
"""
|
||||
def l_parenthesis(self, op, str_join=False):
|
||||
""" Add parenthesis for left operand if necessary """
|
||||
try:
|
||||
if op.mainOp.priority < self.priority:
|
||||
op = flatten_list(["(", op, ")"])
|
||||
@ -155,7 +156,14 @@ class Operator(str):
|
||||
op = ['(', op, ')']
|
||||
except ValueError:
|
||||
pass
|
||||
return flatten_list([op])
|
||||
ans = flatten_list([op])
|
||||
if str_join:
|
||||
ans = ' '.join([str(i) for i in ans])
|
||||
return ans
|
||||
|
||||
def r_parenthesis(self, op, str_join=False):
|
||||
""" Add parenthesis for left operand if necessary """
|
||||
return self.l_parenthesis(op, str_join)
|
||||
|
||||
def save_mainOp(obj, mainOp):
|
||||
"""Create a temporary class build over built-in type to stock the main operation of a calculus
|
||||
@ -188,7 +196,8 @@ def operatorize(fun):
|
||||
* "_render": action use in __txt__ and __tex__
|
||||
* "__txt__": txt rendering
|
||||
* "__tex__": tex rendering
|
||||
* "add_parenthesis": mechanism to add parenthesis
|
||||
* "l_parenthesis": mechanism to add parenthesis for left operande
|
||||
* "r_parenthesis": mechanism to add parenthesis for rigth operande
|
||||
"""
|
||||
def mod_fun(self, *args):
|
||||
ans = fun(self, *args)
|
||||
@ -324,7 +333,7 @@ class op(object):
|
||||
>>> sub1.__tex__('-1')
|
||||
'- (-1)'
|
||||
"""
|
||||
def add_parenthesis(self, op):
|
||||
def l_parenthesis(self, op, str_join=False):
|
||||
""" Add parenthesis if necessary """
|
||||
try:
|
||||
if op.mainOp.priority <= self.priority:
|
||||
@ -336,7 +345,11 @@ class op(object):
|
||||
op = ['(', op, ')']
|
||||
except ValueError:
|
||||
pass
|
||||
return flatten_list([op])
|
||||
|
||||
ans = flatten_list([op])
|
||||
if str_join:
|
||||
ans = ' '.join([str(i) for i in ans])
|
||||
return ans
|
||||
|
||||
caract = {
|
||||
"operator" : "-", \
|
||||
@ -346,7 +359,7 @@ class op(object):
|
||||
"actions" : "__neg__",\
|
||||
"txt" : "- {op1}",\
|
||||
"tex" : "- {op1}",\
|
||||
"add_parenthesis": add_parenthesis,\
|
||||
"l_parenthesis": l_parenthesis,\
|
||||
}
|
||||
|
||||
return caract
|
||||
@ -371,15 +384,17 @@ class op(object):
|
||||
# * can not be display in some cases
|
||||
def _render(self, link, *args):
|
||||
|
||||
replacement = {"op"+str(i+1): ' '.join(self.add_parenthesis(op)) for (i,op) in enumerate(args)}
|
||||
op1 = self.r_parenthesis(args[0], True)
|
||||
op2 = self.l_parenthesis(args[1], True)
|
||||
ans = link.format(op1 = op1, op2 = op2)
|
||||
|
||||
if not self.visibility or args[1][0] == "(" or \
|
||||
(type(args[1][0]) == str and args[1][0].isalpha()):
|
||||
ans = "{op1} {op2}".format(**replacement)
|
||||
if not self.visibility or op2[0] == "(" or \
|
||||
(type(op2[0]) == str and op2[0].isalpha()):
|
||||
ans = "{op1} {op2}".format(op1 = op1, op2 = op2)
|
||||
ans = save_mainOp(ans, self)
|
||||
return ans
|
||||
else:
|
||||
ans = link.format(**replacement)
|
||||
ans = link.format(op1 = op1, op2 = op2)
|
||||
ans = save_mainOp(ans, self)
|
||||
return ans
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user