not very good render especially with negativ numbers... but can work
This commit is contained in:
parent
cb198eb5f0
commit
d566867ae8
@ -75,6 +75,37 @@ def flatten_list(a, result=None):
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def first_elem(ll):
|
||||||
|
"""Get the first element in imbricates lists
|
||||||
|
# TODO: Fonction pourrie mais j'ai pas le temps de faire mieux! |mar. janv. 28 22:32:22 CET 2014
|
||||||
|
|
||||||
|
:param list: list of lists of lists...
|
||||||
|
:returns: the first element
|
||||||
|
|
||||||
|
>>> first_elem(1)
|
||||||
|
1
|
||||||
|
>>> first_elem([1,2])
|
||||||
|
1
|
||||||
|
>>> first_elem([["abc"]])
|
||||||
|
'a'
|
||||||
|
>>> first_elem("abc")
|
||||||
|
'a'
|
||||||
|
>>> first_elem([[[1,2],[3,4]], [5,6]])
|
||||||
|
1
|
||||||
|
>>> first_elem([[["ab",2],[3,4]], [5,6]])
|
||||||
|
'a'
|
||||||
|
|
||||||
|
"""
|
||||||
|
if hasattr(ll, '__contains__'):
|
||||||
|
if len(ll) == 1 and type(ll) == str:
|
||||||
|
return ll[0]
|
||||||
|
else:
|
||||||
|
return first_elem(ll[0])
|
||||||
|
else:
|
||||||
|
return ll
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def expand_list(list_list):
|
def expand_list(list_list):
|
||||||
"""Expand list of list
|
"""Expand list of list
|
||||||
|
|
||||||
@ -189,6 +220,7 @@ if __name__ == '__main__':
|
|||||||
import doctest
|
import doctest
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Reglages pour 'vim'
|
# Reglages pour 'vim'
|
||||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||||
|
@ -111,12 +111,12 @@ def desc_rdExp(rdExp):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
form = "{a}*-14 / (2*{b}) : -23 / 4"
|
#form = "{a}*-14 / (2*{b}) : -23 / 4"
|
||||||
cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "{a} not in [0,1]", "{b} not in [0,1]"]
|
#cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "{a} not in [0,1]", "{b} not in [0,1]"]
|
||||||
rdExp1 = RdExpression(form, cond)
|
#rdExp1 = RdExpression(form, cond)
|
||||||
desc_rdExp(rdExp1)
|
#desc_rdExp(rdExp1)
|
||||||
rdExp2 = RdExpression(form)
|
#rdExp2 = RdExpression(form)
|
||||||
desc_rdExp(rdExp2)
|
#desc_rdExp(rdExp2)
|
||||||
#form = "{a+a/10}x + {a} + 2*{b}"
|
#form = "{a+a/10}x + {a} + 2*{b}"
|
||||||
#cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "{a} not in [0,1]", "{b} not in [0,1]"]
|
#cond = ["{a} + {b} in [1, 2, 3, 4, 5]", "{a} not in [0,1]", "{b} not in [0,1]"]
|
||||||
#rdExp3 = RdExpression(form)
|
#rdExp3 = RdExpression(form)
|
||||||
|
@ -7,7 +7,7 @@ from .formal import FormalExp
|
|||||||
|
|
||||||
class Render(object):
|
class Render(object):
|
||||||
"""A class which aims to create render functions from three dictionnaries:
|
"""A class which aims to create render functions from three dictionnaries:
|
||||||
- op_infix: dict of caracters
|
- op_infix: dict of caracters or two argument functions
|
||||||
- op_postfix: dict of 2 arguments functions
|
- op_postfix: dict of 2 arguments functions
|
||||||
- other: dict of caracters
|
- other: dict of caracters
|
||||||
Those three dictionnaries while define how a postfix expression will be transform into a string.
|
Those three dictionnaries while define how a postfix expression will be transform into a string.
|
||||||
@ -57,7 +57,11 @@ class Render(object):
|
|||||||
op1 = [self.other["("] , op1 , self.other[")"]]
|
op1 = [self.other["("] , op1 , self.other[")"]]
|
||||||
|
|
||||||
if token in self.op_infix:
|
if token in self.op_infix:
|
||||||
|
if type(self.op_infix[token]) == str:
|
||||||
res = flist([op1 , self.op_infix[token] , op2])
|
res = flist([op1 , self.op_infix[token] , op2])
|
||||||
|
else:
|
||||||
|
res = flist([self.op_infix[token](op1, op2)])
|
||||||
|
|
||||||
|
|
||||||
elif token in self.op_postfix:
|
elif token in self.op_postfix:
|
||||||
res = flist([self.op_postfix[token](op1, op2)])
|
res = flist([self.op_postfix[token](op1, op2)])
|
||||||
@ -73,6 +77,7 @@ class Render(object):
|
|||||||
|
|
||||||
# Manip pour gerer les cas de listes imbriquées dans d'autres listes
|
# Manip pour gerer les cas de listes imbriquées dans d'autres listes
|
||||||
infix_tokens = operandeStack.pop()
|
infix_tokens = operandeStack.pop()
|
||||||
|
|
||||||
if type(infix_tokens) == list or type(infix_tokens) == flist:
|
if type(infix_tokens) == list or type(infix_tokens) == flist:
|
||||||
infix_tokens = flatten_list(infix_tokens)
|
infix_tokens = flatten_list(infix_tokens)
|
||||||
elif self.isNumerande(infix_tokens):
|
elif self.isNumerande(infix_tokens):
|
||||||
|
13
renders.py
13
renders.py
@ -4,6 +4,7 @@
|
|||||||
from render import Render
|
from render import Render
|
||||||
from fraction import Fraction
|
from fraction import Fraction
|
||||||
from formal import FormalExp
|
from formal import FormalExp
|
||||||
|
from generic import first_elem
|
||||||
|
|
||||||
# ------------------------
|
# ------------------------
|
||||||
# A console render
|
# A console render
|
||||||
@ -36,7 +37,17 @@ def texSlash(op1, op2):
|
|||||||
def texFrac(frac):
|
def texFrac(frac):
|
||||||
return ["\\frac{" , str(frac._num) , "}{" , str(frac._denom) , "}"]
|
return ["\\frac{" , str(frac._num) , "}{" , str(frac._denom) , "}"]
|
||||||
|
|
||||||
tex_infix = {"+": " + ", "-": " - ", "*": " \\times ", ":": ":", "^":"^"}
|
def texMult(op1,op2):
|
||||||
|
fe = first_elem(op2)
|
||||||
|
if type(fe) == FormalExp or fe.isalpha():
|
||||||
|
if type(op1) == list and op1[0] == "(":
|
||||||
|
return ["(", op1[1:-1], op2, ")"]
|
||||||
|
else:
|
||||||
|
return [op1, op2]
|
||||||
|
else:
|
||||||
|
return [op1, "\\times", op2]
|
||||||
|
|
||||||
|
tex_infix = {"+": " + ", "-": " - ", "*": texMult , ":": ":", "^":"^"}
|
||||||
tex_postfix = {"/": texSlash}
|
tex_postfix = {"/": texSlash}
|
||||||
tex_other = {"(": "(", ")": ")"}
|
tex_other = {"(": "(", ")": ")"}
|
||||||
tex_type_render = {int: str, Fraction: texFrac, FormalExp: str}
|
tex_type_render = {int: str, Fraction: texFrac, FormalExp: str}
|
||||||
|
Loading…
Reference in New Issue
Block a user