James McCool commited on
Commit
933cab1
·
1 Parent(s): 8db1f1d

Refactor grab_contest_data function to streamline data processing

Browse files

- Removed debug print statements to clean up the code and improve performance.
- Simplified the creation of the player ID mapping by eliminating unnecessary formatting and directly associating player IDs with their respective positions.
- These changes enhance the clarity and efficiency of the data handling process within the application.

Files changed (1) hide show
  1. global_func/grab_contest_data.py +6 -19
global_func/grab_contest_data.py CHANGED
@@ -14,10 +14,6 @@ def grab_contest_data(sport, contest_name, contest_id_map, contest_date):
14
  # Remove the leading colon and split by the remaining colons
15
  player_ids = lineup_hash.lstrip(':').split(':')
16
 
17
- # Print example of what we're working with
18
- print(f"Example lineup_hash: {lineup_hash}")
19
- print(f"Split player_ids: {player_ids}")
20
-
21
  # Check if the number of IDs matches the number of positions
22
  if len(player_ids) != len(positions):
23
  print(f"Warning: Mismatch for hash {lineup_hash}. IDs: {len(player_ids)}, Positions: {len(positions)}")
@@ -25,11 +21,7 @@ def grab_contest_data(sport, contest_name, contest_id_map, contest_date):
25
 
26
  # Combine positions and player IDs
27
  combined_parts = [pos + pid for pos, pid in zip(positions, player_ids)]
28
- result = "".join(combined_parts)
29
-
30
- # Print example of the result
31
- print(f"Formatted result: {result}")
32
- return result
33
 
34
  lineups_json = requests.get(lineups_url).json()
35
  data_json = requests.get(data_url).json()
@@ -52,11 +44,11 @@ def grab_contest_data(sport, contest_name, contest_id_map, contest_date):
52
  players_df = pd.DataFrame(player_data)
53
  players_df = players_df.sort_values(by='ownership', ascending=False).reset_index(drop=True)
54
  players_df = players_df.rename(columns={'fullName': 'Player', 'rosterPosition': 'Roster Position', 'ownership': '%Drafted', 'actualPoints': 'FPTS', 'salary': 'Salary', 'currentTeam': 'Team'})
55
- pid_map = dict(zip(players_df['playerId'].astype(str).apply(lambda x: f" {x} "), players_df['Player']))
56
- print("Example pid_map entries:")
57
- for i, (pid, name) in enumerate(pid_map.items()):
58
- if i < 3: # Print first 3 entries
59
- print(f"ID: '{pid}' -> Name: '{name}'")
60
 
61
  for lineup_hash, lineup_info in lineups_json['lineups'].items():
62
  lineup_data.append({
@@ -75,12 +67,7 @@ def grab_contest_data(sport, contest_name, contest_id_map, contest_date):
75
  lineups_df = lineups_df.rename(columns={'index': 'Rank', 'points': 'Points', 'entryNameList': 'EntryName', 'lineupHash': 'Lineup'})
76
  lineups_df['EntryName'] = lineups_df['EntryName'] + ' (1/1)'
77
  lineups_df['Lineup'] = lineups_df['Lineup'].apply(lambda x: format_lineup_string(x, position_inserts))
78
- # Print example before and after replacement
79
- print("\nExample before replacement:")
80
- print(lineups_df['Lineup'].iloc[0])
81
  lineups_df['Lineup'] = lineups_df['Lineup'].replace(pid_map, regex=False)
82
- print("\nExample after replacement:")
83
- print(lineups_df['Lineup'].iloc[0])
84
  lineups_df = lineups_df[['Rank', 'EntryId', 'EntryName', 'TimeRemaining', 'Points', 'Lineup']]
85
 
86
  total_data = lineups_df.merge(players_df, how='left', left_index=True, right_index=True)
 
14
  # Remove the leading colon and split by the remaining colons
15
  player_ids = lineup_hash.lstrip(':').split(':')
16
 
 
 
 
 
17
  # Check if the number of IDs matches the number of positions
18
  if len(player_ids) != len(positions):
19
  print(f"Warning: Mismatch for hash {lineup_hash}. IDs: {len(player_ids)}, Positions: {len(positions)}")
 
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()
27
  data_json = requests.get(data_url).json()
 
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
  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)