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: else:
operandeStack.push(token) operandeStack.push(token)
# 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()
@ -118,6 +117,11 @@ class Render(object):
and posi == "after": and posi == "after":
return 1 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 # Si c'est un polynom
elif type(operande) == Polynom: elif type(operande) == Polynom:
if operator in ["*", "/", "^"]: if operator in ["*", "/", "^"]:
@ -128,7 +132,7 @@ class Render(object):
return 0 return 0
elif not self.isNumber(operande): 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) stand_alone = self.get_main_op(operande)
# Si la priorité de l'operande est plus faible que celle de l'opérateur # 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] minor_priority = self.PRIORITY[self.get_main_op(operande)] < self.PRIORITY[operator]

View File

@ -4,16 +4,7 @@
from .render import Render from .render import Render
from .fraction import Fraction from .fraction import Fraction
from .polynom import Polynom from .polynom import Polynom
from .generic import first_elem from .generic import first_elem, last_elem
# ------------------------
# A console render
txt_infix = {"+": "+", "-": "-", "*": "*", "/" : "/", ":": ":", "^":"^"}
txt_postfix = {}
txt_other = {"(": "(", ")": ")"}
txt_render = Render(txt_infix, txt_postfix, txt_other)
# ------------------------ # ------------------------
# A infix to postfix list convertor # A infix to postfix list convertor
@ -24,6 +15,33 @@ p2i_other = {"(": "(", ")": ")"}
post2in_fix = Render(p2i_infix, p2i_postfix, p2i_other, join = False) 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 # A latex render
@ -40,12 +58,19 @@ def texFrac(frac):
return ["\\frac{" , str(frac._num) , "}{" , str(frac._denom) , "}"] return ["\\frac{" , str(frac._num) , "}{" , str(frac._denom) , "}"]
def texMult(op1,op2): def texMult(op1,op2):
""" Tex render for * """ """ Tex render for *
fe = first_elem(op2) Cases where \\times won't be displayed
if type(fe) != int and (type(fe) == Polynom or fe.isalpha()): * nbr letter
if type(op1) == list and op1[0] == "(": * nbr (
return ["(", op1[1:-1], op2, ")"] * )(
else: """
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] return [op1, op2]
else: else:
return [op1, "\\times", op2] 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__': if __name__ == '__main__':
exp = [2, 5, '^', 1, '-', 3, 4, '*', ':'] #exp = [2, 5, '^', 1, '-', 3, 4, '*', ':']
print(txt_render(exp)) #print(txt_render(exp))
exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, 5, '/', ':'] #exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, 5, '/', ':']
exp = [2, -3, "*"]
print(tex_render(exp)) print(tex_render(exp))
exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, '+'] #exp = [2, 5, '^', 1, '-', 3, 4, '*', '/', 3, '+']
print(post2in_fix(exp)) #print(post2in_fix(exp))

View File

@ -34,7 +34,7 @@ class TestTexRender(unittest.TestCase):
def test_mult_letter(self): def test_mult_letter(self):
exps = [ [2, "a", "*"], ["a", 3, "*"], [-2, "a", "*"], ["a", -2, "*"]] 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): for (i,e) in enumerate(exps):
rend = tex_render(e) rend = tex_render(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
@ -79,7 +79,7 @@ class TesttxtRender(unittest.TestCase):
def test_mult_letter(self): def test_mult_letter(self):
exps = [ [2, "a", "*"], ["a", 3, "*"], [-2, "a", "*"], ["a", -2, "*"]] 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): for (i,e) in enumerate(exps):
rend = txt_render(e) rend = txt_render(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])