def create_previous_game_pitched(self) -> float: """1試合前に投げたイニングを取得する Returns: float: 1試合前に投げたイニングを返す ex)7.2 Notes: 練習試合、または登板していない場合は0を返す """ competition_choices = c.competition_choices_to_dict() # GamesとPitcherResultsは必ず存在するのでチェックしない if Games.objects.latest( 'pk').competition_type == competition_choices['練習試合']: return 0 previous_game = Games.objects.filter( competition_type__gt=competition_choices['練習試合']).latest('pk') pr = PitcherResults.objects.select_related( 'player_id', 'game_id').filter(game_id=previous_game, player_id=self.player_id) if pr.exists(): pr_latest = pr.latest('pk') return p.innings_conversion_for_display( pr_latest.innings_pitched, pr_latest.innings_pitched_fraction) else: return 0
def test_innings_conversion_for_display(self): self.assertEqual(cps.innings_conversion_for_display(50, 0), 50.0) self.assertEqual(cps.innings_conversion_for_display(10, 1), 10.1) self.assertEqual(cps.innings_conversion_for_display(0, 10), 3.1) self.assertEqual(cps.innings_conversion_for_display(0, 9), 3.0) self.assertEqual(cps.innings_conversion_for_display(5, 6), 7.0) self.assertEqual(cps.innings_conversion_for_display(5, 7), 7.1)
def create_pitcher_total_results( self, pitcher_results: PitcherResults) -> PitcherTotalResults: """集計したPitcherResultsをもとに更新用のPitcherTotalResultsを作る Args: pitcher_results (PitcherResults): 集計したPitcherResults Returns: PitcherTotalResults: 計算した指標を代入したPitcherTotalResults Notes: セイバーメトリクスはここで計算する """ pitcher_total_results = PitcherTotalResults.objects.select_related( 'player').get(player_id=self.player_id) pitcher_total_results.games = pitcher_results['pk__count'] pitcher_total_results.games_started = pitcher_results['games_started'] pitcher_total_results.number_of_pitch = pitcher_results[ 'number_of_pitch__sum'] pitcher_total_results.total_batters_faced = pitcher_results[ 'total_batters_faced__sum'] pitcher_total_results.hit = pitcher_results['hit__sum'] pitcher_total_results.strike_out = pitcher_results['strike_out__sum'] pitcher_total_results.bb_hbp = pitcher_results['bb_hbp__sum'] pitcher_total_results.run = pitcher_results['run__sum'] pitcher_total_results.earned_run = pitcher_results['earned_run__sum'] pitcher_total_results.wild_pitch = pitcher_results['wild_pitch__sum'] pitcher_total_results.home_run = pitcher_results['home_run__sum'] pitcher_total_results.previous_game_pitched = pitcher_results[ 'previous_game_pitched'] sum_innings_pitched = p.innings_conversion_for_calculate( pitcher_results['innings_pitched__sum'], pitcher_results['innings_pitched_fraction__sum']) pitcher_total_results.innings_pitched = p.innings_conversion_for_display( pitcher_results['innings_pitched__sum'], pitcher_results['innings_pitched_fraction__sum']) pitcher_total_results.fip = p.fielding_independent_pitching( sum_innings_pitched, pitcher_results['home_run__sum'], pitcher_results['bb_hbp__sum'], pitcher_results['strike_out__sum']) pitcher_total_results.era = p.earned_runs_average( sum_innings_pitched, pitcher_results['earned_run__sum']) pitcher_total_results.ura = p.runs_average(sum_innings_pitched, pitcher_results['run__sum']) pitcher_total_results.whip = p.walks_plus_hits_per_inning_pitched( sum_innings_pitched, pitcher_results['hit__sum'], pitcher_results['bb_hbp__sum']) pitcher_total_results.k_bbhp = p.strike_out_per_bbhp( pitcher_results['bb_hbp__sum'], pitcher_results['strike_out__sum']) pitcher_total_results.k_9 = p.strike_out_per_game( sum_innings_pitched, pitcher_results['strike_out__sum']) pitcher_total_results.k_percent = p.strike_out_percentage( pitcher_results['total_batters_faced__sum'], pitcher_results['strike_out__sum']) pitcher_total_results.bbhp_9 = p.bbhp_per_game( sum_innings_pitched, pitcher_results['bb_hbp__sum']) pitcher_total_results.p_bbhp_percent = p.bbhp_percentage( pitcher_results['total_batters_faced__sum'], pitcher_results['bb_hbp__sum']) pitcher_total_results.h_9 = p.hit_per_game(sum_innings_pitched, pitcher_results['hit__sum']) pitcher_total_results.h_percent = p.hit_percentage( pitcher_results['total_batters_faced__sum'], pitcher_results['hit__sum']) pitcher_total_results.hr_9 = p.home_run_per_game( sum_innings_pitched, pitcher_results['home_run__sum']) pitcher_total_results.hr_percent = p.home_run_percentage( pitcher_results['total_batters_faced__sum'], pitcher_results['home_run__sum']) pitcher_total_results.lob_percent = p.left_on_base_percentage( pitcher_results['hit__sum'], pitcher_results['bb_hbp__sum'], pitcher_results['home_run__sum'], pitcher_results['run__sum']) pitcher_total_results.p_ip = p.pitch_per_inning( sum_innings_pitched, pitcher_results['number_of_pitch__sum']) return pitcher_total_results