Skip to content

Instantly share code, notes, and snippets.

@eihli
Created September 28, 2014 21:13
Show Gist options
  • Save eihli/aa791f7136d9fd85ea74 to your computer and use it in GitHub Desktop.
Save eihli/aa791f7136d9fd85ea74 to your computer and use it in GitHub Desktop.
Use of __new__
class Game (object):
"""
Game represents a single pre- or regular-season game. It provides a window
into the statistics of every player that played into the game, along with
the winner of the game, the score and a list of all the scoring plays.
"""
def __new__(cls, eid=None, fpath=None):
# If we can't get a valid JSON data, exit out and return None.
try:
rawData = _get_json_data(eid, fpath)
except urllib2.URLError:
return None
if rawData is None or rawData.strip() == '{}':
return None
game = object.__new__(cls)
game.rawData = rawData
try:
if eid is not None:
game.eid = eid
game.data = json.loads(game.rawData)[game.eid]
else: # For when we have rawData (fpath) and no eid.
game.eid = None
game.data = json.loads(game.rawData)
for k, v in game.data.iteritems():
if isinstance(v, dict):
game.eid = k
game.data = v
break
assert game.eid is not None
except ValueError:
return None
return game
def __init__(self, eid=None, fpath=None):
"""
Creates a new Game instance given a game identifier.
The game identifier is used by NFL.com's GameCenter live update web
pages. It is used to construct a URL to download JSON data for the
game.
If the game has been completed, the JSON data will be cached to disk
so that subsequent accesses will not re-download the data but instead
read it from disk.
When the JSON data is written to disk, it is compressed using gzip.
"""
# Make the schedule info more accessible.
self.schedule = nflgame.sched.games.get(self.eid, None)
# Home and team cumulative statistics.
self.home = self.data['home']['abbr']
self.away = self.data['away']['abbr']
self.stats_home = _json_team_stats(self.data['home']['stats']['team'])
self.stats_away = _json_team_stats(self.data['away']['stats']['team'])
# Load up some simple static values.
self.gamekey = nflgame.sched.games[self.eid]['gamekey']
self.time = GameClock(self.data['qtr'], self.data['clock'])
self.down = _tryint(self.data['down'])
self.togo = _tryint(self.data['togo'])
self.score_home = int(self.data['home']['score']['T'])
self.score_away = int(self.data['away']['score']['T'])
for q in (1, 2, 3, 4, 5):
for team in ('home', 'away'):
score = self.data[team]['score'][str(q)]
self.__dict__['score_%s_q%d' % (team, q)] = int(score)
if not self.game_over():
self.winner = None
else:
if self.score_home > self.score_away:
self.winner = self.home
self.loser = self.away
elif self.score_away > self.score_home:
self.winner = self.away
self.loser = self.home
else:
self.winner = '%s/%s' % (self.home, self.away)
self.loser = '%s/%s' % (self.home, self.away)
# Load the scoring summary into a simple list of strings.
self.scores = []
for k in sorted(map(int, self.data['scrsummary'])):
play = self.data['scrsummary'][str(k)]
s = '%s - Q%d - %s - %s' \
% (play['team'], play['qtr'], play['type'], play['desc'])
self.scores.append(s)
# Check to see if the game is over, and if so, cache the data.
if self.game_over() and not os.access(_jsonf % eid, os.R_OK):
self.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment