Exemplo n.º 1
0
 def test_row_type(self):
     # Successful
     self.assertEqual(row_type("End of Overtime 38 35 0 0"), 6)
     self.assertEqual(
         row_type("Quarter Time Down ToGo Location Detail RAV DEN EPB EPA"),
         -1)
     self.assertEqual(
         row_type("""OT 15:00 1 10 DEN 34R Player passes but there are
                 dinosaurs on the field! 35 35 3.31 3.04"""), 0)
     self.assertEqual(row_type("Overtime"), 5)
     self.assertEqual(row_type("1st Quarter"), 1)
     self.assertEqual(row_type("2nd Quarter"), 2)
     self.assertEqual(row_type("3rd Quarter"), 3)
     self.assertEqual(row_type("4th Quarter"), 4)
 def test_row_type(self):
     # Successful
     self.assertEqual(row_type("End of Overtime 38 35 0 0"), 6)
     self.assertEqual(
             row_type(
                 "Quarter Time Down ToGo Location Detail RAV DEN EPB EPA"
                 ),
             -1
             )
     self.assertEqual(
             row_type("""OT 15:00 1 10 DEN 34R Player passes but there are
                 dinosaurs on the field! 35 35 3.31 3.04"""),
             0
             )
     self.assertEqual(row_type("Overtime"), 5)
     self.assertEqual(row_type("1st Quarter"), 1)
     self.assertEqual(row_type("2nd Quarter"), 2)
     self.assertEqual(row_type("3rd Quarter"), 3)
     self.assertEqual(row_type("4th Quarter"), 4)
Exemplo n.º 3
0
    def __parse_play(self):
        """ Set up the team stats dictionaries and add it to self.json """
        soup = self.soup
        # Find each row of the table
        rows = soup.find_all("tr")
        for row in rows:
            # Deal with the different row types
            r_type = row_type(row.get_text(' ', strip=True))
            # When setting the quarter, we need a special case to handle
            # overtimes after the first
            if r_type == 5 and self.last_play_info["quarter"] >= 5:
                self.current_play_info["quarter"] = self.last_play_info["quarter"] + 1
            # Skip all other special rows
            if r_type != 0:  # 0 indicates a normal row
                self.last_play_info = deepcopy(self.current_play_info)
                continue

            # Set the play class, which is used to indicate scores, turnovers,
            # and penalties
            self.__set_class(row)

            # Find each column of the table
            cols = row.find_all("td")
            if cols:  # This if removes the header
                pbp_dict = {}
                # Set the number of the play first, so that if a play fails to
                # parse, we have a gap in the numbering to help us detect it
                self.current_play_info["number"] = self.last_play_info["number"] + 1
                pbp_dict["number"] = self.current_play_info["number"]

                # Extract the plain text description and store it, because it
                # is used so often
                description = cols[5].get_text(' ', strip=True).replace('\n', ' ')
                # Sanitize replay challenges that they only the final result is used
                sanitized_description = remove_challenge(description)
                self.current_play_info["description"] = sanitized_description

                # Assign offense
                # We correct the 1999, 2013 kick offs when setting is_pchange
                if self.is_pchange:
                    # For change of possession, we change the team with the
                    # ball
                    if self.last_play_info["offense"] == "home":
                        self.current_play_info["offense"] = "away"
                    else:
                        self.current_play_info["offense"] = "home"
                else:
                    self.current_play_info["offense"] = self.last_play_info["offense"]

                # Set current score
                pbp_dict["score"] = self.__set_score(cols)

                # Check the type of play
                pbp_dict["play"] = self.__set_play(cols)

                # Sometimes there are blank plays
                if pbp_dict["play"]["type"] is None:
                    self.last_play_info = deepcopy(self.current_play_info)
                    continue

                # On a kickoff, we make sure we have the team right
                if pbp_dict["play"]["type"] in {"kick off", "onside kick"}:
                    kick_text = cols[4].get_text(' ', strip=True).replace('\n', ' ')
                    kick_team = get_kicking_offense(
                            kick_text,
                            self.current_play_info["description"],
                            self.home,
                            self.away,
                            self.home_players,
                            self.away_players
                            )
                    if kick_team in ["home", "away"]:
                        self.current_play_info["offense"] = kick_team
                    else:  # Assume the last team with the ball is kicking
                        flipped_team = self.__flip(self.last_play_info["offense"])
                        self.current_play_info["offense"] = flipped_team

                # Set penalty
                if self.is_penalty:
                    pbp_dict["penalty"] = self.__set_penalty()

                # Parse state
                pbp_dict["state"] = self.__set_state(cols)
                turnovers = self.__set_turnover()
                if turnovers:
                    pbp_dict["turnovers"] = turnovers
                self.json.append(pbp_dict)

                # Set last play info to current play info
                self.last_play_info = deepcopy(self.current_play_info)