<commit_before>import logging
from river.handlers.backends.base import powerset

from river.handlers.backends.memory import MemoryHandlerBackend
from river.models.handler import Handler

__author__ = 'ahmetdal'

LOGGER = logging.getLogger(__name__)


class DatabaseHandlerBackend(MemoryHandlerBackend):
    def __init__(self):
        super(DatabaseHandlerBackend, self).__init__()
        self.__register(Handler.objects.filter(enabled=True))

    def __register(self, handler_objs):
        handlers = []
        if handler_objs.exists():
            for handler in handler_objs:
                module, method_name = handler.method.rsplit('.', 1)
                try:
                    method = getattr(__import__(module, fromlist=[method]), method, None)
                    if method:
                        self.handlers[handler.hash] = method
                        handlers.append(method)
                        LOGGER.debug("Handler '%s' from database is registered initially from database as method '%s' and module '%s'. " % (handler.hash, method_name, module))
                    else:
                        LOGGER.warning("Handler '%s' from database can not be registered. Because method '%s' is not in module '%s'. " % (handler.hash, method_name, module))
                except ImportError:
                    LOGGER.warning("Handler '%s' from database can not be registered. Because module '%s'  does not exists. " % (handler.hash, module))
        return handlers

    def register(self, handler_cls, handler, workflow_object, field, override=False, *args, **kwargs):
        hash = super(DatabaseHandlerBackend, self).register(handler_cls, handler, workflow_object, field, override=override, *args, **kwargs)
        handler_obj, created = Handler.objects.update_or_create(
            hash=hash,
            defaults={
                'method': '%s.%s' % (handler.__module__, handler.__name__),
                'handler_cls': '%s.%s' % (handler_cls.__module__, handler_cls.__name__),
            }
        )
        if created:
            LOGGER.debug("Handler '%s' is registered in database as method %s and module %s. " % (handler_obj.hash, handler.__name__, handler.__module__))
        else:
            LOGGER.warning("Handler '%s' is already registered in database as method %s and module %s. " % (handler_obj.hash, handler.__name__, handler.__module__))<commit_msg>minor fix<commit_after>import logging
from river.handlers.backends.base import powerset

from river.handlers.backends.memory import MemoryHandlerBackend
from river.models.handler import Handler

__author__ = 'ahmetdal'

LOGGER = logging.getLogger(__name__)


class DatabaseHandlerBackend(MemoryHandlerBackend):
    def __init__(self):
        super(DatabaseHandlerBackend, self).__init__()
        self.__register(Handler.objects.filter(enabled=True))

    def __register(self, handler_objs):
        handlers = []
        if handler_objs.exists():
            for handler in handler_objs:
                module, method_name = handler.method.rsplit('.', 1)
                try:
                    method = getattr(__import__(module, fromlist=[method_name]), method_name, None)
                    if method:
                        self.handlers[handler.hash] = method
                        handlers.append(method)
                        LOGGER.debug("Handler '%s' from database is registered initially from database as method '%s' and module '%s'. " % (handler.hash, method_name, module))
                    else:
                        LOGGER.warning("Handler '%s' from database can not be registered. Because method '%s' is not in module '%s'. " % (handler.hash, method_name, module))
                except ImportError:
                    LOGGER.warning("Handler '%s' from database can not be registered. Because module '%s'  does not exists. " % (handler.hash, module))
        return handlers

    def register(self, handler_cls, handler, workflow_object, field, override=False, *args, **kwargs):
        hash = super(DatabaseHandlerBackend, self).register(handler_cls, handler, workflow_object, field, override=override, *args, **kwargs)
        handler_obj, created = Handler.objects.update_or_create(
            hash=hash,
            defaults={
                'method': '%s.%s' % (handler.__module__, handler.__name__),
                'handler_cls': '%s.%s' % (handler_cls.__module__, handler_cls.__name__),
            }
        )
        if created:
            LOGGER.debug("Handler '%s' is registered in database as method %s and module %s. " % (handler_obj.hash, handler.__name__, handler.__module__))
        else:
            LOGGER.warning("Handler '%s' is already registered in database as method %s and module %s. " % (handler_obj.hash, handler.__name__, handler.__module__))
{'fraction_non_alphanumeric': 0.10653349979192676, 'fraction_numerical': 0.0004161464835622139, 'mean_word_length': 2.5660237388724036, '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': '6326741', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1237, 'n_words': 346}