def test_toi_shift_summary(self): from nhlscrapi.games.game import GameKey from nhlscrapi.games.toi import TOI toi = TOI(game_key=GameKey(2015,3,224)) # 2015, playoffs (3), NYR/WSH game 3 (game 224) try: toi.load_all() except Exception as e: self.assertEqual(0, 1, 'Loading error: {0}'.format(e)) def get_most(shift_d, ex): return sorted( ((pn, sh) for pn, sh in shift_d.items() if pn not in ex), key=lambda k: -(k[1].game_summ['toi']['min']*60+k[1].game_summ['toi']['sec']) )[0] # players with most TOI excluding goalies # for home team, exclude number 70: Holty most_home_num, most_home_summ = get_most(toi.home_shift_summ, [70]) # for away, exclude number 30: Lundqvist most_away_num, most_away_summ = get_most(toi.away_shift_summ, [30]) # niskanen played 23:19 self.assertEqual(most_home_num, 2) self.assertEqual(most_home_summ.game_summ['toi'], { 'min': 23, 'sec': 19 }) # mcdonagh played 23:52 self.assertEqual(most_away_num, 27) self.assertEqual(most_away_summ.game_summ['toi'], { 'min': 23, 'sec': 52 })
def __init__(self, game_key = None, extractors = {}, cum_stats = {}): self.game_key = game_key self.toi = TOI(game_key) self.rosters = Rosters(game_key) #self.summary = GameSummary(game_key) self.play_by_play = PlayByPlay(game_key, extractors, cum_stats)
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 __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): # add constructor argument for json source def __init__(self, game_key = None, extractors = {}, cum_stats = {}): self.game_key = game_key self.toi = TOI(game_key) self.rosters = Rosters(game_key) #self.summary = GameSummary(game_key) self.play_by_play = PlayByPlay(game_key, extractors, cum_stats) def load_all(self): """Force all reports to be loaded and parsed instead of lazy loading on demand.""" try: self.toi.load_all() self.rosters.load_all() self.summary.load_all() self.play_by_play.load_all() return True except: return False ######################################### ## ## convenience wrapper properties ## ######################################### @property def matchup(self): if self.play_by_play.matchup: return self.play_by_play.matchup elif self.rosters.matchup: return self.rosters.matchup else: return self.toi.matchup # # play related # @property def plays(self): return self.play_by_play.plays() @property def extractors(self): return self.play_by_play.extractors @property def cum_stats(self): self.play_by_play.compute_stats() return self.play_by_play.cum_stats # # personnel related # @property def home_skaters(self): return self.rosters.home_skaters() @property def home_coach(self): return self.rosters.home_coach() @property def away_skaters(self): return self.rosters.away_skaters() @property def away_coach(self): return self.rosters.away_coach() @property def refs(self): return self.rosters.refs() @property def linesman(self): return self.rosters.linesman() # # toi related # @property def home_toi(self): return self.toi.home_shift_summ() @property def away_toi(self): return self.toi.away_shift_summ()
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