Mapytex/mapytex/calculus/operator/add.py

71 lines
1.6 KiB
Python

#!/usr/bin/env python
# encoding: utf-8
from .operator import Operator, save_mainOp
class Add(Operator):
r""" The operator +
>>> add = Add()
>>> add
+
>>> add(1, 2)
3
>>> add.__tex__('1','2')
'1 + 2'
>>> add.__tex__('-1','2')
'-1 + 2'
>>> add.__tex__('1','-2')
'1 - 2'
>>> add.__txt__('1','2')
'1 + 2'
>>> add.__txt__('-1','2')
'-1 + 2'
>>> add.__txt__('1','-2')
'1 - 2'
"""
_CARACT = {
"operator": "+",
"name": "add",
"priority": 1,
"arity": 2,
"actions": ("__add__", "__radd__"),
"txt": "{op1} + {op2}",
"tex": "{op1} + {op2}",
}
def __init__(self):
""" Initiate Add Operator """
super(Add, self).__init__(**self._CARACT)
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
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del