James McCool commited on
Commit
ce6c2b2
·
1 Parent(s): 405e093

Refactor name matching and export dictionary handling in app.py: streamline the name-to-ID mapping process by ensuring it only occurs when necessary, and enhance match handling by fixing the conversion to match_dict, improving overall efficiency and user experience during exports.

Browse files
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -145,40 +145,42 @@ with tab1:
145
 
146
  # Update projections_df with any new matches
147
  st.session_state['projections_df'] = find_name_mismatches(st.session_state['portfolio'], st.session_state['projections_df'])
148
- try:
149
- name_id_map = dict(zip(
150
- st.session_state['csv_file']['Name'],
151
- st.session_state['csv_file']['Name + ID']
152
- ))
153
- except:
154
- name_id_map = dict(zip(
155
- st.session_state['csv_file']['Nickname'],
156
- st.session_state['csv_file']['Id']
157
- ))
158
-
159
- # Get all names at once
160
- names = projections['player_names'].tolist()
161
- choices = list(name_id_map.keys())
162
-
163
- # Process all names in one batch
164
- matches = process.extract(
165
- names,
166
- choices,
167
- scorer=fuzz.ratio,
168
- score_cutoff=85,
169
- limit=1 # Only get the best match
170
- )
171
 
172
- match_dict = {}
173
- for name, match_list in zip(names, matches):
174
- if match_list and match_list[0][1] >= 85: # Check if we have a match and it meets threshold
175
- match_dict[name] = name_id_map[match_list[0][0]] # Use the matched name to get the ID
176
- else:
177
- match_dict[name] = name # Keep original name if no good match
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
 
179
- # Apply the matches
180
- projections['upload_match'] = projections['player_names'].map(match_dict)
181
- st.session_state['export_dict'] = match_dict # Use match_dict directly
182
  st.write(st.session_state['export_dict'])
183
 
184
  st.session_state['origin_portfolio'] = st.session_state['portfolio'].copy()
 
145
 
146
  # Update projections_df with any new matches
147
  st.session_state['projections_df'] = find_name_mismatches(st.session_state['portfolio'], st.session_state['projections_df'])
148
+ if 'export_dict' not in st.session_state and csv_file is not None:
149
+ try:
150
+ name_id_map = dict(zip(
151
+ st.session_state['csv_file']['Name'],
152
+ st.session_state['csv_file']['Name + ID']
153
+ ))
154
+ except:
155
+ name_id_map = dict(zip(
156
+ st.session_state['csv_file']['Nickname'],
157
+ st.session_state['csv_file']['Id']
158
+ ))
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
+ # Get all names at once
161
+ names = projections['player_names'].tolist()
162
+ choices = list(name_id_map.keys())
163
+
164
+ # Process all names in one batch
165
+ matches = process.extract(
166
+ names,
167
+ choices,
168
+ scorer=fuzz.ratio,
169
+ score_cutoff=85,
170
+ limit=1 # Only get the best match
171
+ )
172
+
173
+ # Convert matches to dictionary - Fixed the match handling
174
+ match_dict = {}
175
+ for name, match_list in zip(names, matches):
176
+ if match_list and match_list[0][1] >= 85: # Check if we have a match and it meets threshold
177
+ match_dict[name] = name_id_map[match_list[0][0]] # Use the matched name to get the ID
178
+ else:
179
+ match_dict[name] = name # Keep original name if no good match
180
 
181
+ # Apply the matches
182
+ projections['upload_match'] = projections['player_names'].map(match_dict)
183
+ st.session_state['export_dict'] = match_dict # Use match_dict directly
184
  st.write(st.session_state['export_dict'])
185
 
186
  st.session_state['origin_portfolio'] = st.session_state['portfolio'].copy()