solve render bug with (..) - (..) to .. - (..)

This commit is contained in:
Lafrite 2015-03-07 19:35:34 +01:00
parent c00c0a86cc
commit cdfd70d3f3

View File

@ -54,8 +54,8 @@ class Operator(str):
ans = link.format(op1 = op1) ans = link.format(op1 = op1)
elif self.arity == 2: elif self.arity == 2:
op1 = self.r_parenthesis(args[0], True) op1 = self.l_parenthesis(args[0], True)
op2 = self.l_parenthesis(args[1], True) op2 = self.r_parenthesis(args[1], True)
ans = link.format(op1 = op1, op2 = op2) ans = link.format(op1 = op1, op2 = op2)
ans = save_mainOp(ans, self) ans = save_mainOp(ans, self)
@ -144,8 +144,8 @@ class Operator(str):
ans = save_mainOp(ans, self) ans = save_mainOp(ans, self)
return ans return ans
def l_parenthesis(self, op, str_join=False): def all_parenthesis(self, op, str_join=False):
""" Add parenthesis for left operand if necessary """ """ Add parenthesis if necessary (left and rigth)"""
try: try:
if op.mainOp.priority < self.priority: if op.mainOp.priority < self.priority:
op = flatten_list(["(", op, ")"]) op = flatten_list(["(", op, ")"])
@ -161,9 +161,13 @@ class Operator(str):
ans = ' '.join([str(i) for i in ans]) ans = ' '.join([str(i) for i in ans])
return ans return ans
def l_parenthesis(self, op, str_join=False):
""" Add parenthesis for left operand if necessary """
return self.all_parenthesis(op, str_join)
def r_parenthesis(self, op, str_join=False): def r_parenthesis(self, op, str_join=False):
""" Add parenthesis for left operand if necessary """ """ Add parenthesis for left operand if necessary """
return self.l_parenthesis(op, str_join) return self.all_parenthesis(op, str_join)
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
@ -196,6 +200,7 @@ def operatorize(fun):
* "_render": action use in __txt__ and __tex__ * "_render": action use in __txt__ and __tex__
* "__txt__": txt rendering * "__txt__": txt rendering
* "__tex__": tex rendering * "__tex__": tex rendering
* "all_parenthesis": mechanism to add parenthesis for left or rigth operande
* "l_parenthesis": mechanism to add parenthesis for left operande * "l_parenthesis": mechanism to add parenthesis for left operande
* "r_parenthesis": mechanism to add parenthesis for rigth operande * "r_parenthesis": mechanism to add parenthesis for rigth operande
""" """
@ -303,7 +308,11 @@ class op(object):
'1 - 2' '1 - 2'
>>> sub.__tex__('1','-2') >>> sub.__tex__('1','-2')
'1 - (-2)' '1 - (-2)'
>>> sub.__tex__('-1','2')
'i-1 - 2'
""" """
def l_parenthesis(self, op, str_join=False):
return op
caract = { caract = {
"operator" : "-", \ "operator" : "-", \
"name" : "sub",\ "name" : "sub",\
@ -312,6 +321,7 @@ class op(object):
"actions" : ("__sub__","__rsub__"), \ "actions" : ("__sub__","__rsub__"), \
"txt" : "{op1} - {op2}",\ "txt" : "{op1} - {op2}",\
"tex" : "{op1} - {op2}",\ "tex" : "{op1} - {op2}",\
"l_parenthesis": l_parenthesis,\
} }
return caract return caract