diff --git a/mapytex/__init__.py b/mapytex/__init__.py index 691d8cc..57e3ee5 100644 --- a/mapytex/__init__.py +++ b/mapytex/__init__.py @@ -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 diff --git a/mapytex/calculus/API/__init__.py b/mapytex/calculus/API/__init__.py index 7b77ce3..30bb26f 100644 --- a/mapytex/calculus/API/__init__.py +++ b/mapytex/calculus/API/__init__.py @@ -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) diff --git a/mapytex/calculus/API/expression.py b/mapytex/calculus/API/expression.py index 5b42b47..c207636 100644 --- a/mapytex/calculus/API/expression.py +++ b/mapytex/calculus/API/expression.py @@ -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): >>> Expression.random("{a}/{a*k} - 3*{b}", variables_scope={'a':{'min_max':(10, 30)}}) # doctest: +SKIP - >>> 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) diff --git a/mapytex/calculus/API/tokens/__init__.py b/mapytex/calculus/API/tokens/__init__.py index 2d83125..49b796b 100644 --- a/mapytex/calculus/API/tokens/__init__.py +++ b/mapytex/calculus/API/tokens/__init__.py @@ -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"] diff --git a/mapytex/calculus/API/tokens/token.py b/mapytex/calculus/API/tokens/token.py index 63871ff..1a343f2 100644 --- a/mapytex/calculus/API/tokens/token.py +++ b/mapytex/calculus/API/tokens/token.py @@ -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 diff --git a/mapytex/calculus/__init__.py b/mapytex/calculus/__init__.py index 3914e98..91f40c9 100644 --- a/mapytex/calculus/__init__.py +++ b/mapytex/calculus/__init__.py @@ -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"] # -----------------------------