reorganise renders

This commit is contained in:
Benjamin Bertrand 2016-02-13 11:24:31 +03:00
parent a5d84e89e3
commit 589e92a780
5 changed files with 107 additions and 111 deletions

View File

@ -131,12 +131,6 @@ class AbstractPolynom(Explicable):
def __repr__(self):
return "< " + str(self.__class__) + " " + str(self._coef) + ">"
def __txt__(self):
return txt(self.postfix_tokens)
def __tex__(self):
return tex(self.postfix_tokens)
def coef_postfix(self, a, i):
"""Return the postfix display of a coeficient

View File

@ -1,77 +1,7 @@
#!/usr/bin/env python
# encoding: utf-8
from .render import txt, tex
class Renderable(object):
"""
A Renderable object is an object which can work with Render class. It means that it has to have attribute postfix_tokens.
"""
STR_RENDER = tex
DEFAULT_RENDER = tex
@classmethod
def set_render(cls, render):
cls.STR_RENDER = render
@classmethod
def get_render(cls):
return cls.STR_RENDER
@classmethod
def set_default_render(cls):
cls.set_render(cls.DEFAULT_RENDER)
@classmethod
def tmp_render(cls, render=tex):
""" Create a container in which Expression render is temporary modify
The default temporary render is Expression in order to perform calculus inside numbers
>>> from .expression import Expression
>>> exp = Expression("2*3/5")
>>> print(exp)
2 \\times \\frac{ 3 }{ 5 }
>>> for i in exp.simplify().explain():
... print(i)
2 \\times \\frac{ 3 }{ 5 }
\\frac{ 3 }{ 5 } \\times 2
\\frac{ 3 \\times 2 }{ 5 }
\\frac{ 6 }{ 5 }
>>> with Expression.tmp_render(txt):
... for i in exp.simplify().explain():
... print(i)
2 * 3 / 5
3 / 5 * 2
( 3 * 2 ) / 5
6 / 5
>>> for i in exp.simplify().explain():
... print(i)
2 \\times \\frac{ 3 }{ 5 }
\\frac{ 3 }{ 5 } \\times 2
\\frac{ 3 \\times 2 }{ 5 }
\\frac{ 6 }{ 5 }
# TODO: essayer de ne pas afficher ce changement de position. |lun. avril 6 17:29:56 CEST 2015
"""
class TmpRenderEnv(object):
def __enter__(self):
self.old_render = Renderable.get_render()
Renderable.set_render(render)
def __exit__(self, type, value, traceback):
Renderable.set_render(self.old_render)
return TmpRenderEnv()
def __eq__(self, other):
""" Two Renderable objects are the same if they have same postfix_tokens """
try:
return self.postfix_tokens == other.postfix_tokens
except AttributeError:
return False
from .render import Renderable
class Explicable(Renderable):
@ -85,6 +15,7 @@ class Explicable(Renderable):
"""
def __init__(self, *args, **kwargs):
super(Explicable, self).__init__()
self.steps = []
def explain(self, noself=True):
@ -97,13 +28,15 @@ class Explicable(Renderable):
# les étapes pour l'atteindre
try:
for s in self.steps:
if hasattr(s, 'postfix_tokens'):
try:
new_s = self.STR_RENDER(s.postfix_tokens)
else:
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
@ -127,6 +60,25 @@ class Explicable(Renderable):
else:
return False
class Explicable_int(int, Explicable):
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)
# -----------------------------
# Reglages pour 'vim'

View File

@ -7,26 +7,13 @@
from .generic import Stack, flatten_list, expand_list, isNumber, isOperator, isNumerand
from .str2tokens import str2tokens
from .operator import op
from .explicable import Explicable
from .explicable import Explicable, Explicable_int
from .random_expression import RdExpression
__all__ = ['Expression']
class Explicable_int(int, Explicable):
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)
class Expression(Explicable):
"""A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown"""

View File

@ -113,22 +113,6 @@ class Fraction(Explicable):
return "< Fraction {num} / {denom}>".format(
num=self._num, denom=self._denom)
def __txt__(self):
old_render = Expression.get_render()
Expression.set_render(txt)
_txt = self.__str__()
Expression.set_render(old_render)
return _txt
def __tex__(self):
old_render = Expression.get_render()
Expression.set_render(tex)
_tex = self.__str__()
Expression.set_render(old_render)
return _tex
def __float__(self):
return self._num / self._denom

View File

@ -56,7 +56,6 @@ def txt_render(token):
txt = Render(txt_render)
def tex_render(token):
def render(*args):
try:
@ -66,7 +65,6 @@ def tex_render(token):
return render
tex = Render(tex_render)
def p2i_render(token):
def render(*args):
try:
@ -76,6 +74,87 @@ def p2i_render(token):
return render
p2i = Render(p2i_render)
class Renderable(object):
"""
A Renderable object is an object which can work with Render class. It means that it has to have attribute postfix_tokens.
"""
STR_RENDER = tex
DEFAULT_RENDER = tex
@classmethod
def set_render(cls, render):
cls.STR_RENDER = render
@classmethod
def get_render(cls):
return cls.STR_RENDER
@classmethod
def set_default_render(cls):
cls.set_render(cls.DEFAULT_RENDER)
@classmethod
def tmp_render(cls, render=tex):
""" Create a container in which Expression render is temporary modify
The default temporary render is Expression in order to perform calculus inside numbers
>>> from .expression import Expression
>>> exp = Expression("2*3/5")
>>> print(exp)
2 \\times \\frac{ 3 }{ 5 }
>>> for i in exp.simplify().explain():
... print(i)
2 \\times \\frac{ 3 }{ 5 }
\\frac{ 3 }{ 5 } \\times 2
\\frac{ 3 \\times 2 }{ 5 }
\\frac{ 6 }{ 5 }
>>> with Expression.tmp_render(txt):
... for i in exp.simplify().explain():
... print(i)
2 * 3 / 5
3 / 5 * 2
( 3 * 2 ) / 5
6 / 5
>>> for i in exp.simplify().explain():
... print(i)
2 \\times \\frac{ 3 }{ 5 }
\\frac{ 3 }{ 5 } \\times 2
\\frac{ 3 \\times 2 }{ 5 }
\\frac{ 6 }{ 5 }
# TODO: essayer de ne pas afficher ce changement de position. |lun. avril 6 17:29:56 CEST 2015
"""
class TmpRenderEnv(object):
def __enter__(self):
self.old_render = Renderable.get_render()
Renderable.set_render(render)
def __exit__(self, type, value, traceback):
Renderable.set_render(self.old_render)
return TmpRenderEnv()
def __eq__(self, other):
""" Two Renderable objects are the same if they have same postfix_tokens """
try:
return self.postfix_tokens == other.postfix_tokens
except AttributeError:
return False
def __txt__(self):
try:
return txt(self.postfix_tokens)
except AttributeError:
return self
def __tex__(self):
try:
return tex(self.postfix_tokens)
except AttributeError:
return self
if __name__ == '__main__':
from .operator import op
from itertools import permutations