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 = 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 __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
def _build_roster(session, game_key): def _player_name_to_id(player_name): player_name = player_name.replace("'", "\\'") pls = session.query(Player).filter( Player.full_name.ilike('%{0}%'.format(player_name.lower()))).all() if len(pls) == 1: return pls[0].player_id player_name = ''.join( [c for c in player_name if c == ' ' or c.isalnum()]) first_name = player_name.split(' ')[0] last_name = ''.join(player_name.split(' ')[1:]) pl_id = '{0}{1}'.format(last_name[:min([5, len(last_name)])], first_name[:1]) pls = session.query(Player).filter( Player.player_id.ilike('%{0}%'.format(pl_id.lower()))).filter( Player.start_year <= game_key.season).filter( Player.last_year >= game_key.season).all() if len(pls): return pls[0].player_id else: return None rosters = Rosters(game_key=game_key) game_id = _scrapi_game_to_game_id(rosters) print(rosters.matchup) print('Game id: {0}'.format(game_id)) _game = session.query(Game).filter(Game.game_id == game_id).first() _game.home_coach = rosters.home_coach _game.away_coach = rosters.away_coach _rosters = [] _rosters += [ Roster(team_name=rosters.matchup['home'], jersey_num=j, game_id=game_id, player_id=_player_name_to_id(rosters.home_skaters[j]['name']), position=rosters.home_skaters[j]['position']) for j in rosters.home_skaters ] _rosters += [ Roster(team_name=rosters.matchup['away'], jersey_num=j, game_id=game_id, player_id=_player_name_to_id(rosters.away_skaters[j]['name']), position=rosters.away_skaters[j]['position']) for j in rosters.away_skaters ] _scratches = [] _scratches += [ Scratch(team_name=rosters.matchup['home'], jersey_num=j, game_id=game_id, player_id=_player_name_to_id( rosters.home_scratches[j]['name']), position=rosters.home_scratches[j]['position']) for j in rosters.home_scratches ] _scratches += [ Scratch(team_name=rosters.matchup['away'], jersey_noum=j, game_id=game_id, player_id=_player_name_to_id( rosters.away_scratches[j]['name']), position=rosters.away_scratches[j]['position']) for j in rosters.away_scratches ] session.add(_game) session.add_all(_rosters) session.add_all(_scratches)