Moving older scripts into holding cell

This commit is contained in:
Cal Corum 2024-10-17 09:28:02 -05:00
parent f0f77ffb16
commit 639e032586
26 changed files with 1312 additions and 3 deletions

1
.gitignore vendored
View File

@ -132,3 +132,4 @@ dmypy.json
*.db
.idea
theshow/
.vscode/

View File

@ -2,7 +2,7 @@ import csv
import datetime
import logging
import math
from decimal import Decimal
from decimal import ROUND_HALF_EVEN, Decimal
import pandas as pd
import pybaseball as pb
@ -860,10 +860,38 @@ def defense_rg(all_pos: list) -> list:
def sanitize_chance_output(total_chances, min_chances=1.0, rounding=0.05):
if total_chances < min_chances:
logging.debug(f'sanitize: {total_chances} is less than min_chances ({min_chances}); returning 0')
return 0
rounded_decimal = round(Decimal(str(total_chances)) / Decimal(str(rounding))) * Decimal(str(rounding))
exact_chances = [
Decimal('1.05'), Decimal('1.1'), Decimal('1.2'), Decimal('1.25'), Decimal('1.3'), Decimal('1.35'),
Decimal('1.4'), Decimal('1.5'), Decimal('1.6'), Decimal('1.65'), Decimal('1.7'), Decimal('1.75'),
Decimal('1.8'), Decimal('1.9'), Decimal('1.95'), Decimal('2.1'), Decimal('2.2'), Decimal('2.25'),
Decimal('2.4'), Decimal('2.5'), Decimal('2.55'), Decimal('2.6'), Decimal('2.7'), Decimal('2.75'),
Decimal('2.8'), Decimal('2.85'), Decimal('3.2'), Decimal('3.25'), Decimal('3.3'), Decimal('3.4'),
Decimal('3.5'), Decimal('3.6'), Decimal('3.75'), Decimal('3.8'), Decimal('3.9'), Decimal('4.2'),
Decimal('4.25'), Decimal('4.5'), Decimal('4.75'), Decimal('4.8'), Decimal('5.1'), Decimal('5.4'),
Decimal('5.7')
]
if rounded_decimal > exact_chances[-1]:
return float(rounded_decimal)
for x in exact_chances:
if rounded_decimal <= x:
return float(x)
raise ArithmeticError(f'Attempt to sanitize {total_chances} rounded to {rounded_decimal} and could not be matched to an exact result')
def legacy_sanitize_chance_output(total_chances: float, min_chances: float = 1.0, rounding: float = 0.05):
# r_val = mround(total_chances) if total_chances >= min_chances else 0
r_val = Decimal(total_chances) if total_chances >= min_chances else Decimal(0)
logging.debug(f'r_val: {r_val}')
rounded_val = Decimal(float(math.floor(r_val / Decimal(rounding)) * Decimal(rounding))).quantize(Decimal("0.05"))
rounded_val = Decimal(float(math.floor(r_val / Decimal(rounding)) * Decimal(rounding))).quantize(Decimal("0.05"), ROUND_HALF_EVEN)
if math.floor(rounded_val) == rounded_val:
return rounded_val
@ -885,6 +913,7 @@ def sanitize_chance_output(total_chances, min_chances=1.0, rounding=0.05):
return x
def mlbteam_and_franchise(mlbam_playerid):
api_url = f'https://statsapi.mlb.com/api/v1/people/{mlbam_playerid}?hydrate=currentTeam'
logging.info(f'Calling {api_url}')

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@ from typing import Literal, Optional
AUTH_TOKEN = {'Authorization': f'Bearer Tp3aO3jhYve5NJF1IqOmJTmk'}
DB_URL = 'https://pd.manticorum.com/api'
master_debug = True
alt_database = False
alt_database = 'dev'
if alt_database == 'dev':
DB_URL = 'https://pddev.manticorum.com/api'

97
pybaseball_doodling.py Normal file
View File

@ -0,0 +1,97 @@
import asyncio
import requests
import sys
import time
from bs4 import BeautifulSoup
import pandas as pd
import pybaseball as pb
from app.creation_helpers import get_pitching_peripherals
def pull_stats(start_date_string: str, end_date_string: str):
print(f'Pulling stats from {start_date_string} to {end_date_string}')
data = pb.batting_stats_range(start_date_string, end_date_string)
print(f'Got it!')
return data
def batting_game_log_table(bbref_id: str, season: int = 1998):
page_url = f'https://www.baseball-reference.com/players/gl.fcgi?id={bbref_id}&t=b&year={season}'
soup = BeautifulSoup(requests.get(page_url).text, 'html.parser')
time.sleep(3)
table = soup.find('table', {'id': 'batting_gamelogs'})
tbody = table.find('tbody')
headers = []
data = []
indeces = []
for row in tbody.find_all('tr'):
row_data = []
col_names = []
for cell in row.find_all('td'):
row_data.append(cell.text)
if len(headers) == 0:
col_names.append(cell['data-stat'])
if len(row_data) > 0:
data.append(row_data)
indeces.append(row_data[0])
if len(headers) == 0:
headers.extend(col_names)
player_frame = pd.DataFrame(data, index=indeces, columns=headers)
return player_frame
def batting_splits_table(bbref_id: str, season: int = 1998):
page_url = f'https://www.baseball-reference.com/players/split.fcgi?id={bbref_id}&year={season}&t=b'
soup = BeautifulSoup(requests.get(page_url).text, 'lxml')
time.sleep(3)
table = soup.find('table', {'id': 'plato'})
tbody = table.find('tbody')
headers = []
data = []
indeces = []
for row in tbody.findAll('tr'):
row_data = []
col_names = []
for cell in row.find_all('td'):
row_data.append(cell.text)
if len(headers) == 0:
col_names.append(cell['data-stat'])
if len(row_data) > 0:
data.append(row_data)
indeces.append(row_data[0])
if len(headers) == 0:
headers.extend(col_names)
bsplit_frame = pd.DataFrame(data, index=indeces, columns=headers)
return bsplit_frame
async def main(args):
# print('Fetching peripherals')
# peripherals = get_pitching_peripherals(2021)
# print('Got them!')
# print(f'There are {len(peripherals)} records')
# month_1 = pull_stats('1997-03-01', '1997-04-30')
# print(f'Received {len(month_1)} records.')
# print(f'Grabbing game logs...')
# player_frame = batting_game_log_table('jeterde01')
# print(f'Received {len(player_frame)} records')
print(f'Grabbing game logs...')
player_frame = batting_splits_table('jeterde01')
print(f'Received {len(player_frame)} records')
pass
if __name__ == '__main__':
asyncio.run(main(sys.argv[1:]))

4
pytest.ini Normal file
View File

@ -0,0 +1,4 @@
[pytest]
asyncio_mode = auto
filterwarnings =
ignore::DeprecationWarning

51
requirements.txt Normal file
View File

@ -0,0 +1,51 @@
aiohappyeyeballs==2.4.3
aiohttp==3.10.10
aiosignal==1.3.1
annotated-types==0.7.0
attrs==24.2.0
beautifulsoup4==4.12.3
bs4==0.0.2
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.4.0
contourpy==1.3.0
cryptography==43.0.1
cycler==0.12.1
Deprecated==1.2.14
fonttools==4.54.1
frozenlist==1.4.1
idna==3.10
iniconfig==2.0.0
kiwisolver==1.4.7
lxml==5.3.0
matplotlib==3.9.2
multidict==6.1.0
numpy==2.1.2
packaging==24.1
pandas==2.2.3
pillow==11.0.0
pluggy==1.5.0
propcache==0.2.0
pyarrow==17.0.0
pybaseball==2.2.7
pycparser==2.22
pydantic==2.9.2
pydantic_core==2.23.4
PyGithub==2.4.0
PyJWT==2.9.0
PyNaCl==1.5.0
pyparsing==3.2.0
pytest==8.3.3
pytest-asyncio==0.24.0
python-dateutil==2.9.0.post0
pytz==2024.2
requests==2.32.3
scipy==1.14.1
six==1.16.0
soupsieve==2.6
tqdm==4.66.5
typing_extensions==4.12.2
tzdata==2024.2
urllib3==2.2.3
wrapt==1.16.0
yarl==1.15.4

View File

@ -0,0 +1,68 @@
from decimal import ROUND_HALF_EVEN, Decimal
import math
from batters.calcs_batter import bp_singles, wh_singles, sanitize_chance_output
from creation_helpers import sanitize_chance_output, mround
def test_sanitize():
# def my_round_decimal(i: float):
# return Decimal(i).quantize(Decimal('0.05'), ROUND_HALF_EVEN)
# def my_round(num: float, to: float = 0.05):
# num, to = Decimal(str(num)), Decimal(str(to))
# return float(round(num / to) * to)
# assert my_round(6) == 6
# assert my_round(5.96) == 5.95
# assert my_round(5.84) == 5.85
# assert my_round(3.123) == 3.1
# assert math.floor(my_round(6)) == 6
# assert math.floor(my_round(5.96)) == 5
assert sanitize_chance_output(6) == 6.0
assert sanitize_chance_output(1.21) == 1.2
assert sanitize_chance_output(4.77) == 4.75
assert sanitize_chance_output(4.78) == 4.8
# step_1 = Decimal(6) / Decimal(0.05)
# step_1_5 = round(step_1, )
# step_2 = round(step_1)
# step_3 = float(step_2 * Decimal(0.05))
# step_4 = Decimal(step_3)
# assert Decimal(6) == Decimal(6).quantize(Decimal('0.05'), ROUND_HALF_EVEN)
# assert round(step_1) == 120
# # assert step_1 == 120
# assert step_1_5 == 120
# assert step_2 == 120
# assert step_3 == 6.0
# assert step_4 == Decimal('6.0')
# rounded_val = step_4.quantize(Decimal("0.05"), ROUND_HALF_EVEN)
# assert rounded_val == 6
# assert sanitize_chance_output(6) == 6
def test_mround():
assert mround(6) == 6.0
assert mround(1.21) == 1.2
assert mround(4.77) == 4.75
assert mround(4.78) == 4.8
def test_decimals():
assert Decimal(8) == 8
def test_bp_singles():
assert bp_singles(23) == 5
assert bp_singles(6) == 5
assert bp_singles(3) == 0
def test_wh_singles():
assert wh_singles(11, .36) == 3.75
assert wh_singles(12, .45) == Decimal('7.95')

7
tests/test_helpers.py Normal file
View File

@ -0,0 +1,7 @@
from creation_helpers import pd_positions_df
def test_positions_df():
cardset_19_pos = pd_positions_df(19)
assert True == True