James McCool
commited on
Commit
·
894a694
1
Parent(s):
c0713d8
Refine player lineup processing logic in load_contest_file function
Browse files- Enhanced the handling of player names and position indicators by implementing a more robust splitting mechanism, ensuring accurate player assignments.
- Improved debug output to display processed players with their respective positions, aiding in traceability during execution.
- These changes contribute to ongoing efforts to enhance data handling and improve user experience within the application.
- global_func/load_contest_file.py +51 -32
global_func/load_contest_file.py
CHANGED
|
@@ -87,51 +87,70 @@ def load_contest_file(upload, sport):
|
|
| 87 |
print(f"\nProcessing lineup {idx}:")
|
| 88 |
print(f"Original lineup string: {row['Lineup']}")
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
|
|
|
| 95 |
current_position = None
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# If the item is just a position indicator, store it
|
| 102 |
-
if item in pos_values:
|
| 103 |
-
current_position = item
|
| 104 |
-
print(f"Found position: {current_position}")
|
| 105 |
continue
|
| 106 |
|
| 107 |
-
#
|
| 108 |
-
if
|
| 109 |
-
|
| 110 |
-
for
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
else:
|
| 117 |
-
#
|
| 118 |
for pos in pos_values:
|
| 119 |
-
if
|
| 120 |
-
|
|
|
|
|
|
|
| 121 |
break
|
| 122 |
-
print(f"Adding player without position: {item}")
|
| 123 |
-
cleaned_players.append(item)
|
| 124 |
|
| 125 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
# First pass: fill required positions (excluding OF)
|
| 128 |
required_positions = ['SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS']
|
| 129 |
for pos in required_positions:
|
| 130 |
-
for player in
|
| 131 |
if is_eligible_for_position(player, pos):
|
| 132 |
print(f"Assigning {player} to {pos}")
|
| 133 |
df.at[idx, pos] = player
|
| 134 |
-
|
| 135 |
break
|
| 136 |
else:
|
| 137 |
print(f"No player found for {pos}")
|
|
@@ -139,11 +158,11 @@ def load_contest_file(upload, sport):
|
|
| 139 |
# Second pass: fill OF positions with remaining players
|
| 140 |
of_positions = ['OF1', 'OF2', 'OF3']
|
| 141 |
for pos in of_positions:
|
| 142 |
-
for player in
|
| 143 |
if 'OF' in pos_dict.get(player, '').split('/'):
|
| 144 |
print(f"Assigning {player} to {pos}")
|
| 145 |
df.at[idx, pos] = player
|
| 146 |
-
|
| 147 |
break
|
| 148 |
else:
|
| 149 |
print(f"No player found for {pos}, using -1")
|
|
|
|
| 87 |
print(f"\nProcessing lineup {idx}:")
|
| 88 |
print(f"Original lineup string: {row['Lineup']}")
|
| 89 |
|
| 90 |
+
# First split by position indicators to preserve player names
|
| 91 |
+
lineup_parts = []
|
| 92 |
+
current_part = row['Lineup']
|
| 93 |
+
for pos in pos_values:
|
| 94 |
+
if pos in current_part:
|
| 95 |
+
parts = current_part.split(pos)
|
| 96 |
+
if len(parts) > 1:
|
| 97 |
+
lineup_parts.append(pos) # Add the position
|
| 98 |
+
current_part = parts[1] # Keep the rest for further processing
|
| 99 |
|
| 100 |
+
# Now split the remaining parts by commas, but only if they're not part of a player name
|
| 101 |
+
players = []
|
| 102 |
current_position = None
|
| 103 |
+
for part in lineup_parts:
|
| 104 |
+
part = part.strip()
|
| 105 |
+
if part in pos_values:
|
| 106 |
+
current_position = part
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
continue
|
| 108 |
|
| 109 |
+
# Split by comma only if it's followed by a position indicator
|
| 110 |
+
if ',' in part:
|
| 111 |
+
subparts = part.split(',')
|
| 112 |
+
for subpart in subparts:
|
| 113 |
+
subpart = subpart.strip()
|
| 114 |
+
# Check if this subpart ends with a position
|
| 115 |
+
has_position = any(subpart.endswith(pos) for pos in pos_values)
|
| 116 |
+
if has_position:
|
| 117 |
+
# This is a complete player entry
|
| 118 |
+
for pos in pos_values:
|
| 119 |
+
if subpart.endswith(pos):
|
| 120 |
+
player = subpart[:-len(pos)].strip()
|
| 121 |
+
players.append((current_position, player))
|
| 122 |
+
current_position = pos
|
| 123 |
+
break
|
| 124 |
+
else:
|
| 125 |
+
# This might be part of a player name (like J.P., Crawford)
|
| 126 |
+
# Combine with the next part
|
| 127 |
+
if players:
|
| 128 |
+
last_pos, last_player = players[-1]
|
| 129 |
+
players[-1] = (last_pos, last_player + ',' + subpart)
|
| 130 |
+
else:
|
| 131 |
+
players.append((current_position, subpart))
|
| 132 |
else:
|
| 133 |
+
# No comma, just clean and add
|
| 134 |
for pos in pos_values:
|
| 135 |
+
if part.endswith(pos):
|
| 136 |
+
player = part[:-len(pos)].strip()
|
| 137 |
+
players.append((current_position, player))
|
| 138 |
+
current_position = pos
|
| 139 |
break
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
print(f"Processed players with positions: {players}")
|
| 142 |
+
|
| 143 |
+
# Now fill the positions using the processed players
|
| 144 |
+
cleaned_players = [player for _, player in players]
|
| 145 |
|
| 146 |
# First pass: fill required positions (excluding OF)
|
| 147 |
required_positions = ['SP1', 'SP2', 'C', '1B', '2B', '3B', 'SS']
|
| 148 |
for pos in required_positions:
|
| 149 |
+
for position, player in players:
|
| 150 |
if is_eligible_for_position(player, pos):
|
| 151 |
print(f"Assigning {player} to {pos}")
|
| 152 |
df.at[idx, pos] = player
|
| 153 |
+
players.remove((position, player))
|
| 154 |
break
|
| 155 |
else:
|
| 156 |
print(f"No player found for {pos}")
|
|
|
|
| 158 |
# Second pass: fill OF positions with remaining players
|
| 159 |
of_positions = ['OF1', 'OF2', 'OF3']
|
| 160 |
for pos in of_positions:
|
| 161 |
+
for position, player in players:
|
| 162 |
if 'OF' in pos_dict.get(player, '').split('/'):
|
| 163 |
print(f"Assigning {player} to {pos}")
|
| 164 |
df.at[idx, pos] = player
|
| 165 |
+
players.remove((position, player))
|
| 166 |
break
|
| 167 |
else:
|
| 168 |
print(f"No player found for {pos}, using -1")
|