2018-01-21 08:26:34 +00:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:fenc=utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2017 lafrite <lafrite@Poivre>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
|
|
|
|
OPERATORS = {
|
2018-01-21 15:16:08 +00:00
|
|
|
"+": {'repr': "+",
|
|
|
|
'precedence': 0,
|
|
|
|
'operate': lambda x: x.__getattribute__("__add__"),
|
|
|
|
'roperate': lambda x: x.__getattribute__("__radd__"),
|
|
|
|
},
|
|
|
|
"-": {'repr': "-",
|
|
|
|
'precedence': 1,
|
|
|
|
'_operate': lambda x: x.__getattribute__("__neg__"),
|
|
|
|
'operate': lambda x: x.__getattribute__("__sub__"),
|
|
|
|
'roperate': lambda x: x.__getattribute__("__rsub__"),
|
|
|
|
},
|
|
|
|
"*": {'repr': "*",
|
|
|
|
'precedence': 2,
|
|
|
|
'operate': lambda x: x.__getattribute__("__mul__"),
|
|
|
|
'roperate': lambda x: x.__getattribute__("__rmul__"),
|
|
|
|
},
|
|
|
|
"/": {'repr': "/",
|
|
|
|
'precedence': 3,
|
|
|
|
'operate': lambda x: x.__getattribute__("__div__"),
|
|
|
|
'roperate': lambda x: x.__getattribute__("__rdiv__"),
|
|
|
|
},
|
|
|
|
"^": {'repr': "^",
|
|
|
|
'precedence': 4,
|
|
|
|
'operate': lambda x: x.__getattribute__("__pow__"),
|
|
|
|
'roperate': lambda x: x.__getattribute__("__rpow__"),
|
|
|
|
},
|
2018-01-21 08:26:34 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 15:02:42 +00:00
|
|
|
def is_operator(string):
|
|
|
|
""" Return whether a string is an operator or not
|
|
|
|
|
|
|
|
:param string: string to test
|
|
|
|
:returns: boolean
|
|
|
|
|
|
|
|
:example:
|
|
|
|
|
|
|
|
>>> is_operator("+")
|
|
|
|
True
|
|
|
|
>>> is_operator("i")
|
|
|
|
False
|
|
|
|
|
|
|
|
"""
|
|
|
|
return string in OPERATORS.keys()
|
|
|
|
|
2018-01-21 08:26:34 +00:00
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|