def __init__(self, game_key = None, cum_stats = {}): # conversion to GameKey from tuple allowed self.game_key = game_key if hasattr(game_key, 'to_tuple') else GameKey(key_tup=game_key) self.toi = TOI(self.game_key) """The :py:class:`.TOI` summary""" self.rosters = Rosters(self.game_key) """The :py:class:`.Rosters` summary""" self.summary = getGameSummary(self.game_key) self.face_off_comp = FaceOffComparison(self.game_key) """The :py:class:`.FaceOffComparison` summary""" self.play_by_play = PlayByPlay(self.game_key, cum_stats) """The :py:class:`.PlayByPlay` summary""" self.event_summary = EventSummary(self.game_key) """The :py:class:`.EventSummary` summary"""
def test_face_off_comparison(self): from nhlscrapi.games.game import GameKey from nhlscrapi.games.faceoffcomp import FaceOffComparison gk = GameKey(2015, 3, 224) # 2015, playoffs (3), NYR/WSH game 3 (game 224) foc = FaceOffComparison(gk) try: foc.load_all() hth = foc.head_to_head(21, 21) # laich v stepan except Exception as e: self.assertEqual(0, 1, 'Loading error: {0}'.format(e)) # stepan 2/4 overall self.assertEqual(hth['away']['all'], {'won': 2, 'total': 4}) # laich 1/1 in the defensive zone self.assertEqual(hth['home']['def'], {'won': 1, 'total': 1}) # equivalently, stepan 0/1 in offensive self.assertEqual(hth['away']['off'], {'won': 0, 'total': 1}) # face off win % rnd = {k: round(v, 2) for k, v in foc.fo_pct.items()} self.assertEqual(rnd, {'home': 0.57, 'away': 0.43}) # neut zone face off records self.assertEqual(foc.by_zone['away']['neut'], {'won': 10, 'total': 24}) self.assertEqual(foc.by_zone['home']['neut'], {'won': 14, 'total': 24}) # neut zone face off % self.assertEqual(round(foc.fo_pct_by_zone['away']['neut'], 2), 0.42) # away off/home def zone face off records self.assertEqual(foc.by_zone['away']['off'], {'won': 9, 'total': 23}) self.assertEqual(foc.by_zone['home']['def'], {'won': 14, 'total': 23})
def test_face_off_comparison(self): from nhlscrapi.games.game import GameKey from nhlscrapi.games.faceoffcomp import FaceOffComparison gk = GameKey(2015,3,224) # 2015, playoffs (3), NYR/WSH game 3 (game 224) foc = FaceOffComparison(gk) try: foc.load_all() hth = foc.head_to_head(21, 21) # laich v stepan except Exception as e: self.assertEqual(0, 1, 'Loading error: {0}'.format(e)) # stepan 2/4 overall self.assertEqual(hth['away']['all'], { 'won': 2, 'total': 4 }) # laich 1/1 in the defensive zone self.assertEqual(hth['home']['def'], { 'won': 1, 'total': 1 }) # equivalently, stepan 0/1 in offensive self.assertEqual(hth['away']['off'], { 'won': 0, 'total': 1 }) # face off win % rnd = { k: round(v, 2) for k, v in foc.fo_pct.items() } self.assertEqual(rnd, { 'home': 0.57, 'away': 0.43 }) # neut zone face off records self.assertEqual(foc.by_zone['away']['neut'], { 'won': 10, 'total': 24 }) self.assertEqual(foc.by_zone['home']['neut'], { 'won': 14, 'total': 24 }) # neut zone face off % self.assertEqual(round(foc.fo_pct_by_zone['away']['neut'],2), 0.42) # away off/home def zone face off records self.assertEqual(foc.by_zone['away']['off'], { 'won': 9, 'total': 23 }) self.assertEqual(foc.by_zone['home']['def'], { 'won': 14, 'total': 23 })
def __init__(self, game_key = None, cum_stats = {}): # conversion to GameKey from tuple allowed self.game_key = game_key if hasattr(game_key, 'to_tuple') else GameKey(key_tup=game_key) self.toi = TOI(self.game_key) """The :py:class:`.TOI` summary""" self.rosters = Rosters(self.game_key) """The :py:class:`.Rosters` summary""" #self.summary = GameSummary(game_key) self.face_off_comp = FaceOffComparison(self.game_key) """The :py:class:`.FaceOffComparison` summary""" self.play_by_play = PlayByPlay(self.game_key, cum_stats) """The :py:class:`.PlayByPlay` summary""" self.event_summary = EventSummary(self.game_key) """The :py:class:`.EventSummary` summary"""
class Game(object): """ This the primary interface to the collection of summary reports associated with every game. The supported reports include :py:class:`.PlayByPlay`, :py:class:`.TOI`, :py:class:`.Rosters`, and :py:class:`.FaceOffComparison`. Reports can be either lazy loaded at time of property calls or all loaded at once by calling ``load_all()``. :param game_key: either object :py:class:`.GameKey` or (season, game_type, game_num) tuple :param cum_stats: dict, values are of type :py:class:`.AccumulateStats` to be collected in play-by-play :Example: .. code:: python # # example: using the Game object # from nhlscrapi.games.game import GameKey, Game from nhlscrapi.games.cumstats import Corsi gk = GameKey(2015, 2, 224) g = Game(gk, { 'Corsi': Corsi() }) # since play-by-play hasn't yet been loaded the RTSS report will # be parsed and the Corsi computed for each team print(g.cum_stats['Corsi'].share()) # load the rest of the reports g.load_all() # report back the game's linesman print(g.linesman) """ def __init__(self, game_key = None, cum_stats = {}): # conversion to GameKey from tuple allowed self.game_key = game_key if hasattr(game_key, 'to_tuple') else GameKey(key_tup=game_key) self.toi = TOI(self.game_key) """The :py:class:`.TOI` summary""" self.rosters = Rosters(self.game_key) """The :py:class:`.Rosters` summary""" #self.summary = GameSummary(game_key) self.face_off_comp = FaceOffComparison(self.game_key) """The :py:class:`.FaceOffComparison` summary""" self.play_by_play = PlayByPlay(self.game_key, cum_stats) """The :py:class:`.PlayByPlay` summary""" self.event_summary = EventSummary(self.game_key) """The :py:class:`.EventSummary` summary""" def load_all(self): """ Force all reports to be loaded and parsed instead of lazy loading on demand. :returns: ``self`` or ``None`` if load fails """ try: self.toi.load_all() self.rosters.load_all() #self.summary.load_all() self.play_by_play.load_all() self.face_off_comp.load_all() return self except Exception as e: print(e) return None ######################################### ## ## convenience wrapper properties ## ######################################### @property def matchup(self): """ Return the game meta information displayed in report banners including team names, final score, game date, location, and attendance. Data format is .. code:: python { 'home': home, 'away': away, 'final': final, 'attendance': att, 'date': date, 'location': loc } :returns: matchup banner info :rtype: dict """ if self.play_by_play.matchup: return self.play_by_play.matchup elif self.rosters.matchup: return self.rosters.matchup elif self.toi.matchup: return self.toi.matchup else: self.face_off_comp.matchup # # play related # @property def plays(self): """ :returns: the plays from the game :rtype: list """ return self.play_by_play.plays #@property #def extractors(self): # return self.play_by_play.extractors @property def cum_stats(self): """ :returns: the computed cumulative stats of :py:class:`.AccumulateStats` from play-by-play :rtype: dict passed to ctor, values are type """ return self.play_by_play.compute_stats() # # personnel related # @property def home_skaters(self): """ :returns: the skaters that dressed for the home team :rtype: dict keyed by player number """ return self.rosters.home_skaters @property def home_coach(self): """ :returns: coach for the home team :rtype: string """ return self.rosters.home_coach @property def away_skaters(self): """ :returns: the skaters that dressed for the away team :rtype: dict keyed by player number """ return self.rosters.away_skaters @property def away_coach(self): """ :returns: coach for the away team :rtype: string """ return self.rosters.away_coach @property def refs(self): """ :returns: refs for the game :rtype: dict ``{ number: 'name' }`` """ return self.rosters.refs @property def linesman(self): """ :returns: the linesman for the game :rtype: dict ``{ number: 'name' }`` """ return self.rosters.linesman # # toi related # @property def home_toi(self): """ :returns: TOI shift summary for skaters on the home team :rtype: dict keyed by player number, value :py:class:`.ShiftSummary` """ return self.toi.home_shift_summ @property def away_toi(self): """ :returns: TOI shift summary for skaters on the away team :rtype: dict keyed by player number, value :py:class:`.ShiftSummary` """ return self.toi.away_shift_summ # # face off related # @property def home_fo_summ(self): """ :returns: home face off summary :rtype: dict keyed by player number """ return self.face_off_comp.home_fo @property def away_fo_summ(self): """ :returns: away face off summary :rtype: dict keyed by player number """ return self.face_off_comp.away_fo
class Game(object): """ This the primary interface to the collection of summary reports associated with every game. The supported reports include :py:class:`.PlayByPlay`, :py:class:`.TOI`, :py:class:`.Rosters`, and :py:class:`.FaceOffComparison`. Reports can be either lazy loaded at time of property calls or all loaded at once by calling ``load_all()``. :param game_key: either object :py:class:`.GameKey` or (season, game_type, game_num) tuple :param cum_stats: dict, values are of type :py:class:`.AccumulateStats` to be collected in play-by-play :Example: .. code:: python # # example: using the Game object # from nhlscrapi.games.game import GameKey, Game from nhlscrapi.games.cumstats import Corsi gk = GameKey(2015, 2, 224) g = Game(gk, { 'Corsi': Corsi() }) # since play-by-play hasn't yet been loaded the RTSS report will # be parsed and the Corsi computed for each team print(g.cum_stats['Corsi'].share()) # load the rest of the reports g.load_all() # report back the game's linesman print(g.linesman) """ def __init__(self, game_key=None, cum_stats={}): # conversion to GameKey from tuple allowed self.game_key = game_key if hasattr(game_key, 'to_tuple') else GameKey( key_tup=game_key) self.toi = TOI(self.game_key) """The :py:class:`.TOI` summary""" self.rosters = Rosters(self.game_key) """The :py:class:`.Rosters` summary""" #self.summary = GameSummary(game_key) self.face_off_comp = FaceOffComparison(self.game_key) """The :py:class:`.FaceOffComparison` summary""" self.play_by_play = PlayByPlay(self.game_key, cum_stats, game=self) """The :py:class:`.PlayByPlay` summary""" self.event_summary = EventSummary(self.game_key) """The :py:class:`.EventSummary` summary""" def load_all(self): """ Force all reports to be loaded and parsed instead of lazy loading on demand. :returns: ``self`` or ``None`` if load fails """ try: self.toi.load_all() self.rosters.load_all() #self.summary.load_all() self.play_by_play.load_all() self.face_off_comp.load_all() return self except Exception as e: print(e) return None ######################################### ## ## convenience wrapper properties ## ######################################### @property def matchup(self): """ Return the game meta information displayed in report banners including team names, final score, game date, location, and attendance. Data format is .. code:: python { 'home': home, 'away': away, 'final': final, 'attendance': att, 'date': date, 'location': loc } :returns: matchup banner info :rtype: dict """ if self.play_by_play.matchup: return self.play_by_play.matchup elif self.rosters.matchup: return self.rosters.matchup elif self.toi.matchup: return self.toi.matchup else: self.face_off_comp.matchup # # play related # @property def plays(self): """ :returns: the plays from the game :rtype: list """ return self.play_by_play.plays #@property #def extractors(self): # return self.play_by_play.extractors @property def cum_stats(self): """ :returns: the computed cumulative stats of :py:class:`.AccumulateStats` from play-by-play :rtype: dict passed to ctor, values are type """ return self.play_by_play.compute_stats() # # personnel related # @property def home_skaters(self): """ :returns: the skaters that dressed for the home team :rtype: dict keyed by player number """ return self.rosters.home_skaters @property def home_coach(self): """ :returns: coach for the home team :rtype: string """ return self.rosters.home_coach @property def away_skaters(self): """ :returns: the skaters that dressed for the away team :rtype: dict keyed by player number """ return self.rosters.away_skaters @property def away_coach(self): """ :returns: coach for the away team :rtype: string """ return self.rosters.away_coach @property def refs(self): """ :returns: refs for the game :rtype: dict ``{ number: 'name' }`` """ return self.rosters.refs @property def linesman(self): """ :returns: the linesman for the game :rtype: dict ``{ number: 'name' }`` """ return self.rosters.linesman # # toi related # @property def home_toi(self): """ :returns: TOI shift summary for skaters on the home team :rtype: dict keyed by player number, value :py:class:`.ShiftSummary` """ return self.toi.home_shift_summ @property def away_toi(self): """ :returns: TOI shift summary for skaters on the away team :rtype: dict keyed by player number, value :py:class:`.ShiftSummary` """ return self.toi.away_shift_summ # # face off related # @property def home_fo_summ(self): """ :returns: home face off summary :rtype: dict keyed by player number """ return self.face_off_comp.home_fo @property def away_fo_summ(self): """ :returns: away face off summary :rtype: dict keyed by player number """ return self.face_off_comp.away_fo