Skip to content

Commit

Permalink
updated coaching efficiency eligibility to normalize player names
Browse files Browse the repository at this point in the history
  • Loading branch information
uberfastman committed Oct 17, 2024
1 parent 1bbebe1 commit 8e4672c
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 26 deletions.
10 changes: 8 additions & 2 deletions calculate/coaching_efficiency.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dao.base import BasePlayer, BaseLeague
from utilities.constants import prohibited_statuses
from utilities.logger import get_logger
from utilities.utils import normalize_player_name

logger = get_logger(__name__, propagate=False)

Expand Down Expand Up @@ -70,7 +71,11 @@ def _is_player_ineligible(self, player: BasePlayer, week, inactives):
if player.points != 0.0:
return False
else:
return player.status in self.inactive_statuses or player.bye_week == week or player.full_name in inactives
return (
player.status in self.inactive_statuses
or player.bye_week == week
or normalize_player_name(player.full_name) in inactives
)

def _get_player_open_positions(self, player: BasePlayer, optimal_lineup: Dict[str, RosterSlot]):

Expand Down Expand Up @@ -255,7 +260,8 @@ def execute_coaching_efficiency(self, team_name, team_roster, team_points, posit
p for p in team_roster if p.selected_position == "BN"
] # exclude IR players
ineligible_efficiency_player_count = len(
[p for p in bench_players if self._is_player_ineligible(p, week, inactive_players)])
[p for p in bench_players if self._is_player_ineligible(p, week, inactive_players)]
)

if Counter(self.roster_active_slots) == Counter(positions_filled_active):
num_bench_slots = self.roster_slot_counts.get("BN", 0) # excludes IR players/slots
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
services:

app:
image: ghcr.io/uberfastman/fantasy-football-metrics-weekly-report:19.2.2
image: ghcr.io/uberfastman/fantasy-football-metrics-weekly-report:19.2.3
platform: linux/amd64
ports:
- "5001:5000"
Expand Down
20 changes: 1 addition & 19 deletions features/base/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from abc import ABC, abstractmethod
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Any
from typing import Dict, Any

from utilities.logger import get_logger

Expand Down Expand Up @@ -38,9 +38,6 @@ def __init__(self, feature_type: str, feature_web_base_url: str, data_dir: Path,
self.raw_feature_data_file_path: Path = self.data_dir / f"{self.feature_type_str}_raw_data.json"
self.feature_data_file_path: Path = self.data_dir / f"{self.feature_type_str}_data.json"

self.player_name_punctuation: List[str] = [".", "'"]
self.player_name_suffixes: List[str] = ["Jr", "Sr", "V", "IV", "III", "II", "I"] # ordered for str.removesuffix

start = datetime.now()

data_retrieved_from_web = False
Expand Down Expand Up @@ -107,21 +104,6 @@ def _save_feature_data(self) -> None:
with open(self.raw_feature_data_file_path, "w", encoding="utf-8") as feature_raw_data_out:
json.dump(self.raw_feature_data, feature_raw_data_out, ensure_ascii=False, indent=2)

def _normalize_player_name(self, player_name: str) -> str:
"""Remove all punctuation and name suffixes from player names and covert them to title case.
"""
normalized_player_name: str = player_name.strip()
if (any(punc in player_name for punc in self.player_name_punctuation)
or any(suffix in player_name for suffix in self.player_name_suffixes)):

for punc in self.player_name_punctuation:
normalized_player_name = normalized_player_name.replace(punc, "")

for suffix in self.player_name_suffixes:
normalized_player_name = normalized_player_name.removesuffix(suffix)

return normalized_player_name.strip().title()

@abstractmethod
def _get_feature_data(self) -> None:
raise NotImplementedError
5 changes: 3 additions & 2 deletions features/high_roller.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from features.base.feature import BaseFeature
from utilities.constants import nfl_team_abbreviations, nfl_team_abbreviation_conversions
from utilities.logger import get_logger
from utilities.utils import normalize_player_name

logger = get_logger(__name__, propagate=False)

Expand Down Expand Up @@ -101,7 +102,7 @@ def _get_feature_data(self):

if player_name not in self.feature_data.keys():
self.feature_data[player_name] = {
"normalized_name": self._normalize_player_name(player_name),
"normalized_name": normalize_player_name(player_name),
"team": player_team,
"position": player_position,
"position_type": self.position_types[player_position],
Expand Down Expand Up @@ -172,7 +173,7 @@ def _get_player_high_roller_stats(self, player_first_name: str, player_last_name
})
else:
player.update({
"normalized_name": self._normalize_player_name(player_full_name),
"normalized_name": normalize_player_name(player_full_name),
"team": player_team_abbr,
"fines": [],
})
Expand Down
3 changes: 2 additions & 1 deletion report/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from utilities.app import add_report_team_stats, get_player_game_time_statuses
from utilities.logger import get_logger
from utilities.settings import settings
from utilities.utils import normalize_player_name

logger = get_logger(__name__, propagate=False)

Expand Down Expand Up @@ -39,7 +40,7 @@ def __init__(self, league: BaseLeague, season_weekly_teams_results, week_counter
if player_status_info:
player_status = player_status_info.text.strip()
if player_status == "Out":
inactive_players.append(player_name)
inactive_players.append(normalize_player_name(player_name))

self.teams_results = {
team.team_id: add_report_team_stats(
Expand Down
6 changes: 5 additions & 1 deletion utilities/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

# prohibited player statuses to check team coaching efficiency eligibility if dq_ce = True
prohibited_statuses = {
prohibited_statuses: Dict[str, str] = {
"O": "Out",
"Out": "Out",
"NA": "Inactive: Coach's Decision or Not on Roster",
Expand All @@ -40,3 +40,7 @@
"RET": "Reserve: Retired",
"Reserve-Ret": "Reserve: Retired"
}

player_name_punctuation: List[str] = [".", "'"]

player_name_suffixes: List[str] = ["Jr", "Sr", "V", "IV", "III", "II", "I"] # ordered for str.removesuffix support
21 changes: 21 additions & 0 deletions utilities/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
__author__ = "Wren J. R. (uberfastman)"
__email__ = "[email protected]"

import re

from utilities.constants import player_name_punctuation, player_name_suffixes
from utilities.logger import get_logger
from utilities.settings import settings

Expand Down Expand Up @@ -35,3 +38,21 @@ def truncate_cell_for_display(cell_text: str, halve_max_chars: bool = False, ses

else:
return cell_text


def normalize_player_name(player_full_name: str) -> str:
"""Remove all punctuation and name suffixes from player names, combine whitespace, and covert them to title case.
"""
regex_all_whitespace = re.compile(r"\s+")
normalized_player_name: str = regex_all_whitespace.sub(" ", player_full_name).strip()

if (any(punc in player_full_name for punc in player_name_punctuation)
or any(suffix in player_full_name for suffix in player_name_suffixes)):

for punc in player_name_punctuation:
normalized_player_name = normalized_player_name.replace(punc, "")

for suffix in player_name_suffixes:
normalized_player_name = normalized_player_name.removesuffix(suffix)

return normalized_player_name.strip().title()

0 comments on commit 8e4672c

Please sign in to comment.