<commit_before>from collections import OrderedDict
import os
import os.path as path
import gzip
import json
import sys
import urllib2

import nflgame.player as player

_jsonf = path.join(path.split(__file__)[0], 'gamecenter-json', '%s.json.gz')
_json_base_url = "http://www.nfl.com/liveupdate/game-center/%s/%s_gtd.json"


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):
        # If we can't get a valid JSON data, exit out and return None.
        rawData = _get_json_data(eid)
        if rawData is None or rawData.strip() == '{}':
            return None
        game = object.__new__(cls, eid)
        game.rawData = rawData
        return game

    def __init__(self, eid):
        """
        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.
        """

        self.eid = eid
        self.data = json.loads(self.rawData)[self.eid]

        self.__load_all_players(self.data)
        self.players = player.Players(self.__players)

        # Load up some simple static values.
        self.qtr = self.data['qtr']
        self.clock = self.data['clock']
        self.home = self.data['home']['abbr']
        self.away = self.data['away']['abbr']
        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 self.score_home > self.score_away:
            self.winner = self.home
        elif self.score_away > self.score_home:
            self.winner = self.away
        else:
            self.winner = 'TIE'

        # 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)

        fpath = _jsonf % eid
        if self.game_over() and not os.access(fpath, os.R_OK):
            try:
                print >> gzip.open(fpath, 'w+'), self.rawData,
            except IOError:
                print >> sys.stderr, "Could not cache JSON data. Please " \
                                     "make '%s' writable." \
                                     % os.path.dirname(fpath)

    def game_over(self):
        """game_over returns true if the game is no longer being played."""
        return self.qtr == 'Final'

    def playing(self):
        """playing returns true if the game is currently being played."""
        return self.qtr != 'Pregame' and self.qtr != 'Final'

    def time(self):
        """
        Returns a nicely formatted string indicating the current time of the
        game. Examples include "Q1 10:52", "Q4 1:25", "Pregame", "Halftime"
        and "Final".
        """
        try:
            q = int(self.qtr)
            return 'Q%d %s' % (q, self.clock)
        except ValueError:
            return self.qtr

    def __load_all_players(self, gameData):
        self.__players = OrderedDict()
        for team in ("home", "away"):
            for category in player.categories:
                if category not in gameData[team]["stats"]:
                    continue
                catplayers = gameData[team]["stats"][category]
                for playerid, stats in catplayers.iteritems():
                    p = self.__get_or_add_player(playerid, stats["name"],
                                                 team == "home")
                    p._add_stats(category, stats)

    def __get_or_add_player(self, playerid, name, home):
        if playerid in self.__players:
            return self.__players[playerid]
        p = player.Player(playerid, name, home)
        self.__players[playerid] = p
        return p


def _get_json_data(eid):
    """
    Returns the JSON data corresponding to the game represented by eid.

    If the JSON data is already on disk, it is read, decompressed and returned.

    Otherwise, the JSON data is downloaded from the NFL web site. If the data
    doesn't exist yet or there was an error, _get_json_data returns None.
    """
    fpath = _jsonf % eid
    if os.access(fpath, os.R_OK):
        return gzip.open(fpath).read()
    try:
        return urllib2.urlopen(_json_base_url % (eid, eid)).read()
    except urllib2.HTTPError:
        pass
    return None
<commit_msg>Add loser field for convenience.<commit_after>from collections import OrderedDict
import os
import os.path as path
import gzip
import json
import sys
import urllib2

import nflgame.player as player

_jsonf = path.join(path.split(__file__)[0], 'gamecenter-json', '%s.json.gz')
_json_base_url = "http://www.nfl.com/liveupdate/game-center/%s/%s_gtd.json"


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):
        # If we can't get a valid JSON data, exit out and return None.
        rawData = _get_json_data(eid)
        if rawData is None or rawData.strip() == '{}':
            return None
        game = object.__new__(cls, eid)
        game.rawData = rawData
        return game

    def __init__(self, eid):
        """
        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.
        """

        self.eid = eid
        self.data = json.loads(self.rawData)[self.eid]

        self.__load_all_players(self.data)
        self.players = player.Players(self.__players)

        # Load up some simple static values.
        self.qtr = self.data['qtr']
        self.clock = self.data['clock']
        self.home = self.data['home']['abbr']
        self.away = self.data['away']['abbr']
        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)

        fpath = _jsonf % eid
        if self.game_over() and not os.access(fpath, os.R_OK):
            try:
                print >> gzip.open(fpath, 'w+'), self.rawData,
            except IOError:
                print >> sys.stderr, "Could not cache JSON data. Please " \
                                     "make '%s' writable." \
                                     % os.path.dirname(fpath)

    def game_over(self):
        """game_over returns true if the game is no longer being played."""
        return self.qtr == 'Final'

    def playing(self):
        """playing returns true if the game is currently being played."""
        return self.qtr != 'Pregame' and self.qtr != 'Final'

    def time(self):
        """
        Returns a nicely formatted string indicating the current time of the
        game. Examples include "Q1 10:52", "Q4 1:25", "Pregame", "Halftime"
        and "Final".
        """
        try:
            q = int(self.qtr)
            return 'Q%d %s' % (q, self.clock)
        except ValueError:
            return self.qtr

    def __load_all_players(self, gameData):
        self.__players = OrderedDict()
        for team in ("home", "away"):
            for category in player.categories:
                if category not in gameData[team]["stats"]:
                    continue
                catplayers = gameData[team]["stats"][category]
                for playerid, stats in catplayers.iteritems():
                    p = self.__get_or_add_player(playerid, stats["name"],
                                                 team == "home")
                    p._add_stats(category, stats)

    def __get_or_add_player(self, playerid, name, home):
        if playerid in self.__players:
            return self.__players[playerid]
        p = player.Player(playerid, name, home)
        self.__players[playerid] = p
        return p


def _get_json_data(eid):
    """
    Returns the JSON data corresponding to the game represented by eid.

    If the JSON data is already on disk, it is read, decompressed and returned.

    Otherwise, the JSON data is downloaded from the NFL web site. If the data
    doesn't exist yet or there was an error, _get_json_data returns None.
    """
    fpath = _jsonf % eid
    if os.access(fpath, os.R_OK):
        return gzip.open(fpath).read()
    try:
        return urllib2.urlopen(_json_base_url % (eid, eid)).read()
    except urllib2.HTTPError:
        pass
    return None

{'fraction_non_alphanumeric': 0.10924763421718355, 'fraction_numerical': 0.0033729972828633, 'mean_word_length': 1.9666481378543634, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6368024', 'n_tokens_mistral': 3174, 'n_tokens_neox': 3077, 'n_words': 1000}