<commit_before>from __future__ import absolute_import, unicode_literals
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse
from django.utils import importlib
from django.views.generic import TemplateView
from django.conf import settings


WIDGETS = getattr(settings, 'WIDGETS', None) or []


def import_widget(path):
    try:
        parts = path.split('.')
        module_path, class_name = '.'.join(parts[:-1]), parts[-1]
        module = importlib.import_module(module_path)
        return getattr(module, class_name)
    except ImportError as e:
        msg = "Could not import '%s'. %s: %s." % (path, e.__class__.__name__, e)
        raise ImportError(msg)


class WidgetSelectorMixin(object):

    # request
    # kwargs
    # widget_classes

    def get_widget(self):
        if self.get_widget_code() is None:
            return None
        return self.get_widget_class(self.get_widget_code())(self.request.GET)

    def get_widget_code(self):
        return self.kwargs.get('widget', None)

    @property
    def widgets(self):
        if not hasattr(self, 'widget_classes'):
            setattr(self, 'widget_classes', {})
            for path in WIDGETS:
                widget_class = import_widget(path)
                self.widget_classes[widget_class.code] = widget_class
        return getattr(self, 'widget_classes')<commit_msg>add ordinable dict for widgets<commit_after>from __future__ import absolute_import, unicode_literals
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse
from django.utils import importlib
from django.views.generic import TemplateView
from django.conf import settings
from collections import OrderedDict


WIDGETS = getattr(settings, 'WIDGETS', None) or []


def import_widget(path):
    try:
        parts = path.split('.')
        module_path, class_name = '.'.join(parts[:-1]), parts[-1]
        module = importlib.import_module(module_path)
        return getattr(module, class_name)
    except ImportError as e:
        msg = "Could not import '%s'. %s: %s." % (path, e.__class__.__name__, e)
        raise ImportError(msg)


class WidgetSelectorMixin(object):

    # request
    # kwargs
    # widget_classes

    def get_widget(self):
        if self.get_widget_code() is None:
            return None
        return self.get_widget_class(self.get_widget_code())(self.request.GET)

    def get_widget_code(self):
        return self.kwargs.get('widget', None)

    @property
    def widgets(self):
        if not hasattr(self, 'widget_classes'):
            setattr(self, 'widget_classes', OrderedDict())
            for path in WIDGETS:
                widget_class = import_widget(path)
                self.widget_classes[widget_class.code] = widget_class
        return getattr(self, 'widget_classes')
{'fraction_non_alphanumeric': 0.09859154929577464, 'fraction_numerical': 0.0014084507042253522, 'mean_word_length': 3.1114327062228653, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3345212', 'n_tokens_mistral': 837, 'n_tokens_neox': 827, 'n_words': 208}