2015-02-26 18:02:20 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
2016-02-13 08:24:31 +00:00
|
|
|
from .render import Renderable
|
2016-02-15 12:20:24 +00:00
|
|
|
from decimal import Decimal
|
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
|
|
|
|
2015-02-26 18:02:20 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2016-02-13 08:24:31 +00:00
|
|
|
super(Explicable, self).__init__()
|
2015-02-26 18:02:20 +00:00
|
|
|
self.steps = []
|
|
|
|
|
2016-02-13 04:04:08 +00:00
|
|
|
def explain(self, noself=True):
|
2015-04-06 15:57:13 +00:00
|
|
|
""" Generate and render steps which leed to itself
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2015-04-06 15:57:13 +00:00
|
|
|
:param noself: does explain return self
|
|
|
|
|
|
|
|
"""
|
2015-02-26 18:02:20 +00:00
|
|
|
old_s = ''
|
|
|
|
# les étapes pour l'atteindre
|
|
|
|
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-02-13 08:24:31 +00:00
|
|
|
|
2015-04-17 14:38:16 +00:00
|
|
|
if not self.is_same_step(new_s, old_s):
|
2015-02-26 18:02:20 +00:00
|
|
|
old_s = new_s
|
|
|
|
yield new_s
|
2016-02-13 08:24:31 +00:00
|
|
|
|
2015-02-26 18:02:20 +00:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2015-04-06 15:57:13 +00:00
|
|
|
if noself:
|
|
|
|
# Lui même
|
|
|
|
new_s = self.STR_RENDER(self.postfix_tokens)
|
2015-04-17 14:38:16 +00:00
|
|
|
if not self.is_same_step(new_s, old_s):
|
2015-04-06 15:57:13 +00:00
|
|
|
yield new_s
|
2015-04-17 14:38:16 +00:00
|
|
|
|
|
|
|
def is_same_step(self, new, old):
|
|
|
|
"""Return whether the new step is the same than old step
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if new.replace(" ", "") == old.replace(" ", ""):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except AttributeError:
|
|
|
|
if new == old:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2016-02-13 08:24:31 +00:00
|
|
|
class Explicable_int(int, Explicable):
|
2016-02-13 16:47:35 +00:00
|
|
|
""" An Explicable_int is an int which can be explain """
|
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-02-15 12:20:24 +00:00
|
|
|
class Explicable_Decimal(Decimal, Explicable):
|
|
|
|
""" An Explicable_Decimal is an decimal which can be explain """
|
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
|
|
|
|
2016-02-13 03:29:26 +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
|