<commit_before>from __future__ import unicode_literals
from copy import copy

from .things import Thing
from .exceptions import BoardGameGeekError
from .utils import fix_url


class CollectionBoardGame(Thing):
    """
    A boardgame retrieved from the collection information, which has
    less information than the one retrieved via the /thing api and which
    also contains some user-specific information
    """
    def __repr__(self):
        return "CollectionBoardGame (id: {})".format(self.id)

    def _format(self, log):
        log.info("boardgame id      : {}".format(self.id))
        log.info("boardgame name    : {}".format(self.name))

        log.info("last modified     : {}".format(self.lastmodified))

        log.info("rating            : {}".format(self.rating))
        log.info("own               : {}".format(self.owned))
        log.info("preordered        : {}".format(self.preordered))
        log.info("previously owned  : {}".format(self.prev_owned))
        log.info("want              : {}".format(self.want))
        log.info("want to buy       : {}".format(self.want_to_buy))
        log.info("want to play      : {}".format(self.want_to_play))
        log.info("wishlist          : {}".format(self.wishlist))
        log.info("wishlist priority : {}".format(self.wishlist_priority))
        log.info("for trade         : {}".format(self.for_trade))

    @property
    def lastmodified(self):
        return self._data.get("lastmodified")

    @property
    def rating(self):
        return self._data.get("rating")

    @property
    def owned(self):
        return bool(int(self._data.get("own", 0)))

    @property
    def preordered(self):
        return bool(int(self._data.get("preordered", 0)))

    @property
    def prev_owned(self):
        return bool(int(self._data.get("prevowned", 0)))

    @property
    def want(self):
        return bool(int(self._data.get("want", 0)))

    @property
    def want_to_buy(self):
        return bool(int(self._data.get("wanttobuy", 0)))

    @property
    def want_to_play(self):
        return bool(int(self._data.get("wanttoplay", 0)))

    @property
    def for_trade(self):
        return bool(int(self._data.get("fortrade", 0)))

    @property
    def wishlist(self):
        return bool(int(self._data.get("wishlist", 0)))

    @property
    def wishlist_priority(self):
        return self._data.get("wishlistpriority")


class BoardGame(Thing):
    """
    An object containing the core information about a game.
    """
    def __init__(self, data):

        kw = copy(data)

        # if we have any "expansions" for this item..
        if "expansions" not in kw:
            kw["expansions"] = []

        for to_fix in ["thumbnail", "image"]:
            if to_fix in kw:
                kw[to_fix] = fix_url(kw[to_fix])

        self._expansions = []           # list of Thing for the expansions
        self._expansions_set = set()    # set for making sure things are unique
        for data in kw["expansions"]:
            try:
                if data["id"] not in self._expansions_set:
                    self._expansions_set.add(data["id"])
                    self._expansions.append(Thing(data))
            except KeyError:
                raise BoardGameGeekError("invalid expansion data")

        # if this item expands something...
        if "expands" not in kw:
            kw["expands"] = []

        self._expands = []              # list of Thing which this item expands
        self._expands_set = set()       # set for keeping things unique
        for data in kw["expands"]:         # for all the items this game expands, create a Thing
            try:
                if data["id"] not in self._expands_set:
                    self._expands_set.add(data["id"])
                    self._expands.append(Thing(data))
            except KeyError:
                raise BoardGameGeekError("invalid expanded game data")

        self.boardgame_rank = -1

        if "ranks" in kw:
            # try to search for the boardgame rank of this game
            for rank in kw["ranks"]:
                if rank.get("name") == "boardgame":
                    self.boardgame_rank = int(rank.get("value", -1))
                    break

        super(BoardGame, self).__init__(kw)

    def __repr__(self):
        return "BoardGame (id: {})".format(self.id)

    def add_expanded_game(self, data):
        try:
            if data["id"] not in self._expands_set:
                self._data["expands"].append(data)
                self._expands_set.add(data["id"])
                self._expands.append(Thing(data))
        except KeyError:
            raise BoardGameGeekError("invalid expanded game data")

    def add_expansion(self, data):
        try:
            if data["id"] not in self._expansions_set:
                self._data["expansions"].append(data)
                self._expansions_set.add(data["id"])
                self._expansions.append(Thing(data))
        except KeyError:
            raise BoardGameGeekError("invalid expansion data")

    def _format(self, log):<commit_msg>There are games which don't have a boardgame rank; added a check for that.<commit_after>from __future__ import unicode_literals
from copy import copy

from .things import Thing
from .exceptions import BoardGameGeekError
from .utils import fix_url


class CollectionBoardGame(Thing):
    """
    A boardgame retrieved from the collection information, which has
    less information than the one retrieved via the /thing api and which
    also contains some user-specific information
    """
    def __repr__(self):
        return "CollectionBoardGame (id: {})".format(self.id)

    def _format(self, log):
        log.info("boardgame id      : {}".format(self.id))
        log.info("boardgame name    : {}".format(self.name))

        log.info("last modified     : {}".format(self.lastmodified))

        log.info("rating            : {}".format(self.rating))
        log.info("own               : {}".format(self.owned))
        log.info("preordered        : {}".format(self.preordered))
        log.info("previously owned  : {}".format(self.prev_owned))
        log.info("want              : {}".format(self.want))
        log.info("want to buy       : {}".format(self.want_to_buy))
        log.info("want to play      : {}".format(self.want_to_play))
        log.info("wishlist          : {}".format(self.wishlist))
        log.info("wishlist priority : {}".format(self.wishlist_priority))
        log.info("for trade         : {}".format(self.for_trade))

    @property
    def lastmodified(self):
        return self._data.get("lastmodified")

    @property
    def rating(self):
        return self._data.get("rating")

    @property
    def owned(self):
        return bool(int(self._data.get("own", 0)))

    @property
    def preordered(self):
        return bool(int(self._data.get("preordered", 0)))

    @property
    def prev_owned(self):
        return bool(int(self._data.get("prevowned", 0)))

    @property
    def want(self):
        return bool(int(self._data.get("want", 0)))

    @property
    def want_to_buy(self):
        return bool(int(self._data.get("wanttobuy", 0)))

    @property
    def want_to_play(self):
        return bool(int(self._data.get("wanttoplay", 0)))

    @property
    def for_trade(self):
        return bool(int(self._data.get("fortrade", 0)))

    @property
    def wishlist(self):
        return bool(int(self._data.get("wishlist", 0)))

    @property
    def wishlist_priority(self):
        return self._data.get("wishlistpriority")


class BoardGame(Thing):
    """
    An object containing the core information about a game.
    """
    def __init__(self, data):

        kw = copy(data)

        # if we have any "expansions" for this item..
        if "expansions" not in kw:
            kw["expansions"] = []

        for to_fix in ["thumbnail", "image"]:
            if to_fix in kw:
                kw[to_fix] = fix_url(kw[to_fix])

        self._expansions = []           # list of Thing for the expansions
        self._expansions_set = set()    # set for making sure things are unique
        for data in kw["expansions"]:
            try:
                if data["id"] not in self._expansions_set:
                    self._expansions_set.add(data["id"])
                    self._expansions.append(Thing(data))
            except KeyError:
                raise BoardGameGeekError("invalid expansion data")

        # if this item expands something...
        if "expands" not in kw:
            kw["expands"] = []

        self._expands = []              # list of Thing which this item expands
        self._expands_set = set()       # set for keeping things unique
        for data in kw["expands"]:         # for all the items this game expands, create a Thing
            try:
                if data["id"] not in self._expands_set:
                    self._expands_set.add(data["id"])
                    self._expands.append(Thing(data))
            except KeyError:
                raise BoardGameGeekError("invalid expanded game data")

        self.boardgame_rank = -1

        if "ranks" in kw:
            # try to search for the boardgame rank of this game
            for rank in kw["ranks"]:
                if rank.get("name") == "boardgame":
                    value = rank.get("value")
                    if value is None:
                        self.boardgame_rank = -1
                    else:
                        self.boardgame_rank = int(value)
                    break

        super(BoardGame, self).__init__(kw)

    def __repr__(self):
        return "BoardGame (id: {})".format(self.id)

    def add_expanded_game(self, data):
        try:
            if data["id"] not in self._expands_set:
                self._data["expands"].append(data)
                self._expands_set.add(data["id"])
                self._expands.append(Thing(data))
        except KeyError:
            raise BoardGameGeekError("invalid expanded game data")

    def add_expansion(self, data):
        try:
            if data["id"] not in self._expansions_set:
                self._data["expansions"].append(data)
                self._expansions_set.add(data["id"])
                self._expansions.append(Thing(data))
        except KeyError:
            raise BoardGameGeekError("invalid expansion data")

    def _format(self, log):
{'fraction_non_alphanumeric': 0.11984264056802917, 'fraction_numerical': 0.0019190174630589138, 'mean_word_length': 2.061985898942421, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1034378', 'n_tokens_mistral': 3085, 'n_tokens_neox': 2941, 'n_words': 742}