2015-02-26 18:02:20 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
2016-03-10 09:02:40 +00:00
|
|
|
from .renderable import Renderable
|
2016-03-10 10:27:08 +00:00
|
|
|
from .step import Step
|
2016-02-15 12:20:24 +00:00
|
|
|
from decimal import Decimal
|
2016-03-07 06:58:22 +00:00
|
|
|
from .generic import transpose_fill
|
2016-03-15 09:43:34 +00:00
|
|
|
from .decorators import is_same_step, no_repetition
|
2015-04-21 16:10:14 +00:00
|
|
|
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2015-02-27 16:46:16 +00:00
|
|
|
class Explicable(Renderable):
|
2015-02-26 18:02:20 +00:00
|
|
|
|
|
|
|
""" An Explicable object is an object which can be explicable!
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2015-02-26 18:02:20 +00:00
|
|
|
It's a parent class of a more classical Expression, Fraction and Polynom (and later square root)
|
|
|
|
Child class will have the following method
|
|
|
|
* explain: Generator which return steps which leed to himself
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2015-02-26 18:02:20 +00:00
|
|
|
"""
|
2016-02-13 04:04:08 +00:00
|
|
|
|
2016-03-09 07:48:29 +00:00
|
|
|
def __init__(self, pstf_tokens):
|
|
|
|
super(Explicable, self).__init__(pstf_tokens)
|
2015-02-26 18:02:20 +00:00
|
|
|
self.steps = []
|
2016-03-07 10:05:12 +00:00
|
|
|
self.explained = 0
|
2015-02-26 18:02:20 +00:00
|
|
|
|
2016-03-07 08:01:57 +00:00
|
|
|
def explain(self):
|
2016-03-02 14:13:21 +00:00
|
|
|
r""" Generate and render steps which leed to itself
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2016-03-07 10:05:12 +00:00
|
|
|
After beening explained, the Explicable become amnesiac. It will return itself in a list.
|
2016-03-07 08:01:57 +00:00
|
|
|
See 'history_generator' to explain it once again.
|
2015-04-06 15:57:13 +00:00
|
|
|
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> action = Explicable(['42'])
|
2016-03-02 14:13:21 +00:00
|
|
|
>>> from .expression import Expression
|
|
|
|
>>> action.this_append_before([Expression('2+10*4'), Expression('2+40')])
|
|
|
|
>>> for i in action.explain():
|
|
|
|
... print(i)
|
|
|
|
2 + 10 \times 4
|
|
|
|
2 + 40
|
|
|
|
42
|
2016-03-07 08:01:57 +00:00
|
|
|
>>> # Now action is amnesiac
|
|
|
|
>>> for i in action.explain():
|
|
|
|
... print(i)
|
2016-03-07 10:05:12 +00:00
|
|
|
42
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> action = Explicable(['42'])
|
2016-03-07 10:05:12 +00:00
|
|
|
>>> for i in action.explain():
|
|
|
|
... print(i)
|
|
|
|
42
|
2016-03-07 08:01:57 +00:00
|
|
|
|
|
|
|
"""
|
2016-03-07 10:05:12 +00:00
|
|
|
if self.explained:
|
2016-03-15 09:12:56 +00:00
|
|
|
return self.self_generator()
|
2016-03-07 10:05:12 +00:00
|
|
|
else:
|
|
|
|
self.explained = 1
|
|
|
|
return self.history_generator()
|
2016-03-06 15:18:01 +00:00
|
|
|
|
2016-03-15 09:12:56 +00:00
|
|
|
def self_generator(self):
|
|
|
|
""" Generator which yield itself rendered """
|
|
|
|
yield self.STR_RENDER(self.postfix_tokens)
|
|
|
|
|
2016-03-15 09:43:34 +00:00
|
|
|
@no_repetition(is_same_step)
|
2016-03-07 08:01:57 +00:00
|
|
|
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.
|
|
|
|
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> action = Explicable(['42'])
|
2016-03-07 08:01:57 +00:00
|
|
|
>>> from .expression import Expression
|
2016-03-07 08:43:33 +00:00
|
|
|
>>> for i in action.history_generator():
|
|
|
|
... print(i)
|
|
|
|
42
|
2016-03-07 08:01:57 +00:00
|
|
|
>>> 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
|
2015-04-06 15:57:13 +00:00
|
|
|
"""
|
2015-02-26 18:02:20 +00:00
|
|
|
try:
|
|
|
|
for s in self.steps:
|
2016-02-13 08:24:31 +00:00
|
|
|
try:
|
2015-02-27 17:02:27 +00:00
|
|
|
new_s = self.STR_RENDER(s.postfix_tokens)
|
2016-02-13 08:24:31 +00:00
|
|
|
except AttributeError:
|
2015-02-27 17:02:27 +00:00
|
|
|
new_s = self.STR_RENDER(s)
|
2016-03-15 09:43:34 +00:00
|
|
|
yield new_s
|
2016-02-13 08:24:31 +00:00
|
|
|
|
2015-02-26 18:02:20 +00:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2016-03-07 08:01:57 +00:00
|
|
|
# if noself:
|
|
|
|
new_s = self.STR_RENDER(self.postfix_tokens)
|
2016-03-15 09:43:34 +00:00
|
|
|
yield new_s
|
2015-04-17 14:38:16 +00:00
|
|
|
|
2016-03-02 13:52:42 +00:00
|
|
|
def this_append_before(self, steps):
|
|
|
|
""" Add steps at the beginning of self.steps
|
|
|
|
|
|
|
|
:param steps: list of steps that append before
|
|
|
|
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> e = Explicable(['Actions'])
|
2016-03-02 13:52:42 +00:00
|
|
|
>>> s = ['eat', 'sleep']
|
|
|
|
>>> e.this_append_before(s)
|
|
|
|
>>> print(e.steps)
|
|
|
|
['eat', 'sleep']
|
|
|
|
>>> s0 = ['cook']
|
|
|
|
>>> e.this_append_before(s0)
|
|
|
|
>>> print(e.steps)
|
|
|
|
['cook', 'eat', 'sleep']
|
|
|
|
"""
|
2016-03-15 06:04:23 +00:00
|
|
|
self.steps = list(steps) + self.steps
|
2016-03-02 13:52:42 +00:00
|
|
|
|
2016-03-09 06:27:27 +00:00
|
|
|
def steal_history(self, arg1):
|
|
|
|
""" Steal the history of arg1 for itself
|
|
|
|
|
|
|
|
:param arg1: a potential Explicable
|
|
|
|
|
2016-03-12 02:26:05 +00:00
|
|
|
>>> e = Explicable(['Actions'])
|
|
|
|
>>> s = [Step(['eat']), Step(['sleep'])]
|
|
|
|
>>> e.this_append_before(s)
|
|
|
|
>>> f = Explicable(['Stolen actions'])
|
|
|
|
>>> f.steal_history(e)
|
|
|
|
>>> for i in e.explain():
|
|
|
|
... print(i)
|
|
|
|
Actions
|
|
|
|
>>> for i in f.explain():
|
|
|
|
... print(i)
|
|
|
|
eat
|
|
|
|
sleep
|
|
|
|
Actions
|
|
|
|
Stolen actions
|
|
|
|
|
|
|
|
|
2016-03-09 06:27:27 +00:00
|
|
|
"""
|
|
|
|
try:
|
2016-03-10 10:27:08 +00:00
|
|
|
with Step.tmp_render():
|
|
|
|
self.this_append_before(list(arg1.explain()))
|
2016-03-09 06:27:27 +00:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2016-03-09 07:48:29 +00:00
|
|
|
@classmethod
|
|
|
|
def merge_history(cls, explicables):
|
2016-03-07 09:32:51 +00:00
|
|
|
r""" Take a list of Explicable objects, explain where they are from and merge their history
|
2016-03-07 06:58:22 +00:00
|
|
|
|
2016-03-07 09:32:51 +00:00
|
|
|
This method try to use 'explain' method from Explicable. This means that after using this method, those objects will become amnesiac.
|
2016-03-07 09:09:56 +00:00
|
|
|
|
2016-03-07 09:32:51 +00:00
|
|
|
:param explicables: list
|
2016-03-07 06:58:22 +00:00
|
|
|
:returns: the list of steps
|
|
|
|
|
|
|
|
>>> from .expression import Expression
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> action1 = Explicable(['42'])
|
2016-03-10 10:27:08 +00:00
|
|
|
>>> action1.this_append_before([['2 + 4 * 10'], ['2 + 40']])
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> action2 = Explicable(['24'])
|
2016-03-10 10:27:08 +00:00
|
|
|
>>> action2.this_append_before([['12 * 2']])
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> m_history = Explicable.merge_history([action1, action2])
|
2016-03-07 06:58:22 +00:00
|
|
|
>>> m_history
|
|
|
|
<generator object transpose_fill ...>
|
|
|
|
>>> for i in m_history:
|
|
|
|
... print(i)
|
2016-03-10 10:27:08 +00:00
|
|
|
[< Step ['2 + 4 * 10']>, < Step ['12 * 2']>]
|
|
|
|
[< Step ['2 + 40']>, < Step ['24']>]
|
|
|
|
[< Step ['42']>, < Step ['24']>]
|
2016-03-07 10:05:12 +00:00
|
|
|
>>> # Now they are amnesiac
|
2016-03-07 09:09:56 +00:00
|
|
|
>>> for i in action1.explain():
|
|
|
|
... print(i)
|
2016-03-07 10:05:12 +00:00
|
|
|
42
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> m_history = Explicable.merge_history([action1, action2])
|
2016-03-07 10:05:12 +00:00
|
|
|
>>> for i in m_history:
|
|
|
|
... print(i)
|
2016-03-10 10:27:08 +00:00
|
|
|
[< Step ['42']>, < Step ['24']>]
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> action1 = Explicable(['42'])
|
2016-03-10 10:27:08 +00:00
|
|
|
>>> action1.this_append_before([['2+10*4'], ['2+40']])
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> m_history = Explicable.merge_history([action1, 12])
|
2016-03-07 09:09:56 +00:00
|
|
|
>>> m_history
|
|
|
|
<generator object transpose_fill ...>
|
|
|
|
>>> for i in m_history:
|
|
|
|
... print(i)
|
2016-03-10 10:27:08 +00:00
|
|
|
[< Step ['2+10*4']>, < Step [12]>]
|
|
|
|
[< Step ['2+40']>, < Step [12]>]
|
|
|
|
[< Step ['42']>, < Step [12]>]
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> action1 = Explicable(['42'])
|
2016-03-10 10:27:08 +00:00
|
|
|
>>> action1.this_append_before([['2+10*4'], ['2+40']])
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> action2 = Explicable(['24'])
|
2016-03-10 10:27:08 +00:00
|
|
|
>>> action2.this_append_before([['2*12']])
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> action3 = Explicable(['0'])
|
2016-03-10 10:27:08 +00:00
|
|
|
>>> action3.this_append_before([['3-3']])
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> m_history = Explicable.merge_history([action1, action2, action3])
|
2016-03-07 09:32:51 +00:00
|
|
|
>>> for i in m_history:
|
|
|
|
... print(i)
|
2016-03-10 10:27:08 +00:00
|
|
|
[< Step ['2+10*4']>, < Step ['2*12']>, < Step ['3-3']>]
|
|
|
|
[< Step ['2+40']>, < Step ['24']>, < Step ['0']>]
|
|
|
|
[< Step ['42']>, < Step ['24']>, < Step ['0']>]
|
2016-03-09 07:48:29 +00:00
|
|
|
>>> m_history = Explicable.merge_history([1,2,3])
|
2016-03-07 10:05:12 +00:00
|
|
|
>>> for i in m_history:
|
|
|
|
... print(i)
|
2016-03-10 10:27:08 +00:00
|
|
|
[< Step [1]>, < Step [2]>, < Step [3]>]
|
2016-03-07 06:58:22 +00:00
|
|
|
"""
|
2016-03-07 09:32:51 +00:00
|
|
|
steps = []
|
|
|
|
for expl in explicables:
|
|
|
|
try:
|
2016-03-10 10:27:08 +00:00
|
|
|
with Step.tmp_render():
|
|
|
|
steps.append(list(expl.explain()))
|
2016-03-07 09:32:51 +00:00
|
|
|
except AttributeError:
|
2016-03-10 10:27:08 +00:00
|
|
|
steps.append([Step([expl])])
|
2016-03-07 09:32:51 +00:00
|
|
|
|
|
|
|
return transpose_fill(steps)
|
2016-03-07 06:58:22 +00:00
|
|
|
|
2016-03-06 15:18:01 +00:00
|
|
|
|
2016-02-13 08:24:31 +00:00
|
|
|
class Explicable_int(int, Explicable):
|
2016-03-06 15:18:01 +00:00
|
|
|
|
2016-02-13 16:47:35 +00:00
|
|
|
""" An Explicable_int is an int which can be explain """
|
2016-03-06 15:18:01 +00:00
|
|
|
|
2016-02-13 08:24:31 +00:00
|
|
|
isNumber = True
|
|
|
|
|
|
|
|
def __init__(self, val):
|
|
|
|
super(Explicable_int, self).__init__(val)
|
|
|
|
self._val = val
|
|
|
|
self.postfix_tokens = [self]
|
|
|
|
self.steps = []
|
|
|
|
|
|
|
|
def simplify(self):
|
|
|
|
return Explicable_int(self._val)
|
|
|
|
|
|
|
|
def __txt__(self):
|
|
|
|
return str(self._val)
|
|
|
|
|
|
|
|
def __tex__(self):
|
|
|
|
return str(self._val)
|
|
|
|
|
2016-03-06 15:18:01 +00:00
|
|
|
|
2016-02-15 12:20:24 +00:00
|
|
|
class Explicable_Decimal(Decimal, Explicable):
|
2016-03-06 15:18:01 +00:00
|
|
|
|
2016-02-15 12:20:24 +00:00
|
|
|
""" An Explicable_Decimal is an decimal which can be explain """
|
2016-03-06 15:18:01 +00:00
|
|
|
|
2016-02-13 16:47:35 +00:00
|
|
|
isNumber = True
|
|
|
|
|
|
|
|
def __init__(self, val):
|
2016-02-15 12:20:24 +00:00
|
|
|
super(Explicable_Decimal, self).__init__(val)
|
2016-02-13 16:47:35 +00:00
|
|
|
self._val = val
|
|
|
|
self.postfix_tokens = [self]
|
|
|
|
self.steps = []
|
|
|
|
|
|
|
|
def simplify(self):
|
2016-02-15 12:20:24 +00:00
|
|
|
return Explicable_Decimal(self._val)
|
2016-02-13 16:47:35 +00:00
|
|
|
|
|
|
|
def __txt__(self):
|
|
|
|
return str(self._val)
|
|
|
|
|
|
|
|
def __tex__(self):
|
|
|
|
return str(self._val)
|
|
|
|
|
2016-02-13 08:24:31 +00:00
|
|
|
|
2015-02-26 18:02:20 +00:00
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
2016-02-13 03:29:26 +00:00
|
|
|
# cursor: 16 del
|