def test_TwoPenaltiesHasTwoPenaltyElements(self): self.mitigation.addPenalty( Penalty("asdf", 1, "asdf", Remediation("myId", "do this"), "asdf")) self.mitigation.addPenalty( Penalty("asdf", 1, "asdf", Remediation("myId", "do this"), "asdf")) self.assertEqual( len(self.mitigation.toElement().findall("./*/penalty")), 2)
def setUp(self): self.penaltyName = "name" self.value = 1 self.reasons = "I have my reasons" self.penaltyId = "penaltyId" self.remediationText = "This is how you do better" self.remediation = Remediation(self.penaltyId, self.remediationText) self.penalty = Penalty(self.penaltyName, self.value, self.reasons, self.remediation, self.penaltyId)
def get_penalties(pbp_data): plays = pbp_data["liveData"]["plays"]["allPlays"] penalties_json = [play for play in plays if play["result"] and play["result"]["eventTypeId"] == "PENALTY"] penalties = [] for penalty_json in penalties_json: players = penalty_json["players"] penalty_on = [player["player"]["fullName"] for player in players if player["playerType"] == "PenaltyOn"][0] # the nhl remains unaware of the past perfect drawn_by_array = [player["player"]["fullName"] for player in players if player["playerType"] == "DrewBy"] # bench minors, unsportsmanlike conduct, etc. don't have a drawn by so we gotta be careful drawn_by = drawn_by_array[0] if drawn_by_array else None result = penalty_json["result"] about = penalty_json["about"] penalty = Penalty( penalty_on, drawn_by, result["secondaryType"], result["penaltySeverity"], result["penaltyMinutes"], penalty_json["team"]["name"], about["period"], about["periodTime"] ) penalties.append(penalty) return penalties
class Test_PenaltyToElement(unittest.TestCase): def setUp(self): self.penaltyName = "name" self.value = 1 self.reasons = "I have my reasons" self.penaltyId = "penaltyId" self.remediationText = "This is how you do better" self.remediation = Remediation(self.penaltyId, self.remediationText) self.penalty = Penalty(self.penaltyName, self.value, self.reasons, self.remediation, self.penaltyId) def tearDown(self): pass def test_PenaltyCreatesPenaltyTag(self): xml = self.penalty.toElement() self.assertEqual(xml.tag, "penalty") def test_PenaltyCreatesCorrectAttributesWithTag(self): element = self.penalty.toElement() self.assertEqual(element.attrib["name"], self.penaltyName) self.assertEqual(element.attrib["value"], str(self.value)) self.assertEqual(element.attrib["id"], self.penaltyId) def test_PenaltyCreatesReasonChildElement(self): element = self.penalty.toElement() reasonChild = element.find("reason") self.assertIsNotNone(reasonChild) self.assertEqual(reasonChild.tag, "reason") self.assertEqual(reasonChild.text, self.reasons) def test_PenaltyCreatesRemediationChildElement(self): element = self.penalty.toElement() remediationChild = element.find("remediation") self.assertIsNotNone(remediationChild) self.assertEqual(remediationChild.tag, "remediation") self.assertEqual(remediationChild.text, self.remediation.getText()) def test_PenaltyToElementAsAChild(self): parent = ET.Element("top") self.penalty.toElement(parent=parent) self.assertIsNotNone(parent.find("penalty"))
def test_PenaltyInvalidValueGreaterThan100(self): self.assertFalse(Penalty.isValidPenaltyValue(100.01))
def test_PenaltyValue100IsValid(self): self.assertTrue(Penalty.isValidPenaltyValue(100))
def test_PenaltyCreationWithInvalidNumericalValueThrowsError(self): with self.assertRaises(InvalidPenaltyValueError): Penalty(None, -1, None, None, None)
def test_PenaltyCreationReturnsPenaltyType(self): penalty = Penalty("name", 1, "reason", "remediation", "penaltyId") self.assertIsNotNone(penalty) self.assertIsInstance(penalty, Penalty)
def test_scoreShouldOnlyReturnOneDecimalPlace(self): self.mitigation.addPenalty(Penalty(None, 12, None, None, None)) self.assertEqual(self.mitigation.getScore(), 8.9)
def scan_game_page(self, game_page): soup = BeautifulSoup(game_page, 'html.parser') scoring = soup.find(id='all_scoring') penalty = soup.find(id='all_penalty') period_start = 0 # Scan through game information section # Get link, date, away, home link = soup.find('link', {'rel': 'canonical'}).get('href') date = soup.find('div', {'class': 'scorebox_meta'}).find_all('div') date = date[0].get_text().split(',') date = (date[0] + ', ' + date[1]) # home and visitor done in steps to hopefully make it more readable visitor = soup.find(id='inner_nav').find('section').find_all('p') visitor = visitor[1].find('a').get('href') visitor = visitor.split('/') visitor = visitor[2] home = soup.find(id='inner_nav').find('section').find_all('p') home = home[2].find('a').get('href') home = home.split('/') home = home[2] #print(link + ' ' + date + ' ' + visitor + ' @ ' + home) # Create game game = Game(link, date, visitor, home) print(game) # Scan through the scoring section print('Goals') scoring_rows = scoring.find_all('tr') for row in scoring_rows: cells = row.find_all(['td', 'th']) # If row is a period header or not if (cells[0].name == 'th'): period_start = self.find_period_start(cells[0].get_text()) print('period starts: ' + str(period_start)) # Skip shootout data if period_start == -2: print("Skipping Shootout") break # Error check period_start if period_start == -1: print("error with period") exit() # else it should be a 'td' and be a goal summary # time | team | PP/SH | Goal Scorer | Assits else: time = cells[0].get_text() seconds = self.convert_score_clock_to_seconds( time) + period_start team = cells[1].get_text() type = '' if 'PP' in cells[2].get_text(): type = 'PP' elif 'SH' in cells[2].get_text(): type = 'SH' player = cells[3].get_text().split('(')[0].strip('\n') goal_row = (str(seconds) + ', ' + team + ', ' + type + ', ' + player) print(goal_row) game.goals.append(Goal(int(seconds), team, player, type)) # Scan through the penalty section print('Penalties') period_start = 0 penalty_rows = penalty.find_all('tr') for row in penalty_rows: cells = row.find_all(['td', 'th']) # If row is a period header or not if (cells[0].name == 'th'): period_start = self.find_period_start(cells[0].get_text()) print('period starts: ' + str(period_start)) # Error check period_start if period_start == -1: print("error with period") exit() # else it should be a 'td' and be a goal summary # time | team | player | infraction | duration else: time = cells[0].get_text() seconds = self.convert_score_clock_to_seconds( time) + period_start team = cells[1].get_text() player = cells[2].get_text() infraction = cells[3].get_text() if cells[4].get_text().endswith('min'): duration = int(cells[4].get_text().split(' ')[0]) * 60 else: duration = 0 print( str(seconds) + ', ' + team + ', ' + player + ', ' + infraction + ', ' + str(duration)) game.penalties.append( Penalty(int(seconds), team, player, infraction, duration)) #print(game) return game
def test_PenaltyWithNonIntegerParameter(self): with self.assertRaises(InvalidPenaltyValueError): Penalty("name", "value", "reason", "remediation", "penaltyId")
def test_penaltyHasTwoReasonsAfterOneIsAdded(self): penalty = Penalty("name", 1, "reason", "remediation", "penaltyId") penalty.addReason("reason2") self.assertEqual(len(penalty.reasons), 2)
def test_penaltyHasOneReasonAfterCreation(self): penalty = Penalty("name", 1, "reason", "remediation", "penaltyId") self.assertEqual(len(penalty.reasons), 1)
def set_penalization(self, params): self.actions = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1]]) # penalty_game self.action_space = gym.spaces.Discrete(n=len(self.actions)) self.penalization = Penalty(penalty=params.penalty) self.penalization.actions = self.actions
def test_PenaltyInvalidValueEqualToZero(self): self.assertFalse(Penalty.isValidPenaltyValue(0))
def test_PenaltyInvalidValueLTEZero(self): self.assertFalse(Penalty.isValidPenaltyValue(-1))
def test_MitigationAddPenaltySizeIsNowOne(self): self.mitigation.addPenalty(Penalty("asdf", 1, "asdf", None, "asdf")) self.assertEqual(len(self.mitigation.penalties), 1)
def test_reasonIsOfTypeSet(self): penalty = Penalty("name", 1, "reason", "remediation", "penaltyId") self.assertIsInstance(penalty.reasons, set)
def test_scoreWithOneHundredPercentPenaltiesIsOne(self): self.mitigation.addPenalty(Penalty(None, 100, None, None, None)) self.assertEqual(self.mitigation.getScore(), 1.0)