Nice way for setting render <3
This commit is contained in:
parent
3e9277d86b
commit
3b5583bd61
@ -34,28 +34,16 @@ On peut ensuite afficher l'expression avec un *print*.
|
||||
Et si l'on souhaite un rendu plus adapté à la console:
|
||||
|
||||
>>> from pymath.render import txt
|
||||
>>> exp.render(render = txt)
|
||||
>>> Expression.set_render(txt)
|
||||
>>> exp.render()
|
||||
1 + 2 * 3
|
||||
|
||||
Ou encore
|
||||
|
||||
>>> from pymath.renders import txt
|
||||
>>> Expression.STR_RENDER = txt
|
||||
>>> print(exp)
|
||||
1 + 2 * 3
|
||||
|
||||
/!\ Il ne faut pas faire
|
||||
|
||||
>>> exp.STR_RENDER = txt
|
||||
|
||||
car le changement ne se propagera pas à toute la classe. Ce qui posera problème quand on utilisera la méthode *simplify*(les sous expressions auront l'ancien STR_RENDER).
|
||||
|
||||
### Simplification des expressions
|
||||
Une fois les expressions créées, elles peuvent se réduire en expliquant les étapes et en respectant les règles de priorités. Les exemples suivants seront données avec un rendu texte.
|
||||
|
||||
>>> from pymath.expression import Expression
|
||||
>>> from pymath.renders import txt
|
||||
>>> Expression.STR_RENDER = txt
|
||||
>>> Expression.set_render(txt)
|
||||
|
||||
>>> exp = Expression("1 + 2 * 3")
|
||||
>>> for i in exp.simplify():
|
||||
|
@ -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"
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
||||
|
@ -11,6 +11,8 @@ from pymath.generic import first_elem
|
||||
from pymath.render import txt, tex
|
||||
|
||||
|
||||
Expression.set_render(txt)
|
||||
|
||||
class TestExpression(unittest.TestCase):
|
||||
"""Testing functions from pymath.expression"""
|
||||
|
||||
@ -26,16 +28,16 @@ class TestExpression(unittest.TestCase):
|
||||
self.assertEqual(exp.postfix_tokens, [2, 3, "+"])
|
||||
|
||||
def test_simplify_frac(self):
|
||||
render = lambda x : str(x)
|
||||
render = lambda _,x : str(x)
|
||||
Expression.set_render(render)
|
||||
exp = Expression("1/2 - 4")
|
||||
steps = ["[1, 2, '/', 4, '-']", \
|
||||
"[< Fraction 1 / 2>, 4, '-']", \
|
||||
"[1, 1, '*', 2, 1, '*', '/', 4, 2, '*', 1, 2, '*', '/', '-']", \
|
||||
"[1, 8, '-', 2, '/']", \
|
||||
'[< Fraction -7 / 2>]']
|
||||
self.assertEqual(steps, list(exp.simplify(render = render)))
|
||||
|
||||
Expression.STR_RENDER = tex
|
||||
self.assertEqual(steps, list(exp.simplify()))
|
||||
Expression.set_render(txt)
|
||||
|
||||
def test_add_exp(self):
|
||||
e = Expression("12- 4")
|
||||
|
Loading…
Reference in New Issue
Block a user