Feat: set_render to tex for module import and to txt in doctest
This commit is contained in:
parent
f037d5545f
commit
f1424c8704
|
@ -3,7 +3,7 @@
|
|||
|
||||
from .calculus import Expression#, Polynom, Fraction, random_str, txt, Equation
|
||||
|
||||
Expression.RENDER = 'tex'
|
||||
Expression.set_render('tex')
|
||||
|
||||
from .stat import Dataset, WeightedDataset
|
||||
from .geometry import random_pythagore
|
||||
|
|
|
@ -11,6 +11,7 @@ Generate and compute like a student!
|
|||
|
||||
:example:
|
||||
|
||||
>>> Expression.set_render("txt")
|
||||
>>> e = Expression.from_str("2+3*4")
|
||||
>>> e_simplified = e.simplify()
|
||||
>>> print(e_simplified)
|
||||
|
|
|
@ -13,7 +13,7 @@ Expression
|
|||
from ..core import AssocialTree, Tree, compute, typing, TypingError
|
||||
from ..core.random import extract_rdleaf, extract_rv, random_generator, compute_leafs, replace_rdleaf
|
||||
from ..core.MO import moify
|
||||
from .tokens import factory
|
||||
from .tokens import factory, Token
|
||||
from .renders import renders
|
||||
|
||||
|
||||
|
@ -24,6 +24,7 @@ class Expression(object):
|
|||
|
||||
:example:
|
||||
|
||||
>>> Expression.set_render("txt")
|
||||
>>> e = Expression.from_str("2+3*4")
|
||||
>>> e2 = e.simplify()
|
||||
>>> print(e2)
|
||||
|
@ -43,6 +44,40 @@ class Expression(object):
|
|||
self._tree = tree
|
||||
self._ancestor = ancestor
|
||||
|
||||
@classmethod
|
||||
def set_render(cls, render):
|
||||
""" Define default render function
|
||||
|
||||
:param render: render name (txt or tex)
|
||||
|
||||
:example:
|
||||
>>> Expression.set_render('txt')
|
||||
>>> 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
|
||||
>>> Expression.set_render('tex')
|
||||
>>> Expression.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 + \\frac{3}{4}
|
||||
>>> es = e.simplify()
|
||||
>>> print(es)
|
||||
\\frac{11}{4}
|
||||
>>> Expression.set_render('txt')
|
||||
"""
|
||||
Token.set_render(render)
|
||||
cls.RENDER = render
|
||||
|
||||
@classmethod
|
||||
def from_str(cls, string, typing=True):
|
||||
""" Initiate the expression from a string
|
||||
|
@ -99,10 +134,12 @@ class Expression(object):
|
|||
<Exp: -3 / -15>
|
||||
>>> Expression.random("{a}/{a*k} - 3*{b}", variables_scope={'a':{'min_max':(10, 30)}}) # doctest: +SKIP
|
||||
<Exp: 18 / 108 - 3 * 9>
|
||||
>>> e = Expression.random("{a}*x + {b}*x + 3")
|
||||
>>> e = Expression.random("{a}*x + {b}*x + 3", ["a>b"], rejected=[0, 1])
|
||||
>>> ee = e.simplify()
|
||||
>>> print(e) # doctest: +SKIP
|
||||
10x - 6x + 3
|
||||
>>> print(ee) # doctest: +SKIP
|
||||
4x + 3
|
||||
|
||||
"""
|
||||
rd_t = Tree.from_str(template, random=True)
|
||||
|
@ -128,27 +165,6 @@ class Expression(object):
|
|||
except TypeError as e:
|
||||
return cls(t)
|
||||
|
||||
@classmethod
|
||||
def set_render(cls, render):
|
||||
""" Define default render function
|
||||
|
||||
:param render: render function Tree -> str
|
||||
|
||||
:example:
|
||||
>>> Expression.RENDER
|
||||
'txt'
|
||||
>>> e = Expression.from_str("2+3*4")
|
||||
>>> print(e)
|
||||
2 + 3 * 4
|
||||
>>> Expression.set_render('tex')
|
||||
>>> Expression.RENDER
|
||||
'tex'
|
||||
>>> print(e)
|
||||
2 + 3 \\times 4
|
||||
>>> Expression.set_render('txt')
|
||||
"""
|
||||
cls.RENDER = render
|
||||
|
||||
def __str__(self):
|
||||
return renders[self.RENDER](self._tree)
|
||||
|
||||
|
|
|
@ -19,6 +19,8 @@ from decimal import Decimal as _Decimal
|
|||
from .number import Integer, Decimal, Fraction
|
||||
from .polynomial import Polynomial, Linear, Quadratic
|
||||
|
||||
from .token import Token
|
||||
|
||||
__all__ = ["factory"]
|
||||
|
||||
|
||||
|
|
|
@ -29,6 +29,15 @@ class Token(object):
|
|||
def random(cls):
|
||||
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):
|
||||
""" Yield every calculus step which have lead to self
|
||||
|
||||
|
|
|
@ -13,6 +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.
|
||||
|
||||
>>> from mapytex.calculus import Expression
|
||||
>>> Expression.set_render("txt")
|
||||
>>> e = Expression.from_str("2x + 6 - 3x")
|
||||
>>> print(e)
|
||||
2x + 6 - 3x
|
||||
|
@ -31,7 +32,7 @@ Expression is the classe wich handle all calculus. It can randomly generate or i
|
|||
|
||||
from .API import Expression
|
||||
|
||||
__all__ = [Expression]
|
||||
__all__ = ["Expression"]
|
||||
|
||||
|
||||
# -----------------------------
|
||||
|
|
Loading…
Reference in New Issue