Feat(random): random str parsing is done
This commit is contained in:
parent
33ded0d90e
commit
ca7cd001d8
@ -9,6 +9,45 @@
|
||||
"""
|
||||
|
||||
"""
|
||||
from ..coroutine import *
|
||||
|
||||
@coroutine
|
||||
def look_for_rdleaf(target):
|
||||
""" Coroutine which look to "{...}" which are RdLeaf
|
||||
|
||||
:example:
|
||||
>>> from ..str2 import list_sink
|
||||
>>> str2list = look_for_rdleaf(list_sink)
|
||||
>>> for i in "{a}+{a*b}-2":
|
||||
... str2list.send(i)
|
||||
>>> a = str2list.throw(STOOOP)
|
||||
>>> a
|
||||
[<RdLeaf a>, '+', <RdLeaf a*b>, '-', '2']
|
||||
|
||||
"""
|
||||
try:
|
||||
target_ = target()
|
||||
except TypeError:
|
||||
target_ = target
|
||||
|
||||
try:
|
||||
while True:
|
||||
tok = yield
|
||||
if tok == "{":
|
||||
stack = ""
|
||||
stacking = True
|
||||
elif tok == "}":
|
||||
target_.send(RdLeaf(stack))
|
||||
stack = ""
|
||||
stacking = False
|
||||
else:
|
||||
if stacking:
|
||||
stack += tok
|
||||
else:
|
||||
target_.send(tok)
|
||||
|
||||
except STOOOP as err:
|
||||
yield target_.throw(err)
|
||||
|
||||
class RdLeaf():
|
||||
""" Random leaf
|
||||
@ -23,10 +62,10 @@ class RdLeaf():
|
||||
return self._name
|
||||
|
||||
def replace(self, computed):
|
||||
return computed[self.name]
|
||||
return computed[self._name]
|
||||
|
||||
def __str__(self):
|
||||
return "{" + self.name + "}"
|
||||
return "{" + self._name + "}"
|
||||
|
||||
def __repr__(self):
|
||||
return f"<{self.__class__.__name__} {self.__str__}>"
|
||||
return f"<{self.__class__.__name__} {self._name}>"
|
||||
|
@ -15,6 +15,7 @@ from decimal import Decimal, InvalidOperation
|
||||
from .coroutine import *
|
||||
from .operator import is_operator
|
||||
from .MO import moify
|
||||
from .random.leaf import look_for_rdleaf
|
||||
|
||||
__all__ = ["str2", ]
|
||||
|
||||
@ -466,7 +467,7 @@ def lookforNumbers(target):
|
||||
if tok is not None:
|
||||
try:
|
||||
int(tok)
|
||||
except ValueError:
|
||||
except (ValueError, TypeError):
|
||||
if tok == '.':
|
||||
if current.replace("-", "", 1).isdigit():
|
||||
current += tok
|
||||
@ -487,7 +488,7 @@ def lookforNumbers(target):
|
||||
else:
|
||||
try:
|
||||
target_.send(typifiy_numbers(current))
|
||||
except InvalidOperation:
|
||||
except (InvalidOperation, TypeError):
|
||||
target_.send(current)
|
||||
target_.send('+')
|
||||
current = tok
|
||||
@ -499,7 +500,7 @@ def lookforNumbers(target):
|
||||
else:
|
||||
try:
|
||||
target_.send(typifiy_numbers(current))
|
||||
except InvalidOperation:
|
||||
except (InvalidOperation, TypeError):
|
||||
target_.send(current)
|
||||
current = tok
|
||||
else:
|
||||
@ -518,7 +519,7 @@ def lookforNumbers(target):
|
||||
if current:
|
||||
try:
|
||||
target_.send(typifiy_numbers(current))
|
||||
except InvalidOperation:
|
||||
except (InvalidOperation, TypeError):
|
||||
target_.send(current)
|
||||
yield target_.throw(err)
|
||||
|
||||
@ -589,7 +590,7 @@ def list_sink():
|
||||
yield ans
|
||||
|
||||
def str2(sink, convert_to_mo=True):
|
||||
""" Return a pipeline which parse an expression with the sink as an endpont
|
||||
""" Return a pipeline which parse an expression with the sink as an endpoint
|
||||
|
||||
:example:
|
||||
|
||||
@ -790,6 +791,26 @@ def str2(sink, convert_to_mo=True):
|
||||
|
||||
return pipeline
|
||||
|
||||
def rdstr2(sink):
|
||||
""" Return a pipeline which parse random expression and with sink as endpoint
|
||||
|
||||
:example:
|
||||
>>> rdstr2list = rdstr2(list_sink)
|
||||
>>> rdstr2list("{a}+{a*b}-2")
|
||||
[<RdLeaf a>, '+', <RdLeaf a*b>, '+', <MOnumber - 2>]
|
||||
"""
|
||||
lfop = lookfor(is_operator)
|
||||
operator_corout = partial(concurent_broadcast, lookfors=[lfop])
|
||||
def pipeline(expression):
|
||||
str2_corout = look_for_rdleaf(lookforNumbers(operator_corout(missing_times(moify(pparser(sink))))))
|
||||
|
||||
for i in expression.replace(" ",""):
|
||||
str2_corout.send(i)
|
||||
a = str2_corout.throw(STOOOP)
|
||||
|
||||
return a
|
||||
|
||||
return pipeline
|
||||
|
||||
str2nestedlist = str2(list_sink)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user