<commit_before>"""A custom query set that wraps around MongoDB cursors"""


class QuerySet(object):
    """A query set that wraps around MongoDB cursors and returns
    :class:`~simon.MongoModel` objects
    """

    def __init__(self, cursor=None, cls=None):
        """Creates a new query set.

        :param cursor: The result set.
        :type cursor: :class:`~pymongo.cursor.Cursor`.
        :param cls: Model class to map the results to.
        :type cls: :class:`~simon.MongoModel`.

        .. versionadded:: 0.1.0
        """

        self._cls = cls
        self._cursor = cursor
        self._count = None

        self._items = []

    def count(self):
        """Gets the number of documents in the :class:`QuerySet`.

        :returns: int -- the number of documents.

        .. versionadded:: 0.1.0
        """

        # Store the count interally so that the call doesn't need to
        # be made over and over
        if self._count is None:
            # Without setting with_limit_and_skip to True, the count
            # would reflect all documents matching the query, not just
            # those available through the cursor
            self._count = self._cursor.count(with_limit_and_skip=True)
        return self._count

    def distinct(self, key):
        """Gets a list of distinct values for ``key`` across all
        documents in the :class:`QuerySet`.

        :param key: Name of the key.
        :type key: str.
        :returns: list -- distinct values for the key.

        .. versionadded:: 0.1.0
        """

        # If the QuerySet has a model class, check for key in the
        # class's field map
        if self._cls:
            key = self._cls._meta.field_map.get(key, key)

        return self._cursor.distinct(key)

    def limit(self, limit):
        """Applies a limit to the number of documents in the
        :class:`QuerySet`.

        :param limit: Number of documents to return.
        :type limit: int.
        :returns: :class:`QuerySet` -- the documents with the limit
                  applied.

        .. versionadded:: 0.1.0
        """

        # Make sure to clone the cursor so as not to alter the original
        return QuerySet(self._cursor.clone().limit(limit), self._cls)

    def skip(self, skip):
        """Skips a number of documents in the :class:`QuerySet`.

        :param skip: Number of documents to skip.
        :type skip: int.
        :returns: :class:`QuerySet` -- the documents remaining.

        .. versionadded:: 0.1.0
        """

        # Make sure to clone the cursor so as not to alter the original
        return QuerySet(self._cursor.clone().skip(skip), self._cls)

    def sort(self, *keys):
        """Sorts the documents in the :class:`QuerySet`.

        By default all sorting is done in ascending order. To switch
        any key to sort in descending order, place a ``-`` before the
        name of the key.

        ..

            >>> qs.sort('id')
            >>> qs.sort('grade', '-score')

        :param keys: Names of the fields to sort by.
        :type keys: args.
        :returns: :class:`QuerySet` -- the sorted documents.

        .. versionadded: 0.1.0
        """

        # Build the list of sorting (key, direction) pairs. If the
        # QuerySet has a model class, check for the key in the class's
        # field map
        sorting = []
        for key in keys:
            if key[0] == '-':
                key = key[1:]
                direction = -1
            else:
                direction = 1

            if self._cls:
                key = self._cls._meta.field_map.get(key, key)

            sorting.append((key, direction))

        # Make sure to clone the cursor so as not to alter the original
        return QuerySet(self._cursor.clone().sort(sorting), self._cls)

    def _fill_to(self, index):
        """Buils the cache of documents retrieved from the cursor.

        :class:`QuerySet` objects keep documents loaded from the
        cursor in an internal cache to avoid repetitive trips to the
        database. In order to properly load a cache that can be
        iterated over, any documents before the requested one must be
        loaded.<commit_msg>Stop making up words<commit_after>"""A custom query set that wraps around MongoDB cursors"""


class QuerySet(object):
    """A query set that wraps around MongoDB cursors and returns
    :class:`~simon.MongoModel` objects
    """

    def __init__(self, cursor=None, cls=None):
        """Creates a new query set.

        :param cursor: The result set.
        :type cursor: :class:`~pymongo.cursor.Cursor`.
        :param cls: Model class to map the results to.
        :type cls: :class:`~simon.MongoModel`.

        .. versionadded:: 0.1.0
        """

        self._cls = cls
        self._cursor = cursor
        self._count = None

        self._items = []

    def count(self):
        """Gets the number of documents in the :class:`QuerySet`.

        :returns: int -- the number of documents.

        .. versionadded:: 0.1.0
        """

        # Store the count interally so that the call doesn't need to
        # be made over and over
        if self._count is None:
            # Without setting with_limit_and_skip to True, the count
            # would reflect all documents matching the query, not just
            # those available through the cursor
            self._count = self._cursor.count(with_limit_and_skip=True)
        return self._count

    def distinct(self, key):
        """Gets a list of distinct values for ``key`` across all
        documents in the :class:`QuerySet`.

        :param key: Name of the key.
        :type key: str.
        :returns: list -- distinct values for the key.

        .. versionadded:: 0.1.0
        """

        # If the QuerySet has a model class, check for key in the
        # class's field map
        if self._cls:
            key = self._cls._meta.field_map.get(key, key)

        return self._cursor.distinct(key)

    def limit(self, limit):
        """Applies a limit to the number of documents in the
        :class:`QuerySet`.

        :param limit: Number of documents to return.
        :type limit: int.
        :returns: :class:`QuerySet` -- the documents with the limit
                  applied.

        .. versionadded:: 0.1.0
        """

        # Make sure to clone the cursor so as not to alter the original
        return QuerySet(self._cursor.clone().limit(limit), self._cls)

    def skip(self, skip):
        """Skips a number of documents in the :class:`QuerySet`.

        :param skip: Number of documents to skip.
        :type skip: int.
        :returns: :class:`QuerySet` -- the documents remaining.

        .. versionadded:: 0.1.0
        """

        # Make sure to clone the cursor so as not to alter the original
        return QuerySet(self._cursor.clone().skip(skip), self._cls)

    def sort(self, *keys):
        """Sorts the documents in the :class:`QuerySet`.

        By default all sorting is done in ascending order. To switch
        any key to sort in descending order, place a ``-`` before the
        name of the key.

        ..

            >>> qs.sort('id')
            >>> qs.sort('grade', '-score')

        :param keys: Names of the fields to sort by.
        :type keys: args.
        :returns: :class:`QuerySet` -- the sorted documents.

        .. versionadded: 0.1.0
        """

        # Build the list of sorting (key, direction) pairs. If the
        # QuerySet has a model class, check for the key in the class's
        # field map
        sorting = []
        for key in keys:
            if key[0] == '-':
                key = key[1:]
                direction = -1
            else:
                direction = 1

            if self._cls:
                key = self._cls._meta.field_map.get(key, key)

            sorting.append((key, direction))

        # Make sure to clone the cursor so as not to alter the original
        return QuerySet(self._cursor.clone().sort(sorting), self._cls)

    def _fill_to(self, index):
        """Builds the cache of documents retrieved from the cursor.

        :class:`QuerySet` objects keep documents loaded from the
        cursor in an internal cache to avoid repetitive trips to the
        database. In order to properly load a cache that can be
        iterated over, any documents before the requested one must be
        loaded.
{'fraction_non_alphanumeric': 0.09736404654476372, 'fraction_numerical': 0.005224412253621468, 'mean_word_length': 1.8945017182130583, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '712146', 'n_tokens_mistral': 2335, 'n_tokens_neox': 2216, 'n_words': 892}