Number are now int or Decimal when an expression is parsed
This commit is contained in:
parent
251ec6c83b
commit
a916035260
@ -11,6 +11,7 @@ Converting a string with coroutines
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
from decimal import Decimal
|
||||||
from .coroutine import *
|
from .coroutine import *
|
||||||
|
|
||||||
__all__ = ["str2", ]
|
__all__ = ["str2", ]
|
||||||
@ -158,7 +159,7 @@ def lookfor(condition, replace = lambda x:''.join(x)):
|
|||||||
else:
|
else:
|
||||||
if c is not None:
|
if c is not None:
|
||||||
acc.append(c)
|
acc.append(c)
|
||||||
found = condition(''.join(acc))
|
found = condition(''.join([str(i) for i in acc]))
|
||||||
if found == "maybe":
|
if found == "maybe":
|
||||||
ans = "maybe"
|
ans = "maybe"
|
||||||
elif found:
|
elif found:
|
||||||
@ -322,14 +323,14 @@ def lookforNumbers(target):
|
|||||||
... str2list.send(i)
|
... str2list.send(i)
|
||||||
>>> a = str2list.throw(STOOOP)
|
>>> a = str2list.throw(STOOOP)
|
||||||
>>> print(a)
|
>>> print(a)
|
||||||
['12', '+', '1234', '*', '67']
|
[12, '+', 1234, '*', 67]
|
||||||
|
|
||||||
>>> str2list = lookforNumbers(list_sink)
|
>>> str2list = lookforNumbers(list_sink)
|
||||||
>>> for i in "1.2+12.34*67":
|
>>> for i in "1.2+12.34*67":
|
||||||
... str2list.send(i)
|
... str2list.send(i)
|
||||||
>>> a = str2list.throw(STOOOP)
|
>>> a = str2list.throw(STOOOP)
|
||||||
>>> print(a)
|
>>> print(a)
|
||||||
['1.2', '+', '12.34', '*', '67']
|
[Decimal('1.2'), '+', Decimal('12.34'), '*', 67]
|
||||||
|
|
||||||
>>> str2list = lookforNumbers(list_sink)
|
>>> str2list = lookforNumbers(list_sink)
|
||||||
>>> for i in "12.3.4*67":
|
>>> for i in "12.3.4*67":
|
||||||
@ -368,7 +369,7 @@ def lookforNumbers(target):
|
|||||||
current += tok
|
current += tok
|
||||||
else:
|
else:
|
||||||
if current:
|
if current:
|
||||||
target_.send(current)
|
target_.send(typifiy_numbers(current))
|
||||||
current = ""
|
current = ""
|
||||||
target_.send(tok)
|
target_.send(tok)
|
||||||
else:
|
else:
|
||||||
@ -376,9 +377,16 @@ def lookforNumbers(target):
|
|||||||
|
|
||||||
except STOOOP as err:
|
except STOOOP as err:
|
||||||
if current:
|
if current:
|
||||||
target_.send(current)
|
target_.send(typifiy_numbers(current))
|
||||||
yield target_.throw(err)
|
yield target_.throw(err)
|
||||||
|
|
||||||
|
def typifiy_numbers(number):
|
||||||
|
""" Transform a str number into a integer or a decimal """
|
||||||
|
try:
|
||||||
|
return int(number)
|
||||||
|
except ValueError:
|
||||||
|
return Decimal(number)
|
||||||
|
|
||||||
@coroutine
|
@coroutine
|
||||||
def pparser(target):
|
def pparser(target):
|
||||||
""" Parenthesis parser sink
|
""" Parenthesis parser sink
|
||||||
@ -447,15 +455,15 @@ def str2(sink):
|
|||||||
>>> exp = "12+3*4"
|
>>> exp = "12+3*4"
|
||||||
>>> t = str2nestedlist(exp)
|
>>> t = str2nestedlist(exp)
|
||||||
>>> print(t)
|
>>> print(t)
|
||||||
['12', '+', '3', '*', '4']
|
[12, '+', 3, '*', 4]
|
||||||
>>> exp = "12*3+4"
|
>>> exp = "12*3+4"
|
||||||
>>> t = str2nestedlist(exp)
|
>>> t = str2nestedlist(exp)
|
||||||
>>> print(t)
|
>>> print(t)
|
||||||
['12', '*', '3', '+', '4']
|
[12, '*', 3, '+', 4]
|
||||||
>>> exp = "12*(3+4)"
|
>>> exp = "12*(3+4)"
|
||||||
>>> t = str2nestedlist(exp)
|
>>> t = str2nestedlist(exp)
|
||||||
>>> print(t)
|
>>> print(t)
|
||||||
['12', '*', ['3', '+', '4']]
|
[12, '*', [3, '+', 4]]
|
||||||
|
|
||||||
>>> from .tree import MutableTree
|
>>> from .tree import MutableTree
|
||||||
>>> str2tree = str2(MutableTree.sink)
|
>>> str2tree = str2(MutableTree.sink)
|
||||||
|
Loading…
Reference in New Issue
Block a user