def _remove_zero_pts(self): """Exclude players with zero predicted points.""" players = [] # change_idx stores the indices of where the player positions change in the new # player list change_idx = [0] last_pos = self.positions[0] for p in self.players: gw_pts = get_predicted_points_for_player(p, self.tag, season=self.season) total_pts = sum( [pts for gw, pts in gw_pts.items() if gw in self.gw_range]) if total_pts > 0: if p.position(self.season) != last_pos: change_idx.append(len(players)) last_pos = p.position(self.season) players.append(p) change_idx.append(len(players)) position_idx = { self.positions[i - 1]: (change_idx[i - 1], change_idx[i] - 1) for i in range(1, len(change_idx)) } self.players = players self.position_idx = position_idx
def combine_player_info(player_id, dbsession=DBSESSION): """ Get player's name, club, recent scores, upcoming fixtures, and upcoming predictions if available """ info_dict = {"player_id": player_id} p = get_player(player_id, dbsession=dbsession) info_dict["player_name"] = p.name team = p.team(CURRENT_SEASON, NEXT_GAMEWEEK) info_dict["team"] = team # get recent scores for the player rs = get_recent_scores_for_player(p, dbsession=dbsession) recent_scores = [{"gameweek": k, "score": v} for k, v in rs.items()] info_dict["recent_scores"] = recent_scores # get upcoming fixtures fixtures = get_fixtures_for_player(p, dbsession=dbsession)[:3] info_dict["fixtures"] = [] for f in fixtures: home_or_away = "home" if f.home_team == team else "away" opponent = f.away_team if home_or_away == "home" else f.home_team info_dict["fixtures"].append({ "gameweek": f.gameweek, "opponent": opponent, "home_or_away": home_or_away }) try: tag = get_latest_prediction_tag(dbsession=dbsession) predicted_points = get_predicted_points_for_player(p, tag, dbsession=dbsession) info_dict["predictions"] = predicted_points except (RuntimeError): pass return info_dict
def calc_predicted_points(self, method): """ get expected points from the db. Will be a dict of dicts, keyed by method and gameweeek """ if not method in self.predicted_points.keys(): self.predicted_points[method] = get_predicted_points_for_player( self.player_id, method, dbsession=self.dbsession)
def get_session_prediction(player_id, session_id, gw=None, pred_tag=None, dbsession=DBSESSION): """ Query the fixture and predictedscore tables for a specified player """ if not gw: gw = get_next_gameweek(CURRENT_SEASON, dbsession) if not pred_tag: pred_tag = get_latest_prediction_tag() return_dict = { "predicted_score": get_predicted_points_for_player(pid, pred_tag, CURRENT_SEASON, dbsession)[gw], "fixture": get_next_fixture_for_player(pid,CURRENT_SEASON,dbsession) } return return_dict