Skip to content

Commit

Permalink
Use objects qualified name in caching (#269)
Browse files Browse the repository at this point in the history
* validate cache CI step

* adds script

* move cache dectorator to concrete class

- this lets the class name be part of the function signature

* qualname if not local
  • Loading branch information
bdilday committed May 22, 2022
1 parent 9c09390 commit 1ee4585
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
- name: Timing test
timeout-minutes: 2
run: python -m scripts.statcast_timing
- name: Cache test
run: make validate-cache
- name: Run MyPy
run: make mypy ONLY_MODIFIED=0
continue-on-error: true
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ mypy:
test:
pytest $(TEST_RUN_AGAINST) $(TEST_FLAGS) --doctest-modules --cov=pybaseball --cov-report term-missing

validate-cache: install
python ./scripts/validate_cache.py

# The test-github-actions is here to allow any local developer to test the GitHub actions on their code
# before pushing and creating a PR. Just install act from https://github.com/nektos/act and run
# make test-github-actions
Expand Down
5 changes: 4 additions & 1 deletion pybaseball/cache/func_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ def get_func_name(func: Callable) -> str:
if '__self__' in dir(func):
# This is a class method
return f"{func.__getattribute__('__self__').__class__.__name__}.{func.__name__}"
# it's a method of an instantiated object
elif "__qualname__" in dir(func) and r'<locals>' not in func.__qualname__:
return func.__qualname__

return f"{func.__name__}"
return func.__name__
13 changes: 12 additions & 1 deletion pybaseball/datasources/fangraphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def _sort(self, data: pd.DataFrame, columns: List[str], ascending: bool = True)
def _validate(self, data: pd.DataFrame) -> pd.DataFrame:
return data

@cache.df_cache()
def fetch(self, start_season: int, end_season: Optional[int] = None, league: str = 'ALL', ind: int = 1,
stat_columns: Union[str, List[str]] = 'ALL', qual: Optional[int] = None, split_seasons: bool = True,
month: str = 'ALL', on_active_roster: bool = False, minimum_age: int = MIN_AGE,
Expand Down Expand Up @@ -172,6 +171,10 @@ class FangraphsBattingStatsTable(FangraphsDataTable):
ROW_ID_FUNC: RowIdFunction = player_row_id_func
ROW_ID_NAME = 'IDfg'

@cache.df_cache()
def fetch(self, *args, **kwargs):
return super().fetch(*args, **kwargs)

def _postprocess(self, data: pd.DataFrame) -> pd.DataFrame:
return self._sort(data, ["WAR", "OPS"], ascending=False)

Expand All @@ -182,6 +185,10 @@ class FangraphsFieldingStatsTable(FangraphsDataTable):
ROW_ID_FUNC: RowIdFunction = player_row_id_func
ROW_ID_NAME = 'IDfg'

@cache.df_cache()
def fetch(self, *args, **kwargs):
return super().fetch(*args, **kwargs)

def _postprocess(self, data: pd.DataFrame) -> pd.DataFrame:
return self._sort(data, ["DEF"], ascending=False)

Expand All @@ -191,6 +198,10 @@ class FangraphsPitchingStatsTable(FangraphsDataTable):
ROW_ID_FUNC: RowIdFunction = player_row_id_func
ROW_ID_NAME = 'IDfg'

@cache.df_cache()
def fetch(self, *args, **kwargs):
return super().fetch(*args, **kwargs)

def _postprocess(self, data: pd.DataFrame) -> pd.DataFrame:
if "WAR" in data.columns:
new_position = min(7, len(data.columns) - 1)
Expand Down
14 changes: 14 additions & 0 deletions scripts/validate_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import sys
import pybaseball
import pandas as pd

if __name__ == "__main__":
season = 2020
pybaseball.cache.enable()
batting = pybaseball.batting_stats(season)
pitching = pybaseball.pitching_stats(season)
columns_same = list(batting.columns) == list(pitching.columns)
shape_same = batting.shape == pitching.shape
cache_failed = columns_same and shape_same
sys.exit(int(cache_failed))

0 comments on commit 1ee4585

Please sign in to comment.