Mapytex/pymath/calculus/operator/sub.py
2016-03-22 08:39:20 +03:00

70 lines
1.6 KiB
Python

#!/usr/bin/env python
# encoding: utf-8
from .operator import Operator
from ..generic import flatten_list
class Sub(Operator):
""" The operator -
>>> sub = Sub()
>>> sub
-
>>> sub(1, 2)
-1
>>> sub.__tex__('1','2')
'1 - 2'
>>> sub.__tex__('1','-2')
'1 - ( -2 )'
>>> sub.__tex__('-1','2')
'-1 - 2'
>>> sub.__txt__('1','2')
'1 - 2'
>>> sub.__txt__('1','-2')
'1 - ( -2 )'
>>> sub.__txt__('-1','2')
'-1 - 2'
"""
_CARACT = {
"operator": "-",
"name": "sub",
"priority": 2,
"arity": 2,
"actions": ("__sub__", "__rsub__"),
"txt": "{op1} - {op2}",
"tex": "{op1} - {op2}",
}
def __init__(self):
""" Initiate Sub Operator """
super(Sub, self).__init__(**self._CARACT)
def l_parenthesis(self, op, str_join=False):
return op
def r_parenthesis(self, op, str_join=False):
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:
if int(op) < 0:
op = ['(', op, ')']
except ValueError:
pass
ans = flatten_list([op])
if str_join:
ans = ' '.join([str(i) for i in ans])
return ans
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del