107 lines
2.7 KiB
Python
107 lines
2.7 KiB
Python
#!/usr/bin/env python
|
|
# encoding: utf-8
|
|
|
|
from .render import Renderable
|
|
|
|
|
|
class Explicable(Renderable):
|
|
|
|
""" An Explicable object is an object which can be explicable!
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(Explicable, self).__init__()
|
|
self.steps = []
|
|
|
|
def explain(self, noself=True):
|
|
""" Generate and render steps which leed to itself
|
|
|
|
:param noself: does explain return self
|
|
|
|
"""
|
|
old_s = ''
|
|
# les étapes pour l'atteindre
|
|
try:
|
|
for s in self.steps:
|
|
try:
|
|
new_s = self.STR_RENDER(s.postfix_tokens)
|
|
except AttributeError:
|
|
new_s = self.STR_RENDER(s)
|
|
|
|
if not self.is_same_step(new_s, old_s):
|
|
old_s = new_s
|
|
yield new_s
|
|
|
|
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
|
|
|
|
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
|
|
|
|
class Explicable_int(int, Explicable):
|
|
""" An Explicable_int is an int which can be explain """
|
|
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)
|
|
|
|
class Explicable_float(float, Explicable):
|
|
""" An Explicable_float is an float which can be explain """
|
|
isNumber = True
|
|
|
|
def __init__(self, val):
|
|
super(Explicable_float, self).__init__(val)
|
|
self._val = val
|
|
self.postfix_tokens = [self]
|
|
self.steps = []
|
|
|
|
def simplify(self):
|
|
return Explicable_float(self._val)
|
|
|
|
def __txt__(self):
|
|
return str(self._val)
|
|
|
|
def __tex__(self):
|
|
return str(self._val)
|
|
|
|
|
|
|
|
# -----------------------------
|
|
# Reglages pour 'vim'
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
# cursor: 16 del
|