Пример #1
0
 def __parse_team_stats(self):
     """ Set up the team stats dictionaries and add it to self.json """
     soup = self.soups["team_stats"]
     # Find each row of the table
     rows = soup.find_all("tr")
     home_dict = self.json["team stats"]["home"]
     away_dict = self.json["team stats"]["away"]
     for row in rows:
         # Find each column of the table
         cols = row.find_all("td")
         if cols:  # This if removes the header
             # Extract the key and both the home and away team values
             key = cols[0].get_text(strip=True)
             tmp_away = cols[1].get_text(strip=True)
             tmp_home = cols[2].get_text(strip=True)
             if key == "First downs":
                 away_dict["first downs"] = int(tmp_away)
                 home_dict["first downs"] = int(tmp_home)
             elif key == "Rush-yards-TDs":
                 away_dict["rush"] = convert_rush_info(tmp_away)
                 home_dict["rush"] = convert_rush_info(tmp_home)
             elif key == "Comp-Att-Yd-TD-INT":
                 away_dict["pass"] = convert_pass_info(tmp_away)
                 home_dict["pass"] = convert_pass_info(tmp_home)
             elif key == "Sacked-yards":
                 away_dict["sacks"] = convert_sack_info(tmp_away)
                 home_dict["sacks"] = convert_sack_info(tmp_home)
             elif key == "Fumbles-lost":
                 away_dict["fumbles"] = convert_fumble_info(tmp_away)
                 home_dict["fumbles"] = convert_fumble_info(tmp_home)
             elif key == "Penalties-yards":
                 away_dict["penalties"] = convert_penalty_info(tmp_away)
                 home_dict["penalties"] = convert_penalty_info(tmp_home)
 def test_convert_rush_info(self):
     # Successful
     self.assertEqual(convert_rush_info("16-34-0"),
             {"plays": 16, "yards": 34, "touchdowns": 0})
     # Failure returns None
     self.assertEqual(convert_rush_info("16-34 0"), None)
     self.assertEqual(convert_rush_info("14-5-C"), None)
 def test_convert_rush_info(self):
     # Successful
     self.assertEqual(convert_rush_info("16-34-0"),
             {"plays": 16, "yards": 34, "touchdowns": 0}
             )
     self.assertEqual(convert_rush_info("16--34-0"),
             {"plays": 16, "yards": -34, "touchdowns": 0}
             )
     # Failure raises ValueError
     self.assertRaises(ValueError, convert_rush_info, "16-34 0")
     self.assertRaises(ValueError, convert_rush_info, "14-5-C")