From abbaf8c97c708e0e02476a6d6a85dfa18d631169 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Thu, 15 Jan 2015 13:36:15 +0100 Subject: Rewrite monkey.action test code to use Django db --- monkey/action.py | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/monkey/action.py b/monkey/action.py index 057ed0e..403c015 100755 --- a/monkey/action.py +++ b/monkey/action.py @@ -226,48 +226,33 @@ def compress(actions): # some sample code if __name__ == '__main__': - import sys - if len(sys.argv) < 2: - print('usage: ' + sys.argv[0] + ' ') - sys.exit(1) + import os + import django + os.environ['DJANGO_SETTINGS_MODULE'] = 'webmonkey.settings' + django.setup() - import sqlite3 - conn = sqlite3.connect(sys.argv[1]) - conn.text_factory = bytes - c = conn.cursor() + from tutor.models import Problem, Attempt # print all problem ids print('problems:') - c.execute('select * from problems') - for problem in c.fetchall(): - # problem = (id, name, description, details, solution, library) - # name: predicate name + arity (e.g. conc/2) - # desc: one-line problem description - # details: detailed problem description - # solution: official solution - # library: fact database for testing (e.g. for parent, brother, … relations) - print(' ' + str(problem[0]) + '\t' + str(problem[1], encoding='utf-8')) + for problem in Problem.objects.all(): + print(' {}\t{}'.format(problem.pk, problem.name)) print() pid = input('enter problem id: ') - c.execute('select id from attempts where problem=?', (pid,)) - attempts = list(c.fetchall()) + problem = Problem.objects.get(pk=pid) # print all attempt ids for the selected problem print('attempts for problem ' + str(pid) + ':') - print(', '.join([str(attempt[0]) for attempt in attempts])) + attempts = Attempt.objects.filter(problem=problem) + print(', '.join([str(attempt.pk) for attempt in attempts])) print() aid = input('enter attempt id: ') - c.execute('select * from attempts where id=?', (aid,)) - attempt = c.fetchone() - # attempt = (id, problem_id, user_id, log, content, done, session) - # log: action sequence log - # content: final version for this attempt - # done: did any version of the program pass all tests? - # session: irrelevant + attempt = Attempt.objects.get(pk=aid) + try: - actions = parse(attempt[3]) + actions = parse(attempt.trace) print('read ' + str(len(actions)) + ' actions from log') compress(actions) print('after compression: ' + str(len(actions)) + ' actions') -- cgit v1.2.1