Add year checks

Defense 2013 and beyond adds three columns
This commit is contained in:
Cal Corum 2023-03-04 17:31:36 -06:00
parent 3a45ce6b89
commit 2bda40e8ef

View File

@ -3266,7 +3266,15 @@ async def main(argv):
for row in reader:
try:
player = Player.get_or_none(Player.br_id == row[29])
if int(cardset_name[:4]) > 2012:
br_id_col = 29
cs_col = 27
pick_col = 28
else:
br_id_col = 26
cs_col = 24
pick_col = 25
player = Player.get_or_none(Player.br_id == row[br_id_col])
if player and row[15] != '' and row[12] != '':
# Build Position object and save
this_pos = Position(
@ -3283,10 +3291,11 @@ async def main(argv):
this_pos.save()
spow, rpow = d.pow_ratings(d.innings_float(row[8]), int(row[6]), int(row[5]))
this_pit = PitcherData(
player=player,
cardset=cardset,
hold=d.hold_pitcher(row[27], int(row[28]), season_pct),
hold=d.hold_pitcher(row[cs_col], int(row[pick_col]), season_pct),
starter_rating=spow,
relief_rating=rpow
)
@ -3318,11 +3327,9 @@ async def main(argv):
# No player match
else:
logging.error(f'Could not match bbref id {row[29]}')
print(f'Could not match bbref id {row[29]}')
logging.error(f'Could not match bbref id {row[br_id_col]}')
except Exception as e:
logging.error(f'Failed to process fielder {row[0]} ({row[2]}): {type(e)}: {e}')
print(f'Failed to process fielder {row[0]} ({row[2]})')
logging.error(f'Failed to process fielder {row[1]}: {type(e)}: {e}')
# https://www.baseball-reference.com/leagues/majors/2022-standard-pitching.shtml
with open(f'{input_path}pitcher-data.csv', 'r', encoding='utf8') as file:
@ -3334,12 +3341,13 @@ async def main(argv):
all_data = PitcherData.select().where(
(PitcherData.player == player) & (PitcherData.cardset == cardset)
).limit(1)
this_data = None
try:
this_data = all_data[0]
except Exception as e:
logging.error(f'Could not find existing PitcherData for {player.name}')
if this_data:
if this_data is not None:
closer_rating = p.closer_rating(int(row[11]), int(row[14]), int(row[9]))
this_data.balk = p.balks(int(row[24]), int(float(row[15])), season_pct)
this_data.wild_pitch = p.wild_pitches(int(row[25]), int(float(row[15])), season_pct)
@ -3360,14 +3368,17 @@ async def main(argv):
else:
logging.error(f'Could not match bbref id {row[35]}')
print(f'Could not match bbref id {row[35]}')
with open(f'{input_path}defense-c.csv', 'r', encoding='utf8') as file:
reader = csv.reader(file)
curr_pos = 'C'
for row in reader:
player = Player.get_or_none(Player.br_id == row[34])
if int(cardset_name[:4]) > 2012:
br_id_col = 34
else:
br_id_col = 31
player = Player.get_or_none(Player.br_id == row[br_id_col])
if player:
# Build Position object and save
this_pos = Position(
@ -3377,8 +3388,8 @@ async def main(argv):
innings=int(float(row[8])),
range=d.range_catcher(int(row[17]), season_pct),
error=d.error_catcher(int(row[12]), int(row[9]), season_pct),
arm=d.arm_catcher(row[33], int(row[25]), season_pct),
pb=d.pb_catcher(int(row[29]), int(float(row[8])), season_pct),
arm=d.arm_catcher(row[30], int(row[22]), season_pct),
pb=d.pb_catcher(int(row[26]), int(float(row[8])), season_pct),
overthrow=d.ot_catcher(int(row[12]), int(row[9]), season_pct)
)
dupe = Position.delete().where(
@ -3388,15 +3399,19 @@ async def main(argv):
# No player match
else:
logging.error(f'Could not match bbref id {row[34]}')
print(f'Could not match bbref id {row[34]}')
logging.error(f'Could not match bbref id {row[br_id_col]}')
print(f'Could not match bbref id {row[br_id_col]}')
with open(f'{input_path}defense-1b.csv', 'r', encoding='utf8') as file:
reader = csv.reader(file)
curr_pos = '1B'
for row in reader:
player = Player.get_or_none(Player.br_id == row[29])
if int(cardset_name[:4]) > 2012:
br_id_col = 29
else:
br_id_col = 26
player = Player.get_or_none(Player.br_id == row[br_id_col])
if player and row[24] != '' and row[18] != '':
# Build Position object and save
this_pos = Position(
@ -3404,7 +3419,7 @@ async def main(argv):
cardset=cardset,
position=curr_pos,
innings=int(float(row[8])),
range=d.range_first_base(int(row[24]), int(row[18]), season_pct),
range=d.range_first_base(int(row[19]), int(row[18]), season_pct),
error=d.error_first_base(int(row[12]), int(row[9]), season_pct),
)
dupe = Position.delete().where(
@ -3414,15 +3429,18 @@ async def main(argv):
# No player match
else:
logging.error(f'Could not match bbref id {row[29]}')
print(f'Could not match bbref id {row[29]}')
logging.error(f'Could not match bbref id {row[br_id_col]}')
with open(f'{input_path}defense-2b.csv', 'r', encoding='utf8') as file:
reader = csv.reader(file)
curr_pos = '2B'
for row in reader:
player = Player.get_or_none(Player.br_id == row[29])
if int(cardset_name[:4]) > 2012:
br_id_col = 29
else:
br_id_col = 26
player = Player.get_or_none(Player.br_id == row[br_id_col])
if player and row[25] != '' and row[18] != '':
# Build Position object and save
this_pos = Position(
@ -3430,7 +3448,7 @@ async def main(argv):
cardset=cardset,
position=curr_pos,
innings=int(float(row[8])),
range=d.range_second_base(int(row[25]), int(row[18]), season_pct),
range=d.range_second_base(int(row[19]), int(row[18]), season_pct),
error=d.error_second_base(int(row[12]), int(row[9]), season_pct),
)
dupe = Position.delete().where(
@ -3441,16 +3459,18 @@ async def main(argv):
# No player match
else:
p_err = f'{player.name if player else "player not found"}'
logging.error(f'Could not match bbref id {row[29]}')
print(f'Could not match bbref id {row[29]} / player: {p_err} / row[27]: {row[25]} / '
f'row[20]: {row[18]}')
logging.error(f'Could not match bbref id {row[br_id_col]}')
with open(f'{input_path}defense-3b.csv', 'r', encoding='utf8') as file:
reader = csv.reader(file)
curr_pos = '3B'
for row in reader:
player = Player.get_or_none(Player.br_id == row[29])
if int(cardset_name[:4]) > 2012:
br_id_col = 29
else:
br_id_col = 26
player = Player.get_or_none(Player.br_id == row[br_id_col])
if player and row[24] != '' and row[18] != '':
# Build Position object and save
this_pos = Position(
@ -3458,7 +3478,7 @@ async def main(argv):
cardset=cardset,
position=curr_pos,
innings=int(float(row[8])),
range=d.range_third_base(int(row[24]), int(row[18]), season_pct),
range=d.range_third_base(int(row[19]), int(row[18]), season_pct),
error=d.error_third_base(int(row[12]), int(row[9]), season_pct),
)
dupe = Position.delete().where(
@ -3468,15 +3488,18 @@ async def main(argv):
# No player match
else:
logging.error(f'Could not match bbref id {row[29]}')
print(f'Could not match bbref id {row[29]}')
logging.error(f'Could not match bbref id {row[br_id_col]}')
with open(f'{input_path}defense-ss.csv', 'r', encoding='utf8') as file:
reader = csv.reader(file)
curr_pos = 'SS'
for row in reader:
player = Player.get_or_none(Player.br_id == row[29])
if int(cardset_name[:4]) > 2012:
br_id_col = 29
else:
br_id_col = 26
player = Player.get_or_none(Player.br_id == row[br_id_col])
if player and row[25] != '' and row[18] != '':
# Build Position object and save
this_pos = Position(
@ -3484,7 +3507,7 @@ async def main(argv):
cardset=cardset,
position=curr_pos,
innings=int(float(row[8])),
range=d.range_shortstop(int(row[25]), int(row[18]), season_pct),
range=d.range_shortstop(int(row[19]), int(row[18]), season_pct),
error=d.error_shortstop(int(row[12]), int(row[9]), season_pct),
)
dupe = Position.delete().where(
@ -3494,8 +3517,7 @@ async def main(argv):
# No player match
else:
logging.error(f'Could not match bbref id {row[29]}')
print(f'Could not match bbref id {row[29]}')
logging.error(f'Could not match bbref id {row[br_id_col]}')
with open(f'{input_path}defense-lf.csv', 'r', encoding='utf8') as file:
reader = csv.reader(file)
@ -3522,7 +3544,6 @@ async def main(argv):
# No player match
else:
logging.error(f'Could not match bbref id {row[26]}')
print(f'Could not match bbref id {row[26]}')
with open(f'{input_path}defense-cf.csv', 'r', encoding='utf8') as file:
reader = csv.reader(file)
@ -3549,7 +3570,6 @@ async def main(argv):
# No player match
else:
logging.error(f'Could not match bbref id {row[26]}')
print(f'Could not match bbref id {row[26]}')
with open(f'{input_path}defense-rf.csv', 'r', encoding='utf8') as file:
reader = csv.reader(file)
@ -3576,7 +3596,6 @@ async def main(argv):
# No player match
else:
logging.error(f'Could not match bbref id {row[26]}')
print(f'Could not match bbref id {row[26]}')
with open(f'{input_path}defense-of.csv', 'r', encoding='utf8') as file:
reader = csv.reader(file)
@ -3606,7 +3625,6 @@ async def main(argv):
# No player match
else:
logging.error(f'Could not match bbref id {row[26]}')
print(f'Could not match bbref id {row[26]}')
if __name__ == '__main__':
asyncio.run(main(sys.argv[1:]))