operator selector
This commit is contained in:
parent
cecf06e082
commit
e1da8d2453
@ -9,11 +9,12 @@ class Operator(str):
|
|||||||
|
|
||||||
"""The operator class, is a string (representation of the operator) with its arity"""
|
"""The operator class, is a string (representation of the operator) with its arity"""
|
||||||
|
|
||||||
def __new__(cls, operator = "", priority = 0, actions = ("",""), txt = "", tex = "", arity = 2):
|
def __new__(cls, operator = "", name = "", priority = 0, actions = ("",""), txt = "", tex = "", arity = 2):
|
||||||
""" Create an Operator """
|
""" Create an Operator """
|
||||||
#def __new__(cls, operator, arity = 2):
|
#def __new__(cls, operator, arity = 2):
|
||||||
op = str.__new__(cls, operator)
|
op = str.__new__(cls, operator)
|
||||||
op.name = operator
|
op.operator = operator
|
||||||
|
op.name = name
|
||||||
op.arity = arity
|
op.arity = arity
|
||||||
op.priority = priority
|
op.priority = priority
|
||||||
op.actions = actions
|
op.actions = actions
|
||||||
@ -184,8 +185,6 @@ def operatorize(fun):
|
|||||||
op = Operator(ans["operator"])
|
op = Operator(ans["operator"])
|
||||||
for (attr, value) in ans.items():
|
for (attr, value) in ans.items():
|
||||||
if hasattr(value, '__call__'):
|
if hasattr(value, '__call__'):
|
||||||
#callback = lambda *args, **kwrds: value(op, *args, **kwrds)
|
|
||||||
#setattr(op, attr, callback)
|
|
||||||
setattr(op, attr, types.MethodType(value, op))
|
setattr(op, attr, types.MethodType(value, op))
|
||||||
else:
|
else:
|
||||||
setattr(op, attr, value)
|
setattr(op, attr, value)
|
||||||
@ -204,12 +203,31 @@ class ClassProperty(object):
|
|||||||
class op(object):
|
class op(object):
|
||||||
""" List of admited operations """
|
""" List of admited operations """
|
||||||
|
|
||||||
|
_operators = {("+",2): "add",\
|
||||||
|
("-", 2): "sub",\
|
||||||
|
("-", 1): "sub1",\
|
||||||
|
("*", 2): "mul",\
|
||||||
|
("/", 2): "div",\
|
||||||
|
("^", 2): "pw",\
|
||||||
|
(")", 2): "par",\
|
||||||
|
}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_op(cls, op, arity = 2):
|
||||||
|
"""Return the corresponding operator
|
||||||
|
|
||||||
|
:op: symbole of the op
|
||||||
|
:arity: the arity
|
||||||
|
"""
|
||||||
|
return getattr(cls, cls._operators[(op, arity)])
|
||||||
|
|
||||||
@ClassProperty
|
@ClassProperty
|
||||||
@operatorize
|
@operatorize
|
||||||
def add(cls):
|
def add(cls):
|
||||||
""" The operator + """
|
""" The operator + """
|
||||||
caract = {
|
caract = {
|
||||||
"operator" : "+", \
|
"operator" : "+", \
|
||||||
|
"name" : "add",\
|
||||||
"priority" : 1, \
|
"priority" : 1, \
|
||||||
"arity" : 2, \
|
"arity" : 2, \
|
||||||
"action" : ("__add__","__radd__"), \
|
"action" : ("__add__","__radd__"), \
|
||||||
@ -225,6 +243,7 @@ class op(object):
|
|||||||
""" The operator - """
|
""" The operator - """
|
||||||
caract = {
|
caract = {
|
||||||
"operator" : "-", \
|
"operator" : "-", \
|
||||||
|
"name" : "sub",\
|
||||||
"priority" : 1, \
|
"priority" : 1, \
|
||||||
"arity" : 2, \
|
"arity" : 2, \
|
||||||
"action" : ("__sub__","__rsub__"), \
|
"action" : ("__sub__","__rsub__"), \
|
||||||
@ -254,6 +273,7 @@ class op(object):
|
|||||||
|
|
||||||
caract = {
|
caract = {
|
||||||
"operator" : "-", \
|
"operator" : "-", \
|
||||||
|
"name" : "sub1",\
|
||||||
"priority" : 2, \
|
"priority" : 2, \
|
||||||
"arity" : 1, \
|
"arity" : 1, \
|
||||||
"action" : "__neg__",\
|
"action" : "__neg__",\
|
||||||
@ -285,6 +305,7 @@ class op(object):
|
|||||||
|
|
||||||
caract = {
|
caract = {
|
||||||
"operator" : "*", \
|
"operator" : "*", \
|
||||||
|
"name" : "mul",\
|
||||||
"priority" : 4, \
|
"priority" : 4, \
|
||||||
"arity" : 2, \
|
"arity" : 2, \
|
||||||
"action" : ("__mul__","__rmul__"), \
|
"action" : ("__mul__","__rmul__"), \
|
||||||
@ -316,6 +337,7 @@ class op(object):
|
|||||||
|
|
||||||
caract = {
|
caract = {
|
||||||
"operator" : "/", \
|
"operator" : "/", \
|
||||||
|
"name" : "div",\
|
||||||
"priority" : 4, \
|
"priority" : 4, \
|
||||||
"arity" : 2, \
|
"arity" : 2, \
|
||||||
"txt" : "{op1} /^ {op2}",\
|
"txt" : "{op1} /^ {op2}",\
|
||||||
@ -332,6 +354,7 @@ class op(object):
|
|||||||
""" The operator ^ """
|
""" The operator ^ """
|
||||||
caract = {
|
caract = {
|
||||||
"operator" : "^", \
|
"operator" : "^", \
|
||||||
|
"name" : "pw",\
|
||||||
"priority" : 5, \
|
"priority" : 5, \
|
||||||
"arity" : 2, \
|
"arity" : 2, \
|
||||||
"action" : ("__pow__",""), \
|
"action" : ("__pow__",""), \
|
||||||
@ -347,6 +370,7 @@ class op(object):
|
|||||||
""" The operator ( """
|
""" The operator ( """
|
||||||
caract = {
|
caract = {
|
||||||
"operator" : "(", \
|
"operator" : "(", \
|
||||||
|
"name" : "par",\
|
||||||
"priority" : 0, \
|
"priority" : 0, \
|
||||||
"arity" : 0, \
|
"arity" : 0, \
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user