From 138101ec997ee80e8f33f690ae47e451b5b278f7 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 22 Sep 2015 17:26:24 +0200 Subject: Added basic handling of exception to util.py --- python/util.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'python') diff --git a/python/util.py b/python/util.py index 21426ca..ecd5ee8 100644 --- a/python/util.py +++ b/python/util.py @@ -18,7 +18,7 @@ def has_token_sequence(tokens, sequence): def almost_equal(a, b, prec=1): """ Compares values a and b using at most prec decimal values. """ - return int(a*10**prec) == int(b*10**prec) + return round(a*10**prec) == round(b*10**prec) def get_numbers(s): """ Extracts numbers from string s. """ @@ -46,6 +46,21 @@ def string_contains_number(s, a): return a in get_numbers(s) +def get_exception_desc(exc): + # if have an exception! + if exc: + if 'EOFError' in exc: + return [{'id':'eof_error'}] + if 'timed out' in exc: + return [{'id':'timed_out'}] + if 'NameError' in exc: + return [{'id':'name_error', 'args': {'message': exc}}] + elif 'TypeError' in exc: + return [{'id':'type_error', 'args': {'message': exc}}] + else: + return [{'id':'error', 'args': {'message': exc}}] + return None + if __name__ == '__main__': print(has_token_sequence(get_tokens('x + y >= 0'), ['>=', '0'])) print(has_token_sequence(get_tokens('x + y > 0'), ['>=', '0'])) -- cgit v1.2.1