basic simplify and explain methods for Expression

This commit is contained in:
Bertrand Benjamin 2018-09-17 18:18:29 +02:00
parent 5687ccb302
commit 40cc8d00b6
3 changed files with 115 additions and 13 deletions

View File

@ -0,0 +1,19 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# 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

View File

@ -10,9 +10,8 @@
Expression Expression
""" """
from ..core import Tree, compute, tree2txt, tree2tex from ..core import Tree, compute
from .renders import renders
class Expression(object): class Expression(object):
@ -25,19 +24,20 @@ class Expression(object):
>>> e2 = e.simplify() >>> e2 = e.simplify()
>>> print(e2) >>> print(e2)
14 14
>>> e2.explain() >>> for s in e2.explain():
'2 + 3 * 4' ... print(s)
'2 + 12' 2 + 3 * 4
'15' 2 + 12
14
""" """
RENDER = tree2txt RENDER = 'txt'
def __init__(self, tree): def __init__(self, tree, ancestor=None):
""" """
""" """
self._tree = tree self._tree = tree
self._ancestor = ancestor
@classmethod @classmethod
def from_str(cls, string): def from_str(cls, string):
@ -69,20 +69,66 @@ class Expression(object):
:param render: render function Tree -> str :param render: render function Tree -> str
:example: :example:
>>> Expression.RENDER
'txt'
>>> e = Expression.from_str("2+3*4") >>> e = Expression.from_str("2+3*4")
>>> print(e) >>> print(e)
2 + 3 * 4 2 + 3 * 4
>>> Expression.set_render(tree2tex) >>> Expression.set_render('tex')
>>> Expression.RENDER
'tex'
>>> print(e) >>> print(e)
2 + 3 \\times 4 2 + 3 \\times 4
""" """
cls.RENDER = render cls.RENDER = render
def __str__(self): def __str__(self):
return self.__class__.RENDER(self._tree) return renders[self.RENDER](self._tree)
def __repr__(self): def __repr__(self):
return f"<Tree {self.__class__.RENDER(self._tree)}>" return f"<Exp: {renders[self.RENDER](self._tree)}>"
def simplify(self):
""" Compute as much as possible the expression
:return: an expression
:example:
>>> e = Expression.from_str("2+3*4")
>>> e
<Exp: 2 + 3 \\times 4>
>>> f = e.simplify()
>>> f
<Exp: 14>
>>> f._ancestor
<Exp: 2 + 12>
"""
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' # Reglages pour 'vim'

View File

@ -0,0 +1,37 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 lafrite <lafrite@Poivre>
#
# 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