From ca33a00877a89bc13b396498b1dfc5cc1fecdbe2 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Thu, 16 May 2019 17:17:59 +0200 Subject: [PATCH] Feat: Split moify into the function and the coroutine --- mapytex/calculus/core/MO/__init__.py | 2 +- mapytex/calculus/core/MO/atoms.py | 22 +++++++++++++--------- mapytex/calculus/core/str2.py | 6 +++--- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/mapytex/calculus/core/MO/__init__.py b/mapytex/calculus/core/MO/__init__.py index 38ab8a6..3c7c8ad 100644 --- a/mapytex/calculus/core/MO/__init__.py +++ b/mapytex/calculus/core/MO/__init__.py @@ -11,7 +11,7 @@ MO: math objects """ from .mo import MO -from .atoms import MOnumber, MOstr, moify +from .atoms import MOnumber, MOstr, moify, moify_cor # ----------------------------- # Reglages pour 'vim' diff --git a/mapytex/calculus/core/MO/atoms.py b/mapytex/calculus/core/MO/atoms.py index 3e42a81..6f8df1d 100644 --- a/mapytex/calculus/core/MO/atoms.py +++ b/mapytex/calculus/core/MO/atoms.py @@ -16,13 +16,23 @@ from ..coroutine import coroutine, STOOOP __all__ = ["moify", "MOnumber", "MOstr"] +def moify(token): + try: + return MOnumber(token) + except MOError: + pass + try: + return MOstr(token) + except MOError: + return token + @coroutine -def moify(target): +def moify_cor(target): """ Coroutine which try to convert a parsed token into an MO :example: >>> from ..str2 import list_sink - >>> list2molist = moify(list_sink) + >>> list2molist = moify_cor(list_sink) >>> for i in [-2, "+", "x", "*", Decimal("3.3")]: ... list2molist.send(i) >>> list2molist.throw(STOOOP) @@ -37,13 +47,7 @@ def moify(target): try: while True: tok = yield - try: - target_.send(MOnumber(tok)) - except MOError: - try: - target_.send(MOstr(tok)) - except MOError: - target_.send(tok) + target_.send(moify(tok)) except STOOOP as err: yield target_.throw(err) diff --git a/mapytex/calculus/core/str2.py b/mapytex/calculus/core/str2.py index 672ac10..85cf785 100644 --- a/mapytex/calculus/core/str2.py +++ b/mapytex/calculus/core/str2.py @@ -14,7 +14,7 @@ from functools import partial from decimal import Decimal, InvalidOperation from .coroutine import * from .operator import is_operator -from .MO import moify +from .MO import moify_cor from .random.leaf import look_for_rdleaf __all__ = ["str2"] @@ -797,7 +797,7 @@ def str2(sink, convert_to_mo=True): def pipeline(expression): if convert_to_mo: str2_corout = lookforNumbers( - operator_corout(missing_times(moify(pparser(sink)))) + operator_corout(missing_times(moify_cor(pparser(sink)))) ) else: str2_corout = lookforNumbers(operator_corout(missing_times(pparser(sink)))) @@ -824,7 +824,7 @@ def rdstr2(sink): def pipeline(expression): str2_corout = look_for_rdleaf( - lookforNumbers(operator_corout(missing_times(moify(pparser(sink))))) + lookforNumbers(operator_corout(missing_times(moify_cor(pparser(sink))))) ) for i in expression.replace(" ", ""):