<commit_before>try:
    set
except NameError:
    from sets import Set as set

from django.core.paginator import Paginator, Page, InvalidPage
from django.db.models import F
from django.http import Http404

from coffin import template
from jinja2 import nodes
from jinja2.ext import Extension
from jinja2.exceptions import TemplateSyntaxError

from tcc import settings

register = template.Library()

# Most of the code below is borrowed from the django_pagination module by <NAME> and Pinax Team,
# http://pinaxproject.com/docs/dev/external/pagination/index.html


class ParentCommentPaginator(Paginator):
    def page(self, number):
        "Returns a Page object for the given 1-based page number."
        number = self.validate_number(number)
        if self.count == 0:
            return Page(self.object_list, number, self)
        bottom = (number - 1) * self.per_page
        # This results in a query to the database ...
        bottomdate = getattr(self.parentcomments[bottom], settings.SORTORDER)
        try:
            # This too results in a query to the database ...
            top = self.parentcomments[bottom+self.per_page-1].sortdate
            kwargs = {'%s__range' % settings.SORTORDER: (top, bottomdate)}
            object_list = self.object_list.filter(**kwargs)
        except IndexError:
            kwargs = {'%s__lte' % setting.SORTORDER: bottomdate}
            object_list = self.object_list.filter(**kwargs)
        # And another (final) call to the database 
        return Page(object_list, number, self)

    def _get_count(self):
        "Returns the total number of objects, across all pages."
        if self._count is None:
            try:
                self.parentcomments = self.object_list.filter(parent__isnull=True)
                self._count = self.parentcomments.count()
            except (AttributeError, TypeError):
                # AttributeError if object_list has no count() method.
                # TypeError if object_list.count() requires arguments
                # (i.e. is of type list).
                self._count = len(self.object_list)
        return self._count
    count = property(_get_count)
        

class AutopaginateExtension(Extension):
    """ 
        Applies pagination to the given dataset (and saves truncated dataset to the context variable), 
        sets context variable with data enough to build html for paginator
        
        General syntax:

        {% autopaginate dataset [as ctx_variable] %}<commit_msg>Fix typo<commit_after>try:
    set
except NameError:
    from sets import Set as set

from django.core.paginator import Paginator, Page, InvalidPage
from django.db.models import F
from django.http import Http404

from coffin import template
from jinja2 import nodes
from jinja2.ext import Extension
from jinja2.exceptions import TemplateSyntaxError

from tcc import settings

register = template.Library()

# Most of the code below is borrowed from the django_pagination module by <NAME> and Pinax Team,
# http://pinaxproject.com/docs/dev/external/pagination/index.html


class ParentCommentPaginator(Paginator):
    def page(self, number):
        "Returns a Page object for the given 1-based page number."
        number = self.validate_number(number)
        if self.count == 0:
            return Page(self.object_list, number, self)
        bottom = (number - 1) * self.per_page
        # This results in a query to the database ...
        bottomdate = getattr(self.parentcomments[bottom], settings.SORTORDER)
        try:
            # This too results in a query to the database ...
            top = self.parentcomments[bottom+self.per_page-1].sortdate
            kwargs = {'%s__range' % settings.SORTORDER: (top, bottomdate)}
            object_list = self.object_list.filter(**kwargs)
        except IndexError:
            kwargs = {'%s__lte' % settings.SORTORDER: bottomdate}
            object_list = self.object_list.filter(**kwargs)
        # And another (final) call to the database 
        return Page(object_list, number, self)

    def _get_count(self):
        "Returns the total number of objects, across all pages."
        if self._count is None:
            try:
                self.parentcomments = self.object_list.filter(parent__isnull=True)
                self._count = self.parentcomments.count()
            except (AttributeError, TypeError):
                # AttributeError if object_list has no count() method.
                # TypeError if object_list.count() requires arguments
                # (i.e. is of type list).
                self._count = len(self.object_list)
        return self._count
    count = property(_get_count)
        

class AutopaginateExtension(Extension):
    """ 
        Applies pagination to the given dataset (and saves truncated dataset to the context variable), 
        sets context variable with data enough to build html for paginator
        
        General syntax:

        {% autopaginate dataset [as ctx_variable] %}
{'fraction_non_alphanumeric': 0.0788, 'fraction_numerical': 0.004, 'mean_word_length': 2.556899004267425, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5266308', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1302, 'n_words': 439}