change op.add and op.sub for better - management

This commit is contained in:
Lafrite 2015-03-28 09:09:32 +01:00
parent 4ca14c078e
commit b0e506897a
2 changed files with 30 additions and 3 deletions

View File

@ -281,8 +281,32 @@ class op(object):
>>> add.__txt__('1','2')
'1 + 2'
>>> add.__tex__('1','-2')
'1 + (-2)'
'1 - 2'
"""
def _render(self, link, *args):
"""Global step for __txt__ and __tex__
:param link: the link between operators
:param *args: the operands
:returns: the string with operator and operands
"""
if args[1][0] == "-":
op1 = self.l_parenthesis(args[0], True)
op2 = self.r_parenthesis(args[1][1:], True)
ans = link.replace('+','-').format(op1 = op1, op2 = op2)
ans = save_mainOp(ans, self)
return ans
else:
op1 = self.l_parenthesis(args[0], True)
op2 = self.r_parenthesis(args[1], True)
ans = link.format(op1 = op1, op2 = op2)
ans = save_mainOp(ans, self)
return ans
caract = {
"operator" : "+", \
"name" : "add",\
@ -291,6 +315,7 @@ class op(object):
"actions" : ("__add__","__radd__"), \
"txt" : "{op1} + {op2}",\
"tex" : "{op1} + {op2}",\
"_render": _render,\
}
return caract
@ -321,6 +346,8 @@ class op(object):
try:
if op.mainOp.priority <= self.priority:
op = flatten_list(["(", op, ")"])
elif op[0] == '-':
op = flatten_list(["(", op, ")"])
except AttributeError:
# op has not the attribute priority
try:

View File

@ -33,7 +33,7 @@ class TestTexRender(unittest.TestCase):
[-2, 3, op.add ],
]
wanted_render = [ "2 + 3",
"2 + ( -3 )",
"2 - 3",
"-2 + 3",
]
for (i,e) in enumerate(exps):
@ -49,7 +49,7 @@ class TestTexRender(unittest.TestCase):
wanted_render = [ "2 + a",
"a + 3",
"-2 + a",
"a + ( -2 )",
"a - 2",
]
for (i,e) in enumerate(exps):
rend = tex(e)