diff --git a/card_creation.py b/card_creation.py index 4d517e3..4cf9ce6 100644 --- a/card_creation.py +++ b/card_creation.py @@ -92,7 +92,7 @@ async def main(argv): Process pitcher stats into raw chances """ count_pitchers = 0 - with open(f'{input_path}pitcher-stats.csv', 'r') as file: + with open(f'{input_path}pitcher-stats.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) for row in reader: @@ -1753,36 +1753,40 @@ async def main(argv): lets_go = input(f'Should I run batters (y/n)? ') if lets_go in YES: # https://www.baseball-reference.com/leagues/majors/2022-baserunning-batting.shtml - with open(f'{input_path}baserunning-data.csv', 'r') as file: + with open(f'{input_path}baserunning-data.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) for row in reader: - player = Player.get_or_none(Player.br_id == row[36]) - if player: - dupe = BatterData.delete().where( - (BatterData.player == player) & (BatterData.cardset == cardset) - ).execute() + try: + player = Player.get_or_none(Player.br_id == row[36]) + if player: + dupe = BatterData.delete().where( + (BatterData.player == player) & (BatterData.cardset == cardset) + ).execute() - stealing = b.stealing( - int(row[8]), int(row[12]), int(row[13]), int(row[14]), int(row[15]), season_pct - ) - this_data = BatterData( - player=player, - cardset=cardset, - stealing=b.stealing_line(stealing), - st_low=stealing[0], - st_high=stealing[1], - st_auto=stealing[2], - st_jump=stealing[3], - running=b.running(row[26]), - hit_and_run='C', - bunting='C' - ) - this_data.save() + stealing = b.stealing( + int(row[8]), int(row[12]), int(row[13]), int(row[14]), int(row[15]), season_pct + ) + this_data = BatterData( + player=player, + cardset=cardset, + stealing=b.stealing_line(stealing), + st_low=stealing[0], + st_high=stealing[1], + st_auto=stealing[2], + st_jump=stealing[3], + running=b.running(row[26]), + hit_and_run='C', + bunting='C' + ) + this_data.save() - else: - logging.error(f'Could not match bbref id {row[36]}') - print(f'Could not match bbref id {row[36]}') + else: + logging.error(f'Could not match bbref id {row[36]}') + print(f'Could not match bbref id {row[36]}') + except Exception as e: + logging.error(f'Failed to process runner {row[0]} ({row[2]}): {type(e)}: {e}') + print(f'Failed to process runner {row[0]} ({row[2]})') existing_columns = CardColumns.delete().where( CardColumns.id.endswith(f'-{cardset.id}') & CardColumns.b_ratings.is_null(False) @@ -1805,7 +1809,7 @@ async def main(argv): # 'vr_two_2d6', 'vr_two_results', 'vr_two_splits', # 'vr_three_2d6', 'vr_three_results', 'vr_three_splits']] - with open(f'{input_path}batter-stats.csv', 'r') as file: + with open(f'{input_path}batter-stats.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) for row in reader: @@ -3257,67 +3261,71 @@ async def main(argv): lets_go = input(f'Should I run fielders (y/n)? ') if lets_go in YES: # https://www.baseball-reference.com/leagues/majors/2022-specialpos_p-fielding.shtml - with open(f'{input_path}defense-p.csv', 'r') as file: + with open(f'{input_path}defense-p.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) for row in reader: - player = Player.get_or_none(Player.br_id == row[29]) - if player and row[15] != '' and row[12] != '': - # Build Position object and save - this_pos = Position( - player=player, - cardset=cardset, - position='P', - innings=int(float(row[8])), - range=d.range_pitcher(int(row[15]), season_pct), - error=d.error_pitcher(int(row[12]), int(row[9]), season_pct=season_pct), - ) - dupe = Position.delete().where( - (Position.player == player) & (Position.cardset == cardset) - ).execute() - 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), - starter_rating=spow, - relief_rating=rpow - ) - dupe = PitcherData.delete().where( - (PitcherData.player == player) & (PitcherData.cardset == cardset) - ).execute() - this_pit.save() - - if int(row[6]) >= int(row[5]) * .1: - sp_pos = Position( + try: + player = Player.get_or_none(Player.br_id == row[29]) + if player and row[15] != '' and row[12] != '': + # Build Position object and save + this_pos = Position( player=player, cardset=cardset, - position='SP', - innings=int(float(row[8])) * (int(row[6]) / int(row[5])), - range=69, - error=420 + position='P', + innings=int(float(row[8])), + range=d.range_pitcher(int(row[15]), season_pct), + error=d.error_pitcher(int(row[12]), int(row[9]), season_pct=season_pct), ) - sp_pos.save() - if int(row[6]) <= int(row[5]) * .8: - rp_pos = Position( + dupe = Position.delete().where( + (Position.player == player) & (Position.cardset == cardset) + ).execute() + 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, - position='RP', - innings=int(float(row[8])) * (1 - (int(row[6]) / int(row[5]))), - range=69, - error=420 + hold=d.hold_pitcher(row[27], int(row[28]), season_pct), + starter_rating=spow, + relief_rating=rpow ) - rp_pos.save() + dupe = PitcherData.delete().where( + (PitcherData.player == player) & (PitcherData.cardset == cardset) + ).execute() + this_pit.save() - # No player match - else: - logging.error(f'Could not match bbref id {row[29]}') - print(f'Could not match bbref id {row[29]}') + if int(row[6]) >= int(row[5]) * .1: + sp_pos = Position( + player=player, + cardset=cardset, + position='SP', + innings=int(float(row[8])) * (int(row[6]) / int(row[5])), + range=69, + error=420 + ) + sp_pos.save() + if int(row[6]) <= int(row[5]) * .8: + rp_pos = Position( + player=player, + cardset=cardset, + position='RP', + innings=int(float(row[8])) * (1 - (int(row[6]) / int(row[5]))), + range=69, + error=420 + ) + rp_pos.save() + + # No player match + else: + logging.error(f'Could not match bbref id {row[29]}') + print(f'Could not match bbref id {row[29]}') + 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]})') # https://www.baseball-reference.com/leagues/majors/2022-standard-pitching.shtml - with open(f'{input_path}pitcher-data.csv', 'r') as file: + with open(f'{input_path}pitcher-data.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) for row in reader: @@ -3354,7 +3362,7 @@ async def main(argv): 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') as file: + with open(f'{input_path}defense-c.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) curr_pos = 'C' @@ -3383,7 +3391,7 @@ async def main(argv): logging.error(f'Could not match bbref id {row[34]}') print(f'Could not match bbref id {row[34]}') - with open(f'{input_path}defense-1b.csv', 'r') as file: + with open(f'{input_path}defense-1b.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) curr_pos = '1B' @@ -3409,7 +3417,7 @@ async def main(argv): logging.error(f'Could not match bbref id {row[29]}') print(f'Could not match bbref id {row[29]}') - with open(f'{input_path}defense-2b.csv', 'r') as file: + with open(f'{input_path}defense-2b.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) curr_pos = '2B' @@ -3437,7 +3445,7 @@ async def main(argv): print(f'Could not match bbref id {row[29]} / player: {p_err} / row[27]: {row[25]} / ' f'row[20]: {row[18]}') - with open(f'{input_path}defense-3b.csv', 'r') as file: + with open(f'{input_path}defense-3b.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) curr_pos = '3B' @@ -3463,7 +3471,7 @@ async def main(argv): logging.error(f'Could not match bbref id {row[29]}') print(f'Could not match bbref id {row[29]}') - with open(f'{input_path}defense-ss.csv', 'r') as file: + with open(f'{input_path}defense-ss.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) curr_pos = 'SS' @@ -3489,7 +3497,7 @@ async def main(argv): logging.error(f'Could not match bbref id {row[29]}') print(f'Could not match bbref id {row[29]}') - with open(f'{input_path}defense-lf.csv', 'r') as file: + with open(f'{input_path}defense-lf.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) curr_pos = 'LF' @@ -3516,7 +3524,7 @@ async def main(argv): 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') as file: + with open(f'{input_path}defense-cf.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) curr_pos = 'CF' @@ -3543,7 +3551,7 @@ async def main(argv): 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') as file: + with open(f'{input_path}defense-rf.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) curr_pos = 'RF' @@ -3570,7 +3578,7 @@ async def main(argv): 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') as file: + with open(f'{input_path}defense-of.csv', 'r', encoding='utf8') as file: reader = csv.reader(file) for row in reader: