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') >>> add.__txt__('1','2')
'1 + 2' '1 + 2'
>>> add.__tex__('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 = { caract = {
"operator" : "+", \ "operator" : "+", \
"name" : "add",\ "name" : "add",\
@ -291,6 +315,7 @@ class op(object):
"actions" : ("__add__","__radd__"), \ "actions" : ("__add__","__radd__"), \
"txt" : "{op1} + {op2}",\ "txt" : "{op1} + {op2}",\
"tex" : "{op1} + {op2}",\ "tex" : "{op1} + {op2}",\
"_render": _render,\
} }
return caract return caract
@ -321,6 +346,8 @@ class op(object):
try: try:
if op.mainOp.priority <= self.priority: if op.mainOp.priority <= self.priority:
op = flatten_list(["(", op, ")"]) op = flatten_list(["(", op, ")"])
elif op[0] == '-':
op = flatten_list(["(", op, ")"])
except AttributeError: except AttributeError:
# op has not the attribute priority # op has not the attribute priority
try: try:

View File

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