James McCool commited on
Commit
ec03891
·
1 Parent(s): 5dffa53

Refactor query construction in DK seed frame function to streamline player and team locking/removal logic. Consolidated query conditions and improved execution flow for better performance and readability.

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +18 -16
src/streamlit_app.py CHANGED
@@ -70,45 +70,47 @@ def init_DK_seed_frames(sharp_split, player_lock_var1, team_lock_var1, player_re
70
  names_dict = dict(zip(raw_data['key'], raw_data['value']))
71
 
72
  collection = db["DK_NFL_seed_frame"]
 
 
 
73
  if player_lock_var1 != [] or team_lock_var1 != []:
74
  # Build query to check if locked players exist in any position column
75
- player_conditions = []
 
76
  if player_lock_var1:
77
  position_columns = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
 
78
  for column in position_columns:
79
  player_conditions.append({column: {'$in': player_lock_var1}})
80
-
81
- query_conditions = []
82
- if player_conditions:
83
  query_conditions.append({'$or': player_conditions})
 
84
  if team_lock_var1:
85
  query_conditions.append({'Team': {'$in': team_lock_var1}})
86
 
87
  if len(query_conditions) == 1:
88
- collection = collection.find(query_conditions[0])
89
  else:
90
- collection = collection.find({'$and': query_conditions})
 
91
  elif player_remove_var1 != [] or team_remove_var1 != []:
92
  # Build query to exclude lineups containing removed players
93
- exclusion_conditions = []
 
94
  if player_remove_var1:
95
  position_columns = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
96
  for column in position_columns:
97
- exclusion_conditions.append({column: {'$nin': player_remove_var1}})
98
 
99
- query_conditions = []
100
- if exclusion_conditions:
101
- query_conditions.extend(exclusion_conditions)
102
  if team_remove_var1:
103
  query_conditions.append({'Team': {'$nin': team_remove_var1}})
104
 
105
  if len(query_conditions) == 1:
106
- collection = collection.find(query_conditions[0])
107
  else:
108
- collection = collection.find({'$and': query_conditions})
109
- else:
110
- collection = collection.find()
111
- cursor = collection.find().limit(sharp_split)
112
 
113
  raw_display = pd.DataFrame(list(cursor))
114
  raw_display = raw_display[['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'salary', 'proj', 'Team', 'Team_count', 'Secondary', 'Secondary_count', 'Own']]
 
70
  names_dict = dict(zip(raw_data['key'], raw_data['value']))
71
 
72
  collection = db["DK_NFL_seed_frame"]
73
+
74
+ # Build the query
75
+ query = {}
76
  if player_lock_var1 != [] or team_lock_var1 != []:
77
  # Build query to check if locked players exist in any position column
78
+ query_conditions = []
79
+
80
  if player_lock_var1:
81
  position_columns = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
82
+ player_conditions = []
83
  for column in position_columns:
84
  player_conditions.append({column: {'$in': player_lock_var1}})
 
 
 
85
  query_conditions.append({'$or': player_conditions})
86
+
87
  if team_lock_var1:
88
  query_conditions.append({'Team': {'$in': team_lock_var1}})
89
 
90
  if len(query_conditions) == 1:
91
+ query = query_conditions[0]
92
  else:
93
+ query = {'$and': query_conditions}
94
+
95
  elif player_remove_var1 != [] or team_remove_var1 != []:
96
  # Build query to exclude lineups containing removed players
97
+ query_conditions = []
98
+
99
  if player_remove_var1:
100
  position_columns = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
101
  for column in position_columns:
102
+ query_conditions.append({column: {'$nin': player_remove_var1}})
103
 
 
 
 
104
  if team_remove_var1:
105
  query_conditions.append({'Team': {'$nin': team_remove_var1}})
106
 
107
  if len(query_conditions) == 1:
108
+ query = query_conditions[0]
109
  else:
110
+ query = {'$and': query_conditions}
111
+
112
+ # Execute the query once
113
+ cursor = collection.find(query).limit(sharp_split)
114
 
115
  raw_display = pd.DataFrame(list(cursor))
116
  raw_display = raw_display[['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'salary', 'proj', 'Team', 'Team_count', 'Secondary', 'Secondary_count', 'Own']]