modif for be able to use it for first big use
This commit is contained in:
parent
9ec684477f
commit
5a8da2f470
|
@ -3,7 +3,7 @@
|
|||
|
||||
from .generic import Stack, flatten_list, expand_list
|
||||
from .fraction import Fraction
|
||||
from .render import txt_render, post2in_fix, tex_render
|
||||
from .renders import txt_render, post2in_fix, tex_render
|
||||
from .formal import FormalExp
|
||||
|
||||
class Expression(object):
|
||||
|
|
|
@ -5,7 +5,6 @@ from .generic import Stack,flatten_list
|
|||
from .fraction import Fraction
|
||||
from .formal import FormalExp
|
||||
|
||||
|
||||
class Render(object):
|
||||
"""A class which aims to create render functions from three dictionnaries:
|
||||
- op_infix: dict of caracters
|
||||
|
@ -14,11 +13,7 @@ class Render(object):
|
|||
Those three dictionnaries while define how a postfix expression will be transform into a string.
|
||||
"""
|
||||
|
||||
<<<<<<< HEAD
|
||||
PRIORITY = {"^": 4,"*" : 3, "/": 3, ":": 3, "+": 2, "-":2, "(": 1}
|
||||
=======
|
||||
PRIORITY = {"*" : 3, "/": 3, ":": 3, "+": 2, "-":2, "(": 1}
|
||||
>>>>>>> render accept ":"
|
||||
|
||||
def __init__(self, op_infix = {}, op_postfix = {}, other = {}, join = " ", type_render = {int: str, Fraction: str, FormalExp: str}):
|
||||
"""Initiate the render
|
||||
|
@ -206,53 +201,6 @@ class flist(list):
|
|||
pass
|
||||
|
||||
|
||||
# ------------------------
|
||||
# A console render
|
||||
|
||||
txt_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":": ":"}
|
||||
txt_postfix = {}
|
||||
txt_other = {"(": "(", ")": ")"}
|
||||
|
||||
txt_render = Render(txt_infix, txt_postfix, txt_other)
|
||||
|
||||
# ------------------------
|
||||
# A infix to postfix list convertor
|
||||
|
||||
p2i_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":":":"}
|
||||
p2i_postfix = {}
|
||||
p2i_other = {"(": "(", ")": ")"}
|
||||
|
||||
post2in_fix = Render(p2i_infix, p2i_postfix, p2i_other, join = False)
|
||||
|
||||
# ------------------------
|
||||
# A latex render
|
||||
|
||||
def texSlash(op1, op2):
|
||||
if not Render.isNumerande(op1) and op1[0] == "(" and op1[-1] == ")":
|
||||
op1 = op1[1:-1]
|
||||
if not Render.isNumerande(op2) and op2[0] == "(" and op2[-1] == ")":
|
||||
op2 = op2[1:-1]
|
||||
return ["\\frac{" , op1 , "}{" , op2 , "}"]
|
||||
|
||||
def texFrac(frac):
|
||||
return ["\\frac{" , str(frac._num) , "}{" , str(frac._denom) , "}"]
|
||||
|
||||
tex_infix = {"+": " + ", "-": " - ", "*": " \\times ", ":": ":"}
|
||||
tex_postfix = {"/": texSlash}
|
||||
tex_other = {"(": "(", ")": ")"}
|
||||
tex_type_render = {int: str, Fraction: texFrac, FormalExp: str}
|
||||
|
||||
tex_render = Render(tex_infix, tex_postfix, tex_other, type_render = tex_type_render)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exp = [2, 5, '+', 1, '-', 3, 4, '*', ':']
|
||||
print(txt_render(exp))
|
||||
exp = [2, 5, '+', 1, '-', 3, 4, '*', '/', 3, 5, '/', ':']
|
||||
print(tex_render(exp))
|
||||
exp = [2, 5, '+', 1, '-', 3, 4, '*', '/', 3, '+']
|
||||
print(post2in_fix(exp))
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
|
|
Loading…
Reference in New Issue