Feat: Split moify into the function and the coroutine

This commit is contained in:
Bertrand Benjamin 2019-05-16 17:17:59 +02:00
parent 037fd9f68a
commit ca33a00877
3 changed files with 17 additions and 13 deletions

View File

@ -11,7 +11,7 @@ MO: math objects
""" """
from .mo import MO from .mo import MO
from .atoms import MOnumber, MOstr, moify from .atoms import MOnumber, MOstr, moify, moify_cor
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'

View File

@ -16,13 +16,23 @@ from ..coroutine import coroutine, STOOOP
__all__ = ["moify", "MOnumber", "MOstr"] __all__ = ["moify", "MOnumber", "MOstr"]
def moify(token):
try:
return MOnumber(token)
except MOError:
pass
try:
return MOstr(token)
except MOError:
return token
@coroutine @coroutine
def moify(target): def moify_cor(target):
""" Coroutine which try to convert a parsed token into an MO """ Coroutine which try to convert a parsed token into an MO
:example: :example:
>>> from ..str2 import list_sink >>> from ..str2 import list_sink
>>> list2molist = moify(list_sink) >>> list2molist = moify_cor(list_sink)
>>> for i in [-2, "+", "x", "*", Decimal("3.3")]: >>> for i in [-2, "+", "x", "*", Decimal("3.3")]:
... list2molist.send(i) ... list2molist.send(i)
>>> list2molist.throw(STOOOP) >>> list2molist.throw(STOOOP)
@ -37,13 +47,7 @@ def moify(target):
try: try:
while True: while True:
tok = yield tok = yield
try: target_.send(moify(tok))
target_.send(MOnumber(tok))
except MOError:
try:
target_.send(MOstr(tok))
except MOError:
target_.send(tok)
except STOOOP as err: except STOOOP as err:
yield target_.throw(err) yield target_.throw(err)

View File

@ -14,7 +14,7 @@ from functools import partial
from decimal import Decimal, InvalidOperation from decimal import Decimal, InvalidOperation
from .coroutine import * from .coroutine import *
from .operator import is_operator from .operator import is_operator
from .MO import moify from .MO import moify_cor
from .random.leaf import look_for_rdleaf from .random.leaf import look_for_rdleaf
__all__ = ["str2"] __all__ = ["str2"]
@ -797,7 +797,7 @@ def str2(sink, convert_to_mo=True):
def pipeline(expression): def pipeline(expression):
if convert_to_mo: if convert_to_mo:
str2_corout = lookforNumbers( str2_corout = lookforNumbers(
operator_corout(missing_times(moify(pparser(sink)))) operator_corout(missing_times(moify_cor(pparser(sink))))
) )
else: else:
str2_corout = lookforNumbers(operator_corout(missing_times(pparser(sink)))) str2_corout = lookforNumbers(operator_corout(missing_times(pparser(sink))))
@ -824,7 +824,7 @@ def rdstr2(sink):
def pipeline(expression): def pipeline(expression):
str2_corout = look_for_rdleaf( 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(" ", ""): for i in expression.replace(" ", ""):