def autocomplete(): # Assign a member to the function itself (waaaaaaaatt???) because that template is essentially # static, but might change for different versions of the app, so it's not worth hard-coding # anywhere, but in development, it's still nice to see changes reflected, so it bypasses the # cache. if app.config["DEBUG"] or not hasattr(autocomplete, "cache"): app_id_to_name = [{"data": game.app_id, "value": game.name} for game in Game.get_all()] autocomplete.cache = render_template("autocomplete.js", app_id_to_name=json.dumps(app_id_to_name)) return autocomplete.cache
def game_handler(): if request.method == "POST": img_id = str(request.data.get('img_id', '')) diff = int(request.data.get('diff', '')) if img_id and diff: obj = randomiser(diff) rel = [] for num in range(0, diff): num = num + 1 rel.append(obj[num - 1]) win_img = obj['url'] game = Game(img_id, rel, win_img) game.save() relation = {} if game.t1: relation['1'] = game.t1 if game.t2: relation['2'] = game.t2 if game.t3: relation['3'] = game.t3 if game.t4: relation['4'] = game.t4 if game.t5: relation['5'] = game.t5 if game.t6: relation['6'] = game.t6 if game.t7: relation['7'] = game.t7 if game.t8: relation['8'] = game.t8 if game.t9: relation['9'] = game.t9 if game.t10: relation['10'] = game.t10 if game.t11: relation['11'] = game.t11 if game.t12: relation['12'] = game.t12 if game.t13: relation['13'] = game.t13 if game.t14: relation['14'] = game.t14 if game.t15: relation['15'] = game.t15 if game.t16: relation['16'] = game.t16 score = 'null' response = jsonify({ 'id': game.id, 'img_id': game.img_id, 'date_created': game.date_created, 'date_completed': game.date_completed, 'score': 'null', 'relation': relation, 'win_img': game.win_img }) response.status_code = 201 return response else: # GET games = Game.get_all() results = [] for game in games: if game.score: score = game.score.strftime("%H:%M:%S") else: score = 'null' relation = {} if game.t1: relation['1'] = game.t1 if game.t2: relation['2'] = game.t2 if game.t3: relation['3'] = game.t3 if game.t4: relation['4'] = game.t4 if game.t5: relation['5'] = game.t5 if game.t6: relation['6'] = game.t6 if game.t7: relation['7'] = game.t7 if game.t8: relation['8'] = game.t8 if game.t9: relation['9'] = game.t9 if game.t10: relation['10'] = game.t10 if game.t11: relation['11'] = game.t11 if game.t12: relation['12'] = game.t12 if game.t13: relation['13'] = game.t13 if game.t14: relation['14'] = game.t14 if game.t15: relation['15'] = game.t15 if game.t16: relation['16'] = game.t16 obj = { 'id': game.id, 'img_id': game.img_id, 'date_created': game.date_created, 'date_completed': game.date_completed, 'score': score, 'relation': relation, 'win_img': game.win_img } results.append(obj) response = jsonify(results) response.status_code = 200 return response
from __future__ import print_function import json import numpy as np from app.models import Game from app.models.tag import compute_reverse_index from app.utils import data_file if __name__ == '__main__': games = list(Game.get_all()) reverse_index = compute_reverse_index(games) doc_tag_matrix = np.zeros((len(games), len(reverse_index) + 1), dtype=np.int) app_ids = sorted([game.app_id for game in games]) tag_ids = sorted(reverse_index.keys()) doc_tag_matrix[:, 0] = np.array(app_ids) for app_index in xrange(len(games)): for tag_index in xrange(len(reverse_index)): if app_ids[app_index] in reverse_index[tag_ids[tag_index]]: doc_tag_matrix[app_index, tag_index + 1] = 1 with open(data_file("doc_tag_matrix.npy"), "wb") as f: np.save(f, doc_tag_matrix)