Build minus with multipledispatcher
This commit is contained in:
parent
9610ee1c7e
commit
f2fb80c55b
@ -10,62 +10,66 @@
|
|||||||
Minus MO: take the opposit
|
Minus MO: take the opposit
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from multipledispatch import Dispatcher
|
||||||
from ..MO.mo import MO, MOnumber
|
from ..MO.mo import MO, MOnumber
|
||||||
from ..MO.fraction import MOFraction
|
from ..MO.fraction import MOFraction
|
||||||
from .exceptions import MinusError
|
from .exceptions import MinusError
|
||||||
|
|
||||||
|
minus_doc = """ Opposite of a MO
|
||||||
|
|
||||||
def minus(left, right):
|
:param left: None
|
||||||
""" Perform the minusition of left and right
|
|
||||||
|
|
||||||
:param left: left MO
|
|
||||||
:param right: right MO
|
:param right: right MO
|
||||||
:returns: Tree or 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>
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if not left is None:
|
|
||||||
raise MinusError(f"'-' is a 1 arity operator, left should be None (got {left})")
|
|
||||||
|
|
||||||
return MINUSFUNCTIONS[type(right)](right)
|
minus = Dispatcher("minus", doc=minus_doc)
|
||||||
|
|
||||||
def monumber(right):
|
@minus.register(type(None), MOnumber)
|
||||||
""" Minusing monumber
|
def monumber(left, right):
|
||||||
|
"""
|
||||||
:param right: right MOnumber
|
|
||||||
:returns: MONumber
|
|
||||||
|
|
||||||
>>> a = MOnumber(4)
|
>>> a = MOnumber(4)
|
||||||
>>> monumber(a)
|
>>> minus(None, a)
|
||||||
<MOnumber - 4>
|
<MOnumber - 4>
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return MO.factory(- right.value)
|
return MO.factory(- right.value)
|
||||||
|
|
||||||
def mofraction(right):
|
@minus.register(type(None), MOFraction)
|
||||||
""" Minusing a mofraction
|
def mofraction(left, right):
|
||||||
|
""" 4 differents cases
|
||||||
|
|
||||||
:param right: a mofraction
|
Either fraction , numerator or denominator is negative
|
||||||
:returns: Tree with the number converted into a mofraction
|
|
||||||
|
|
||||||
>>> a = MOFraction(6, 5)
|
>>> a = MOFraction(6, 5)
|
||||||
>>> print(mofraction(a))
|
>>> print(minus(None, a))
|
||||||
-
|
-
|
||||||
> None
|
> None
|
||||||
> /
|
> /
|
||||||
| > 6
|
| > 6
|
||||||
| > 5
|
| > 5
|
||||||
|
|
||||||
|
The fraction is negative
|
||||||
|
|
||||||
>>> a = MOFraction(6, 5, True)
|
>>> 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
|
> 6
|
||||||
> 5
|
> 5
|
||||||
@ -87,12 +91,6 @@ def mofraction(right):
|
|||||||
|
|
||||||
return MOFraction(right._numerator, right._denominator, True)
|
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'
|
# Reglages pour 'vim'
|
||||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||||
|
Loading…
Reference in New Issue
Block a user