James McCool commited on
Commit
73a4cc3
·
1 Parent(s): 933cab1

Refactor player ID mapping in grab_contest_data for improved efficiency

Browse files

- Simplified the creation of the player ID mapping by using a dictionary comprehension, enhancing code readability and performance.
- Updated the replacement logic for lineup strings to utilize regex, ensuring accurate formatting of player IDs in the output.
- These changes contribute to ongoing efforts to streamline data processing and improve user experience within the application.

Files changed (1) hide show
  1. global_func/grab_contest_data.py +4 -6
global_func/grab_contest_data.py CHANGED
@@ -21,6 +21,8 @@ def grab_contest_data(sport, contest_name, contest_id_map, contest_date):
21
 
22
  # Combine positions and player IDs
23
  combined_parts = [pos + pid for pos, pid in zip(positions, player_ids)]
 
 
24
  return "".join(combined_parts)
25
 
26
  lineups_json = requests.get(lineups_url).json()
@@ -44,11 +46,7 @@ def grab_contest_data(sport, contest_name, contest_id_map, contest_date):
44
  players_df = pd.DataFrame(player_data)
45
  players_df = players_df.sort_values(by='ownership', ascending=False).reset_index(drop=True)
46
  players_df = players_df.rename(columns={'fullName': 'Player', 'rosterPosition': 'Roster Position', 'ownership': '%Drafted', 'actualPoints': 'FPTS', 'salary': 'Salary', 'currentTeam': 'Team'})
47
- pid_map = {}
48
- for pos in ['1B', '2B', '3B', 'C', 'OF', 'P', 'SS']:
49
- for pid, name in zip(players_df['playerId'].astype(str), players_df['Player']):
50
- # Create entries for each position format
51
- pid_map[f"{pos} {pid}"] = f"{pos} {name}"
52
 
53
  for lineup_hash, lineup_info in lineups_json['lineups'].items():
54
  lineup_data.append({
@@ -67,7 +65,7 @@ def grab_contest_data(sport, contest_name, contest_id_map, contest_date):
67
  lineups_df = lineups_df.rename(columns={'index': 'Rank', 'points': 'Points', 'entryNameList': 'EntryName', 'lineupHash': 'Lineup'})
68
  lineups_df['EntryName'] = lineups_df['EntryName'] + ' (1/1)'
69
  lineups_df['Lineup'] = lineups_df['Lineup'].apply(lambda x: format_lineup_string(x, position_inserts))
70
- lineups_df['Lineup'] = lineups_df['Lineup'].replace(pid_map, regex=False)
71
  lineups_df = lineups_df[['Rank', 'EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup']]
72
 
73
  total_data = lineups_df.merge(players_df, how='left', left_index=True, right_index=True)
 
21
 
22
  # Combine positions and player IDs
23
  combined_parts = [pos + pid for pos, pid in zip(positions, player_ids)]
24
+
25
+ # Join them into a single string
26
  return "".join(combined_parts)
27
 
28
  lineups_json = requests.get(lineups_url).json()
 
46
  players_df = pd.DataFrame(player_data)
47
  players_df = players_df.sort_values(by='ownership', ascending=False).reset_index(drop=True)
48
  players_df = players_df.rename(columns={'fullName': 'Player', 'rosterPosition': 'Roster Position', 'ownership': '%Drafted', 'actualPoints': 'FPTS', 'salary': 'Salary', 'currentTeam': 'Team'})
49
+ pid_map = dict(zip(players_df['playerId'].astype(str), players_df['Player']))
 
 
 
 
50
 
51
  for lineup_hash, lineup_info in lineups_json['lineups'].items():
52
  lineup_data.append({
 
65
  lineups_df = lineups_df.rename(columns={'index': 'Rank', 'points': 'Points', 'entryNameList': 'EntryName', 'lineupHash': 'Lineup'})
66
  lineups_df['EntryName'] = lineups_df['EntryName'] + ' (1/1)'
67
  lineups_df['Lineup'] = lineups_df['Lineup'].apply(lambda x: format_lineup_string(x, position_inserts))
68
+ lineups_df['Lineup'] = lineups_df['Lineup'].replace(pid_map, regex=True) # Note: back to regex=True
69
  lineups_df = lineups_df[['Rank', 'EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup']]
70
 
71
  total_data = lineups_df.merge(players_df, how='left', left_index=True, right_index=True)