tree2txt is done and doctested
This commit is contained in:
parent
a51386fded
commit
d498af2bab
@ -80,7 +80,7 @@ class MO(object):
|
|||||||
return f"<{self.__class__.__name__} {self.__txt__}>"
|
return f"<{self.__class__.__name__} {self.__txt__}>"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.__txt__
|
return str(self.value)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def __txt__(self):
|
def __txt__(self):
|
||||||
@ -154,11 +154,13 @@ class MOnumber(MO):
|
|||||||
>>> MOnumber(23)
|
>>> MOnumber(23)
|
||||||
<MOnumber 23>
|
<MOnumber 23>
|
||||||
>>> MOnumber(-23)
|
>>> MOnumber(-23)
|
||||||
<MOnumber -23>
|
<MOnumber - 23>
|
||||||
>>> MOnumber(23.3)
|
>>> MOnumber(23.3)
|
||||||
<MOnumber 23.300000000000000710542735760100185871124267578125>
|
<MOnumber 23.300000000000000710542735760100185871124267578125>
|
||||||
>>> MOnumber(Decimal("23.3"))
|
>>> MOnumber(Decimal("23.3"))
|
||||||
<MOnumber 23.3>
|
<MOnumber 23.3>
|
||||||
|
>>> MOnumber(Decimal("-23.3"))
|
||||||
|
<MOnumber - 23.3>
|
||||||
>>> a = MOnumber(23)
|
>>> a = MOnumber(23)
|
||||||
>>> MOnumber(a)
|
>>> MOnumber(a)
|
||||||
<MOnumber 23>
|
<MOnumber 23>
|
||||||
@ -180,6 +182,13 @@ class MOnumber(MO):
|
|||||||
else:
|
else:
|
||||||
raise MOError("The value of an MOnumber need to be a int, a float or a Decimal")
|
raise MOError("The value of an MOnumber need to be a int, a float or a Decimal")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def __txt__(self):
|
||||||
|
if self.value > 0:
|
||||||
|
return str(self.value)
|
||||||
|
|
||||||
|
return f"- {abs(self.value)}"
|
||||||
|
|
||||||
|
|
||||||
class MOstr(MO):
|
class MOstr(MO):
|
||||||
|
|
||||||
|
@ -6,45 +6,210 @@
|
|||||||
#
|
#
|
||||||
# Distributed under terms of the MIT license.
|
# Distributed under terms of the MIT license.
|
||||||
|
|
||||||
|
from .operator import OPERATORS
|
||||||
|
|
||||||
def plus2txt(left, right):
|
def plus2txt(left, right):
|
||||||
""" + rendering
|
""" + rendering
|
||||||
|
|
||||||
>>> from .MO import mo
|
>>> from .MO import mo
|
||||||
>>> plus2txt(mo.MO(2), mo.MO(3))
|
>>> plus2txt(mo.MOnumber(2), mo.MOnumber(3))
|
||||||
'2 + 3'
|
'2 + 3'
|
||||||
>>> from .tree import Tree
|
>>> from .tree import Tree
|
||||||
>>> t = Tree.from_str("1+2")
|
>>> t = Tree.from_str("1+2")
|
||||||
>>> plus2txt(t, mo.MO(3))
|
>>> plus2txt(t, mo.MOnumber(3))
|
||||||
'1 + 2 + 3'
|
'1 + 2 + 3'
|
||||||
|
>>> plus2txt(t, mo.MOnumber(-3))
|
||||||
|
'1 + 2 - 3'
|
||||||
|
>>> plus2txt(mo.MOnumber(-3), t)
|
||||||
|
'- 3 + 1 + 2'
|
||||||
|
>>> t = Tree.from_str("-2*3")
|
||||||
|
>>> plus2txt(mo.MOnumber(3), t)
|
||||||
|
'3 - 2 * 3'
|
||||||
"""
|
"""
|
||||||
|
display_plus = True
|
||||||
try:
|
try:
|
||||||
left.node
|
left.node
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
left_ = left.__txt__
|
left_ = left.__txt__
|
||||||
else:
|
else:
|
||||||
left_ = tree2txt(left)
|
left_ = tree2txt(left)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
right.node
|
right.node
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
right_ = right.__txt__
|
right_ = right.__txt__
|
||||||
else:
|
else:
|
||||||
right_ = tree2txt(right)
|
right_ = tree2txt(right)
|
||||||
|
finally:
|
||||||
|
if right_.startswith("-"):
|
||||||
|
display_plus = False
|
||||||
|
|
||||||
return f"{left_} + {right_}"
|
if display_plus:
|
||||||
|
return f"{left_} + {right_}"
|
||||||
|
|
||||||
|
return f"{left_} {right_}"
|
||||||
|
|
||||||
def minus2txt(left, right):
|
def minus2txt(left, right):
|
||||||
pass
|
""" - rendering
|
||||||
|
|
||||||
|
>>> from .MO import mo
|
||||||
|
>>> minus2txt(None, mo.MO(3))
|
||||||
|
'- 3'
|
||||||
|
>>> from .tree import Tree
|
||||||
|
>>> t = Tree.from_str("1+2")
|
||||||
|
>>> minus2txt(None, t)
|
||||||
|
'- (1 + 2)'
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
right_need_parenthesis = False
|
||||||
|
if OPERATORS[right.node]["precedence"] < OPERATORS['-']["precedence"]:
|
||||||
|
right_need_parenthesis = True
|
||||||
|
except AttributeError:
|
||||||
|
right_ = right.__txt__
|
||||||
|
else:
|
||||||
|
if right_need_parenthesis:
|
||||||
|
right_ = f"({tree2txt(right)})"
|
||||||
|
else:
|
||||||
|
right_ = tree2txt(right)
|
||||||
|
|
||||||
|
return f"- {right_}"
|
||||||
|
|
||||||
def mul2txt(left, right):
|
def mul2txt(left, right):
|
||||||
pass
|
""" * rendering
|
||||||
|
|
||||||
|
>>> from .MO import mo
|
||||||
|
>>> mul2txt(mo.MO(2), mo.MO(3))
|
||||||
|
'2 * 3'
|
||||||
|
>>> from .tree import Tree
|
||||||
|
>>> t = Tree.from_str("1*2")
|
||||||
|
>>> mul2txt(t, mo.MO(3))
|
||||||
|
'1 * 2 * 3'
|
||||||
|
>>> t = Tree.from_str("1+2")
|
||||||
|
>>> mul2txt(t, mo.MO(3))
|
||||||
|
'(1 + 2) * 3'
|
||||||
|
>>> mul2txt(mo.MO(3), t)
|
||||||
|
'3(1 + 2)'
|
||||||
|
>>> a = mo.MOstr('x')
|
||||||
|
>>> mul2txt(mo.MO(3), a)
|
||||||
|
'3x'
|
||||||
|
"""
|
||||||
|
display_time = True
|
||||||
|
try:
|
||||||
|
left_need_parenthesis = False
|
||||||
|
if OPERATORS[left.node]["precedence"] < OPERATORS['*']["precedence"]:
|
||||||
|
left_need_parenthesis = True
|
||||||
|
except AttributeError:
|
||||||
|
left_ = left.__txt__
|
||||||
|
else:
|
||||||
|
if left_need_parenthesis:
|
||||||
|
left_ = f"({tree2txt(left)})"
|
||||||
|
else:
|
||||||
|
left_ = tree2txt(left)
|
||||||
|
|
||||||
|
try:
|
||||||
|
right_need_parenthesis = False
|
||||||
|
if OPERATORS[right.node]["precedence"] < OPERATORS['*']["precedence"]:
|
||||||
|
right_need_parenthesis = True
|
||||||
|
except AttributeError:
|
||||||
|
right_ = right.__txt__
|
||||||
|
else:
|
||||||
|
if right_need_parenthesis:
|
||||||
|
display_time = False
|
||||||
|
right_ = f"({tree2txt(right)})"
|
||||||
|
else:
|
||||||
|
right_ = tree2txt(right)
|
||||||
|
finally:
|
||||||
|
if right_[0].isalpha():
|
||||||
|
display_time = False
|
||||||
|
|
||||||
|
if display_time:
|
||||||
|
return f"{left_} * {right_}"
|
||||||
|
else:
|
||||||
|
return f"{left_}{right_}"
|
||||||
|
|
||||||
def div2txt(left, right):
|
def div2txt(left, right):
|
||||||
pass
|
""" / rendering
|
||||||
|
|
||||||
|
>>> from .MO import mo
|
||||||
|
>>> div2txt(mo.MO(2), mo.MO(3))
|
||||||
|
'2 / 3'
|
||||||
|
>>> from .tree import Tree
|
||||||
|
>>> t = Tree.from_str("1/2")
|
||||||
|
>>> div2txt(t, mo.MO(3))
|
||||||
|
'1 / 2 / 3'
|
||||||
|
>>> t = Tree.from_str("1+2")
|
||||||
|
>>> div2txt(t, mo.MO(3))
|
||||||
|
'(1 + 2) / 3'
|
||||||
|
>>> t = Tree.from_str("1*2")
|
||||||
|
>>> div2txt(mo.MO(3), t)
|
||||||
|
'3 / (1 * 2)'
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
left_need_parenthesis = False
|
||||||
|
if OPERATORS[left.node]["precedence"] < OPERATORS['/']["precedence"]:
|
||||||
|
left_need_parenthesis = True
|
||||||
|
except AttributeError:
|
||||||
|
left_ = left.__txt__
|
||||||
|
else:
|
||||||
|
if left_need_parenthesis:
|
||||||
|
left_ = f"({tree2txt(left)})"
|
||||||
|
else:
|
||||||
|
left_ = tree2txt(left)
|
||||||
|
try:
|
||||||
|
right_need_parenthesis = False
|
||||||
|
if OPERATORS[right.node]["precedence"] < OPERATORS['/']["precedence"]:
|
||||||
|
right_need_parenthesis = True
|
||||||
|
except AttributeError:
|
||||||
|
right_ = right.__txt__
|
||||||
|
else:
|
||||||
|
if right_need_parenthesis:
|
||||||
|
right_ = f"({tree2txt(right)})"
|
||||||
|
else:
|
||||||
|
right_ = tree2txt(right)
|
||||||
|
|
||||||
|
return f"{left_} / {right_}"
|
||||||
|
|
||||||
def pow2txt(left, right):
|
def pow2txt(left, right):
|
||||||
pass
|
""" ^ rendering
|
||||||
|
|
||||||
|
>>> from .MO import mo
|
||||||
|
>>> pow2txt(mo.MO(2), mo.MO(3))
|
||||||
|
'2 ^ 3'
|
||||||
|
>>> from .tree import Tree
|
||||||
|
>>> t = Tree.from_str("1^2")
|
||||||
|
>>> pow2txt(t, mo.MO(3))
|
||||||
|
'1 ^ 2 ^ 3'
|
||||||
|
>>> t = Tree.from_str("1+2")
|
||||||
|
>>> pow2txt(t, mo.MO(3))
|
||||||
|
'(1 + 2) ^ 3'
|
||||||
|
>>> t = Tree.from_str("1*2")
|
||||||
|
>>> pow2txt(mo.MO(3), t)
|
||||||
|
'3 ^ (1 * 2)'
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
left_need_parenthesis = False
|
||||||
|
if OPERATORS[left.node]["precedence"] < OPERATORS['^']["precedence"]:
|
||||||
|
left_need_parenthesis = True
|
||||||
|
except AttributeError:
|
||||||
|
left_ = left.__txt__
|
||||||
|
else:
|
||||||
|
if left_need_parenthesis:
|
||||||
|
left_ = f"({tree2txt(left)})"
|
||||||
|
else:
|
||||||
|
left_ = tree2txt(left)
|
||||||
|
try:
|
||||||
|
right_need_parenthesis = False
|
||||||
|
if OPERATORS[right.node]["precedence"] < OPERATORS['^']["precedence"]:
|
||||||
|
right_need_parenthesis = True
|
||||||
|
except AttributeError:
|
||||||
|
right_ = right.__txt__
|
||||||
|
else:
|
||||||
|
if right_need_parenthesis:
|
||||||
|
right_ = f"({tree2txt(right)})"
|
||||||
|
else:
|
||||||
|
right_ = tree2txt(right)
|
||||||
|
|
||||||
|
return f"{left_} ^ {right_}"
|
||||||
|
|
||||||
OPERATOR2TXT = {
|
OPERATOR2TXT = {
|
||||||
"+": plus2txt,
|
"+": plus2txt,
|
||||||
@ -62,7 +227,7 @@ def tree2txt(tree):
|
|||||||
>>> from .tree import Tree
|
>>> from .tree import Tree
|
||||||
>>> t = Tree.from_str("2+3*4")
|
>>> t = Tree.from_str("2+3*4")
|
||||||
>>> tree2txt(t)
|
>>> tree2txt(t)
|
||||||
"2 + 3 * 4"
|
'2 + 3 * 4'
|
||||||
"""
|
"""
|
||||||
return OPERATOR2TXT[tree.node](tree.left_value, tree.right_value)
|
return OPERATOR2TXT[tree.node](tree.left_value, tree.right_value)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user