<commit_before>import re
import functools
import operator
from dateutil import parser as date_parser
import datetime

from django.core.exceptions import ValidationError
from modularodm import Q
from modularodm.query import queryset as modularodm_queryset
from rest_framework.filters import OrderingFilter
from rest_framework import serializers as ser

from api.base.exceptions import (
    InvalidFilterError,
    InvalidFilterOperator,
    InvalidFilterComparisonType,
    InvalidFilterMatchType,
    InvalidFilterValue,
    InvalidFilterFieldError
)
from api.base import utils
from api.base.serializers import RelationshipField, TargetField

def sort_multiple(fields):
    fields = list(fields)
    def sort_fn(a, b):
        sort_direction = 1
        for index, field in enumerate(fields):
            if field[0] == '-':
                sort_direction = -1
                field = field[1:]
            a_field = getattr(a, field)
            b_field = getattr(b, field)
            if a_field > b_field:
                return 1 * sort_direction
            elif a_field < b_field:
                return -1 * sort_direction
        return 0
    return sort_fn

class ODMOrderingFilter(OrderingFilter):
    """Adaptation of rest_framework.filters.OrderingFilter to work with modular-odm."""
    # override
    def filter_queryset(self, request, queryset, view):
        ordering = self.get_ordering(request, queryset, view)
        if ordering:
            if not isinstance(queryset, modularodm_queryset.BaseQuerySet) and isinstance(ordering, (list, tuple)):
                sorted_list = sorted(queryset, cmp=sort_multiple(ordering))
                return sorted_list
            return queryset.sort(*ordering)
        return queryset


class FilterMixin(object):
    """ View mixin with helper functions for filtering. """

    QUERY_PATTERN = re.compile(r'^filter\[(?P<field>\w+)\](\[(?P<op>\w+)\])?$')<commit_msg>removal of indexing in loop<commit_after>import re
import functools
import operator
from dateutil import parser as date_parser
import datetime

from django.core.exceptions import ValidationError
from modularodm import Q
from modularodm.query import queryset as modularodm_queryset
from rest_framework.filters import OrderingFilter
from rest_framework import serializers as ser

from api.base.exceptions import (
    InvalidFilterError,
    InvalidFilterOperator,
    InvalidFilterComparisonType,
    InvalidFilterMatchType,
    InvalidFilterValue,
    InvalidFilterFieldError
)
from api.base import utils
from api.base.serializers import RelationshipField, TargetField

def sort_multiple(fields):
    fields = list(fields)
    def sort_fn(a, b):
        sort_direction = 1
        for field in fields:
            if field[0] == '-':
                sort_direction = -1
                field = field[1:]
            a_field = getattr(a, field)
            b_field = getattr(b, field)
            if a_field > b_field:
                return 1 * sort_direction
            elif a_field < b_field:
                return -1 * sort_direction
        return 0
    return sort_fn

class ODMOrderingFilter(OrderingFilter):
    """Adaptation of rest_framework.filters.OrderingFilter to work with modular-odm."""
    # override
    def filter_queryset(self, request, queryset, view):
        ordering = self.get_ordering(request, queryset, view)
        if ordering:
            if not isinstance(queryset, modularodm_queryset.BaseQuerySet) and isinstance(ordering, (list, tuple)):
                sorted_list = sorted(queryset, cmp=sort_multiple(ordering))
                return sorted_list
            return queryset.sort(*ordering)
        return queryset


class FilterMixin(object):
    """ View mixin with helper functions for filtering. """

    QUERY_PATTERN = re.compile(r'^filter\[(?P<field>\w+)\](\[(?P<op>\w+)\])?$')
{'fraction_non_alphanumeric': 0.0803014553014553, 'fraction_numerical': 0.0036382536382536385, 'mean_word_length': 2.9598765432098766, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4582512', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1037, 'n_words': 296}