get_all_tracking_stats_for_date_range.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:nba_db 作者: dblackrun 项目源码 文件源码
def main():
    logging.basicConfig(filename='logs/tracking_stats.log',level=logging.ERROR, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
    config=json.loads(open('config.json').read())

    command_line_args = sys.argv

    dates = utils.validate_dates(command_line_args)
    start_date = dates[0]
    end_date = dates[1]

    username = config['username']
    password = config['password']
    host = config['host']
    database = config['database']

    engine = create_engine('mysql://'+username+':'+password+'@'+host+'/'+database)

    DBSession = scoped_session(sessionmaker(autoflush=True,autocommit=False,bind=engine))

    hustle_stats_queue = Queue()
    team_tracking_queue = Queue()
    player_tracking_queue = Queue()
    passes_made_queue = Queue()
    # Create worker threads
    for x in range(8):
        hustle_stats_worker = sportvu.HustleStatsWorker(hustle_stats_queue, DBSession)
        team_tracking_worker = sportvu.TeamStatWorker(team_tracking_queue, DBSession)
        player_tracking_worker = sportvu.PlayerStatWorker(player_tracking_queue, DBSession)
        passes_made_worker = sportvu.PlayerPassesStatWorker(passes_made_queue, DBSession)
        # Setting daemon to True will let the main thread exit even though the workers are blocking
        hustle_stats_worker.daemon = True
        team_tracking_worker.daemon = True
        player_tracking_worker.daemon = True
        passes_made_worker.daemon = True
        hustle_stats_worker.start()
        team_tracking_worker.start()
        player_tracking_worker.start()
        passes_made_worker.start()
    # Put the tasks into the queue as a tuple
    for dt in rrule(DAILY, dtstart=start_date, until=end_date):
        date = dt.strftime("%m/%d/%Y")
        game_team_map, player_team_game_map, daily_game_ids = sportvu.get_player_game_team_maps_and_daily_game_ids(date)
        if len(daily_game_ids) > 0:
            season = utils.get_season_from_game_id(daily_game_ids[0])
            season_type = utils.get_season_type_from_game_id(daily_game_ids[0])
            if season_type != None:
                # hustle stats begin in 2015-16 playoffs
                hustle_stats_queue.put((game_team_map, player_team_game_map, date, season, season_type))
                for stat_type in constants.SPORTVU_GAME_LOG_STAT_TYPE_TABLE_MAPS.keys():
                    team_tracking_queue.put((stat_type, date, season, season_type, game_team_map, constants.SPORTVU_GAME_LOG_STAT_TYPE_TABLE_MAPS[stat_type]['Team']))
                    player_tracking_queue.put((stat_type, date, season, season_type, player_team_game_map, constants.SPORTVU_GAME_LOG_STAT_TYPE_TABLE_MAPS[stat_type]['Player']))
                for player_id in player_team_game_map.keys():
                    passes_made_queue.put((player_id, date, season, season_type, player_team_game_map))
    # Causes the main thread to wait for the queue to finish processing all the tasks
    hustle_stats_queue.join()
    team_tracking_queue.join()
    player_tracking_queue.join()
    passes_made_queue.join()

    DBSession.remove()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号