move __str__ from expression to renderable
This commit is contained in:
parent
6428ab2c9e
commit
bcd5643202
@ -427,7 +427,7 @@ class AbstractPolynom(Explicable):
|
|||||||
return ans
|
return ans
|
||||||
|
|
||||||
def replace_letter(self, letter):
|
def replace_letter(self, letter):
|
||||||
""" Replace the letter in the expression
|
r""" Replace the letter in the expression
|
||||||
|
|
||||||
:param letter: the new letter.
|
:param letter: the new letter.
|
||||||
:returns: The expression with the new letter.
|
:returns: The expression with the new letter.
|
||||||
|
@ -73,14 +73,6 @@ class Expression(Explicable):
|
|||||||
self.steal_history(exp)
|
self.steal_history(exp)
|
||||||
self._isExpression = 1
|
self._isExpression = 1
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
"""
|
|
||||||
Overload str
|
|
||||||
|
|
||||||
If you want to changer render use Expression.set_render(...) or use tmp_render context manager.
|
|
||||||
"""
|
|
||||||
return self.STR_RENDER(self.postfix_tokens)
|
|
||||||
|
|
||||||
def simplify(self):
|
def simplify(self):
|
||||||
""" Compute entirely the expression and return the result with .steps attribute """
|
""" Compute entirely the expression and return the result with .steps attribute """
|
||||||
try:
|
try:
|
||||||
@ -177,7 +169,7 @@ class Expression(Explicable):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def develop_steps(cls, tokenList):
|
def develop_steps(cls, tokenList):
|
||||||
"""
|
r"""
|
||||||
From a list of tokens, it develops steps of each tokens and transpose it into steps respecting the stucture of the tokenList.
|
From a list of tokens, it develops steps of each tokens and transpose it into steps respecting the stucture of the tokenList.
|
||||||
|
|
||||||
It try to use 'explain' method for every tokens. After using this methode, tokens becom amnesiac.
|
It try to use 'explain' method for every tokens. After using this methode, tokens becom amnesiac.
|
||||||
@ -186,8 +178,14 @@ class Expression(Explicable):
|
|||||||
>>> e1 = e.simplify()
|
>>> e1 = e.simplify()
|
||||||
>>> f = Expression('3*4+5')
|
>>> f = Expression('3*4+5')
|
||||||
>>> f1 = f.simplify()
|
>>> f1 = f.simplify()
|
||||||
>>> Expression.develop_steps([e1, f1, op.add])
|
>>> dev_steps = Expression.develop_steps([e1, f1, op.add])
|
||||||
|
>>> dev_steps
|
||||||
[< Step [1, 2, +, 3, 4, *, 5, +, +]>, < Step [3, 12, 5, +, +]>, < Step [3, 17, +]>]
|
[< Step [1, 2, +, 3, 4, *, 5, +, +]>, < Step [3, 12, 5, +, +]>, < Step [3, 17, +]>]
|
||||||
|
>>> for i in dev_steps:
|
||||||
|
... print(i)
|
||||||
|
1 + 2 + 3 \times 4 + 5
|
||||||
|
3 + 12 + 5
|
||||||
|
3 + 17
|
||||||
>>> e = Expression('1+2')
|
>>> e = Expression('1+2')
|
||||||
>>> e1 = e.simplify()
|
>>> e1 = e.simplify()
|
||||||
>>> f = Expression('3*4+5')
|
>>> f = Expression('3*4+5')
|
||||||
|
@ -76,6 +76,14 @@ class Renderable(object):
|
|||||||
"""
|
"""
|
||||||
self.postfix_tokens = pstf_tokens
|
self.postfix_tokens = pstf_tokens
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
"""
|
||||||
|
Overload str
|
||||||
|
|
||||||
|
If you want to changer render use Expression.set_render(...) or use tmp_render context manager.
|
||||||
|
"""
|
||||||
|
return self.STR_RENDER(self.postfix_tokens)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "< {cls} {pstf_tokens}>".format(
|
return "< {cls} {pstf_tokens}>".format(
|
||||||
cls = str(self.__class__).split('.')[-1][:-2],
|
cls = str(self.__class__).split('.')[-1][:-2],
|
||||||
|
@ -26,7 +26,7 @@ class Step(Renderable):
|
|||||||
\\frac{ 6 }{ 5 }
|
\\frac{ 6 }{ 5 }
|
||||||
>>> with Step.tmp_render():
|
>>> with Step.tmp_render():
|
||||||
... for i in exp.simplify().explain():
|
... for i in exp.simplify().explain():
|
||||||
... print(i)
|
... print(repr(i))
|
||||||
< Step [2, 3, 5, /, *]>
|
< Step [2, 3, 5, /, *]>
|
||||||
< Step [3, 5, /, 2, *]>
|
< Step [3, 5, /, 2, *]>
|
||||||
< Step [3, 2, *, 5, /]>
|
< Step [3, 2, *, 5, /]>
|
||||||
|
Loading…
Reference in New Issue
Block a user