Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions pybaseball/playerid_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import io
import os
import re
import time
import zipfile

from typing import List, Tuple, Iterable
Expand Down Expand Up @@ -40,12 +41,19 @@ def _extract_people_table(zip_archive: zipfile.ZipFile) -> pd.DataFrame:
return pd.concat(dfs, axis=0)


def _is_register_stale(path):
age = time.time() - os.path.getmtime(path)
max_age = cache.config.default_expiration * 86400 # days -> seconds
return age > max_age


@cache.df_cache()
def chadwick_register(save: bool = False) -> pd.DataFrame:
''' Get the Chadwick register Database '''

if os.path.exists(get_register_file()):
table = pd.read_csv(get_register_file())
path = get_register_file()
if os.path.exists(path) and not _is_register_stale(path):
table = pd.read_csv(path)
return table

print('Gathering player lookup table. This may take a moment.')
Expand All @@ -67,7 +75,7 @@ def chadwick_register(save: bool = False) -> pd.DataFrame:
table = table[cols_to_keep]

if save:
table.to_csv(get_register_file(), index=False)
table.to_csv(path, index=False)

return table

Expand Down Expand Up @@ -136,10 +144,9 @@ def search(self, last: str, first: str = None, fuzzy: bool = False, ignore_accen
# If no matches, return 5 closest names
if len(results) == 0 and fuzzy:
print("No identically matched names found! Returning the 5 most similar names.")
results=get_closest_names(last=last, first=first, player_table=self.table)

return results
results = get_closest_names(last=last, first=first, player_table=self.table)

return results

def search_list(self, player_list: List[Tuple[str, str]]) -> pd.DataFrame:
'''
Expand All @@ -150,14 +157,13 @@ def search_list(self, player_list: List[Tuple[str, str]]) -> pd.DataFrame:

Returns:
pd.DataFrame: DataFrame of playerIDs, name, years played
'''
'''
results = pd.DataFrame()

for last, first in player_list:
results = results.append(self.search(last, first), ignore_index=True)

return results

return results

def reverse_lookup(self, player_ids: List[str], key_type: str = 'mlbam') -> pd.DataFrame:
"""Retrieve a table of player information given a list of player ids
Expand Down Expand Up @@ -193,6 +199,7 @@ def _get_client() -> _PlayerSearchClient:
_client = _PlayerSearchClient()
return _client


def playerid_lookup(last: str, first: str = None, fuzzy: bool = False, ignore_accents: bool = False) -> pd.DataFrame:
"""Lookup playerIDs (MLB AM, bbref, retrosheet, FG) for a given player

Expand All @@ -208,6 +215,7 @@ def playerid_lookup(last: str, first: str = None, fuzzy: bool = False, ignore_ac
client = _get_client()
return client.search(last, first, fuzzy, ignore_accents)


def player_search_list(player_list: List[Tuple[str, str]]) -> pd.DataFrame:
'''
Lookup playerIDs (MLB AM, bbref, retrosheet, FG) for a list of players.
Expand All @@ -217,10 +225,11 @@ def player_search_list(player_list: List[Tuple[str, str]]) -> pd.DataFrame:

Returns:
pd.DataFrame: DataFrame of playerIDs, name, years played
'''
'''
client = _get_client()
return client.search_list(player_list)


def playerid_reverse_lookup(player_ids: List[str], key_type: str = 'mlbam') -> pd.DataFrame:
"""Retrieve a table of player information given a list of player ids

Expand All @@ -234,6 +243,7 @@ def playerid_reverse_lookup(player_ids: List[str], key_type: str = 'mlbam') -> p
client = _get_client()
return client.reverse_lookup(player_ids, key_type)


def normalize_accents(s: str) -> str:
"""Removes accented letters from a string

Expand All @@ -243,4 +253,4 @@ def normalize_accents(s: str) -> str:
Returns:
str: string with accented letters normalized
"""
return ''.join(c for c in unicodedata.normalize('NFD', str(s)) if unicodedata.category(c) != 'Mn')
return ''.join(c for c in unicodedata.normalize('NFD', str(s)) if unicodedata.category(c) != 'Mn')
36 changes: 36 additions & 0 deletions tests/pybaseball/test_playerid_lookup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import sys
import time
import tempfile
from pathlib import Path

import pytest

from pybaseball.playerid_lookup import chadwick_register

pil = sys.modules["pybaseball.playerid_lookup"]


def test_chadwick_register_refreshes_when_stale(monkeypatch):
monkeypatch.setattr(pil.cache.config, "enabled", False)
with tempfile.TemporaryDirectory() as td:
fake_register = Path(td) / "chadwick-register.csv"
fake_register.write_text("name_last,name_first,key_mlbam\nstale,player,1\n")

monkeypatch.setattr(pil, "get_register_file", lambda: str(fake_register))

old = time.time() - 100 * 86400
os.utime(fake_register, (old, old))

calls = {"n": 0}

def fake_get(*args, **kwargs):
calls["n"] += 1
raise RuntimeError("network hit")

monkeypatch.setattr(pil.requests, "get", fake_get)

with pytest.raises(RuntimeError, match="network hit"):
chadwick_register()

assert calls["n"] == 1