Ejemplo n.º 1
0
def get_runs_for(team, year):
    db = DatabaseConnection(sandbox_mode)
    runs_for = int(
        db.read('select r from team_years where teamid = "' + team +
                '" and year=' + str(year) + ';')[0][0])
    db.close()
    return runs_for
def pitcher_spray_chart_constructor(year):
    print("creating pitcher spray charts")
    start_time = time.time()
    global bad_gateway_data
    bad_gateway_data = []
    logger.log("Downloading " + str(year) + " pitcher spray charts || Timestamp: " + datetime.datetime.today()\
               .strftime('%Y-%m-%d %H:%M:%S'))
    if year >= 1988:
        driver_logger.log("\tCreating pitcher spray charts")
        db = DatabaseConnection(sandbox_mode)
        pt_uid_players = set(
            db.read(
                'select PT_uniqueidentifier from player_pitching where year = '
                + str(year) + ' and pa_infield is NULL;'))
        db.close()
        with ThreadPoolExecutor(os.cpu_count()) as executor:
            for ent in pt_uid_players:
                executor.submit(reduce_functionality, year, ent)
        driver_logger.log("\t\tTime = " +
                          time_converter(time.time() - start_time))
    else:
        driver_logger.log("\tNo pitcher spray chart data before 1988")
        logger.log("\tNo spray pitcher chart data before 1988")
        return
    if len(bad_gateway_data) > 0:
        revisit_bad_gateways(year, bad_gateway_data)
    logger.log("Done downloading pitcher spray charts: time = " +
               time_converter(time.time() - start_time) + '\n\n')
Ejemplo n.º 3
0
def get_games(team, year):
    db = DatabaseConnection(sandbox_mode)
    games = int(
        db.read('select g from team_years where teamid = "' + team +
                '" and year = ' + str(year) + ';')[0][0])
    db.close()
    return games
Ejemplo n.º 4
0
 def retrieve_team_info(self):
     db = DatabaseConnection(sandbox_mode)
     team_info = db.read(
         'select team_info from team_years where teamId = "' +
         self.team_id + '" and year = ' + str(self.year) + ';')[0][0]
     db.close()
     return literal_eval(team_info)
Ejemplo n.º 5
0
def manager_tendencies(year):
    driver_logger.log("\tStoring manager tendencies")
    print("storing manager tendencies")
    start_time = time.time()
    logger.log("Downloading " + str(year) +
               " manager tendencies || Timestamp: " +
               datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'))
    logger.log('\tMaking HTTP requests')
    db = DatabaseConnection(sandbox_mode)
    managers = db.read(
        'select manager_teams.managerid, manager_teams.teamid from manager_teams, manager_year where '
        'manager_year.year = ' + str(year) +
        ' and manager_year.mt_uniqueidentifier = manager_teams.'
        'mt_uniqueidentifier;')
    db.close()
    with ThreadPoolExecutor(os.cpu_count()) as executor:
        for manager in managers:
            executor.submit(load_url, manager[0], manager[1])
    logger.log('\t\tTime = ' + time_converter(time.time() - start_time))
    process_manager_tendencies(year)
    write_time = time.time()
    logger.log('\tWriting data to database')
    global stats
    with ThreadPoolExecutor(os.cpu_count()) as executor2:
        for manager_team, tendencies in stats.items():
            if len(tendencies) > 0:
                executor2.submit(write_to_file, year, manager_team, tendencies)
    logger.log('\t\tTime = ' + time_converter(time.time() - write_time))
    total_time = time_converter(time.time() - start_time)
    driver_logger.log("\t\tTime = " + total_time)
    logger.log("Done storing manager tendencies: time = " + total_time +
               '\n\n')
Ejemplo n.º 6
0
def populate_teams_table(year):
    driver_logger.log('\tPopulating teams table')
    print("Populating teams table")
    start_time = time.time()
    logger.log('Begin populating teams table for ' + str(year) +
               ' || Timestamp: ' +
               datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'))
    with open(os.path.join("..", "background", "yearTeams.txt"), 'rt') as file:
        db = DatabaseConnection(sandbox_mode)
        db.write('ALTER TABLE teams DROP INDEX teamId;')
        for line in file:
            if str(year) in line:
                temp_line = line.split(',')[1:-1]
                for team in temp_line:
                    team_id = team.split(';')[0]
                    db.write('insert into teams (teamId, teamName) values ("' +
                             team_id + '", "' +
                             translate_team_name(team_id).replace("'", "\'") +
                             '");')
                break
    db.write('ALTER TABLE teams ADD INDEX(teamId);')
    db.close()
    total_time = time.time() - start_time
    logger.log('Populating teams table completed: ' +
               time_converter(total_time))
    driver_logger.log('\t\tTime = ' + time_converter(total_time))
Ejemplo n.º 7
0
 def retrieve_secondary_positions(self):
     db = DatabaseConnection(sandbox_mode)
     position = db.read(
         'select secondaryPositions from players where playerId = "' +
         self.player_id + '";')[0][0]
     db.close()
     return position
Ejemplo n.º 8
0
def player_is_on_this_team(ty_uid, p_uid, player_type, year):
    player_on_team = False
    db = DatabaseConnection(sandbox_mode=True)
    this_players_uid_corresponding_team_id = \
        db.read('select teamId from player_teams where pt_uniqueidentifier = (select pt_uniqueidentifier from player_'
                + player_type + ' where p' + player_type[0] + '_uniqueidentifier = ' + str(p_uid[0]) + ' and year = '
                + str(year) + ');')[0][0]
    if this_players_uid_corresponding_team_id == db.read(
            'select teamId from team_years where ty_uniqueidentifier = ' +
            str(ty_uid) + ';')[0][0]:
        player_on_team = True
    elif this_players_uid_corresponding_team_id == 'TOT':
        for pt_uid in db.read(
                'select pt_uniqueidentifier from player_teams where playerId = (select playerId from '
                'player_teams where pt_uniqueidentifier = (select pt_uniqueidentifier from player_'
                + player_type + ' where year = ' + str(year) + ' and p' +
                player_type[0] + '_uniqueidentifier = ' + str(p_uid[0]) +
                '));'):
            if db.read('select count(*) from player_' + player_type + ' where year = ' + str(year) + ' and '
                       'pt_uniqueidentifier = ' + str(pt_uid[0]) + ';')[0][0] > 0 and \
                    (db.read('select teamId from team_years where ty_uniqueidentifier = ' + str(ty_uid) + ';')[0][0] ==
                     db.read('select teamId from player_teams where pt_uniqueidentifier = ' + str(pt_uid[0])
                             + ';')[0][0]):
                player_on_team = True
    db.close()
    return player_on_team
Ejemplo n.º 9
0
def write_to_file(year):
    for team_id, table in pages.items():
        logger.log("\t\tgathering and writing " + team_id + " positions")
        db = DatabaseConnection(sandbox_mode)
        primary_keys = []
        try:
            for row in table:
                if 'data-append-csv="' in row:
                    this_string = ""
                    primary_keys.append(
                        row.split('data-append-csv="')[1].split('"')
                        [0].replace("'", "\'"))
                    this_string += '"' + primary_keys[-1] + '","'
                    position_summary = row.split(
                        'data-stat="pos_summary" >')[1].split('<')[0]
                    if '-' in position_summary:
                        positions = position_summary.split('-')
                        for position_index in range(len(positions)):
                            if this_is_position_player_pitching(primary_keys[-1], positions, position_index, team_id,
                                                                year) or \
                                    this_is_pitcher_playing_in_the_field(primary_keys[-1], positions, position_index,
                                                                         team_id, year):
                                continue  # don't give positions players RP eligibility who threw mop-up innings
                            else:
                                if position_index != len(positions) - 1:
                                    this_string += positions[
                                        position_index] + ","
                                else:
                                    this_string += positions[position_index]
                    else:
                        this_string += position_summary
                    this_string += '"'
                    ty_uid = str(
                        db.read(
                            'select TY_uniqueidentifier from team_years where teamId = "'
                            + team_id + '" and year = ' + str(year) +
                            ';')[0][0])
                    if len(
                            db.read(
                                'select PPos_uniqueidentifier from player_positions where playerId='
                                + this_string.split(',')[0] +
                                ' and TY_uniqueidentifier = ' + ty_uid +
                                ';')) == 0:
                        db.write(
                            'insert into player_positions (PPos_uniqueidentifier, playerId, positions, '
                            'TY_uniqueidentifier) values (default, ' +
                            this_string + ', ' + ty_uid + ');')
                    else:
                        split_positions = this_string.split(',')[1:]
                        if split_positions[-1] == '"':
                            del split_positions[-1]
                            split_positions[-1] += '"'
                        db.write('update player_positions set positions = ' +
                                 ','.join(split_positions) + ' where '
                                 'playerId = ' + this_string.split(',')[0] +
                                 ' and TY_uniqueidentifier = ' + ty_uid + ';')
        except IndexError:
            pass
        db.close()
Ejemplo n.º 10
0
def get_runs_against(team, year):
    global runs_against
    db = DatabaseConnection(sandbox_mode)
    runs_against = int(
        db.read('select ra from team_years where teamid = "' + team +
                '" and year = ' + str(year) + ';')[0][0])
    db.close()
    return runs_against
Ejemplo n.º 11
0
 def retrieve_full_name(self):
     db = DatabaseConnection(sandbox_mode)
     name = db.read(
         'select firstName, lastName from players where playerId = "' +
         self.player_id + '";')[0]
     self.first_name = name[0]
     self.last_name = name[1]
     db.close()
Ejemplo n.º 12
0
def get_team_id(uid, player_type):
    db = DatabaseConnection(sandbox_mode=True)
    team_id = db.read(
        'select teamId from player_teams where pt_uniqueidentifier = (select pt_uniqueidentifier from '
        'player_' + player_type + ' where p' + player_type[0] +
        '_uniqueidentifier = ' + str(uid) + ')')[0][0]
    db.close()
    return team_id
Ejemplo n.º 13
0
def consolidate_player_positions(ty_uid):
    db = DatabaseConnection(sandbox_mode=True)
    players_positions = db.read(
        'select playerId, positions from player_positions where ty_uniqueidentifier = '
        + str(ty_uid) + ';')
    db.close()
    roster = {}
    for player in players_positions:
        roster[player[0]] = player[1].split(',')
    return stringify_player_positions(roster)
Ejemplo n.º 14
0
def get_most_recent_year():
    db = DatabaseConnection(sandbox_mode=False)
    try:
        most_recent_year = int(
            db.read("select year from years order by year desc limit 1;")[0]
            [0])
    except:
        most_recent_year = 1876
    finally:
        db.close()
    return most_recent_year
Ejemplo n.º 15
0
def player_was_on_more_than_one_team(p_uid, player_type, year):
    db = DatabaseConnection(sandbox_mode=True)
    this_players_uid_corresponding_team_id = \
        db.read('select teamId from player_teams where pt_uniqueidentifier = (select pt_uniqueidentifier from player_'
                + player_type + ' where p' + player_type[0] + '_uniqueidentifier = ' + str(p_uid[0]) + ' and year = '
                + str(year) + ');')[0][0]
    db.close()
    if this_players_uid_corresponding_team_id == 'TOT':
        return True
    else:
        return False
Ejemplo n.º 16
0
def data_continuity(most_recent_year):
    db = DatabaseConnection(sandbox_mode=True)
    for year in range(most_recent_year, 1875, -1):
        if len(
                db.read('select year from years where year = ' + str(year) +
                        ';')) == 0:
            continuous = False
            break
        else:
            continuous = True
    db.close()
    return continuous
Ejemplo n.º 17
0
def hof_finder():
    print("adding HOF data")
    driver_logger.log("\tAdding HOF data")
    start_time = time.time()
    logger.log("Begin finding hall of famers || Timestamp: " +
               datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'))
    hof_table = str(BeautifulSoup(urlopen('https://www.baseball-reference.com/awards/hof.shtml'), 'html.parser')).\
        split('<tbody>')[1].split('</tbody>')[0]
    rows = hof_table.split('<tr>')[1:]
    db = DatabaseConnection(sandbox_mode)
    for row in rows:
        person = row.split('data-append-csv="')[1].split('"')[0]
        year = row.split('<a href="/awards/hof_')[1].split('.shtml')[0]
        induction_type = row.split('data-stat="category_hof">')[1].split(
            '<')[0]
        if induction_type == 'Player':
            db.write('update players set HOF = ' + str(year) +
                     ' where playerId = "' + person + '";')
        elif induction_type == 'Manager':
            db.write('update managers set HOF = ' + str(year) +
                     ' where managerId = "' + person + '";')
        else:
            continue
    db.close()
    total_time = time_converter(time.time() - start_time)
    logger.log("Done finding hall of famers: time = " + total_time + '\n\n')
    driver_logger.log("\t\tTime = " + total_time)
Ejemplo n.º 18
0
def get_uid_of_player_for_this_team(ty_uid, p_uid, player_type, year):
    db = DatabaseConnection(sandbox_mode=True)
    uid = db.read(
        'select p' + player_type[0] + '_uniqueidentifier from player_' +
        player_type + ' where year = ' + str(year) +
        ' and pt_uniqueidentifier = (select pt_uniqueidentifier from player_teams where '
        'playerId = (select playerId from player_teams where pt_uniqueidentifier = (select '
        'pt_uniqueidentifier from player_' + player_type + ' where p' +
        player_type[0] + '_uniqueidentifier '
        '= ' + str(p_uid[0]) +
        ')) and teamId = (select teamId from team_years where ty_uniqueidentifier = '
        + str(ty_uid) + '));')[0][0]
    db.close()
    return uid
Ejemplo n.º 19
0
def get_pitch_fx_data(year, month=None, day=None):
    if year < 2008:
        driver_logger.log("\tNo pitch fx data to download before 2008")
        return
    start_time = time.time()
    if month is None and day is None:
        driver_logger.log("\tFetching " + str(year) + " pitch fx data")
        print("Fetching " + str(year) + " pitch fx data")
        logger.log("Downloading pitch fx data for " + str(year) +
                   " || Timestamp: " +
                   datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'))
        db = DatabaseConnection(sandbox_mode)
        opening_day = db.read('select opening_day from years where year = ' +
                              str(year) + ';')[0][0]
        db.close()
        for month in range(3, 12, 1):
            # if month > 11:
            if month >= int(opening_day.split('-')[0]):
                for day in range(1, 32, 1):
                    # if day > 14:
                    if month == int(
                            opening_day.split('-')[0]) and int(day) < int(
                                opening_day.split('-')[1]):
                        continue
                    if len(str(day)) == 1:
                        this_day = '0' + str(day)
                    else:
                        this_day = str(day)
                    if len(str(month)) == 1:
                        this_month = '0' + str(month)
                    else:
                        this_month = str(month)
                    get_day_data(this_day, this_month, str(year))
        logger.log("Done fetching " + str(year) + " pitch fx data: time = " +
                   time_converter(time.time() - start_time) + '\n\n\n\n')
        driver_logger.log("\t\tTime = " +
                          time_converter(time.time() - start_time))
        aggregate_pitch_fx(year)
    else:
        driver_logger.log("\tFetching " + str(month) + "-" + str(day) + "-" +
                          str(year) + " pitch fx data")
        print("Fetching " + str(month) + "-" + str(day) + "-" + str(year) +
              " pitch fx data")
        logger.log("Downloading pitch fx data for " + str(month) + "-" +
                   str(day) + "-" + str(year) + " || Timestamp: " +
                   datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'))
        get_day_data(str(day), str(month), str(year))
        driver_logger.log("\t\tTime = " +
                          time_converter(time.time() - start_time))
        aggregate_pitch_fx(year, month, day)
Ejemplo n.º 20
0
def determine_pitcher_roles_year(year):
    driver_logger.log("\tDetermining Pitcher Roles")
    print("Determining Pitcher Roles")
    start_time = time.time()
    logger.log("Determining Pitcher Roles || Timestamp: " + datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'))
    db = DatabaseConnection(sandbox_mode)
    with ThreadPoolExecutor(os.cpu_count()) as executor:
        for pt_uid in db.read('select pt_uniqueidentifier from player_pitching where year = ' + str(year) + ';'):
            player_id_team_id = db.read('select playerid, teamid from player_teams where pt_uniqueidentifier='
                                        + str(pt_uid[0]) + ';')[0]
            player_id = player_id_team_id[0]
            team_id = player_id_team_id[1]
            if team_id == 'TOT':
                continue
            ty_uid = str(db.read('select ty_uniqueidentifier from team_years where teamId = "' + team_id
                                 + '" and year = ' + str(year) + ';')[0][0])
            try:
                positions = db.read('select positions from player_positions where playerId = "' + player_id + '" and '
                                    'ty_uniqueidentifier = ' + ty_uid + ';')[0][0]
            except IndexError:
                continue
            update_positions = []
            if 'P' in positions:
                appearances_starts = db.read('select G, GS from player_pitching where pt_uniqueidentifier = '
                                             + str(pt_uid[0]) + ' and year = ' + str(year) + ';')[0]
                appearances = appearances_starts[0]
                starts = appearances_starts[1]
                start_percent = starts / appearances
                if start_percent > 0.75:
                    role = ['SP']
                elif start_percent > 0.50:
                    role = ['SP', 'RP']
                elif start_percent > 0.25:
                    role = ['RP', 'SP']
                else:
                    role = ['RP']
                for position in positions.split(','):
                    if position == 'P':
                        update_positions += role
                    else:
                        update_positions.append(position)
                executor.submit(db.write('update player_positions set positions = "' + ','.join(update_positions)
                                         + '" where ty_uniqueidentifier = ' + ty_uid + ' and playerId = "' + player_id
                                         + '";'))
    db.close()
    total_time = time_converter(time.time() - start_time)
    logger.log("Done: Time = " + total_time + '\n\n')
    driver_logger.log("\t\tTime = " + total_time)
Ejemplo n.º 21
0
def load_url(player_id):
    global temp_pages
    db = DatabaseConnection(sandbox_mode)
    if len(
            db.read('select * from players where playerid = "' + player_id +
                    '";')) == 0:
        try:
            temp_pages[player_id] = BeautifulSoup(
                urlopen("https://www.baseball-reference.com/players/" +
                        player_id[0] + "/" + player_id + ".shtml"),
                'html.parser')
        except:
            temp_pages[player_id] = None
    else:
        temp_pages[player_id] = None
    db.close()
Ejemplo n.º 22
0
def this_is_position_player_pitching(player_id, position_list, position_index,
                                     team_id, year):
    this_is_a_position_player_pitching = False
    if position_list[position_index] == 'P' and \
            any(position in position_list for position in ['C', '1B', '2B', '3B', 'SS', 'LF', 'CF', 'RF', 'DH']):
        db = DatabaseConnection(sandbox_mode=True)
        if float(
                db.read(
                    'select ip from player_pitching where year = ' +
                    str(year) + ' and pt_uniqueidentifier = '
                    '(select pt_uniqueidentifier from player_teams where playerId = "'
                    + player_id + '" and teamId'
                    ' = "' + team_id + '");')[0][0]) < 10.0:
            this_is_a_position_player_pitching = True
        db.close()
    return this_is_a_position_player_pitching
Ejemplo n.º 23
0
def write_to_file(final_data, greater_than, field):
    db = DatabaseConnection(sandbox_mode)
    counter = 0
    while len(final_data) > 0:
        target = None
        target_year = None
        for a, b in final_data.items():
            if target is not None:
                if greater_than:
                    if b[0][1] > target[1]:
                        target = b[0]
                        target_year = a
                    else:
                        continue
                else:
                    if b[0][1] < target[1]:
                        target = b[0]
                        target_year = a
                    else:
                        continue
            else:
                target = b[0]
                target_year = a
        if db.read('select league from team_years where teamId = "' + translate_team_id(target[0], target_year)
                   + '" and year = ' + str(target_year) + ';')[0][0].upper() in ['AL', 'NL']:
            counter += 1
            db.write('update team_years set ' + field + ' = ' + str(counter) + ' where teamId = "'
                     + translate_team_id(target[0], target_year) + '" and year = ' + str(target_year) + ';')
        del final_data[target_year][0]
        if len(final_data[target_year]) == 0:
            del final_data[target_year]
        else:
            continue
    db.close()
Ejemplo n.º 24
0
def write_to_file(year, all_stars):
    db = DatabaseConnection(sandbox_mode)
    for player in all_stars:
        pt_uids = db.read('select PT_uniqueidentifier from player_teams ' +
                          'where playerId = "' + player + '";')
        for pt_uid in pt_uids:
            db.write(
                'update player_batting set all_star = TRUE where PT_uniqueidentifier = '
                + str(pt_uid[0]) + ' and '
                'year = ' + str(year) + ';')
            db.write(
                'update player_pitching set all_star = TRUE where PT_uniqueidentifier = '
                + str(pt_uid[0]) + ' and'
                ' year = ' + str(year) + ';')
    db.close()
Ejemplo n.º 25
0
def write_to_file(year, player_id, stat_list):
    db = DatabaseConnection(sandbox_mode)
    pa = []
    records = []
    pt_uids = list(
        db.read(
            'select PT_uniqueidentifier from player_teams where playerId = "' +
            player_id + '";'))
    for pt_uid in pt_uids:
        if len(
                db.read(
                    "select PP_uniqueidentifier from player_pitching where PT_uniqueidentifier = "
                    + str(pt_uid[0]) + " and year = " + str(year) + ";")) != 0:
            try:
                pa.append(
                    int(
                        db.read(
                            "select pa from player_pitching where pt_uniqueidentifier = "
                            + str(pt_uid[0]) + " and year = " + str(year) +
                            ";")[0][0]))
            except TypeError:
                continue
            records.append(pt_uid)
    if len(pa) != 0:
        for pt in records:
            query_string = ""
            for key, value in stat_list.items():
                if key != "PA_unknown":
                    query_string += value[0] + ' = ' + value[1] + ', '
                else:
                    try:
                        query_string += value[0] + ' = ' + str(
                            pa[0] / (pa[0] + int(value[1]))) + ', '
                    except ZeroDivisionError:
                        query_string += value[0] + ' = ' + '1.0' + ', '
            if len(
                    db.read(
                        'select pp_pa from player_pitching where PT_uniqueidentifier = '
                        + str(pt[0]) + ' and year = ' + str(year) + ';')):
                db.write('update player_pitching set ' + query_string[:-2] +
                         ' where PT_uniqueidentifier = ' + str(pt[0]) +
                         ' and year = ' + str(year) + ';')
    db.close()
Ejemplo n.º 26
0
def get_pitcher_stats(pitcher, year):
    stats = {
        'IP': 0,
        'H': 0,
        '2B': 0,
        '3B': 0,
        'HR': 0,
        'SO': 0,
        'BB': 0,
        'ER': 0,
        'SV': 0
    }
    db = DatabaseConnection(sandbox_mode)
    pa = int(
        db.read(
            'select player_pitching.pa from player_pitching, player_teams where player_pitching.'
            'PT_uniqueidentifier = player_teams.PT_uniqueidentifier and player_teams.playerId = "'
            + pitcher + '" and year = ' + str(year) + ';')[0][0])
    for key, value in stats.items():
        try:
            stats[key] = float(
                db.read(
                    'select player_pitching.' + key +
                    ' from player_pitching, player_teams where '
                    'player_pitching.PT_uniqueidentifier = player_teams.PT_uniqueidentifier and '
                    'player_teams.playerId = "' + pitcher + '" and year = ' +
                    str(year) + ';')[0][0])
        except TypeError:
            continue
    db.close()
    return pa, stats
Ejemplo n.º 27
0
def consolidate_data(year):
    driver_logger.log("\tConsolidating data")
    print("Consolidating data")
    start_time = time.time()
    logger.log("Consolidating team data || Timestamp: " +
               datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'))
    db = DatabaseConnection(sandbox_mode)
    for ty_uid in db.read(
            'select ty_uniqueidentifier from team_years where year = ' +
            str(year) + ';'):
        team_start_time = time.time()
        logger.log('\t' + db.read(
            'select teamId from team_years where ty_uniqueidentifier = ' +
            str(ty_uid[0]) + ';')[0][0])
        write_roster_info(
            ty_uid[0], {
                'hitter_spots':
                consolidate_hitter_spots(ty_uid[0]),
                'player_positions':
                consolidate_player_positions(ty_uid[0]),
                'batter_stats':
                consolidate_player_stats(ty_uid[0], 'batting', year),
                'pitcher_stats':
                consolidate_player_stats(ty_uid[0], 'pitching', year),
                'fielder_stats':
                consolidate_player_stats(ty_uid[0], 'fielding', year)
            })
        logger.log('\t\tTime = ' +
                   time_converter(time.time() - team_start_time))
    db.close()
    total_time = time_converter(time.time() - start_time)
    logger.log("Done consolidating team data: Time = " + total_time + '\n\n')
    driver_logger.log("\t\tTime = " + total_time)
Ejemplo n.º 28
0
def accept_post_request():
    # form = QuickSimForm()
    # if form.validate_on_submit():
    #     games = form.games.data
    #     return redirect(url_for('simulate.sim_results'))
    # else:
    post_id = int(request.form.get('post_id'))
    if post_id == 1:
        print('form not submitted')
        new_year = request.form.get('newest_year')
        league_structure = get_league_structure(new_year)
        return json.dumps({
            'new_year': league_structure,
            'league_len': len(league_structure),
            'division_len': len(league_structure['nl']),
            'year': new_year
        })
    else:
        away_info = request.form.get('away_team')
        home_info = request.form.get('home_team')
        games = int(request.form.get('games'))
        away_team = away_info.split('.jpg')[0][-7:-4]
        away_year = int(away_info.split('.jpg')[0][-4:])
        home_team = home_info.split('.jpg')[0][-7:-4]
        home_year = int(home_info.split('.jpg')[0][-4:])
        db = DatabaseConnection(sandbox_mode=True)
        away_year_info = literal_eval(
            db.read('select year_info from years where year = ' +
                    str(away_year) + ';')[0][0])
        home_year_info = literal_eval(
            db.read('select year_info from years where year = ' +
                    str(home_year) + ';')[0][0])
        db.close()
        return simulation(away_team, away_year, away_year_info, home_team,
                          home_year, home_year_info, games)
Ejemplo n.º 29
0
def write_to_file(year, comps, comp_type):
    db = DatabaseConnection(sandbox_mode)
    with ThreadPoolExecutor(os.cpu_count()) as executor:
        for player, comp in comps.items():
            if comp is not None:
                comp_pull = 1.0 - float(
                    db.read(
                        'select player_' + comp_type +
                        '.certainty from player_' + comp_type +
                        ', player_teams where player_' + comp_type +
                        '.pt_uniqueidentifier = '
                        'player_teams.pt_uniqueidentifier and player_teams.playerid = "'
                        + player + '" and player_' + comp_type + '.year = ' +
                        str(year) + ';')[0][0])
                comp_stat_id = int(
                    db.read(
                        'select player_' + comp_type + '.p' + comp_type[0] +
                        '_uniqueidentifier from'
                        ' player_' + comp_type +
                        ', player_teams where player_' + comp_type +
                        '.pt_uniqueidentifier=player_teams.pt_uniqueidentifier and player_teams.'
                        'playerid = "' + player + '" and player_' + comp_type +
                        '.year = ' + str(year) + ';')[0][0])
                if len(
                        db.read('select comp_id from comparisons_' +
                                comp_type + '_overall where playerid =' +
                                ' "' + player + '" and year = ' + str(year) +
                                ';')) == 0:
                    executor.submit(
                        transact, 'insert into comparisons_' + comp_type +
                        '_overall (comp_id, playerId, '
                        'year, comp, comp_year, comp_pull, comp_stat_id) values (default, "'
                        + player + '", ' + str(year) + ', "' +
                        comp.split(';')[0] + '", ' + comp.split(';')[1] +
                        ', ' + str(comp_pull) + ', ' + str(comp_stat_id) +
                        ');')
                else:
                    comp_id = int(
                        db.read('select comp_id from comparisons_' +
                                comp_type + '_overall where playerid = "' +
                                player + '" and year = ' + str(year) +
                                ';')[0][0])
                    executor.submit(
                        transact, 'update comparisons_' + comp_type +
                        '_overall set comp = "' + comp.split(';')[0] +
                        '", comp_year = ' + comp.split(';')[1] +
                        ', comp_pull = ' + str(comp_pull) +
                        ', comp_stat_id = ' + str(comp_stat_id) + ' where ' +
                        'comp_id = ' + str(comp_id) + ';')
    db.close()
Ejemplo n.º 30
0
def resolve_player_id(player_num, year, team, player_type):
    players_file = minidom.parse(os.path.join("..", "..", "baseball-sync", "src", "import_data", "player_data",
                                              "pitch_fx", "xml", "players.xml"))
    for ent in players_file.getElementsByTagName('player'):
        if ent.getAttribute('id') == str(player_num):
            last_name = ent.getAttribute('last')
            first_name = ent.getAttribute('first')
            break
    db = DatabaseConnection(sandbox_mode)
    pid = db.read('select playerid from players where lastName="' + last_name + '" and firstName="' + first_name + '";')
    if len(pid) == 0:
        name = name_alterator(first_name, last_name)
        try:
            pid = db.read('select playerid from players where lastName = "' + name.split(';')[1] + '" and firstName = "'
                          + name.split(';')[0] + '";')
        except AttributeError:
            pid = name
    db.close()
    if pid is not None:
        if len(pid) == 1:
            player_id = pid[0][0]
        else:
            player_id = resolve_further(pid, team, year, player_type)

    else:
        player_id = None
    return player_id