def aggregate_summary(usage_data): """ Aggregates list of usage data into {state: total_time} Args usage_data: Sorted List (by start_ts) of usage data tuples [(item_id, catalog_item_id, state, start_ts, end_ts), ...] Return: Dictionary mapping each state to the total time spent in that state for the given period of time """ # Initialize aggregate_summary mapping agg_summary = {} if len(usage_data) > 0: # Add Time for first element in the row _, _, cur_state, cur_start, cur_end = usage_data[0] agg_summary[cur_state] = time_delta(cur_end, cur_start) # Iterate through rest of the usage data rows for row in usage_data[1:]: # Get the next row info _, _, next_state, next_start, next_end = row # Case 1: Next state is the same as the current state if cur_state == next_state: agg_summary[next_state] += time_delta(next_end, cur_end) # Case 2: Next state is not the same as the current state elif next_state in agg_summary: # Mapping exists, add time agg_summary[next_state] += time_delta(next_end, next_start) # Case 3: Mapping does not exist else: agg_summary[next_state] = time_delta(next_end, next_start) # Update current row info _, _, cur_state, cur_start, cur_end = row return agg_summary
def appTravel(environ, start_response): data = util.get_json_post(environ) user = storage.get_user_by_sessid(data.sessid) if user is None: start_response('200 OK', HEADERS_JSON + HEADERS_CORS) return [util.err_json("You are not logged in.")] logger.bigbrother("/travel", user, environ) q = storage.sql( """ SELECT users.id AS id, leaderboard.achievements AS achievements, progress.visited_kingdoms AS kingdoms, progress.cur_kingdom AS cur_kingdom, progress.save_uid AS save_uid, progress.cur_visit_started AS cur_visit_started FROM users LEFT OUTER JOIN progress ON progress.user_id = users.id LEFT OUTER JOIN leaderboard ON leaderboard.user_id = users.id WHERE users.id = ? """, (user.id, ))[0] visit_delay = util.time_delta(q.cur_visit_started, config.RATELIMIT_VISIT_KINGDOM) if visit_delay and user.id != config.ADMIN_USER_ID: start_response('200 OK', HEADERS_JSON + HEADERS_CORS) return [ util.err_json( "You need to wait %i more seconds before visiting a new kingdom." % visit_delay) ] if q.cur_kingdom != 'none' or q.save_uid: start_response('200 OK', HEADERS_JSON + HEADERS_CORS) return [util.err_json("You are already visiting a kingdom.")] kingdoms = json.loads(q.kingdoms) achievements = json.loads(q.achievements) if not checks.is_kingdom_accessible(data.kingdom, kingdoms, achievements): start_response('200 OK', HEADERS_JSON + HEADERS_CORS) return [util.err_json("You can't access this kingdom yet.")] storage.sql( """ UPDATE progress SET cur_kingdom = ?, cur_visit_started = ?, save_uid = '' WHERE user_id = ? """, (data.kingdom, util.unix_time(), user.id)) compiler.queue_compilation(user.id, data.kingdom) start_response('200 OK', HEADERS_JSON + HEADERS_CORS) return [util.json_bytes({"success": True})]
from time import time import keras as k import util from dataset import data_training from model import model start_time = time() data = data_training() print('Training model...') model.fit(data[0], data[1], batch_size=512, epochs=50, callbacks=[k.callbacks.TensorBoard(write_images=True)]) model.save('data/model.h5') print('Done') print('Time:', util.time_delta(time() - start_time))