#!/usr/bin/env python # encoding: utf-8 import types from .add import Add from .div import Div from .mul import Mul from .par import Par from .pw import Pw from .sub import Sub from .sub1 import Sub1 class Operator_set(object): """ Class for sets of operators""" def __init__(self): """ Initiate the operator_set """ self._operators = {} def get_op(self, op, arity = 2): """Return the corresponding operator :op: symbole of the op :arity: the arity >>> op = Operator_set() >>> op.get_op('+') '+' >>> mul = op.get_op('*') >>> mul.tex '{op1} \\\\times {op2}' >>> mul.txt '{op1} * {op2}' """ try: return getattr(self, self._operators[(op, arity)]) except KeyError: raise KeyError("{theOp} (arity: {arity}) is not available".format(theOp = op, arity = arity)) def can_be_operator(cls, symbole): """ Tell if the symbole can be an operator """ if type(symbole) == str: return symbole in [i[0] for i in cls._operators] else: return False def store_operator(self, operator): """ Save the operator as a method and :param operator: the operator (the class) (it will be accessible through .name method name. """ # TODO: faire une vérif si on peut utiliser operator_name |mar. févr. 23 09:09:44 EAT 2016 self._operators[operator.uniq_desc()] = operator.name setattr(self, operator.name, operator) op = Operator_set() op.store_operator(Add()) op.store_operator(Div()) op.store_operator(Mul()) op.store_operator(Par()) op.store_operator(Pw()) op.store_operator(Sub()) op.store_operator(Sub1()) # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: # cursor: 16 del