Skip to content

Commit

Permalink
Fix: ShootCatchQuery - Filter now properly serializes L or R values.
Browse files Browse the repository at this point in the history
- Fixes test for this change.
- Refactors goalie default sorts into init.
  • Loading branch information
coreyjs committed Nov 8, 2024
1 parent 6a21a36 commit 18c1251
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ permissions:
contents: read

jobs:
test:
test-ruff-black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ filters = [
ShootCatchesQuery(shoot_catch="L"),
HomeRoadQuery(home_road="H"),
FranchiseQuery(franchise_id="1"),
StatusQuery(is_active=True) #for active players OR for HOF players StatusQuery(is_hall_of_fame=True),
StatusQuery(is_active=True),#for active players OR for HOF players StatusQuery(is_hall_of_fame=True),
OpponentQuery(opponent_franchise_id="2"),
ExperienceQuery(is_rookie=True), # for rookies || ExperienceQuery(is_rookie=False) #for veteran
DecisionQuery(decision="W") # OR DecisionQuery(decision="L") OR DecisionQuery(decision="O")
Expand Down
60 changes: 59 additions & 1 deletion nhlpy/api/query/filters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Union
from typing import Union, List


class QueryBase(ABC):
Expand All @@ -10,3 +10,61 @@ def to_query(self) -> str:
@abstractmethod
def validate(self) -> Union[bool, None]:
return True


def _goalie_stats_sorts(report: str) -> List[dict]:
"""
This is default criteria for sorting on goalie stats. I hate this method
:param report:
:return:
"""
if report == "summary":
return [
{"property": "wins", "direction": "DESC"},
{"property": "gamesPlayed", "direction": "ASC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "advanced":
return [
{"property": "qualityStart", "direction": "DESC"},
{"property": "goalsAgainstAverage", "direction": "ASC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "bios":
return [
{"property": "lastName", "direction": "ASC_CI"},
{"property": "goalieFullName", "direction": "ASC_CI"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "daysrest":
return [
{"property": "wins", "direction": "DESC"},
{"property": "savePct", "direction": "DESC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "penaltyShots":
return [
{"property": "penaltyShotsSaves", "direction": "DESC"},
{"property": "penaltyShotSavePct", "direction": "DESC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "savesByStrength":
return [
{"property": "wins", "direction": "DESC"},
{"property": "savePct", "direction": "DESC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "shootout":
return [
{"property": "shootoutWins", "direction": "DESC"},
{"property": "shootoutSavePct", "direction": "DESC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "startedVsRelieved":
return [
{"property": "gamesStarted", "direction": "DESC"},
{"property": "gamesStartedSavePct", "direction": "DESC"},
{"property": "playerId", "direction": "ASC"},
]
else:
return [{}]
2 changes: 1 addition & 1 deletion nhlpy/api/query/filters/shoot_catch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, shoot_catch: str):
self.shoot_catch_q = "shootsCatches"

def to_query(self) -> str:
return f"{self.shoot_catch_q}={self.shoot_catch}"
return f"{self.shoot_catch_q}='{self.shoot_catch}'"

def validate(self) -> Union[bool, None]:
return True
61 changes: 3 additions & 58 deletions nhlpy/api/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import List

from nhlpy.api.query.builder import QueryContext
from nhlpy.api.query.filters import _goalie_stats_sorts
from nhlpy.api.query.sorting.sorting_options import SortingOptions
from nhlpy.http_client import HttpClient

Expand All @@ -11,63 +12,6 @@ class Stats:
def __init__(self, http_client: HttpClient):
self.client = http_client

def _goalie_stats_sorts(self, report: str) -> List[dict]:
"""
This is default criteria for sorting on goalie stats. I hate this method. Ill fix it soon.
:param report:
:return:
"""
if report == "summary":
return [
{"property": "wins", "direction": "DESC"},
{"property": "gamesPlayed", "direction": "ASC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "advanced":
return [
{"property": "qualityStart", "direction": "DESC"},
{"property": "goalsAgainstAverage", "direction": "ASC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "bios":
return [
{"property": "lastName", "direction": "ASC_CI"},
{"property": "goalieFullName", "direction": "ASC_CI"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "daysrest":
return [
{"property": "wins", "direction": "DESC"},
{"property": "savePct", "direction": "DESC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "penaltyShots":
return [
{"property": "penaltyShotsSaves", "direction": "DESC"},
{"property": "penaltyShotSavePct", "direction": "DESC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "savesByStrength":
return [
{"property": "wins", "direction": "DESC"},
{"property": "savePct", "direction": "DESC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "shootout":
return [
{"property": "shootoutWins", "direction": "DESC"},
{"property": "shootoutSavePct", "direction": "DESC"},
{"property": "playerId", "direction": "ASC"},
]
elif report == "startedVsRelieved":
return [
{"property": "gamesStarted", "direction": "DESC"},
{"property": "gamesStartedSavePct", "direction": "DESC"},
{"property": "playerId", "direction": "ASC"},
]
else:
return [{}]

def club_stats_season(self, team_abbr: str) -> dict:
"""
This seems to return gameTypes for every season the team was in existence. Maybe its useful?
Expand Down Expand Up @@ -317,7 +261,8 @@ def goalie_stats_summary_simple(
end_season = start_season

if not sort_expr:
sort_expr = self._goalie_stats_sorts(stats_type)
sort_expr = _goalie_stats_sorts(report=stats_type)

q_params["sort"] = urllib.parse.quote(json.dumps(sort_expr))

if not default_cayenne_exp:
Expand Down
4 changes: 2 additions & 2 deletions tests/query/filters/test_shoot_catch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

def test_shoot_catch_l():
shoot_catch = ShootCatchesQuery(shoot_catch="L")
assert shoot_catch.to_query() == "shootsCatches=L"
assert shoot_catch.to_query() == "shootsCatches='L'"


def test_shoot_catch_r():
shoot_catch = ShootCatchesQuery(shoot_catch="R")
assert shoot_catch.to_query() == "shootsCatches=R"
assert shoot_catch.to_query() == "shootsCatches='R'"

0 comments on commit 18c1251

Please sign in to comment.