<commit_before># python imports
import random
import re
import urlparse

from threading import local
_thread_locals = local()

# django imports
from django.conf import settings
from django.dispatch import receiver
from django.template import TemplateDoesNotExist
from django.template.loader import BaseLoader

# lfs imports
from lfs.core.signals import order_submitted


def get_current_request():
    return getattr(_thread_locals, 'request', None)


class Loader(BaseLoader):
    is_usable = True

    def load_template(self, template_name, template_dirs=None):
        request = get_current_request()
        if request is None:
            raise TemplateDoesNotExist(template_name)

        theme = request.session.get("ab_tests")
        if not theme:
            theme = self.get_theme(request)
            request.session["ab_tests"] = theme

        if theme == "a":
            raise TemplateDoesNotExist(template_name)
        else:
            template_name = "%s/%s/%s" % (settings.LFS_AB_TESTS_DIRECTORY, theme, template_name)
            try:
                return open(template_name).read(), template_name
            except IOError:
                raise TemplateDoesNotExist(template_name)

    def get_theme(self, request):
        """
        Returns the theme for the given request.
        """
        from lfs_ab_tests.models import ThemeInformation

        themes = getattr(settings, "LFS_AB_TESTS_THEMES", [])
        for i, theme in enumerate(themes):
            theme, frequency = theme.split(":")
            try:
                frequency = int(frequency)
            except ValueError, TypeError:
                frequency = 1

            ti, created = ThemeInformation.objects.get_or_create(theme=theme)

            if created:
                return theme
            else:
                if ti.count < frequency:<commit_msg>Fixes for MultipleObjectsReturned.<commit_after># python imports
import random
import re
import urlparse

from threading import local
_thread_locals = local()

# django imports
from django.conf import settings
from django.dispatch import receiver
from django.template import TemplateDoesNotExist
from django.template.loader import BaseLoader

# lfs imports
from lfs.core.signals import order_submitted


def get_current_request():
    return getattr(_thread_locals, 'request', None)


class Loader(BaseLoader):
    is_usable = True

    def load_template(self, template_name, template_dirs=None):
        request = get_current_request()
        if request is None:
            raise TemplateDoesNotExist(template_name)

        theme = request.session.get("ab_tests")
        if not theme:
            theme = self.get_theme(request)
            request.session["ab_tests"] = theme

        if theme == "a":
            raise TemplateDoesNotExist(template_name)
        else:
            template_name = "%s/%s/%s" % (settings.LFS_AB_TESTS_DIRECTORY, theme, template_name)
            try:
                return open(template_name).read(), template_name
            except IOError:
                raise TemplateDoesNotExist(template_name)

    def get_theme(self, request):
        """
        Returns the theme for the given request.
        """
        from lfs_ab_tests.models import ThemeInformation

        themes = getattr(settings, "LFS_AB_TESTS_THEMES", [])
        for i, theme in enumerate(themes):
            theme, frequency = theme.split(":")
            try:
                frequency = int(frequency)
            except (ValueError, TypeError):
                frequency = 1

            try:
                ti, created = ThemeInformation.objects.get_or_create(theme=theme)
            except ThemeInformation.MultipleObjectsReturned:
                created = False
                ti = ThemeInformation.objects.filter(theme=theme)[0]

            if created:
                return theme
            else:
                if ti.count < frequency:
{'fraction_non_alphanumeric': 0.07659033078880408, 'fraction_numerical': 0.0007633587786259542, 'mean_word_length': 2.3370118845500847, 'pattern_counts': {'":': 4, '<': 5, '<?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': '1426798', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1014, 'n_words': 274}