<commit_before># This file is part of Archivematica.
#
# Copyright 2010-2013 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Archivematica is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Archivematica.  If not, see <http://www.gnu.org/licenses/>.

from django.utils.dateformat import format
from django.core.paginator import Paginator, EmptyPage
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.core.servers.basehttp import FileWrapper
from django.shortcuts import render
from main import models
import cPickle, pprint, ConfigParser, urllib, os, mimetypes

def pr(object):
    return pprint.pformat(object)

# Used for raw SQL queries to return data in dictionaries instead of lists
def dictfetchall(cursor):
    "Returns all rows from a cursor as a dict"
    desc = cursor.description
    return [
        dict(zip([col[0] for col in desc], row))
        for row in cursor.fetchall()
    ]

def keynat(string):
    r'''A natural sort helper function for sort() and sorted()
    without using regular expressions or exceptions.

    >>> items = ('Z', 'a', '10th', '1st', '9')
    >>> sorted(items)
    ['10th', '1st', '9', 'Z', 'a']
    >>> sorted(items, key=keynat)
    ['1st', '9', '10th', 'a', 'Z']    
    '''
    it = type(1)
    r = []
    for c in string:
        if c.isdigit():
            d = int(c)
            if r and type( r[-1] ) == it: 
                r[-1] = r[-1] * 10 + d
            else: 
                r.append(d)
        else:
            r.append(c.lower())
    return r

# this class wraps Pyes search results so the Django Paginator class
# can work on the results (except object_list doesn't yet work)
class DjangoPaginatableListFromPyesSearchResult:
    def __init__(self, result, start, size):
        self.result = result
        self.start  = start
        self.size   = size

    def count(self):
        return self.result.count()

    def __len__(self):
        return self.count()

    def __getitem__(self, key):
        if key < self.start:
            return
        else:
            if key >= (self.start + self.size):
                return
            else:
                return self.result[key - self.start]

def pager(objects, items_per_page, current_page_number):
    page = {}

    # if a Pyes resultset, wrap it in a class so it emulates
    # a standard Python list
    if objects.__class__.__name__ == 'ResultSet':
        p = Paginator(
            DjangoPaginatableListFromPyesSearchResult(
                objects,<commit_msg>Removed irrelevant comment.<commit_after># This file is part of Archivematica.
#
# Copyright 2010-2013 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Archivematica is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Archivematica.  If not, see <http://www.gnu.org/licenses/>.

from django.utils.dateformat import format
from django.core.paginator import Paginator, EmptyPage
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.core.servers.basehttp import FileWrapper
from django.shortcuts import render
from main import models
import cPickle, pprint, ConfigParser, urllib, os, mimetypes

def pr(object):
    return pprint.pformat(object)

# Used for raw SQL queries to return data in dictionaries instead of lists
def dictfetchall(cursor):
    "Returns all rows from a cursor as a dict"
    desc = cursor.description
    return [
        dict(zip([col[0] for col in desc], row))
        for row in cursor.fetchall()
    ]

def keynat(string):
    r'''A natural sort helper function for sort() and sorted()
    without using regular expressions or exceptions.

    >>> items = ('Z', 'a', '10th', '1st', '9')
    >>> sorted(items)
    ['10th', '1st', '9', 'Z', 'a']
    >>> sorted(items, key=keynat)
    ['1st', '9', '10th', 'a', 'Z']    
    '''
    it = type(1)
    r = []
    for c in string:
        if c.isdigit():
            d = int(c)
            if r and type( r[-1] ) == it: 
                r[-1] = r[-1] * 10 + d
            else: 
                r.append(d)
        else:
            r.append(c.lower())
    return r

# this class wraps Pyes search results so the Django Paginator class
# can work on the results
class DjangoPaginatableListFromPyesSearchResult:
    def __init__(self, result, start, size):
        self.result = result
        self.start  = start
        self.size   = size

    def count(self):
        return self.result.count()

    def __len__(self):
        return self.count()

    def __getitem__(self, key):
        if key < self.start:
            return
        else:
            if key >= (self.start + self.size):
                return
            else:
                return self.result[key - self.start]

def pager(objects, items_per_page, current_page_number):
    page = {}

    # if a Pyes resultset, wrap it in a class so it emulates
    # a standard Python list
    if objects.__class__.__name__ == 'ResultSet':
        p = Paginator(
            DjangoPaginatableListFromPyesSearchResult(
                objects,
{'fraction_non_alphanumeric': 0.09497389033942559, 'fraction_numerical': 0.009138381201044387, 'mean_word_length': 2.6810810810810812, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1675503', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1737, 'n_words': 672}