def test_remove_challenge(self):
     self.__set_sanitizer_consts()
     # Successful
     self.assertEqual(
             remove_challenge(self.challenges[0]),
             self.results[0]
             )
     self.assertEqual(
             remove_challenge(self.challenges[1]),
             self.results[1]
             )
     self.assertEqual(
             remove_challenge(self.challenges[2]),
             self.results[2]
             )
示例#2
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)