Commit fc0ea09672
Unregistered key
Layout: unified · split
.github/workflows/pylint.yml +16 −6
| @@ -1,4 +1,4 @@ | ||
| 1 | name: Pylint | |
| 1 | name: Ruff | |
| 2 | 2 | |
| 3 | 3 | on: [push] |
| 4 | 4 | |
| @@ -7,18 +7,28 @@ jobs: | ||
| 7 | 7 | runs-on: ubuntu-latest |
| 8 | 8 | strategy: |
| 9 | 9 | matrix: |
| 10 | python-version: ["3.8", "3.9", "3.10"] | |
| 10 | python-version: ["3.x"] | |
| 11 | 11 | steps: |
| 12 | 12 | - uses: actions/checkout@v4 |
| 13 | 13 | - name: Set up Python ${{ matrix.python-version }} |
| 14 | uses: actions/setup-python@v4 | |
| 14 | uses: actions/setup-python@v5 | |
| 15 | 15 | with: |
| 16 | 16 | python-version: ${{ matrix.python-version }} |
| 17 | ref: ${{ github.event.pull_request.head.ref }} | |
| 17 | 18 | - name: Install dependencies |
| 18 | 19 | run: | |
| 19 | 20 | python -m pip install --upgrade pip |
| 20 | 21 | pip install -r requirements.txt |
| 21 | pip install pylint | |
| 22 | - name: Analysing the code with pylint | |
| 22 | - name: Install Ruff | |
| 23 | uses: astral-sh/ruff-action@v3.2.2 | |
| 24 | - name: Ruff Actions | |
| 23 | 25 | run: | |
| 24 | pylint -d R0801 -d W0311 -d R0914 $(git ls-files '*.py') | |
| \ No newline at end of file | ||
| 26 | ruff check --fix | |
| 27 | ruff format | |
| 28 | - name: Add and Commit | |
| 29 | uses: EndBug/add-and-commit@v9 | |
| 30 | env: | |
| 31 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| 32 | with: | |
| 33 | default_author: github_actions | |
| 34 | pathspec_error_handling: ignore | |
nba/__main__.py +1
| @@ -4,4 +4,5 @@ Entry point for the app. | ||
| 4 | 4 | |
| 5 | 5 | if __name__ == "__main__": |
| 6 | 6 | from nba.cli import nba |
| 7 | ||
| 7 | 8 | nba() |
nba/cli.py +18 −14
| @@ -3,23 +3,27 @@ This script uses argparse to parse command line arguments. | ||
| 3 | 3 | |
| 4 | 4 | It imports the required modules and sets up a parser with basic options for demonstration purposes. |
| 5 | 5 | """ |
| 6 | ||
| 6 | 7 | import argparse |
| 7 | 8 | from nba import fetch_data, scores, standings |
| 8 | 9 | |
| 10 | ||
| 9 | 11 | def nba() -> None: |
| 10 | """ | |
| 11 | Parse command-line arguments and display either scoreboard or standings. | |
| 12 | """ | |
| 13 | parser = argparse.ArgumentParser(description="NBA Scoreboard and Standings") | |
| 14 | parser.add_argument('--scores', '-sc', action='store_true', help='Display the scoreboard') | |
| 15 | parser.add_argument('--standings', '-st', action='store_true', help='Display the standings') | |
| 16 | args = parser.parse_args() | |
| 12 | """ | |
| 13 | Parse command-line arguments and display either scoreboard or standings. | |
| 14 | """ | |
| 15 | parser = argparse.ArgumentParser(description="NBA Scoreboard and Standings") | |
| 16 | parser.add_argument("--scores", action="store_true", help="Display the scoreboard") | |
| 17 | parser.add_argument( | |
| 18 | "--standings", action="store_true", help="Display the standings" | |
| 19 | ) | |
| 20 | args = parser.parse_args() | |
| 17 | 21 | |
| 18 | games, ranks = fetch_data.fetch_data() | |
| 22 | games, ranks = fetch_data.fetch_data() | |
| 19 | 23 | |
| 20 | if args.scores: | |
| 21 | scores.build_scoreboard(games, ranks) | |
| 22 | elif args.standings: | |
| 23 | standings.build_standings(ranks) | |
| 24 | else: | |
| 25 | print("Please specify --scores or --standings") | |
| 24 | if args.scores: | |
| 25 | scores.build_scoreboard(games, ranks) | |
| 26 | elif args.standings: | |
| 27 | standings.build_standings(ranks) | |
| 28 | else: | |
| 29 | print("Please specify --scores or --standings") | |
nba/fetch_data.py +18 −16
| @@ -1,28 +1,30 @@ | ||
| 1 | 1 | """ |
| 2 | 2 | Fetches data for use in other modules. |
| 3 | 3 | """ |
| 4 | ||
| 4 | 5 | import json |
| 5 | 6 | from nba_api.live.nba.endpoints import scoreboard |
| 6 | 7 | from nba_api.stats.endpoints import leaguestandings |
| 7 | 8 | |
| 9 | ||
| 8 | 10 | def fetch_data() -> tuple: |
| 9 | """ | |
| 10 | Fetches live NBA scoreboard data and standings from the NBA API. | |
| 11 | """ | |
| 12 | Fetches live NBA scoreboard data and standings from the NBA API. | |
| 11 | 13 | |
| 12 | Returns: | |
| 13 | games (dict): JSON parsed games data. | |
| 14 | standings (dict): JSON parsed team standings data. | |
| 15 | """ | |
| 16 | # Get today's scoreboard data | |
| 17 | games_endpoint = scoreboard.ScoreBoard() | |
| 18 | games_json = games_endpoint.get_json() | |
| 14 | Returns: | |
| 15 | games (dict): JSON parsed games data. | |
| 16 | standings (dict): JSON parsed team standings data. | |
| 17 | """ | |
| 18 | # Get today's scoreboard data | |
| 19 | games_endpoint = scoreboard.ScoreBoard() | |
| 20 | games_json = games_endpoint.get_json() | |
| 19 | 21 | |
| 20 | # Get league standings | |
| 21 | standings_endpoint = leaguestandings.LeagueStandings() | |
| 22 | standings_json = standings_endpoint.get_json() | |
| 22 | # Get league standings | |
| 23 | standings_endpoint = leaguestandings.LeagueStandings() | |
| 24 | standings_json = standings_endpoint.get_json() | |
| 23 | 25 | |
| 24 | # Parse the JSON strings into Python dictionaries | |
| 25 | games = json.loads(games_json) | |
| 26 | standings = json.loads(standings_json) | |
| 26 | # Parse the JSON strings into Python dictionaries | |
| 27 | games = json.loads(games_json) | |
| 28 | standings = json.loads(standings_json) | |
| 27 | 29 | |
| 28 | return games, standings | |
| 30 | return games, standings | |
nba/scores.py +79 −74
| @@ -1,85 +1,90 @@ | ||
| 1 | 1 | """ |
| 2 | 2 | Tabulates a scoreboard for today's games. |
| 3 | 3 | """ |
| 4 | ||
| 4 | 5 | from tabulate import tabulate |
| 5 | 6 | |
| 6 | 7 | # ANSI escape codes for text formatting |
| 7 | BOLD = '\033[1m' | |
| 8 | END = '\033[0m' | |
| 9 | RED = '\033[91m' | |
| 10 | GREEN = '\033[32m' | |
| 8 | BOLD = "\033[1m" | |
| 9 | END = "\033[0m" | |
| 10 | RED = "\033[91m" | |
| 11 | GREEN = "\033[32m" | |
| 12 | ||
| 11 | 13 | |
| 12 | 14 | # Function to get team record from standings |
| 13 | 15 | def get_team_record(team_name, standings) -> str: |
| 14 | """ | |
| 15 | Retrieves a team's win-loss record from the standings data. | |
| 16 | ||
| 17 | Args: | |
| 18 | team_name (str): Name of the team. | |
| 19 | standings (dict): Team standings data. | |
| 20 | ||
| 21 | Returns: | |
| 22 | record (str): Team's win-loss record in 'W-L' format. Defaults to 'N/A'. | |
| 23 | """ | |
| 24 | for result_set in standings['resultSets']: | |
| 25 | if result_set['name'] == 'Standings': | |
| 26 | for team in result_set['rowSet']: | |
| 27 | if team[4] == team_name: | |
| 28 | return f"{team[12]}-{team[13]}" | |
| 29 | return "N/A" | |
| 16 | """ | |
| 17 | Retrieves a team's win-loss record from the standings data. | |
| 18 | ||
| 19 | Args: | |
| 20 | team_name (str): Name of the team. | |
| 21 | standings (dict): Team standings data. | |
| 22 | ||
| 23 | Returns: | |
| 24 | record (str): Team's win-loss record in 'W-L' format. Defaults to 'N/A'. | |
| 25 | """ | |
| 26 | for result_set in standings["resultSets"]: | |
| 27 | if result_set["name"] == "Standings": | |
| 28 | for team in result_set["rowSet"]: | |
| 29 | if team[4] == team_name: | |
| 30 | return f"{team[12]}-{team[13]}" | |
| 31 | return "N/A" | |
| 32 | ||
| 30 | 33 | |
| 31 | 34 | def build_scoreboard(games, standings) -> None: |
| 32 | """ | |
| 33 | Prints the current day's games in a table format. | |
| 34 | ||
| 35 | Args: | |
| 36 | games (dict): JSON parsed games data. | |
| 37 | standings (dict): Team standings data. | |
| 38 | """ | |
| 39 | scoreboard_data = games['scoreboard'] | |
| 40 | games = scoreboard_data['games'] | |
| 41 | ||
| 42 | # Prepare the table data | |
| 43 | table_data = [] | |
| 44 | for game in games: | |
| 45 | home_team = game['homeTeam']['teamName'] | |
| 46 | away_team = game['awayTeam']['teamName'] | |
| 47 | game_status = game['gameStatusText'] | |
| 48 | home_score = game['homeTeam']['score'] | |
| 49 | away_score = game['awayTeam']['score'] | |
| 50 | ||
| 51 | home_record = get_team_record(home_team, standings) | |
| 52 | away_record = get_team_record(away_team, standings) | |
| 53 | ||
| 54 | # Determine the winning team | |
| 55 | if home_score > away_score: | |
| 56 | home_team_bold = f"{BOLD}{GREEN}{home_team} ({home_record}){END}{END}" | |
| 57 | away_team_bold = f"{away_team} ({away_record}){END}" | |
| 58 | home_score_bold = f"{BOLD}{GREEN}{home_score}{END}{END}" | |
| 59 | away_score_bold = f"{away_score}{END}" | |
| 60 | elif away_score > home_score: | |
| 61 | home_team_bold = f"{home_team} ({home_record}){END}" | |
| 62 | away_team_bold = f"{BOLD}{GREEN}{away_team} ({away_record}){END}{END}" | |
| 63 | home_score_bold = f"{home_score}{END}" | |
| 64 | away_score_bold = f"{BOLD}{GREEN}{away_score}{END}{END}" | |
| 65 | else: | |
| 66 | home_team_bold = f"{home_team} ({home_record})" | |
| 67 | away_team_bold = f"{away_team} ({away_record})" | |
| 68 | home_score_bold = f"{home_score}" | |
| 69 | away_score_bold = f"{away_score}" | |
| 70 | ||
| 71 | # Determine games still in progress | |
| 72 | if game_status != "Final": | |
| 73 | game_status = f"{RED}{game_status}{END}" | |
| 74 | ||
| 75 | table_data.append([ | |
| 76 | f"{home_team_bold}\n{away_team_bold}", | |
| 77 | f"{home_score_bold}\n{away_score_bold}", | |
| 78 | f"{BOLD}{game_status}{END}" | |
| 79 | ]) | |
| 80 | ||
| 81 | # Define the table headers | |
| 82 | headers = ["Team", "Score", "Game Status"] | |
| 83 | ||
| 84 | # Print the table | |
| 85 | print(tabulate(table_data, headers=headers, tablefmt="grid")) | |
| 35 | """ | |
| 36 | Prints the current day's games in a table format. | |
| 37 | ||
| 38 | Args: | |
| 39 | games (dict): JSON parsed games data. | |
| 40 | standings (dict): Team standings data. | |
| 41 | """ | |
| 42 | scoreboard_data = games["scoreboard"] | |
| 43 | games = scoreboard_data["games"] | |
| 44 | ||
| 45 | # Prepare the table data | |
| 46 | table_data = [] | |
| 47 | for game in games: | |
| 48 | home_team = game["homeTeam"]["teamName"] | |
| 49 | away_team = game["awayTeam"]["teamName"] | |
| 50 | game_status = game["gameStatusText"] | |
| 51 | home_score = game["homeTeam"]["score"] | |
| 52 | away_score = game["awayTeam"]["score"] | |
| 53 | ||
| 54 | home_record = get_team_record(home_team, standings) | |
| 55 | away_record = get_team_record(away_team, standings) | |
| 56 | ||
| 57 | # Determine the winning team | |
| 58 | if home_score > away_score: | |
| 59 | home_team_bold = f"{BOLD}{GREEN}{home_team} ({home_record}){END}{END}" | |
| 60 | away_team_bold = f"{away_team} ({away_record}){END}" | |
| 61 | home_score_bold = f"{BOLD}{GREEN}{home_score}{END}{END}" | |
| 62 | away_score_bold = f"{away_score}{END}" | |
| 63 | elif away_score > home_score: | |
| 64 | home_team_bold = f"{home_team} ({home_record}){END}" | |
| 65 | away_team_bold = f"{BOLD}{GREEN}{away_team} ({away_record}){END}{END}" | |
| 66 | home_score_bold = f"{home_score}{END}" | |
| 67 | away_score_bold = f"{BOLD}{GREEN}{away_score}{END}{END}" | |
| 68 | else: | |
| 69 | home_team_bold = f"{home_team} ({home_record})" | |
| 70 | away_team_bold = f"{away_team} ({away_record})" | |
| 71 | home_score_bold = f"{home_score}" | |
| 72 | away_score_bold = f"{away_score}" | |
| 73 | ||
| 74 | # Determine games still in progress | |
| 75 | if game_status != "Final": | |
| 76 | game_status = f"{RED}{game_status}{END}" | |
| 77 | ||
| 78 | table_data.append( | |
| 79 | [ | |
| 80 | f"{home_team_bold}\n{away_team_bold}", | |
| 81 | f"{home_score_bold}\n{away_score_bold}", | |
| 82 | f"{BOLD}{game_status}{END}", | |
| 83 | ] | |
| 84 | ) | |
| 85 | ||
| 86 | # Define the table headers | |
| 87 | headers = ["Team", "Score", "Game Status"] | |
| 88 | ||
| 89 | # Print the table | |
| 90 | print(tabulate(table_data, headers=headers, tablefmt="grid")) | |
nba/standings.py +68 −62
| @@ -1,77 +1,83 @@ | ||
| 1 | 1 | """ |
| 2 | 2 | Tabulate the current conference standings. |
| 3 | 3 | """ |
| 4 | ||
| 4 | 5 | from tabulate import tabulate |
| 5 | 6 | |
| 6 | 7 | # ANSI escape codes for text formatting |
| 7 | BOLD = '\033[1m' | |
| 8 | END = '\033[0m' | |
| 9 | RED = '\033[91m' | |
| 10 | GREEN = '\033[32m' | |
| 8 | BOLD = "\033[1m" | |
| 9 | END = "\033[0m" | |
| 10 | RED = "\033[91m" | |
| 11 | GREEN = "\033[32m" | |
| 12 | ||
| 11 | 13 | |
| 12 | 14 | def build_standings(standings) -> None: |
| 13 | """ | |
| 14 | Prints team standings in two separate tables. | |
| 15 | """ | |
| 16 | Prints team standings in two separate tables. | |
| 15 | 17 | |
| 16 | Args: | |
| 17 | standings (dict): Team standings data. | |
| 18 | """ | |
| 19 | eastern_data = [] | |
| 20 | western_data = [] | |
| 21 | eastern_rank = 1 | |
| 22 | western_rank = 1 | |
| 18 | Args: | |
| 19 | standings (dict): Team standings data. | |
| 20 | """ | |
| 21 | eastern_data = [] | |
| 22 | western_data = [] | |
| 23 | eastern_rank = 1 | |
| 24 | western_rank = 1 | |
| 23 | 25 | |
| 24 | for result_set in standings['resultSets']: | |
| 25 | if result_set['name'] == 'Standings': | |
| 26 | for team in result_set['rowSet']: | |
| 27 | conference = team[5] | |
| 28 | team_name = team[4] | |
| 29 | wins = team[12] | |
| 30 | losses = team[13] | |
| 31 | win_pct = team[14] | |
| 32 | gb = team[37] | |
| 33 | home_record = team[17] | |
| 34 | away_record = team[18] | |
| 35 | last_10 = team[19] | |
| 36 | streak = team[35] | |
| 26 | for result_set in standings["resultSets"]: | |
| 27 | if result_set["name"] == "Standings": | |
| 28 | for team in result_set["rowSet"]: | |
| 29 | conference = team[5] | |
| 30 | team_name = team[4] | |
| 31 | wins = team[12] | |
| 32 | losses = team[13] | |
| 33 | win_pct = team[14] | |
| 34 | gb = team[37] | |
| 35 | home_record = team[17] | |
| 36 | away_record = team[18] | |
| 37 | last_10 = team[19] | |
| 38 | streak = team[35] | |
| 37 | 39 | |
| 38 | if int(streak) < 0: | |
| 39 | strk_color = f"{RED}{streak}{END}" | |
| 40 | else: | |
| 41 | strk_color = f"{GREEN}{streak}{END}" | |
| 40 | if int(streak) < 0: | |
| 41 | strk_color = f"{RED}{streak}{END}" | |
| 42 | else: | |
| 43 | strk_color = f"{GREEN}{streak}{END}" | |
| 42 | 44 | |
| 43 | if conference == "East": | |
| 44 | eastern_data.append([ | |
| 45 | f"{eastern_rank}", | |
| 46 | f"{team_name}", | |
| 47 | f"{wins}-{losses}", | |
| 48 | f"{win_pct:.3f}", | |
| 49 | f"{gb}", | |
| 50 | f"{strk_color}", | |
| 51 | f"{last_10}", | |
| 52 | f"{home_record}", | |
| 53 | f"{away_record}" | |
| 54 | ]) | |
| 55 | eastern_rank += 1 | |
| 56 | elif conference == "West": | |
| 57 | western_data.append([ | |
| 58 | f"{western_rank}", | |
| 59 | f"{team_name}", | |
| 60 | f"{wins}-{losses}", | |
| 61 | f"{win_pct:.3f}", | |
| 62 | f"{gb}", | |
| 63 | f"{strk_color}", | |
| 64 | f"{last_10}", | |
| 65 | f"{home_record}", | |
| 66 | f"{away_record}" | |
| 67 | ]) | |
| 68 | western_rank += 1 | |
| 45 | if conference == "East": | |
| 46 | eastern_data.append( | |
| 47 | [ | |
| 48 | f"{eastern_rank}", | |
| 49 | f"{team_name}", | |
| 50 | f"{wins}-{losses}", | |
| 51 | f"{win_pct:.3f}", | |
| 52 | f"{gb}", | |
| 53 | f"{strk_color}", | |
| 54 | f"{last_10}", | |
| 55 | f"{home_record}", | |
| 56 | f"{away_record}", | |
| 57 | ] | |
| 58 | ) | |
| 59 | eastern_rank += 1 | |
| 60 | elif conference == "West": | |
| 61 | western_data.append( | |
| 62 | [ | |
| 63 | f"{western_rank}", | |
| 64 | f"{team_name}", | |
| 65 | f"{wins}-{losses}", | |
| 66 | f"{win_pct:.3f}", | |
| 67 | f"{gb}", | |
| 68 | f"{strk_color}", | |
| 69 | f"{last_10}", | |
| 70 | f"{home_record}", | |
| 71 | f"{away_record}", | |
| 72 | ] | |
| 73 | ) | |
| 74 | western_rank += 1 | |
| 69 | 75 | |
| 70 | headers = ["Rank", "Team", "W-L", "PCT", "GB", "STRK", "L10", "HOME", "AWAY"] | |
| 76 | headers = ["Rank", "Team", "W-L", "PCT", "GB", "STRK", "L10", "HOME", "AWAY"] | |
| 71 | 77 | |
| 72 | print(f"{BOLD}Eastern Conference Standings:{END}") | |
| 73 | print(tabulate(eastern_data, headers=headers, tablefmt="grid")) | |
| 78 | print(f"{BOLD}Eastern Conference Standings:{END}") | |
| 79 | print(tabulate(eastern_data, headers=headers, tablefmt="grid")) | |
| 74 | 80 | |
| 75 | print("\n") | |
| 76 | print(f"{BOLD}Western Conference Standings:{END}") | |
| 77 | print(tabulate(western_data, headers=headers, tablefmt="grid")) | |
| 81 | print("\n") | |
| 82 | print(f"{BOLD}Western Conference Standings:{END}") | |
| 83 | print(tabulate(western_data, headers=headers, tablefmt="grid")) | |