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_pass_info(self): # Successful self.assertEqual(convert_pass_info("18-34-331-3-0"), {"plays": 34, "yards": 331, "touchdowns": 3, "interceptions": 0, "successful": 18} ) self.assertEqual(convert_pass_info("18-34--331-3-0"), {"plays": 34, "yards": -331, "touchdowns": 3, "interceptions": 0, "successful": 18} ) # Failure raises ValueError self.assertRaises(ValueError, convert_pass_info, "18 34-331-3-0") self.assertRaises(ValueError, convert_pass_info, "2-1-1-1-E")
def test_convert_pass_info(self): # Successful self.assertEqual( convert_pass_info("18-34-331-3-0"), { "plays": 34, "yards": 331, "touchdowns": 3, "interceptions": 0, "successful": 18 }) self.assertEqual( convert_pass_info("18-34--331-3-0"), { "plays": 34, "yards": -331, "touchdowns": 3, "interceptions": 0, "successful": 18 }) # Failure raises ValueError self.assertRaises(ValueError, convert_pass_info, "18 34-331-3-0") self.assertRaises(ValueError, convert_pass_info, "2-1-1-1-E")