Start writing expression class

This commit is contained in:
Bertrand Benjamin 2018-09-17 16:29:00 +02:00
parent ec25cfab53
commit 5687ccb302
1 changed files with 39 additions and 3 deletions

View File

@ -10,6 +10,7 @@
Expression Expression
""" """
from ..core import Tree, compute, tree2txt, tree2tex
@ -18,12 +19,25 @@ class Expression(object):
""" """
Expression class Expression class
:example:
>>> e = Expression.from_str("2+3*4")
>>> e2 = e.simplify()
>>> print(e2)
14
>>> e2.explain()
'2 + 3 * 4'
'2 + 12'
'15'
""" """
def __init__(self,): RENDER = tree2txt
def __init__(self, tree):
""" """
""" """
pass self._tree = tree
@classmethod @classmethod
def from_str(cls, string): def from_str(cls, string):
@ -33,7 +47,8 @@ class Expression(object):
:returns: TODO :returns: TODO
""" """
pass t = Tree.from_str(string)
return cls(t)
@classmethod @classmethod
def random(self, template, conditions = [], shuffle = False): def random(self, template, conditions = [], shuffle = False):
@ -47,6 +62,27 @@ class Expression(object):
""" """
pass pass
@classmethod
def set_render(cls, render):
""" Define default render function
:param render: render function Tree -> str
:example:
>>> e = Expression.from_str("2+3*4")
>>> print(e)
2 + 3 * 4
>>> Expression.set_render(tree2tex)
>>> print(e)
2 + 3 \\times 4
"""
cls.RENDER = render
def __str__(self):
return self.__class__.RENDER(self._tree)
def __repr__(self):
return f"<Tree {self.__class__.RENDER(self._tree)}>"
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'