From 046463bcebe08c6cbe3504d676c0bf27f8695482 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Fri, 11 Sep 2015 11:01:13 +0200 Subject: Add language hints for Python and use Hint classes --- python/common.py | 19 +++++++++++++++++++ .../introduction/fahrenheit_to_celsius/common.py | 8 +++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 python/common.py (limited to 'python') diff --git a/python/common.py b/python/common.py new file mode 100644 index 0000000..39c323f --- /dev/null +++ b/python/common.py @@ -0,0 +1,19 @@ +# coding=utf-8 + +import ast +from server.hints import Hint, HintSequence + +hint_type = { + 'no_hint': Hint('no_hint'), + 'test_results': Hint('test_results'), + 'syntax_error': Hint('syntax_error'), +} + +def hint(program): + # Check program for syntax errors. + try: + tree = ast.parse(program, filename='user') + except SyntaxError as ex: + error_msg = '{}{}^\n{}'.format(ex.text, ' '*(ex.offset-1), ex.msg) + return [{'id': 'syntax_error', 'args': {'lineno': ex.lineno, 'message': error_msg}}] + return [] diff --git a/python/problems/introduction/fahrenheit_to_celsius/common.py b/python/problems/introduction/fahrenheit_to_celsius/common.py index 1555cf7..5c8e0cd 100644 --- a/python/problems/introduction/fahrenheit_to_celsius/common.py +++ b/python/problems/introduction/fahrenheit_to_celsius/common.py @@ -1,5 +1,8 @@ # coding=utf-8 +from python.util import has_token_sequence +from server.hints import Hint, HintSequence + id = 180 group = 'introduction' number = 1 @@ -11,7 +14,10 @@ f = float(input("Temperatura [F]: ")) print("Temperatura je", c, "C") ''' -from python.util import has_token_sequence +hint_type = { + 'plan': Hint('plan'), + 'no_input_call': Hint('no_input_call'), +} def test(session, code): # List of inputs: (expression to eval, stdin). -- cgit v1.2.1