Render ~work with letters still some bugs

This commit is contained in:
lafrite 2014-01-17 14:57:45 +01:00
parent 65c05a35b5
commit 8a41031fa8
2 changed files with 27 additions and 6 deletions

View File

@ -132,6 +132,12 @@ class Expression(object):
else: else:
tokens.append(int(character)) tokens.append(int(character))
elif character.isalpha():
if str(tokens[-1]).isalpha():
tokens[-1] += character
else:
tokens.append(character)
elif character in "+-*/)": elif character in "+-*/)":
tokens.append(character) tokens.append(character)
@ -143,7 +149,6 @@ class Expression(object):
elif character != " ": elif character != " ":
raise ValueError("{} is an unvalid character".format(character)) raise ValueError("{} is an unvalid character".format(character))
print(tokens[1:])
return tokens[1:] return tokens[1:]
# --------------------- # ---------------------
@ -292,7 +297,9 @@ class Expression(object):
:returns: True if the expression can be a number and false otherwise :returns: True if the expression can be a number and false otherwise
""" """
return type(exp) == int or type(exp) == Fraction return type(exp) == int or \
type(exp) == Fraction or \
exp.isalpha()
@staticmethod @staticmethod
def isOperator(exp): def isOperator(exp):
@ -360,6 +367,15 @@ if __name__ == '__main__':
exp="-2*4(12 + 1)(3-12)" exp="-2*4(12 + 1)(3-12)"
test(exp) test(exp)
exp="-2*a(12 + 1)(3-12)"
e = Expression(exp)
print(e)
# TODO: The next one doesn't work |ven. janv. 17 14:56:58 CET 2014
#exp="-2*(-a)(12 + 1)(3-12)"
#e = Expression(exp)
#print(e)
## Can't handle it yet!! ## Can't handle it yet!!
#exp="-(-2)" #exp="-(-2)"
#test(exp) #test(exp)

View File

@ -16,7 +16,7 @@ class Render(object):
PRIORITY = {"*" : 3, "/": 3, "+": 2, "-":2, "(": 1} PRIORITY = {"*" : 3, "/": 3, "+": 2, "-":2, "(": 1}
def __init__(self, op_infix = {}, op_postfix = {}, other = {}, join = " ", type_render = {int: str, Fraction: str}): def __init__(self, op_infix = {}, op_postfix = {}, other = {}, join = " ", type_render = {int: str, Fraction: str, str: str}):
"""Initiate the render """Initiate the render
@param op_infix: the dictionnary of infix operator with how they have to be render @param op_infix: the dictionnary of infix operator with how they have to be render
@ -107,7 +107,9 @@ class Render(object):
:param posi: "after"(default) if the operande will be after the operator, "before" othewise :param posi: "after"(default) if the operande will be after the operator, "before" othewise
:returns: bollean :returns: bollean
""" """
if self.isNumber(operande) and operande < 0: if self.isNumber(operande) \
and type(operande) != str \
and operande < 0:
return 1 return 1
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 ou un chiffre négatif
@ -159,7 +161,10 @@ class Render(object):
:returns: True if the expression can be a number and false otherwise :returns: True if the expression can be a number and false otherwise
""" """
return type(exp) == int or type(exp) == Fraction return type(exp) == int or \
type(exp) == Fraction or \
(type(exp) == str and exp.isalpha())
#return type(exp) == int or type(exp) == Fraction
def isOperator(self, exp): def isOperator(self, exp):
"""Check if the expression is in self.operators """Check if the expression is in self.operators
@ -209,7 +214,7 @@ def texFrac(frac):
tex_infix = {"+": " + ", "-": " - ", "*": " \\times "} tex_infix = {"+": " + ", "-": " - ", "*": " \\times "}
tex_postfix = {"/": texSlash} tex_postfix = {"/": texSlash}
tex_other = {"(": "(", ")": ")"} tex_other = {"(": "(", ")": ")"}
tex_type_render = {int: str, Fraction: texFrac} tex_type_render = {int: str, Fraction: texFrac, str: str}
tex_render = Render(tex_infix, tex_postfix, tex_other, type_render = tex_type_render) tex_render = Render(tex_infix, tex_postfix, tex_other, type_render = tex_type_render)