2014-11-02 07:19:31 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
|
2014-11-02 10:17:12 +00:00
|
|
|
from .fraction import Fraction
|
|
|
|
|
2014-11-02 07:19:31 +00:00
|
|
|
class Operator(str):
|
|
|
|
|
|
|
|
"""The operator class, is a string (representation of the operator) with its arity"""
|
|
|
|
|
|
|
|
OPERATORS = { \
|
|
|
|
"+": ["", "", ("__add__","__radd__")],\
|
|
|
|
"-": ["", "__neg__", ("__sub__", "__rsub__")], \
|
|
|
|
"*": ["", "", ("__mul__", "__rmul__")], \
|
|
|
|
"/": ["", "", ("__div__","__rdiv__")], \
|
|
|
|
"^": ["", "", ("__pow__", "")] \
|
|
|
|
}
|
|
|
|
|
|
|
|
def __new__(cls, operator, arity = 2):
|
|
|
|
op = str.__new__(cls, operator)
|
|
|
|
op.arity = arity
|
|
|
|
|
|
|
|
op.actions = cls.OPERATORS[operator][arity]
|
|
|
|
|
|
|
|
return op
|
|
|
|
|
|
|
|
def __call__(self, *args):
|
|
|
|
""" Calling this operator performs the rigth calculus """
|
|
|
|
if self.arity == 1:
|
|
|
|
return getattr(args[0], self.actions)()
|
|
|
|
|
|
|
|
elif self.arity == 2:
|
|
|
|
# C'est moche mais je veux que ça marche...
|
|
|
|
if str(self) == "/":
|
2014-11-02 10:17:12 +00:00
|
|
|
ans = [Fraction(args[0], args[1])]
|
2014-11-02 07:19:31 +00:00
|
|
|
ans += ans[0].simplify()
|
|
|
|
return ans
|
|
|
|
else:
|
|
|
|
if type(args[1]) == int:
|
|
|
|
return getattr(args[0], self.actions[0])(args[1])
|
|
|
|
else:
|
|
|
|
return getattr(args[1], self.actions[1])(args[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|