def next_incomplete_season(self): """Fetches the next incomplete season for this circuit. Asks the user to create a new one if either the previous season is complete or does not exist. :return: the next incomplete season. """ if self.current_season is not None and not self.current_season.complete: return self.current_season while True: season_name = next_string('Enter a new season name', 'season%d' % (len(self.seasons) + 1)) if season_name is None: continue if self.seasons.find(season_name) is not None: print('A season by that name already exists') continue previous_season = self.current_season men_season_stats = self.create_season_stats(season_name, self.men) women_season_stats = self.create_season_stats( season_name, self.women) men_scoreboard = self.create_scoreboard(men_season_stats) women_scoreboard = self.create_scoreboard(women_season_stats) season = Season(self, previous_season, season_name, False, men_season_stats, women_season_stats, men_scoreboard, women_scoreboard) self.current_season = season self.seasons.insert(season_name.lower(), season) self.ordered_seasons.append(season) return self.current_season
def seasons(self): """ property that returns a list to all seasons of the show. caches it's value after first call. """ if not self.seasons_: element = self.server.query(self.key) self.seasons_ = [Season(e, self.server) for e in element if ('type' in e.attrib) and (e.attrib['type'] == 'season')] return self.seasons_
def getStaffeln(self): staffeln = [] for staffel in os.listdir(self.path): if not (staffel.startswith(".")): new_staffel = Season(self.path+"/"+staffel, staffel) staffeln.append(new_staffel) return sorted(staffeln, key=lambda x: x.getName())
def main(): teamslist = read_teams('Analytics_Attachment.xlsx') #files gameslist = read_games('Analytics_Attachment.xlsx') # se = Season(teamslist, gameslist) se.run() outlist = se.get_outlist() write_outlist(outlist) #write the list out
def add_season(self, location='', number=0): if number == 0: number = int(location[-2:]) season = Season(location=location, s_nr=number) season.episodes = {} self.seasons[number] = season return season
def sim_and_rank_seasons(lineup, num_rotations=2): ordered_seasons = [] # stores seasons in descending order by runs/game counter = 0 lineup_copy = lineup[:] while counter < num_rotations: # generate random rotation shuffle(lineup_copy) # test all 9 possible orders of given rotation for i in range(9): lineup_copy.append(lineup_copy.pop(0)) # place first batter at end s = Season(lineup=deepcopy(list(lineup_copy)), num_games=162) s.play_season() ordered_seasons.append(s) #print to terminal so people don't freak out waiting for simulations to end sys.stdout.write("\rSeasons Simulated: %d/%d" % (i + counter * 9 + 1, num_rotations * 9)) sys.stdout.flush() counter += 1 ordered_seasons.sort(key=lambda s: s.get_runs_per_game()) sys.stdout.write("\n") return ordered_seasons
def getSeason(): session = getSession() try: r = getRequest() #conn.request("GET", "/mlb/v2/JSON/News?%s" % params, "{body}", # headers) #response = conn.getresponse() if r != None: item = r.json() theID = item['Season'] query = session.query(Season).filter( Season.Season == theID).scalar() thisSeason = Season(**{ k: v for k, v in item.items() if k in Season.__table__.columns }) if query is None: session.add(thisSeason) else: query = session.merge( thisSeason ) #session.query(News).filter(News.NewsID == theID).update(newsItem) session.commit() print(thisSeason) #for key, value{j in item.items(): # print(str(key)+ ': '+ str(value)) except Exception as e: print("[Errno {0}] ".format(e))
def addSeason(self, seasonNum, weight, partOneEp=-1): assert isinstance(weight, int) if weight < 1: print( "WARNING: weights must be integers 1 or larger, setting to 1!") weight = 1 self.seasons[seasonNum] = Season(self.dir, seasonNum, weight, partOneEp) self._newSeason = True
def getMediaObject(self): media = False if self.type == "episode": media = Episode(self.tag, self.server) elif self.type == "movie": media = Movie(self.tag, self.server) elif self.type == "show": media = Show(self.tag, self.server) elif self.type == "season": media = Season(self.tag, self.server) return media
def rbi_sim_and_rank_seasons(lineup, trials_per_pos=2, name="David Oritz"): lineup_copy = lineup[:] this_player = None this_player_index = -1 ordered_seasons = [ [] for _ in range(9) ] # list of list of seasons. Index of list corresponds to player position # take player object of name out of lineup for i, p in enumerate(lineup_copy): if p.get_name() == name: if this_player is None: this_player = p this_player_index = i break else: # menas duplicate player names! print "Error: cannot use duplicate names in lineup!" sys.exit() if this_player is None and this_player_index == -1: print "Player name not in lineup!" sys.exit() lineup_copy.pop(this_player_index) # test player at 1-9 spots of lineup counter = 0 for lineup_pos in range(9): for i in range(trials_per_pos): shuffle(lineup_copy) # randomize lineup lineup_copy.insert( lineup_pos, this_player) # insert this player in appropriate spot s = Season(lineup=deepcopy(list(lineup_copy)), num_games=162) s.play_season() ordered_seasons[lineup_pos].append(s) for i, p in enumerate(lineup_copy): if p.get_name() == name: del lineup_copy[i] break #print to terminal so people don't freak out waiting for simulations to end sys.stdout.write("\rSeasons Simulated: %d/%d" % (counter + 1, trials_per_pos * 9)) sys.stdout.flush() counter += 1 sys.stdout.write("\n") return ordered_seasons
def main(): print(f"{stringy_now()}: Started scraping", flush=True) start = time.time() years = [year for year in range(constants.FIRST_YEAR, constants.PRESENT_YEAR + 1) if year not in constants.BROKEN_YEARS] for year in years: season = Season(year) maybe_make_directory(season) print("Getting schedule") schedule = get(f"https://statsapi.web.nhl.com/api/v1/schedule?season={season.season_id()}").json() response = get_pbp_data(2018020778) with open("response.json", "w") as outfile: json.dump(response, outfile) regular_game_ids = [] playoff_game_ids = [] print("Organizing dates") dates = schedule["dates"] for date in dates: games = date["games"] for game in games: if game["gameType"] == "R": regular_game_ids.append(game["gamePk"]) elif game["gameType"] == "P": playoff_game_ids.append(game["gamePk"]) for regular_game_id in regular_game_ids: regular_game_filename = f"penalty_data/{season.season_id()}/{regular_game_id}.txt" if os.path.isfile(regular_game_filename): print(f"Skipping: {regular_game_id}") else: print(f"Getting game: {regular_game_id}") pbp_data = get_pbp_data(regular_game_id) penalties = get_penalties(pbp_data) seralized_penalties = [str(penalty) for penalty in penalties] write_file(seralized_penalties, regular_game_filename) for playoff_game_id in playoff_game_ids: playoff_game_filename = f"penalty_data/{season.season_id()}/{playoff_game_id}.txt" if os.path.isfile(playoff_game_filename): print(f"Skipping: {playoff_game_id}") else: print(f"Getting game: {playoff_game_id}") pbp_data = get_pbp_data(playoff_game_id) penalties = get_penalties(pbp_data) seralized_penalties = [str(penalty) for penalty in penalties] write_file(seralized_penalties, playoff_game_filename) end = time.time() print(f"Total time: {end-start}", flush=True)
def load_circuit(): """Loads a circuit from resources file, then loads all its progress via the previous sessions outputs. :return: the newly loaded circuit. """ circuit = Circuit() load_tournament_types(circuit.tournament_types) load_ranking_points(circuit.ranking_points) load_circuit_players('men', circuit.men) load_circuit_players('women', circuit.women) circuit.men_scoreboard = load_circuit_player_scoreboard(circuit.men) circuit.women_scoreboard = load_circuit_player_scoreboard(circuit.women) circuit_progress_file = '%s/progress.csv' % OUTPUT if not os.path.isfile(circuit_progress_file): return circuit season = None with open(circuit_progress_file, 'r') as the_file: for line in the_file: csv = parse_csv_line(line) name = csv[0] complete = parse_bool(csv[1]) previous = season men_stats = load_season_player_stats(name, 'men', circuit.men) women_stats = load_season_player_stats(name, 'women', circuit.women) men_scoreboard = load_season_player_scoreboard(men_stats) women_scoreboard = load_season_player_scoreboard(women_stats) season = Season(circuit, previous, name, complete, men_stats, women_stats, men_scoreboard, women_scoreboard) season.tournaments = load_tournaments(season) circuit.seasons.insert(name, season) circuit.ordered_seasons.append(season) circuit.current_season = season return circuit
def main(start, end): for year_int in range(start, end + 1): year = str(year_int) seasons[year] = Season(year) path = "drivers_data/" + year + ".csv" print(path) with open(path, newline='', encoding='utf-8') as csvfile: reader = csv.DictReader(csvfile) raw_races = reader.fieldnames[3:-1] races = [] for race in raw_races: if len(race) != 0 and race != " ": seasons[year].add_race(race) races.append(race) for row in reader: driver = row["Driver"].lstrip(" ").rstrip(" ") if driver not in drivers: drivers[driver] = Driver(driver, year) seasons[year].add_driver(driver) for race in races: # print(year + ", " + driver + ", " + race + " = " + row[race]) seasons[year].add_result(race, driver, row[race]) for driver in seasons[year].drivers: seasons[year].calc_drivers_avg_finish(drivers[driver]) worst = {} for name, driver in drivers.items(): worst[name] = driver.calc_number_weighted_reverse_finish() sorted_worst = {} sorted_keys = sorted(worst, key=worst.get, reverse=True) count = 1 for name in sorted_keys: score = "{:.3f}".format(worst[name]) if (worst[name] > 0): print((str(count).ljust(2)) + ".: " + name.ljust(20) + " Score: " + score + " Races: " + str(drivers[name].get_number_races())) count += 1 print( str(count) + " Drivers Finished 1 Race. " + str(len(worst)) + " Drivers Overall")
def main(): #**** Choose year ****** for year in range(2021, 2022): #**** Choose Sport ****** league = "Football" league = "Boys Basketball" # league = "Girls Basketball" #**** Create Instance ***** season = Season(league, year) #Check if year path exists if not os.path.exists(season.get_year_path()): os.makedirs(season.get_year_path()) #**** Write HTML to File write_HTML(season) #**** Create Game Infos create_game_infos(season) print("Year: ", year)
# if len(west_copy)>1: # west_copy_1, east_copy_1=s2.league.standings(s2.league.teams,display=False); # # print(' ') # print('New copy') # for i in range(len(west)): # print("{0} \t {1:20} \t {2:3f}".format(i+1, west_copy_1[i][0], west_copy_1[i][1])); # print('East copy') # for i in range(len(east)): # print("{0} \t {1:20} \t {2:3f}".format(i+1, east_copy_1[i][0], east_copy_1[i][1])); ## print(elimed) return elimed s = Season("division.csv", "games.csv"); for game in s.games: s.play_game(game); check_for_elim(s, "East", game.date); check_for_elim(s, "West", game.date); with open('stephen_curry.csv','w') as f: f.write("Teams, Date Eliminated\n") for team in sorted(s.league.teams.keys()): if s.league.teams[team].eliminated: f.write(s.league.teams[team].name + ',' + s.league.teams[team].date_elim + '\n') else: f.write(s.league.teams[team].name + ', Playoffs\n')
home_standing, home_remain = sea.standing(home,date) away_standing, away_remain = sea.standing(away,date) return self.i2c[self.clf.predict([[ home_standing, self.c2i[sea.lastGame(home,date)], away_standing, self.c2i[sea.lastGame(away,date)], sea.points_on_date(home,date)-sea.points_on_date(away,date) ]])[0]] def strID(self): return "Decision Tree Predictor" csv_files = ['11-12.csv','12-13.csv','13-14.csv','14-15.csv','15-16.csv'] number_predictors = 6 number_games = 380 tot_acc = [0]*number_predictors acc_OT = [[0]*number_games]*number_predictors totalMarginOfVic = [0]*10 for csv in csv_files: sea = Season(csv) trainingData = [] for train_csv in list(set(csv_files) - set([csv])): trainingData.append(Season(csv)) predictors = [HighPointsPredictor(),HomeTeamPredictor(),AwayTeamPredictor(),LastGamePredictor(),BettingOddsPredictor(),DecisionTreePredictor(trainingData)] for date,home,away,res,bet in sea.matches: for pred in predictors: pred.test(sea,home,away,date,res) #hpp.test(sea,home,away,date,res) #htp.test(sea,home,away,date,res) #atp.test(sea,home,away,date,res) #lgp.test(sea,home,away,date,res) #bop.test(sea,home,away,date,res) #dct.test(sea,home,away,date,res)
# teamnamelist.append(teamlist.teamlist[ele].name) #print(sb.get_win_lose_remain_total('Cleveland Cavaliers', teamnamelist)) #print(sb.get_div_list('Boston Celtics')) #conf_list = sb.get_conf_list('Boston Celtics') #conf_list.sort() #print(conf_list) #li = sb.sort_by_lose(teamnamelist) #print(li) #print(sb.must_div_lead('Orlando Magic')) Se = Season(teamlist, gamelist) Se.run() ''' d1=datetime.datetime.strptime('3/20/2017','%m/%d/%Y') sb = ScoreBoard(teamlist, gamelist, d1) sb.win_all('Minnesota Timberwolves') sb.win_all('Golden State Warriors') sb.win_all('San Antonio Spurs') sb.win_all('LA Clippers') sb.win_all('Houston Rockets') sb.win_all('Memphis Grizzlies') sb.win_all('Oklahoma City Thunder') sb.win_all('Utah Jazz') teamnamelist = list()
def parse_seasons(driver, player_dict): edge_ef_driver = EventFiringWebDriver(driver, LiquipediaNavListener()) season_titles = [] link_texts = [] seasons = [] # quick hack for cleaning the list... bottom of page contains redundant links! link_elements = edge_ef_driver.find_elements_by_partial_link_text("RLCS Season") for link_element in link_elements: if '-' in link_element.wrapped_element.text: # Get season title season_title = link_element.wrapped_element.text season_titles.append(season_title) link_texts.append(link_element.get_attribute("href")) season_num = 0 for link in link_texts: is_final = "Finals" == season_titles[season_num].split('-')[1].strip() edge_ef_driver.get(link) time.sleep(20) # Get teams teamcards = edge_ef_driver.find_elements_by_class_name("teamcard") team_dict = parse_teams(teamcards, season_titles[season_num], player_dict) # Get matches matches = [] if not is_final: # Group stage tables = edge_ef_driver.find_elements_by_class_name("matchlist table table-bordered collapsible") for table in tables: table_entries = table.find_elements_by_class_name("match-row") event = table.find_elements_by_tag_name("th").wrapped_element.text for match in table_entries: team1 = team_dict.get( table_entries[0].find_element_by_tag_name("span").get_attribute("data-highlightingclass")) team2 = team_dict.get( table_entries[3].find_element_by_tag_name("span").get_attribute("data-highlightingclass")) team1_wins = table_entries[1].wrapped_element.text.lstrip() team2_wins = table_entries[2].wrapped_element.text.lstrip() if not team1_wins == "W" and not team1_wins == "FF": for num in range(int(team1_wins)): matches.append(Match(team1=team1, team2=team2, victor=team1, season_title=season_titles[season_num], event=event)) for num in range(int(team2_wins)): matches.append(Match(team1=team1, team2=team2, victor=team2, season_title=season_titles[season_num], event=event)) # Playoffs bracket_web_elements = edge_ef_driver.find_elements_by_class_name("bracket-column-matches") bracket_headers = [bracket.find_elements_by_class_name("bracket-header") for bracket in bracket_web_elements] if re.search(r"Season [789]", season_titles[season_num]): bracket_tuples = make_brackets(bracket_web_elements, bracket_headers, True) else: bracket_tuples = make_brackets(bracket_web_elements, bracket_headers, False) get_bracket_matches(season_titles[season_num], team_dict, bracket_tuples, matches) else: bracket_web_elements = edge_ef_driver.find_elements_by_class_name("bracket-column-matches") bracket_headers = [bracket.find_elements_by_class_name("bracket-header") for bracket in bracket_web_elements] bracket_tuples = make_brackets(bracket_web_elements, bracket_headers, False) get_bracket_matches(season_titles[season_num], team_dict, bracket_tuples, matches) season = Season(season_titles[season_num], player_dict, set(list(team_dict.values())), matches) seasons.append(season) edge_ef_driver.back() season_num += 1 time.sleep(5) return seasons
#this overrides the previous reverse values, but it doesn't matter because we're not interpreting those values yet. mutual_map('N', -1) mutual_map('W', 0) mutual_map('L', 2) scores = [] csv_files = ['11-12.csv', '12-13.csv', '13-14.csv', '14-15.csv', '15-16.csv'] for test_sea in csv_files: test_data = [] test_target = [] data = [] target = [] loocv = set(csv_files) - set([test_sea]) sea = Season(test_sea) for date, home, away, res, bet in sea.matches: home_standing, home_remain = sea.standing(home, date) away_standing, away_remain = sea.standing(away, date) #data.append( [ home_standing, home_remain, c2i[sea.lastGame(home,date)], away_standing, away_remain, c2i[sea.lastGame(away,date)], sea.points_on_date(home,date)-sea.points_on_date(away,date) ] ) test_data.append([ home_standing, c2i[sea.lastGame(home, date)], away_standing, c2i[sea.lastGame(away, date)], sea.points_on_date(home, date) - sea.points_on_date(away, date) ]) feature_names = [ 'Home standing', 'Home last game', 'Away standing', 'Away last game', 'Point difference' ] test_target.append(c2i[res]) for csv in loocv:
class SeasonEnum(Enum): SPRING = Season('Spring', 8, 3, 4) SUMMER = Season('Summer', 6, 0, 3) WINTER = Season('Winter', 11, 5, 5) FALL = Season('Fall', 9, 3, 4)
if tag not in headers: value_index = headers.index(tag) print(tag + ' found in csv file') except: print(tag + ' not found :(') column = {} for h in headers: column[h] = [] for row in reader: for h, v in zip(headers, row): column[h].append(v) return column if __name__ == '__main__': url = 'http://www.football-data.co.uk/mmz4281/1516/E0.csv' download_file(url) raw_data = load_data() season = Season(raw_data) print('----------------') print('Example team:') season.teams[random.randint(0, 20)].print() print('Example week:') season.weeks[random.randint(0, 20)].print() print('Example fixture:') season.weeks[random.randint(0, 38)].fixtures[random.randint(0, 10)].print() print('Example table:') season.weeks[random.randint(0, 20)].table.print()
from season import Season run_exp_hits = 'data/run_exp_hits_2015.pickle' run_exp_count = 'data/run_exp_count_2015.pickle' heatmap = 'data/heatmaps_2015.pickle' season = Season(run_exp_hits, run_exp_count, lastSeason=heatmap) filename = 'data/2016.csv' output_file = 'data/2016_processed.csv' season.process_file(filename, output_file)
def __init__(self): self.wave = Wave().set_wave() self.season = Season().set_season() self.board = Board().set_board() self.wind = Wind().set_wind()
def read_info(): """Reads in all of the files apart from matches and returns: void""" matchFileTour = os.path.join(my_path, ".."+data_paths+"TOURNAMENT INFO.csv") DivInfo = os.path.join(my_path, ".."+data_paths+"DIVISION INFO.csv") ranking_points = os.path.join(my_path, ".."+data_paths+"RANKING POINTS.csv") ranking_list = [] with open(ranking_points, 'r') as csvFile: reader = csv.DictReader(csvFile) for row in reader: temp = int(row["Tournament Ranking Points"]) if temp is not None: ranking_list.sort() if (ex.binary_search(ranking_list, temp) == None): ranking_list.append(temp) ranking_list.sort() # Find out the number of columns in the tournament info csv number_columns = 0 with open(matchFileTour, 'r') as f1: csvlines = csv.reader(f1, delimiter=',') for lineNum, line in enumerate(csvlines): if lineNum == 0: number_columns = (len(line)) break break # Find all of the seasons in the file and load them into seasons season_list = [] with open(matchFileTour, 'r') as csvFile: reader = csv.DictReader(csvFile) for row in reader: temp = row["Season"] if temp is not None: if(ex.binary_search(season_list,temp) == None): season_list.append(temp) for i in season_list: seasons.append(Season(i,ranking_list)) # Load in all tournaments to their respective seasons # Also finds which places get prize money for i in seasons: with open(matchFileTour, 'r') as csvFile: reader = csv.DictReader(csvFile) for row in reader: if(row["Season"] == i.get_name()): temp = [] row_name = "Place " number_places = (number_columns - 3) for x in range(0,number_places): temp.append(float(row[row_name+str(x+1)].replace(',',''))) new_list = list(set(temp)) # Find unique elements in list i.add_tournament(Tournament(row["Tournament"],row["Difficulty"],new_list)) # Load in divisions for each tournament for x in seasons: for j in x.get_tournaments(): with open(DivInfo, 'r') as csvFile: reader = csv.DictReader(csvFile) for row in reader: if (row["Season"] == x.get_name()) and (row["Tournament"] == j.get_name()): j.add_division(Division(row["Division"],row["Best Of"],row["Players"])) # Add players to seasons list_all_divisions = [] for x in seasons: for j in x.get_tournaments(): for k in j.get_divisions(): if list_all_divisions is not None: # Find all of the divisions in the season list_all_divisions.sort() if(ex.binary_search(list_all_divisions,k.get_player_type()) == None): list_all_divisions.append(str(k.get_player_type())) for i in list_all_divisions: with open(os.path.join(my_path, ".."+data_paths+"PLAYERS "+i+".csv"), 'r') as csvFile: reader = csv.DictReader(csvFile) temp = [] for row in reader: player = Player(row["Player"],i) player.set_number_tournaments(x.number_tournaments()) temp.append(player) x.add_participants(temp)