From f2fb80c55b5a3e1f0ebba13bf12d8d31378b251d Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sat, 17 Mar 2018 10:15:46 +0300 Subject: [PATCH] Build minus with multipledispatcher --- mapytex/calculus/core/compute/minus.py | 72 +++++++++++++------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/mapytex/calculus/core/compute/minus.py b/mapytex/calculus/core/compute/minus.py index dda5667..b297c7e 100644 --- a/mapytex/calculus/core/compute/minus.py +++ b/mapytex/calculus/core/compute/minus.py @@ -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) - - >>> a = MOnumber(-4) - >>> minus(None, a) - - >>> b = MOnumber(0) - >>> minus(None, b) - +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) """ 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: