From 7b98be750d401b2b6093d8de61e04c0e5f5fb764 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Mon, 31 Aug 2015 15:39:19 +0200 Subject: Add hint & test functions for PythonSession --- server/python_session.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'server') diff --git a/server/python_session.py b/server/python_session.py index 7a5d22a..24a06f1 100644 --- a/server/python_session.py +++ b/server/python_session.py @@ -2,6 +2,8 @@ import threading import python.engine +from db.models import Problem +from . import problems __all__ = ['PythonSession'] @@ -43,5 +45,24 @@ class PythonSession(object): python.engine.destroy(self._interpreter) self._interpreter = None + def hint(self, user_id, problem_id, program): + language, problem_group, problem = Problem.identifier(problem_id) + + # Try problem-specific hints. + problem_module = problems.load_problem(language, problem_group, problem, 'common') + if hasattr(problem_module, 'hint'): + hints = problem_module.hint(program) + if hints: + return hints + + # Finally return a generic "try thinking a bit" message. + return [{'id': 'no_hint'}] + def test(self, user_id, problem_id, program): - return ['Python testing is not implemented yet'] + language, problem_group, problem = Problem.identifier(problem_id) + problem_module = problems.load_problem(language, problem_group, problem, 'common') + try: + n_correct, n_all = problem_module.test(program) + return [{'id': 'test_results', 'args': {'passed': n_correct, 'total': n_all}}] + except AttributeError as ex: + return [{'id': 'test_results', 'args': {'passed': 0, 'total': 0}}] -- cgit v1.2.1