Пример #1
0
    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
 def test_bbhp_per_game(self):
     self.assertAlmostEqual(cps.bbhp_per_game(536.0, 56), 2.82, 2)
     self.assertEqual(cps.bbhp_per_game(1, 0), 0)
     self.assertEqual(cps.bbhp_per_game(0, 1), 0)