diff options
author | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2015-10-15 15:47:47 +0200 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2015-10-15 15:47:47 +0200 |
commit | ca8dcfdf819dae2aed6c864a3a11f1e30c632e96 (patch) | |
tree | c5dbc06099c417eac2422d9ebd7bfc9ada74e8b8 | |
parent | cbc096f9cb44a7d26b4fa01a40dbba594ab339ca (diff) |
Store identifiers instead of IDs in Problem model
-rw-r--r-- | db/models.py | 19 | ||||
-rw-r--r-- | monkey/edits.py | 4 | ||||
-rwxr-xr-x | monkey/test.py | 25 | ||||
-rw-r--r-- | server/prolog_session.py | 36 | ||||
-rw-r--r-- | server/python_session.py | 12 | ||||
-rw-r--r-- | server/robot_session.py | 12 |
6 files changed, 53 insertions, 55 deletions
diff --git a/db/models.py b/db/models.py index e4b5317..f5c690d 100644 --- a/db/models.py +++ b/db/models.py @@ -26,24 +26,27 @@ class CodeqUser(collections.namedtuple('CodeqUser', ['id', 'username', 'password def solved_problems(user_id, language): return _run_sql('select g.identifier, p.identifier from solution s inner join problem p on p.id = s.problem_id inner join problem_group g on g.id = p.problem_group_id inner join language l on l.id = p.language_id where s.codeq_user_id = %s and l.identifier = %s and s.done = True', (user_id, language), fetch_one=False) -class Problem(collections.namedtuple('Problem', ['id', 'language_id', 'problem_group_id', 'identifier'])): - __sql_prefix = 'select id, language_id, problem_group_id, identifier from problem' +class Problem(collections.namedtuple('Problem', ['id', 'language', 'group', 'identifier'])): + __sql_prefix = '''\ + select p.id, l.identifier, g.identifier, p.identifier + from problem p + inner join problem_group g on g.id = p.problem_group_id + inner join language l on l.id = p.language_id''' + __sql_order = 'p.language_id, p.problem_group_id, p.id' @staticmethod def get(**kwargs): + kwargs = {'p.'+k: v for k, v in kwargs.items()} return _general_get(kwargs, Problem, Problem.__sql_prefix) @staticmethod def list(): - return _general_list(Problem, Problem.__sql_prefix, order='language_id, problem_group_id, id') + return _general_list(Problem, Problem.__sql_prefix, order=Problem.__sql_order) @staticmethod def filter(**kwargs): - return _general_filter(kwargs, Problem, Problem.__sql_prefix) - - @staticmethod - def get_identifier(problem_id): - return _run_sql('select l.identifier, g.identifier, p.identifier from problem p inner join problem_group g on g.id = p.problem_group_id inner join language l on l.id = p.language_id where p.id = %s', (problem_id,), fetch_one=True) + kwargs = {'p.'+k: v for k, v in kwargs.items()} + return _general_filter(kwargs, Problem, Problem.__sql_prefix, order=Problem.__sql_order) # known as Attempt in the original code class Solution(collections.namedtuple('Solution', ['id', 'done', 'content', 'problem_id', 'codeq_user_id', 'trace'])): diff --git a/monkey/edits.py b/monkey/edits.py index 8412f8a..5d07bfd 100644 --- a/monkey/edits.py +++ b/monkey/edits.py @@ -257,10 +257,10 @@ if __name__ == '__main__': solutions = Solution.filter(problem_id=pid, done=True) traces = [s.trace for s in solutions if s.codeq_user_id not in ignored_users] if not traces: - print('No traces for {}'.format(problem.name)) + print('No traces for {}'.format(problem.identifier)) continue - print('Analyzing traces for {}… '.format(problem.name), end='', flush=True) + print('Analyzing traces for {}… '.format(problem.identifier), end='', flush=True) try: edits[pid], submissions[pid], queries[pid], names[pid] = get_edits_from_traces(traces) print('{} edits, {} submissions, {} queries, {} names'.format( diff --git a/monkey/test.py b/monkey/test.py index 9936cdc..03f3571 100755 --- a/monkey/test.py +++ b/monkey/test.py @@ -18,30 +18,29 @@ from .util import indent if len(sys.argv) < 2: print('usage: ' + sys.argv[0] + ' <pid>') sys.exit(1) -pid = int(sys.argv[1]) -language, problem_group, problem = Problem.get_identifier(pid) +problem = Problem.get(id=sys.argv[1]) -solved_problems = [p for p in CodeqUser.solved_problems(1, language) - if p != (problem_group, problem)] -other_solutions = server.problems.solutions_for_problems(language, solved_problems) -problem_module = server.problems.load_problem(language, problem_group, problem, 'common') -name = server.problems.load_problem(language, problem_group, problem, 'en').name +solved_problems = [p for p in CodeqUser.solved_problems(1, problem.language) + if p != (problem.group, problem.identifier)] +other_solutions = server.problems.solutions_for_problems(problem.language, solved_problems) +problem_module = server.problems.load_problem(problem.language, problem.group, problem.identifier, 'common') +name = server.problems.load_problem(problem.language, problem.group, problem.identifier, 'en').name # Testing function. def test(code): correct, hints = problem_module.test(code, solved_problems) return correct -traces = [s.trace for s in Solution.filter(problem_id=pid)] +traces = [s.trace for s in Solution.filter(problem_id=problem.id)] # Load hint database stored in edits.pickle. edits, submissions, queries, names = pickle.load(open('edits.pickle', 'rb')) -edits, submissions, queries, names = edits[pid], submissions[pid], queries[pid], names[pid] +edits, submissions, queries, names = edits[problem.id], submissions[problem.id], queries[problem.id], names[problem.id] # Load current status (programs for which a hint was found). try: - done = pickle.load(open('status-'+str(problem.pk)+'.pickle', 'rb')) + done = pickle.load(open('status-'+str(problem.id)+'.pickle', 'rb')) except: done = [] @@ -106,7 +105,7 @@ elif sys.argv[2] == 'test': print_hint(program, solution, steps, fix_time, n_tested) print() - pickle.dump(done, open('status-'+str(problem.pk)+'.pickle', 'wb')) + pickle.dump(done, open('status-'+str(problem.id)+'.pickle', 'wb')) print('Found hints for ' + str(len(done)) + ' of ' + str(len(incorrect)) + ' incorrect programs') @@ -115,7 +114,7 @@ elif sys.argv[2] == 'info': # With no additional arguments, print some stats. if len(sys.argv) == 3: print('Problem {} ({}): {} edits and {} unique submissions in {} traces'.format( - pid, colored(name, 'yellow'), + problem.id, colored(name, 'yellow'), colored(str(len(edits)), 'yellow'), colored(str(len(submissions)), 'yellow'), colored(str(len(traces)), 'yellow'))) @@ -146,7 +145,7 @@ elif sys.argv[2] == 'info': # Print the edit graph in graphviz dot syntax. elif sys.argv[2] == 'graph' and len(sys.argv) == 4: uid = int(sys.argv[3]) - solution = Solution.get(problem_id=pid, codeq_user_id=uid) + solution = Solution.get(problem_id=problem.id, codeq_user_id=uid) nodes, submissions, queries = trace_graph(solution.trace) diff --git a/server/prolog_session.py b/server/prolog_session.py index 0faad5c..dfb7c28 100644 --- a/server/prolog_session.py +++ b/server/prolog_session.py @@ -93,12 +93,12 @@ class PrologSession(server.LanguageSession): def hint(self, sid, problem_id, program): session = server.user_session.get_session_by_id(sid) - language, problem_group, problem = Problem.get_identifier(problem_id) - language_module = problems.load_language(language, 'common') - problem_module = problems.load_problem(language, problem_group, problem, 'common') + p = Problem.get(id=problem_id) + language_module = problems.load_language(p.language, 'common') + problem_module = problems.load_problem(p.language, p.group, p.identifier, 'common') - solved_problems = [p for p in CodeqUser.solved_problems(session.get_uid(), language) - if p != (problem_group, problem)] + solved_problems = [pp for pp in CodeqUser.solved_problems(session.get_uid(), p.language) + if pp != (p.group, p.identifier)] hints = [] # check if the program is already correct @@ -118,12 +118,12 @@ class PrologSession(server.LanguageSession): def test(self, sid, problem_id, program): session = server.user_session.get_session_by_id(sid) - language, problem_group, problem = Problem.get_identifier(problem_id) - language_module = problems.load_language(language, 'common') - problem_module = problems.load_problem(language, problem_group, problem, 'common') + p = Problem.get(id=problem_id) + language_module = problems.load_language(p.language, 'common') + problem_module = problems.load_problem(p.language, p.group, p.identifier, 'common') - solved_problems = [p for p in CodeqUser.solved_problems(session.get_uid(), language) - if p != (problem_group, problem)] + solved_problems = [pp for pp in CodeqUser.solved_problems(session.get_uid(), p.language) + if pp != (p.group, p.identifier)] try: passed, hints = problem_module.test(program, solved_problems) except AttributeError as ex: @@ -147,13 +147,13 @@ class PrologSession(server.LanguageSession): """A "shorthand" method to start a Prolog session, load correct solutions of all user's solved problems and the given program, and ask a query. """ - language, problem_group, problem = Problem.get_identifier(problem_id) - problem_module = problems.load_problem(language, problem_group, problem, 'common') + p = Problem.get(id=problem_id) + problem_module = problems.load_problem(p.language, p.group, p.identifier, 'common') - solved_problems = [p for p in CodeqUser.solved_problems(user_id, language) - if p != (problem_group, problem)] - other_solutions = problems.solutions_for_problems(language, solved_problems) - problem_facts = problems.get_facts(language, problem_module) or '' + solved_problems = [pp for pp in CodeqUser.solved_problems(user_id, p.language) + if pp != (p.group, p.identifier)] + other_solutions = problems.solutions_for_problems(p.language, solved_problems) + problem_facts = problems.get_facts(p.language, problem_module) or '' code = program + '\n' + other_solutions + '\n' + problem_facts messages, status, have_more = self.run(code) @@ -163,8 +163,4 @@ class PrologSession(server.LanguageSession): self._problem_id = problem_id return messages, status, have_more - def get_problem_id(self): - return self._problem_id - server.language_session_handlers['prolog'] = lambda user_session, problem_id, language_identifier, group_identifier, problem_identifier: PrologSession() - diff --git a/server/python_session.py b/server/python_session.py index 39bd8f4..f7ec53f 100644 --- a/server/python_session.py +++ b/server/python_session.py @@ -83,9 +83,9 @@ class PythonSession(server.LanguageSession): self.destroy() def hint(self, sid, problem_id, program): - language, problem_group, problem = Problem.get_identifier(problem_id) - language_module = server.problems.load_language(language, 'common') - problem_module = server.problems.load_problem(language, problem_group, problem, 'common') + 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 @@ -104,9 +104,9 @@ class PythonSession(server.LanguageSession): return hints def test(self, sid, problem_id, program): - language, problem_group, problem = Problem.get_identifier(problem_id) - language_module = server.problems.load_language(language, 'common') - problem_module = server.problems.load_problem(language, problem_group, problem, 'common') + 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) diff --git a/server/robot_session.py b/server/robot_session.py index b41e233..26da36e 100644 --- a/server/robot_session.py +++ b/server/robot_session.py @@ -25,9 +25,9 @@ class RobotSession(server.LanguageSession): pass def hint(self, sid, problem_id, program): - language, problem_group, problem = Problem.get_identifier(problem_id) - language_module = server.problems.load_language(language, 'common') - problem_module = server.problems.load_problem(language, problem_group, problem, 'common') + 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 = [] if hasattr(language_module, 'hint'): @@ -41,9 +41,9 @@ class RobotSession(server.LanguageSession): return hints def test(self, sid, problem_id, program): - language, problem_group, problem = Problem.get_identifier(problem_id) - language_module = server.problems.load_language(language, 'common') - problem_module = server.problems.load_problem(language, problem_group, problem, 'common') + 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(program) |