Skip to content

Commit

Permalink
Merge pull request #22 from alecglen/main
Browse files Browse the repository at this point in the history
Use OS-independent methods for caching paths
  • Loading branch information
cooperdff authored Feb 23, 2022
2 parents 62b6127 + d527ff7 commit 119f39a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 8 additions & 6 deletions nfl_data_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,18 @@ def import_pbp_data(years, columns=None, downcast=True, cache=False, alt_path=No
if cache is True:

if alt_path is None:
dpath = appdirs.user_cache_dir(appname, appauthor) + '\\pbp'
dpath = os.path.join(appdirs.user_cache_dir(appname, appauthor), 'pbp')
else:
dpath = alt_path

# read in pbp data
for year in years:
if cache is True:
if not os.path.isdir(dpath + '\\' + 'season='+str(year)):
raise ValueError(str(year) + ' cache file does not exist.')
for folder in [dpath + '\\' + x + '\\' for x in os.listdir(dpath) if ('season='+str(year)) in x]:
seasonStr = f'season={year}'
if not os.path.isdir(os.path.join(dpath, seasonStr)):
raise ValueError(f'{year} cache file does not exist.')
for fname in filter(lambda x: seasonStr in x, os.listdir(dpath)):
folder = os.path.join(dpath, fname)
for file in os.listdir(folder):
if file.endswith(".parquet"):
fpath = os.path.join(folder, file)
Expand Down Expand Up @@ -157,14 +159,14 @@ def cache_pbp(years, downcast=True, alt_path=None):
if len(alt_path) > 0:
path = alt_path
else:
path = appdirs.user_cache_dir(appname, appauthor) + '\\pbp'
path = os.path.join(appdirs.user_cache_dir(appname, appauthor), 'pbp')

# check if drectory exists already
if not os.path.isdir(path):
os.makedirs(path)

# delete seasons to be replaced
for folder in [path + '\\' + x + '\\' for x in os.listdir(path) for y in years if ('season='+str(y)) in x]:
for folder in [os.path.join(path, x) for x in os.listdir(path) for y in years if ('season='+str(y)) in x]:
for file in os.listdir(folder):
if file.endswith(".parquet"):
os.remove(os.path.join(folder, file))
Expand Down
2 changes: 2 additions & 0 deletions nfl_data_py/tests/nfl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,5 @@ def test_is_df(self):
class test_cache(TestCase):
def test_cache(self):
nfl.cache_pbp([2020])
s = nfl.import_pbp_data([2020], cache=True)
self.assertEqual(True, isinstance(s, pd.DataFrame))

0 comments on commit 119f39a

Please sign in to comment.