James McCool commited on
Commit
d61cf96
·
1 Parent(s): 1c93e3f

Refactor name-to-ID mapping and matching logic in app.py: streamline the process by consolidating the mapping and matching steps, improving efficiency and maintaining user experience during exports.

Browse files
Files changed (1) hide show
  1. app.py +30 -31
app.py CHANGED
@@ -145,39 +145,38 @@ 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
- 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
174
- match_dict = {name: name_id_map[match[0][0]] if match else name
175
- for name, match in zip(names, matches)}
176
 
177
- # Apply the matches
178
- projections['upload_match'] = projections['player_names'].map(match_dict)
179
- st.session_state['export_dict'] = dict(zip(projections['player_names'], projections['upload_match']))
180
-
181
  st.session_state['origin_portfolio'] = st.session_state['portfolio'].copy()
182
 
183
  # with tab2:
 
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
+ # Convert matches to dictionary
173
+ match_dict = {name: name_id_map[match[0][0]] if match else name
174
+ for name, match in zip(names, matches)}
175
 
176
+ # Apply the matches
177
+ projections['upload_match'] = projections['player_names'].map(match_dict)
178
+ st.session_state['export_dict'] = dict(zip(projections['player_names'], projections['upload_match']))
179
+
180
  st.session_state['origin_portfolio'] = st.session_state['portfolio'].copy()
181
 
182
  # with tab2: