Nice way for setting render <3

This commit is contained in:
Lafrite
2014-11-21 15:49:43 +01:00
parent 3e9277d86b
commit 3b5583bd61
5 changed files with 36 additions and 25 deletions

View File

@@ -12,6 +12,15 @@ class Expression(object):
"""A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown"""
STR_RENDER = tex
DEFAULT_RENDER = tex
@classmethod
def set_render(cls, render):
cls.STR_RENDER = render
@classmethod
def set_df_render(cls):
cls.set_render(cls.DEFAULT_RENDER)
def __init__(self, exp):
""" Initiate the expression
@@ -47,20 +56,20 @@ class Expression(object):
## ---------------------
## Mechanism functions
def simplify(self, render=STR_RENDER):
def simplify(self):
""" Generator which return steps for computing the expression """
if not self.can_go_further():
yield render(self.postfix_tokens)
yield self.STR_RENDER(self.postfix_tokens)
else:
self.compute_exp()
old_s = ''
for s in self.steps:
new_s = render(s)
new_s = self.STR_RENDER(s)
# Astuce pour éviter d'avoir deux fois la même étape (par exemple pour la transfo d'une division en fraction)
if new_s != old_s:
old_s = new_s
yield new_s
for s in self.child.simplify(render = render):
for s in self.child.simplify():
if old_s != s:
yield s
@@ -191,14 +200,20 @@ def test(exp):
print("\n")
if __name__ == '__main__':
Expression.STR_RENDER = txt
Expression.set_render(txt)
exp1 = "2 ^ 3 * 5"
test(exp1)
Expression.set_render(tex)
test(exp1)
from pymath.operator import op
exp = [2, 3, op.pw, 5, op.mul]
test(exp)
Expression.set_render(txt)
test([Expression(exp1), Expression(exp), op.add])
exp = "1 + 3 * 5"

View File

@@ -176,7 +176,9 @@ class Polynom(object):
coef_exp = coef
# On fait réduire l'expression puis on ajoute dans steps
coef_steps = list(coef_exp.simplify(render = lambda x:Expression(x)))
Expression.set_render(lambda _,x:Expression(x))
coef_steps = list(coef_exp.simplify())
Expression.set_df_render()
# On ajoute toutes ces étapes
coefs_steps.append(coef_steps)

View File

@@ -26,6 +26,10 @@ class RdExpression(object):
FORM = "exp"
@classmethod
def set_form(cls, form):
cls.FORM = form
def __init__(self, form, conditions = []):
"""Initiate the generator