65 lines
1.4 KiB
Python
65 lines
1.4 KiB
Python
#!/usr/bin/env python
|
|
# encoding: utf-8
|
|
|
|
|
|
from .operator import Operator
|
|
from ..generic import flatten_list
|
|
|
|
|
|
class Sub1(Operator):
|
|
|
|
""" The operator -
|
|
|
|
>>> sub1 = Sub1()
|
|
>>> sub1
|
|
-
|
|
>>> sub1(1)
|
|
-1
|
|
>>> sub1.__tex__('1')
|
|
'- 1'
|
|
>>> sub1.__tex__('-1')
|
|
'- ( -1 )'
|
|
>>> sub1.__txt__('1')
|
|
'- 1'
|
|
>>> sub1.__txt__('-1')
|
|
'- ( -1 )'
|
|
"""
|
|
|
|
_CARACT = {
|
|
"operator": "-",
|
|
"name": "sub1",
|
|
"priority": 3,
|
|
"arity": 1,
|
|
"actions": "__neg__",
|
|
"txt": "- {op1}",
|
|
"tex": "- {op1}",
|
|
}
|
|
|
|
def __init__(self):
|
|
""" Initiate Sub1 Operator """
|
|
super(Sub1, self).__init__(**self._CARACT)
|
|
|
|
def l_parenthesis(self, op, str_join=False):
|
|
""" Add parenthesis if necessary """
|
|
try:
|
|
if op.mainOp.priority <= self.priority:
|
|
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
|