Doctest for operator_set

This commit is contained in:
Benjamin Bertrand 2016-02-27 11:40:29 +03:00
parent 1e3e0418df
commit 5002fbe4ae

View File

@ -10,6 +10,7 @@ from .par import Par
from .pw import Pw
from .sub import Sub
from .sub1 import Sub1
from .operator import Operator
class Operator_set(object):
@ -20,19 +21,33 @@ class Operator_set(object):
self._operators = {}
def get_op(self, op, arity = 2):
"""Return the corresponding operator
r"""Return the corresponding operator
:op: symbole of the op
:arity: the arity
:arity: the arity (default is 2)
>>> op = Operator_set()
>>> op.store_operator(Add())
>>> op.get_op('+')
'+'
+
>>> mul = op.get_op('*')
Traceback (most recent call last):
...
KeyError: '* (arity: 2) is not available'
>>> op.store_operator(Mul())
>>> mul = op.get_op('*')
>>> mul.tex
'{op1} \\\\times {op2}'
'{op1} \\times {op2}'
>>> mul.txt
'{op1} * {op2}'
>>> op.store_operator(Sub1())
>>> sub1 = op.get_op('-')
Traceback (most recent call last):
...
KeyError: '- (arity: 2) is not available'
>>> sub1 = op.get_op('-', 1)
>>> sub1.tex
'- {op1}'
"""
try:
return getattr(self, self._operators[(op, arity)])
@ -40,7 +55,17 @@ class Operator_set(object):
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 """
r"""
Tell if the symbole can be an operator
>>> op = Operator_set()
>>> op.can_be_operator("+")
False
>>> op.store_operator(Add())
>>> op.can_be_operator("+")
True
"""
if type(symbole) == str:
return symbole in [i[0] for i in cls._operators]
else:
@ -50,10 +75,23 @@ class Operator_set(object):
""" Save the operator as a method and
:param operator: the operator (the class) (it will be accessible through .name method name.
>>> op = Operator_set()
>>> op._operators
{}
>>> op.store_operator(Add())
>>> op._operators
{('+', 2): 'add'}
>>> op.store_operator("+")
Traceback (most recent call last):
...
ValueError: + is not en Operator it's a <class 'str'>
"""
# 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)
if isinstance(operator, Operator):
self._operators[operator.uniq_desc()] = operator.name
setattr(self, operator.name, operator)
else:
raise ValueError("{} is not en Operator it's a {}".format(operator, type(operator)))
op = Operator_set()
@ -65,16 +103,6 @@ op.store_operator(Pw())
op.store_operator(Sub())
op.store_operator(Sub1())
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: