Fix: Rendering and changing it works
This commit is contained in:
parent
95fd12c430
commit
460255b151
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
from .calculus import Expression, Integer, Decimal, random_list
|
from .calculus import Expression, Integer, Decimal, random_list, render
|
||||||
|
|
||||||
# Expression.set_render('tex')
|
# Expression.set_render('tex')
|
||||||
|
|
||||||
|
@ -114,43 +114,19 @@ x^7
|
|||||||
<Quadratic - 2x^2 + 7x - 6>
|
<Quadratic - 2x^2 + 7x - 6>
|
||||||
>>> for s in e_simplified.explain():
|
>>> for s in e_simplified.explain():
|
||||||
... print(s)
|
... print(s)
|
||||||
|
(2x - 3)(- x + 2)
|
||||||
|
(2x - 3)(- x + 2)
|
||||||
|
2x(- x) + 2x * 2 - 3(- x) - 3 * 2
|
||||||
|
2 * - 1 * x^(1 + 1) + 2 * 2 * x - 3 * - 1 * x - 6
|
||||||
|
4x + 3x - 2x^2 - 6
|
||||||
|
(4 + 3) * x - 2x^2 - 6
|
||||||
|
- 2x^2 + 7x - 6
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .expression import Expression
|
from .expression import Expression
|
||||||
from .tokens import Token
|
from .tokens import Token
|
||||||
from .tokens.number import Integer, Decimal
|
from .tokens.number import Integer, Decimal, Fraction
|
||||||
|
from .renders import render
|
||||||
|
|
||||||
def set_render(render):
|
|
||||||
"""
|
|
||||||
:example:
|
|
||||||
>>> e = Expression.from_str("2+3*4")
|
|
||||||
>>> print(e)
|
|
||||||
2 + 3 * 4
|
|
||||||
>>> e = Expression.from_str("2+3/4")
|
|
||||||
>>> print(e)
|
|
||||||
2 + 3 / 4
|
|
||||||
>>> es = e.simplify()
|
|
||||||
>>> print(es)
|
|
||||||
11 / 4
|
|
||||||
>>> set_render('tex')
|
|
||||||
>>> Expression.RENDER
|
|
||||||
'tex'
|
|
||||||
>>> Token.RENDER
|
|
||||||
'tex'
|
|
||||||
>>> e = Expression.from_str("2+3*4")
|
|
||||||
>>> print(e)
|
|
||||||
2 + 3 \\times 4
|
|
||||||
>>> e = Expression.from_str("2+3/4")
|
|
||||||
>>> print(e)
|
|
||||||
2 + \\dfrac{3}{4}
|
|
||||||
>>> es = e.simplify()
|
|
||||||
>>> print(es)
|
|
||||||
\\dfrac{11}{4}
|
|
||||||
>>> set_render('txt')
|
|
||||||
"""
|
|
||||||
Expression.set_render(render)
|
|
||||||
Token.set_render(render)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -21,7 +21,7 @@ from ..core.random import (
|
|||||||
)
|
)
|
||||||
from ..core.MO import moify
|
from ..core.MO import moify
|
||||||
from .tokens import factory
|
from .tokens import factory
|
||||||
from .renders import renders
|
from .renders import render
|
||||||
|
|
||||||
|
|
||||||
class Expression(object):
|
class Expression(object):
|
||||||
@ -42,7 +42,6 @@ class Expression(object):
|
|||||||
14
|
14
|
||||||
"""
|
"""
|
||||||
|
|
||||||
RENDER = "txt"
|
|
||||||
|
|
||||||
def __init__(self, tree, ancestor=None):
|
def __init__(self, tree, ancestor=None):
|
||||||
"""
|
"""
|
||||||
@ -50,15 +49,6 @@ class Expression(object):
|
|||||||
self._tree = tree
|
self._tree = tree
|
||||||
self._ancestor = ancestor
|
self._ancestor = ancestor
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def set_render(cls, render):
|
|
||||||
""" Define default render function
|
|
||||||
|
|
||||||
:param render: render name (txt or tex)
|
|
||||||
|
|
||||||
"""
|
|
||||||
cls.RENDER = render
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_str(cls, string, typing=True):
|
def from_str(cls, string, typing=True):
|
||||||
""" Initiate the expression from a string
|
""" Initiate the expression from a string
|
||||||
@ -148,10 +138,10 @@ class Expression(object):
|
|||||||
return cls(t)
|
return cls(t)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return renders[self.RENDER](self._tree)
|
return render(self._tree)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<Exp: {renders['txt'](self._tree)}>"
|
return f"<Exp: {render(self._tree, 'txt')}>"
|
||||||
|
|
||||||
def _order(self, exclude_nodes=["*", "/", "**"]):
|
def _order(self, exclude_nodes=["*", "/", "**"]):
|
||||||
""" Order the expression base on types
|
""" Order the expression base on types
|
||||||
|
@ -12,34 +12,55 @@ Expression
|
|||||||
"""
|
"""
|
||||||
from ..core import tree2txt, tree2tex
|
from ..core import tree2txt, tree2tex
|
||||||
|
|
||||||
|
class Render(object):
|
||||||
|
""" Object which render Expression or token """
|
||||||
|
|
||||||
def _txt(mo_tree):
|
def __init__(self, default="txt"):
|
||||||
""" txt render for MOs or Trees"""
|
self._default = default
|
||||||
|
self._render = default
|
||||||
|
self.renders = {}
|
||||||
|
|
||||||
|
def register_render(self, name, func, attribute):
|
||||||
|
""" register a render """
|
||||||
try:
|
try:
|
||||||
return tree2txt(mo_tree)
|
self.renders[name]
|
||||||
|
except KeyError:
|
||||||
|
self.renders[name] = {"name": name, "func": func, "attribute": attribute}
|
||||||
|
else:
|
||||||
|
raise ValueError("This render name already exists")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def render(self):
|
||||||
|
return self.renders[self._render]
|
||||||
|
|
||||||
|
def __call__(self, mo_tree, tmp_render=''):
|
||||||
|
if tmp_render:
|
||||||
|
r = self.renders[tmp_render]
|
||||||
|
else:
|
||||||
|
r = self.render
|
||||||
|
|
||||||
|
try:
|
||||||
|
return r["func"](mo_tree)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return mo_tree.__txt__
|
return getattr(mo_tree, r["attribute"])
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return str(mo_tree)
|
return str(mo_tree)
|
||||||
|
|
||||||
|
def set_render(self, render):
|
||||||
def _tex(mo_tree):
|
""" Define the render """
|
||||||
""" Tex render for MOs or Trees"""
|
if render in self.renders.keys():
|
||||||
try:
|
self._render = render
|
||||||
return tree2tex(mo_tree)
|
else:
|
||||||
except ValueError:
|
raise ValueError("This render does not exists")
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
|
||||||
return mo_tree.__tex__
|
|
||||||
except AttributeError:
|
|
||||||
return str(mo_tree)
|
|
||||||
|
|
||||||
|
|
||||||
renders = {"txt": _txt, "tex": _tex}
|
|
||||||
|
render = Render()
|
||||||
|
render.register_render("txt", tree2txt, "__txt__")
|
||||||
|
render.register_render("tex", tree2tex, "__tex__")
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
Tokens: practical envelop of math object
|
Tokens: practical envelop of math object
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from ..renders import renders
|
from ..renders import render
|
||||||
from ...core.MO.atoms import moify
|
from ...core.MO.atoms import moify
|
||||||
|
|
||||||
|
|
||||||
@ -18,8 +18,6 @@ class Token(object):
|
|||||||
|
|
||||||
""" Token: practical envelop of an math object """
|
""" Token: practical envelop of an math object """
|
||||||
|
|
||||||
RENDER = "txt"
|
|
||||||
|
|
||||||
def __init__(self, mo, name="", ancestor=None):
|
def __init__(self, mo, name="", ancestor=None):
|
||||||
self._mo = mo
|
self._mo = mo
|
||||||
self.name = name
|
self.name = name
|
||||||
@ -34,14 +32,6 @@ class Token(object):
|
|||||||
):
|
):
|
||||||
raise NotImplemented
|
raise NotImplemented
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def set_render(cls, render):
|
|
||||||
""" Define default render function
|
|
||||||
|
|
||||||
:param render: render name (txt or tex)
|
|
||||||
"""
|
|
||||||
cls.RENDER = render
|
|
||||||
|
|
||||||
def explain(self):
|
def explain(self):
|
||||||
""" Yield every calculus step which have lead to self
|
""" Yield every calculus step which have lead to self
|
||||||
|
|
||||||
@ -64,10 +54,10 @@ class Token(object):
|
|||||||
yield self
|
yield self
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<{self.__class__.__name__} {renders['txt'](self._mo)}>"
|
return f"<{self.__class__.__name__} {render(self._mo, 'txt')}>"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return renders[self.RENDER](self._mo)
|
return render(self._mo)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def raw(self):
|
def raw(self):
|
||||||
|
@ -13,7 +13,7 @@ Make calculus as a student
|
|||||||
Expression is the classe wich handle all calculus. It can randomly generate or import calculus, simplify them and explain them as a student would do.
|
Expression is the classe wich handle all calculus. It can randomly generate or import calculus, simplify them and explain them as a student would do.
|
||||||
|
|
||||||
>>> from mapytex.calculus import Expression
|
>>> from mapytex.calculus import Expression
|
||||||
>>> Expression.set_render("txt")
|
>>> render.set_render("txt")
|
||||||
>>> e = Expression.from_str("2x + 6 - 3x")
|
>>> e = Expression.from_str("2x + 6 - 3x")
|
||||||
>>> print(e)
|
>>> print(e)
|
||||||
2x + 6 - 3x
|
2x + 6 - 3x
|
||||||
@ -30,7 +30,7 @@ Expression is the classe wich handle all calculus. It can randomly generate or i
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .API import Expression, Integer, Decimal
|
from .API import Expression, Integer, Decimal, render
|
||||||
from .core import random_list
|
from .core import random_list
|
||||||
from decimal import getcontext
|
from decimal import getcontext
|
||||||
#getcontext().prec = 2
|
#getcontext().prec = 2
|
||||||
|
Loading…
Reference in New Issue
Block a user