2016-02-23 10:53:13 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
from .operator import Operator, save_mainOp
|
|
|
|
|
|
|
|
class Add(Operator):
|
2016-02-27 06:07:54 +00:00
|
|
|
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'
|
|
|
|
"""
|
2016-02-23 10:53:13 +00:00
|
|
|
|
|
|
|
_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
|