-
Notifications
You must be signed in to change notification settings - Fork 396
fixed lahman.py; added test_lahman.py #449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mlinenweber
wants to merge
2
commits into
jldbc:master
Choose a base branch
from
mlinenweber:fix_lahman
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,136 +1,161 @@ | ||
| from datetime import timedelta | ||
| from io import BytesIO | ||
| from os import makedirs | ||
| from os import path | ||
| from typing import Optional | ||
| from zipfile import ZipFile | ||
|
|
||
| from bs4 import BeautifulSoup | ||
| import pandas as pd | ||
| from pathlib import Path | ||
| from py7zr import SevenZipFile | ||
| import requests | ||
| from requests_cache import CachedSession | ||
|
|
||
| from . import cache | ||
|
|
||
| url = "https://github.com/chadwickbureau/baseballdatabank/archive/master.zip" | ||
| base_string = "baseballdatabank-master" | ||
|
|
||
| _handle = None | ||
|
|
||
| def get_lahman_zip() -> Optional[ZipFile]: | ||
| # Retrieve the Lahman database zip file, returns None if file already exists in cwd. | ||
| # If we already have the zip file, keep re-using that. | ||
| # Making this a function since everything else will be re-using these lines | ||
| global _handle | ||
| if path.exists(path.join(cache.config.cache_directory, base_string)): | ||
| _handle = None | ||
| elif not _handle: | ||
| s = requests.get(url, stream=True) | ||
| _handle = ZipFile(BytesIO(s.content)) | ||
| return _handle | ||
|
|
||
| def download_lahman(): | ||
| # download entire lahman db to present working directory | ||
| z = get_lahman_zip() | ||
| if z is not None: | ||
| z.extractall(cache.config.cache_directory) | ||
| z = get_lahman_zip() | ||
| # this way we'll now start using the extracted zip directory | ||
| # instead of the session ZipFile object | ||
|
|
||
| def _get_file(tablename: str, quotechar: str = "'") -> pd.DataFrame: | ||
| z = get_lahman_zip() | ||
| f = f'{base_string}/{tablename}' | ||
| # NB: response will be cached for 30 days unless force is True | ||
| def _get_response(force:bool=False) -> requests.Response: | ||
| session = _get_session() | ||
| response = session.get("http://seanlahman.com", refresh=force) | ||
| return response | ||
|
|
||
| # For example, "https://www.dropbox.com/scl/fi/hy0sxw6gaai7ghemrshi8/lahman_1871-2023_csv.7z?rlkey=edw1u63zzxg48gvpcmr3qpnhz&dl=1" | ||
| def _get_download_url(force:bool=False) -> str: | ||
| response = _get_response(force) | ||
| soup = BeautifulSoup(response.content, "html.parser") | ||
|
|
||
| anchor = soup.find("a", string="Comma-delimited version") | ||
| url = anchor["href"].replace("dl=0", "dl=1") | ||
|
|
||
| return url | ||
|
|
||
| def _get_cache_dir() -> str: | ||
| return f"{cache.config.cache_directory}/lahman" | ||
|
|
||
| def _get_session() -> CachedSession: | ||
| return CachedSession(_get_cache_dir(), expire_after=timedelta(days=30)) | ||
|
|
||
| def _get_base_string() -> str: | ||
| url = _get_download_url() | ||
| path = Path(url) | ||
|
|
||
| return path.stem | ||
|
|
||
| def _get_file_path(filename: str = "") -> str: | ||
| base_string = _get_base_string() | ||
| return path.join(_get_cache_dir(), base_string, filename) | ||
|
|
||
| def _get_table(filename: str, | ||
| quotechar: str = "'", | ||
| encoding=None, | ||
| on_bad_lines="error") -> pd.DataFrame: | ||
| filepath = _get_file_path(filename) | ||
| data = pd.read_csv( | ||
| f"{path.join(cache.config.cache_directory, f)}" if z is None else z.open(f), | ||
| filepath, | ||
| header=0, | ||
| sep=',', | ||
| quotechar=quotechar | ||
| sep=",", | ||
| quotechar=quotechar, | ||
| encoding=encoding, | ||
| on_bad_lines=on_bad_lines, | ||
| ) | ||
| return data | ||
|
|
||
| # Return whether download happened (True) or if cache used (False) | ||
| def download_lahman(force: bool = False) -> bool: | ||
| if force or not path.exists(_get_file_path()): | ||
| cache_dir = _get_cache_dir() | ||
| base_string = _get_base_string() | ||
| makedirs(f"{cache_dir}/{base_string}", exist_ok=True) | ||
|
|
||
| # do this for every table in the lahman db so they can exist as separate functions | ||
| def parks() -> pd.DataFrame: | ||
| return _get_file('core/Parks.csv') | ||
| url = _get_download_url(force) | ||
| stream = requests.get(url, stream=True) | ||
| with SevenZipFile(BytesIO(stream.content)) as zip: | ||
| zip.extractall(cache_dir) | ||
| return True | ||
| return False | ||
|
|
||
| # do this for every table in the lahman db so they can exist as separate functions | ||
| def all_star_full() -> pd.DataFrame: | ||
| return _get_file("core/AllstarFull.csv") | ||
| return _get_table("AllstarFull.csv") | ||
|
|
||
| def appearances() -> pd.DataFrame: | ||
| return _get_file("core/Appearances.csv") | ||
| return _get_table("Appearances.csv") | ||
|
|
||
| def awards_managers() -> pd.DataFrame: | ||
| return _get_file("contrib/AwardsManagers.csv") | ||
| return _get_table("AwardsManagers.csv") | ||
|
|
||
| def awards_players() -> pd.DataFrame: | ||
| return _get_file("contrib/AwardsPlayers.csv") | ||
| return _get_table("AwardsPlayers.csv") | ||
|
|
||
| def awards_share_managers() -> pd.DataFrame: | ||
| return _get_file("contrib/AwardsShareManagers.csv") | ||
| return _get_table("AwardsShareManagers.csv") | ||
|
|
||
| def awards_share_players() -> pd.DataFrame: | ||
| return _get_file("contrib/AwardsSharePlayers.csv") | ||
| return _get_table("AwardsSharePlayers.csv") | ||
|
|
||
| def batting() -> pd.DataFrame: | ||
| return _get_file("core/Batting.csv") | ||
| return _get_table("Batting.csv") | ||
|
|
||
| def batting_post() -> pd.DataFrame: | ||
| return _get_file("core/BattingPost.csv") | ||
| return _get_table("BattingPost.csv") | ||
|
|
||
| def college_playing() -> pd.DataFrame: | ||
| return _get_file("contrib/CollegePlaying.csv") | ||
| return _get_table("CollegePlaying.csv") | ||
|
|
||
| def fielding() -> pd.DataFrame: | ||
| return _get_file("core/Fielding.csv") | ||
| return _get_table("Fielding.csv") | ||
|
|
||
| def fielding_of() -> pd.DataFrame: | ||
| return _get_file("core/FieldingOF.csv") | ||
| return _get_table("FieldingOF.csv") | ||
|
|
||
| def fielding_of_split() -> pd.DataFrame: | ||
| return _get_file("core/FieldingOFsplit.csv") | ||
| return _get_table("FieldingOFsplit.csv") | ||
|
|
||
| def fielding_post() -> pd.DataFrame: | ||
| return _get_file("core/FieldingPost.csv") | ||
| return _get_table("FieldingPost.csv") | ||
|
|
||
| def hall_of_fame() -> pd.DataFrame: | ||
| return _get_file("contrib/HallOfFame.csv") | ||
| return _get_table("HallOfFame.csv") | ||
|
|
||
| def home_games() -> pd.DataFrame: | ||
| return _get_file("core/HomeGames.csv") | ||
| return _get_table("HomeGames.csv") | ||
|
|
||
| def managers() -> pd.DataFrame: | ||
| return _get_file("core/Managers.csv") | ||
| return _get_table("Managers.csv") | ||
|
|
||
| def managers_half() -> pd.DataFrame: | ||
| return _get_file("core/ManagersHalf.csv") | ||
| return _get_table("ManagersHalf.csv") | ||
|
|
||
| def master() -> pd.DataFrame: | ||
| # Alias for people -- the new name for master | ||
| return people() | ||
|
|
||
| def parks() -> pd.DataFrame: | ||
| return _get_table("Parks.csv", encoding="unicode_escape") | ||
|
|
||
| def people() -> pd.DataFrame: | ||
| return _get_file("core/People.csv") | ||
| return _get_table("People.csv", encoding="unicode_escape") | ||
|
|
||
| def pitching() -> pd.DataFrame: | ||
| return _get_file("core/Pitching.csv") | ||
| return _get_table("Pitching.csv") | ||
|
|
||
| def pitching_post() -> pd.DataFrame: | ||
| return _get_file("core/PitchingPost.csv") | ||
| return _get_table("PitchingPost.csv") | ||
|
|
||
| def salaries() -> pd.DataFrame: | ||
| return _get_file("contrib/Salaries.csv") | ||
| return _get_table("Salaries.csv") | ||
|
|
||
| def schools() -> pd.DataFrame: | ||
| return _get_file("contrib/Schools.csv", quotechar='"') # different here bc of doublequotes used in some school names | ||
| # NB: one line is bad; "brklyncuny" should use double quotes, but doesn't | ||
| return _get_table("Schools.csv", quotechar='"', on_bad_lines="skip") | ||
|
|
||
| def series_post() -> pd.DataFrame: | ||
| return _get_file("core/SeriesPost.csv") | ||
| return _get_table("SeriesPost.csv") | ||
|
|
||
| def teams_core() -> pd.DataFrame: | ||
| return _get_file("core/Teams.csv") | ||
|
|
||
| def teams_upstream() -> pd.DataFrame: | ||
| return _get_file("upstream/Teams.csv") # manually maintained file | ||
| return _get_table("Teams.csv") | ||
|
|
||
| def teams_franchises() -> pd.DataFrame: | ||
| return _get_file("core/TeamsFranchises.csv") | ||
| return _get_table("TeamsFranchises.csv") | ||
|
|
||
| def teams_half() -> pd.DataFrame: | ||
| return _get_file("core/TeamsHalf.csv") | ||
| return _get_table("TeamsHalf.csv") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mlinenweber Can you explain these 2 lines and what _get_download_url() is supposed to return?
I get the below error when trying to run
people()--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[24], line 31 28 soup = BeautifulSoup(response.content, "html.parser") 30 anchor = soup.find("a", string="Comma-delimited version") ---> 31 url = anchor["href"].replace("dl=0", "dl=1") TypeError: 'NoneType' object is not subscriptableIs
soup.find("a", string="Comma-delimited version")supposed to be None?