From 40cc8d00b69d19864ada7c07a2a759dd03632a23 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Mon, 17 Sep 2018 18:18:29 +0200 Subject: [PATCH] basic simplify and explain methods for Expression --- mapytex/calculus/API/__init__.py | 19 ++++++++ mapytex/calculus/API/expression.py | 72 ++++++++++++++++++++++++------ mapytex/calculus/API/renders.py | 37 +++++++++++++++ 3 files changed, 115 insertions(+), 13 deletions(-) create mode 100644 mapytex/calculus/API/__init__.py create mode 100644 mapytex/calculus/API/renders.py diff --git a/mapytex/calculus/API/__init__.py b/mapytex/calculus/API/__init__.py new file mode 100644 index 0000000..ebce9c7 --- /dev/null +++ b/mapytex/calculus/API/__init__.py @@ -0,0 +1,19 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# +# Copyright © 2017 lafrite +# +# Distributed under terms of the MIT license. + +""" + +""" + +from .expression import Expression + + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del diff --git a/mapytex/calculus/API/expression.py b/mapytex/calculus/API/expression.py index b026a55..0b8dd05 100644 --- a/mapytex/calculus/API/expression.py +++ b/mapytex/calculus/API/expression.py @@ -10,9 +10,8 @@ Expression """ -from ..core import Tree, compute, tree2txt, tree2tex - - +from ..core import Tree, compute +from .renders import renders class Expression(object): @@ -25,19 +24,20 @@ class Expression(object): >>> e2 = e.simplify() >>> print(e2) 14 - >>> e2.explain() - '2 + 3 * 4' - '2 + 12' - '15' - + >>> for s in e2.explain(): + ... print(s) + 2 + 3 * 4 + 2 + 12 + 14 """ - RENDER = tree2txt + RENDER = 'txt' - def __init__(self, tree): + def __init__(self, tree, ancestor=None): """ """ self._tree = tree + self._ancestor = ancestor @classmethod def from_str(cls, string): @@ -69,20 +69,66 @@ class Expression(object): :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(tree2tex) + >>> Expression.set_render('tex') + >>> Expression.RENDER + 'tex' >>> print(e) 2 + 3 \\times 4 """ cls.RENDER = render def __str__(self): - return self.__class__.RENDER(self._tree) + return renders[self.RENDER](self._tree) def __repr__(self): - return f"" + return f"" + + def simplify(self): + """ Compute as much as possible the expression + + :return: an expression + + :example: + >>> e = Expression.from_str("2+3*4") + >>> e + + >>> f = e.simplify() + >>> f + + >>> f._ancestor + + """ + try: + t = self._tree.apply_on_last_level(compute) + except AttributeError: + return self + else: + e = Expression(t, ancestor=self) + return e.simplify() + + def explain(self): + """ Yield every calculus step which have lead to self + + :example: + >>> e = Expression.from_str("2+3*4") + >>> f = e.simplify() + >>> for s in f.explain(): + ... print(s) + 2 + 3 * 4 + 2 + 12 + 14 + """ + try: + yield from self._ancestor.explain() + except AttributeError: + yield self + else: + yield self # ----------------------------- # Reglages pour 'vim' diff --git a/mapytex/calculus/API/renders.py b/mapytex/calculus/API/renders.py new file mode 100644 index 0000000..6332259 --- /dev/null +++ b/mapytex/calculus/API/renders.py @@ -0,0 +1,37 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# +# Copyright © 2017 lafrite +# +# Distributed under terms of the MIT license. + +""" +Expression + +""" +from ..core import tree2txt, tree2tex + +def _txt(mo_tree): + """ txt render for MOs or Trees""" + try: + return tree2txt(mo_tree) + except AttributeError: + return str(mo_tree) + +def _tex(mo_tree): + """ Tex render for MOs or Trees""" + try: + return tree2tex(mo_tree) + except AttributeError: + return str(mo_tree) + +renders = { + 'txt': _txt, + 'tex': _tex, +} + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del