Build minus with multipledispatcher

This commit is contained in:
Bertrand Benjamin 2018-03-17 10:15:46 +03:00
parent 9610ee1c7e
commit f2fb80c55b
1 changed files with 35 additions and 37 deletions

View File

@ -10,62 +10,66 @@
Minus MO: take the opposit
"""
from multipledispatch import Dispatcher
from ..MO.mo import MO, MOnumber
from ..MO.fraction import MOFraction
from .exceptions import MinusError
minus_doc = """ Opposite of a MO
def minus(left, right):
""" Perform the minusition of left and right
:param left: None
:param right: right MO
:returns: Tree or MO
:param left: left MO
:param right: right MO
:returns: Tree or MO
"""
>>> a = MOnumber(4)
>>> minus(None, a)
<MOnumber - 4>
>>> a = MOnumber(-4)
>>> minus(None, a)
<MOnumber 4>
>>> b = MOnumber(0)
>>> minus(None, b)
<MOnumber 0>
minus = Dispatcher("minus", doc=minus_doc)
@minus.register(type(None), MOnumber)
def monumber(left, right):
"""
if not left is None:
raise MinusError(f"'-' is a 1 arity operator, left should be None (got {left})")
return MINUSFUNCTIONS[type(right)](right)
def monumber(right):
""" Minusing monumber
:param right: right MOnumber
:returns: MONumber
>>> a = MOnumber(4)
>>> monumber(a)
>>> minus(None, a)
<MOnumber - 4>
"""
return MO.factory(- right.value)
def mofraction(right):
""" Minusing a mofraction
@minus.register(type(None), MOFraction)
def mofraction(left, right):
""" 4 differents cases
:param right: a mofraction
:returns: Tree with the number converted into a mofraction
Either fraction , numerator or denominator is negative
>>> a = MOFraction(6, 5)
>>> print(mofraction(a))
>>> print(minus(None, a))
-
> None
> /
| > 6
| > 5
The fraction is negative
>>> a = MOFraction(6, 5, True)
>>> print(mofraction(a))
>>> print(minus(None, a))
/
> 6
> 5
Numerator is negative
>>> a = MOFraction(-6, 5)
>>> print(minus(None, a))
/
> 6
> 5
Denominators is negative
>>> a = MOFraction(6, -5)
>>> print(minus(None, a))
/
> 6
> 5
@ -87,12 +91,6 @@ def mofraction(right):
return MOFraction(right._numerator, right._denominator, True)
# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018
MINUSFUNCTIONS = {
MOnumber: monumber,
MOFraction: mofraction,
}
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: