bug and unittests for renders

This commit is contained in:
Lafrite 2014-02-26 12:48:41 +01:00
parent 1ecc25b9bf
commit 232368fd75
3 changed files with 59 additions and 29 deletions

View File

@ -74,7 +74,6 @@ class Render(object):
else:
operandeStack.push(token)
# Manip pour gerer les cas de listes imbriquées dans d'autres listes
infix_tokens = operandeStack.pop()
@ -117,6 +116,11 @@ class Render(object):
and operande < 0 \
and posi == "after":
return 1
# Pas de parenthèses si c'est une lettre ou une fraction
elif (type(operande) == str and operande.isalpha()) \
or type(operande) == Fraction:
return 0
# Si c'est un polynom
elif type(operande) == Polynom:
@ -128,7 +132,7 @@ class Render(object):
return 0
elif not self.isNumber(operande):
# Si c'est une grande expression ou un chiffre négatif
# Si c'est une grande expression
stand_alone = self.get_main_op(operande)
# Si la priorité de l'operande est plus faible que celle de l'opérateur
minor_priority = self.PRIORITY[self.get_main_op(operande)] < self.PRIORITY[operator]
@ -202,7 +206,7 @@ class Render(object):
"""
return (type(exp) == str and exp in self.operators)
class flist(list):
"""Fake list- they are used to stock the main operation of an rendered expression"""
pass

View File

@ -4,26 +4,44 @@
from .render import Render
from .fraction import Fraction
from .polynom import Polynom
from .generic import first_elem
# ------------------------
# A console render
txt_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":": ":", "^":"^"}
txt_postfix = {}
txt_other = {"(": "(", ")": ")"}
txt_render = Render(txt_infix, txt_postfix, txt_other)
from .generic import first_elem, last_elem
# ------------------------
# A infix to postfix list convertor
p2i_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":":":", "^":"^"}
p2i_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":": ":", "^":"^"}
p2i_postfix = {}
p2i_other = {"(": "(", ")": ")"}
post2in_fix = Render(p2i_infix, p2i_postfix, p2i_other, join = False)
# ------------------------
# A console render
def txtMult(op1,op2):
""" Tex render for *
Cases where \\times won't be displayed
* nbr letter
* nbr (
* )(
"""
first_nbr = type(op1) in [int, Fraction]
seg_letter = type(op2) == str and op2.isalpha()
first_par = (first_elem(op2) == "(")
seg_par = (last_elem(op1) == ")")
if (first_nbr and (seg_letter or seg_par)) \
or (first_par and seg_par):
return [op1, op2]
else:
return [op1, "*", op2]
txt_infix = {"+": "+", "-": "-", "*": txtMult, "/" : "/", ":":":", "^":"^"}
txt_postfix = {}
txt_other = {"(": "(", ")": ")"}
txt_render = Render(txt_infix, txt_postfix, txt_other)
# ------------------------
# A latex render
@ -40,13 +58,20 @@ def texFrac(frac):
return ["\\frac{" , str(frac._num) , "}{" , str(frac._denom) , "}"]
def texMult(op1,op2):
""" Tex render for * """
fe = first_elem(op2)
if type(fe) != int and (type(fe) == Polynom or fe.isalpha()):
if type(op1) == list and op1[0] == "(":
return ["(", op1[1:-1], op2, ")"]
else:
return [op1, op2]
""" Tex render for *
Cases where \\times won't be displayed
* nbr letter
* nbr (
* )(
"""
first_nbr = type(op1) in [int, Fraction]
seg_letter = type(op2) == str and op2.isalpha()
first_par = (first_elem(op2) == "(")
seg_par = (last_elem(op1) == ")")
if (first_nbr and (seg_letter or seg_par)) \
or (first_par and seg_par):
return [op1, op2]
else:
return [op1, "\\times", op2]
@ -60,12 +85,13 @@ tex_render = Render(tex_infix, tex_postfix, tex_other, type_render = tex_type_re
if __name__ == '__main__':
exp = [2, 5, '^', 1, '-', 3, 4, '*', ':']
print(txt_render(exp))
exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, 5, '/', ':']
#exp = [2, 5, '^', 1, '-', 3, 4, '*', ':']
#print(txt_render(exp))
#exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, 5, '/', ':']
exp = [2, -3, "*"]
print(tex_render(exp))
exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, '+']
print(post2in_fix(exp))
#exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, '+']
#print(post2in_fix(exp))

View File

@ -34,14 +34,14 @@ class TestTexRender(unittest.TestCase):
def test_mult_letter(self):
exps = [ [2, "a", "*"], ["a", 3, "*"], [-2, "a", "*"], ["a", -2, "*"]]
wanted_render = [ "2 a", "3 a", "-2 a", "-2 a"]
wanted_render = [ "2 a", "a \\times 3", "-2 a", "a \\times ( -2 )"]
for (i,e) in enumerate(exps):
rend = tex_render(e)
self.assertEqual(rend, wanted_render[i])
def test_mult_fraction(self):
exps = [ [2, Fraction(1,2), "*"], [Fraction(1,2), 3, "*"]]
wanted_render = [ "2 \\times \\frac{1}{2}", "\\frac{1}{2} \\times 3"]
wanted_render = [ "2 \\times \\frac{ 1 }{ 2 }", "\\frac{ 1 }{ 2 } \\times 3"]
for (i,e) in enumerate(exps):
rend = tex_render(e)
self.assertEqual(rend, wanted_render[i])
@ -79,7 +79,7 @@ class TesttxtRender(unittest.TestCase):
def test_mult_letter(self):
exps = [ [2, "a", "*"], ["a", 3, "*"], [-2, "a", "*"], ["a", -2, "*"]]
wanted_render = [ "2 a", "3 a", "-2 a", "-2 a"]
wanted_render = [ "2 a", "a * 3", "-2 a", "a * ( -2 )"]
for (i,e) in enumerate(exps):
rend = txt_render(e)
self.assertEqual(rend, wanted_render[i])