diff options
Diffstat (limited to 'monkey')
-rwxr-xr-x | monkey/action.py | 135 |
1 files changed, 78 insertions, 57 deletions
diff --git a/monkey/action.py b/monkey/action.py index f47cbb8..0a6a7cb 100755 --- a/monkey/action.py +++ b/monkey/action.py @@ -47,6 +47,16 @@ class Action: else: return text +_packet_action_map = { + 'ins': lambda packet, time, code: Action('insert', time, offset=packet['off'], text=packet['txt']), + 'rm': lambda packet, time, code: Action('remove', time, offset=packet['off'], text=code[packet['off']:packet['off']+packet['len']]), + 'slv': lambda packet, time, code: Action('solve', time, text=packet['qry']), + 'slva': lambda packet, time, code: Action('solve_all', time, text=packet['qry']), + 'nxt': lambda packet, time, code: Action('next', time), + 'stp': lambda packet, time, code: Action('stop', time), + 'tst': lambda packet, time, code: Action('test', time, total=packet['tot'], passed=packet['pas']), + 'hnt': lambda packet, time, code: Action('hint', time) +} # parse log from database into a list of actions, cleaning up some fluff. # ignore non-text actions (queries and tests) def parse(data): @@ -58,50 +68,58 @@ def parse(data): time = 0 code = '' - i = 0 - while i < len(data): - # parse one action - type = data[i] - i += 1 - dt = int(((data[i] << 8) + (data[i+1])) * 100.0) - time += dt - i += 2 - if type == 1: # insert - offset = (data[i] << 8) + data[i+1] - i += 2 - length = (data[i] << 8) + data[i+1] - i += 2 - text = data[i:i+length].decode() - i += length - action = Action('insert', time, offset=offset, text=text) - elif type == 2: # remove - offset = (data[i] << 8) + data[i+1] - i += 2 - length = (data[i] << 8) + data[i+1] - i += 2 - text = code[offset:offset+length] - action = Action('remove', time, offset=offset, text=text) - elif type == 3 or type == 4: # solve / solve all - length = (data[i] << 8) + data[i+1] - i += 2 - query = data[i:i+length].decode() - i += length - act_type = 'solve' + ('_all' if type == 4 else '') - action = Action(act_type, time, text=query) - elif type == 5: # next solution - action = Action('next', time) - elif type == 7: # stop/end - action = Action('stop', time) - elif type == 8: # test - total = data[i] - i += 1 - passed = data[i] - i += 1 - action = Action('test', time, total=total, passed=passed) - elif type == 9: # hint - action = Action('hint', time) - else: - # unsupported action type +# i = 0 +# while i < len(data): +# # parse one action +# type = data[i] +# i += 1 +# dt = int(((data[i] << 8) + (data[i+1])) * 100.0) +# time += dt +# i += 2 +# if type == 1: # insert +# offset = (data[i] << 8) + data[i+1] +# i += 2 +# length = (data[i] << 8) + data[i+1] +# i += 2 +# text = data[i:i+length].decode() +# i += length +# action = Action('insert', time, offset=offset, text=text) +# elif type == 2: # remove +# offset = (data[i] << 8) + data[i+1] +# i += 2 +# length = (data[i] << 8) + data[i+1] +# i += 2 +# text = code[offset:offset+length] +# action = Action('remove', time, offset=offset, text=text) +# elif type == 3 or type == 4: # solve / solve all +# length = (data[i] << 8) + data[i+1] +# i += 2 +# query = data[i:i+length].decode() +# i += length +# act_type = 'solve' + ('_all' if type == 4 else '') +# action = Action(act_type, time, text=query) +# elif type == 5: # next solution +# action = Action('next', time) +# elif type == 7: # stop/end +# action = Action('stop', time) +# elif type == 8: # test +# total = data[i] +# i += 1 +# passed = data[i] +# i += 1 +# action = Action('test', time, total=total, passed=passed) +# elif type == 9: # hint +# action = Action('hint', time) +# else: +# # unsupported action type +# continue + + for packet in data: + try: + time += packet['dt'] + action = _packet_action_map[packet['typ']](packet, time, code) + except: + # ignore any errors while decoding a packet continue # skip normalization if this is the first action @@ -232,30 +250,33 @@ def compress(actions): # some sample code if __name__ == '__main__': - import os - import django - os.environ['DJANGO_SETTINGS_MODULE'] = 'webmonkey.settings' - django.setup() + import sys, os.path + sys.path.append(os.path.dirname(os.path.realpath(__file__)) + '/..') # the parent directory is the app directory +# import django +# os.environ['DJANGO_SETTINGS_MODULE'] = 'webmonkey.settings' +# django.setup() - from tutor.models import Problem, Attempt + import db.models + db.models.init() + from db.models import Solution # print all problem ids - print('problems:') - for problem in Problem.objects.all(): - print(' {}\t{}'.format(problem.pk, problem.name)) - print() +# print('problems:') +# for problem in Problem.objects.all(): +# print(' {}\t{}'.format(problem.pk, problem.name)) +# print() pid = input('enter problem id: ') - problem = Problem.objects.get(pk=pid) +# problem = Problem.objects.get(pk=pid) # print all attempt ids for the selected problem print('users solving problem ' + str(pid) + ':') - attempts = Attempt.objects.filter(problem=problem) - print(', '.join([str(attempt.user_id) for attempt in attempts])) + attempts = Solution.filter(problem_id=pid) + print(', '.join([str(attempt.codeq_user_id) for attempt in attempts])) print() uid = input('enter user id: ') - attempt = Attempt.objects.get(problem_id=pid, user_id=uid) + attempt = Solution.get(problem_id=pid, codeq_user_id=uid) try: actions = parse(attempt.trace) |