Created
July 25, 2012 15:17
-
-
Save alyssonbruno/3176731 to your computer and use it in GitHub Desktop.
Verificando expressão numérica
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def eval_expr(text): | |
test_char = lambda c: c in '1234567890.,*/+-()' | |
for char in text: | |
if not test_char(char): | |
raise Exception('Invalid Char in String') | |
return long(eval(text,{},{})) |
não testei, mas faria algo assim:
import re
def eval_expr(text):
if not re.match('^[-0-9.,*/+()]*$'):
raise ValueError('Invalid char in string')
return eval(text, {}, {})
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Entendi, mas você pode usar o string.digits e também remover a função lambda.