From e8e0e93719cf276d1d3df6b47c53f30c5ad83ac8 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Wed, 24 Feb 2016 13:15:31 +0100 Subject: Merge hint function into test for {Prolog,Python}Session --- server/handlers.py | 7 +++++-- server/prolog_session.py | 47 ++++++++++++++--------------------------------- server/python_session.py | 47 ++++++++++++++++++----------------------------- 3 files changed, 37 insertions(+), 64 deletions(-) diff --git a/server/handlers.py b/server/handlers.py index 1937730..f427056 100644 --- a/server/handlers.py +++ b/server/handlers.py @@ -272,8 +272,11 @@ class Test(CodeqService): if lang_session is None: request.reply({'code': 3, 'message': 'No active session exists'}) else: - hints = lang_session.test(session.get_sid(), problem_id, program) - request.reply({'code': 0, 'message': 'ok', 'hints': hints}) + try: + hints = lang_session.test(session.get_sid(), problem_id, program) + request.reply({'code': 0, 'message': 'ok', 'hints': hints}) + except Exception as ex: + request.reply({'code': 4, 'message': str(ex)}) class GetAttempts(CodeqService): diff --git a/server/prolog_session.py b/server/prolog_session.py index cac379a..c5fc2be 100644 --- a/server/prolog_session.py +++ b/server/prolog_session.py @@ -111,7 +111,11 @@ class PrologSession(server.LanguageSession): prolog.engine.destroy(self._engine_id) self._engine_id = None + # TODO remove this def hint(self, sid, problem_id, program): + return [] + + def test(self, sid, problem_id, program): session = server.user_session.get_session_by_id(sid) problem = Problem.get(id=problem_id) @@ -119,13 +123,12 @@ class PrologSession(server.LanguageSession): problem_module = load_problem(problem.language, problem.group, problem.identifier, 'common') aux_code = self._aux_code(session.get_uid(), problem, program) - hints = [] - try: - # check if the program is already correct - n_correct, n_all, _ = problem_module.test(program, aux_code=aux_code) - if n_correct == n_all: - hints = [{'id': 'program_already_correct'}] - + # check if the program is correct + n_correct, n_all, msgs = problem_module.test(program, aux_code=aux_code) + if n_correct == n_all: + session.update_solution(problem_id, done=True) + else: + hints = [] if not hints and hasattr(language_module, 'hint'): hints = language_module.hint(program, aux_code=aux_code) if not hints and hasattr(problem_module, 'hint'): @@ -139,33 +142,11 @@ class PrologSession(server.LanguageSession): program, _edits[problem_id], tester, timeout=3) if solution and steps: hints = [{'id': 'monkey_main'}] + monkey.fix_hints(program, steps) - if not hints: - hints = [{'id': 'no_hint'}] - except Exception as ex: - hints = [{'id': 'system_error', 'args': {'message': str(ex)}}] - - self._instantiate_and_save_hints(language_module, problem_module, hints) - return hints - - def test(self, sid, problem_id, program): - session = server.user_session.get_session_by_id(sid) - problem = Problem.get(id=problem_id) - - language_module = load_language(problem.language, 'common') - problem_module = load_problem(problem.language, problem.group, problem.identifier, 'common') - aux_code = self._aux_code(session.get_uid(), problem, program) + if hints: + msgs.extend(hints) - try: - n_correct, n_all, hints = problem_module.test(program, aux_code=aux_code) - if n_correct == n_all: - session.update_solution(problem_id, done=True) - except AttributeError as ex: - hints = [{'id': 'system_error', 'args': {'message': 'test function does not exist'}}] - except Exception as ex: - hints = [{'id': 'system_error', 'args': {'message': str(ex)}}] - - self._instantiate_and_save_hints(language_module, problem_module, hints) - return hints + self._instantiate_and_save_hints(language_module, problem_module, msgs) + return msgs # Return a string with definitions of aux. predicates used in [program] # (but only those that the user has already solved) and any required facts. diff --git a/server/python_session.py b/server/python_session.py index 555a55f..9ceda9b 100644 --- a/server/python_session.py +++ b/server/python_session.py @@ -97,42 +97,31 @@ class PythonSession(server.LanguageSession): def __del__(self): self.destroy() + # TODO remove this def hint(self, sid, problem_id, program): - p = Problem.get(id=problem_id) - language_module = server.problems.load_language(p.language, 'common') - problem_module = server.problems.load_problem(p.language, p.group, p.identifier, 'common') - - hints = [] - # check if the program is already correct - passed, _ = problem_module.test(self.run, program) - if passed: - hints = [{'id': 'program_already_correct'}] - - if not hints and hasattr(language_module, 'hint'): - hints = language_module.hint(self.run, program) - if not hints and hasattr(problem_module, 'hint'): - hints = problem_module.hint(self.run, program) - if not hints: - hints = [{'id': 'no_hint'}] - - self._instantiate_and_save_hints(language_module, problem_module, hints) - return hints + return [] def test(self, sid, problem_id, program): p = Problem.get(id=problem_id) language_module = server.problems.load_language(p.language, 'common') problem_module = server.problems.load_problem(p.language, p.group, p.identifier, 'common') - try: - passed, hints = problem_module.test(self.run, program) - if passed: - session = server.user_session.get_session_by_id(sid) - session.update_solution(problem_id, done=True) - except AttributeError as ex: - hints = [{'id': 'system_error', 'args': {'message': 'test function does not exist'}}] - - self._instantiate_and_save_hints(language_module, problem_module, hints) - return hints + # check if the program is correct + passed, msgs = problem_module.test(self.run, program) + if passed: + session = server.user_session.get_session_by_id(sid) + session.update_solution(problem_id, done=True) + else: + hints = [] + if not hints and hasattr(language_module, 'hint'): + hints = language_module.hint(self.run, program) + if not hints and hasattr(problem_module, 'hint'): + hints = problem_module.hint(self.run, program) + if hints: + msgs.extend(hints) + + self._instantiate_and_save_hints(language_module, problem_module, msgs) + return msgs # Add hint parameters (such as message index) based on hint class. Append # the finalized hints to the list of sent hints. -- cgit v1.2.1