Explain now explain thing only once

This commit is contained in:
Benjamin Bertrand 2016-03-07 11:01:57 +03:00
parent b4df404cbd
commit 190b247fe4
1 changed files with 37 additions and 8 deletions

View File

@ -19,11 +19,13 @@ class Explicable(Renderable):
def __init__(self, *args, **kwargs):
super(Explicable, self).__init__()
self.steps = []
self.explainator = 0
def explain(self, noself=True):
def explain(self):
r""" Generate and render steps which leed to itself
:param noself: does explain return self
After beening explained, the Explicable become amnesiac.
See 'history_generator' to explain it once again.
>>> action = Explicable()
>>> from .expression import Expression
@ -34,10 +36,38 @@ class Explicable(Renderable):
2 + 10 \times 4
2 + 40
42
>>> # Now action is amnesiac
>>> for i in action.explain():
... print(i)
"""
if not self.explainator:
self.explainator = self.history_generator()
return self.explainator
def history_generator(self):
r""" Generator for rendered steps which leed to itself
This is the called method in explain which create the generator.
It create a new generator at each call.
>>> action = Explicable()
>>> from .expression import Expression
>>> action.postfix_tokens = Expression('42')
>>> action.this_append_before([Expression('2+10*4'), Expression('2+40')])
>>> for i in action.history_generator():
... print(i)
2 + 10 \times 4
2 + 40
42
>>> for i in action.history_generator():
... print(i)
2 + 10 \times 4
2 + 40
42
"""
old_s = ''
# les étapes pour l'atteindre
try:
for s in self.steps:
try:
@ -52,11 +82,10 @@ class Explicable(Renderable):
except AttributeError:
pass
if noself:
# Lui même
new_s = self.STR_RENDER(self.postfix_tokens)
if not self.is_same_step(new_s, old_s):
yield new_s
# if noself:
new_s = self.STR_RENDER(self.postfix_tokens)
if not self.is_same_step(new_s, old_s):
yield new_s
def is_same_step(self, new, old):
"""Return whether the new step is the same than old step