<commit_before># Copyright (C) 2014 <NAME> <<EMAIL>>
# Copyright (C) 2014 <NAME> <<EMAIL>>
# Copyright (C) 2014 <NAME> <<EMAIL>>
# Copyright (C) 2014 <NAME> <<EMAIL>>
# This program 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.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.db.models import signals


def signals_switch():
    pre_save = signals.pre_save.receivers
    post_save = signals.post_save.receivers

    def disconnect():
        signals.pre_save.receivers = []
        signals.post_save.receivers = []

    def reconnect():
        signals.pre_save.receivers = pre_save
        signals.post_save.receivers = post_save

    return disconnect, reconnect


disconnect_signals, reconnect_signals = signals_switch()<commit_msg>Decorator for overriding settings while testing<commit_after># Copyright (C) 2014 <NAME> <<EMAIL>>
# Copyright (C) 2014 <NAME> <<EMAIL>>
# Copyright (C) 2014 <NAME> <<EMAIL>>
# Copyright (C) 2014 <NAME> <<EMAIL>>
# This program 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.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import functools

from django.conf import settings
from django.db.models import signals


def signals_switch():
    pre_save = signals.pre_save.receivers
    post_save = signals.post_save.receivers

    def disconnect():
        signals.pre_save.receivers = []
        signals.post_save.receivers = []

    def reconnect():
        signals.pre_save.receivers = pre_save
        signals.post_save.receivers = post_save

    return disconnect, reconnect


disconnect_signals, reconnect_signals = signals_switch()


def set_settings(**new_settings):
    """Decorator for set django settings that will be only available during the
    wrapped-function execution.

    For example:
        @set_settings(FOO='bar')
        def myfunc():
            ...

       @set_settings(FOO='bar')
       class TestCase:
           ...
    """
    def decorator(testcase):
        if type(testcase) is type:
            namespace = {"OVERRIDE_SETTINGS": new_settings, "ORIGINAL_SETTINGS": {}}
            wrapper = type(testcase.__name__, (SettingsTestCase, testcase), namespace)
        else:
            @functools.wraps(testcase)
            def wrapper(*args, **kwargs):
                old_settings = override_settings(new_settings)
                try:
                    testcase(*args, **kwargs)
                finally:
                    override_settings(old_settings)

        return wrapper

    return decorator


def override_settings(new_settings):
    old_settings = {}
    for name, new_value in new_settings.items():
        old_settings[name] = getattr(settings, name, None)
        setattr(settings, name, new_value)
    return old_settings


class SettingsTestCase(object):
    @classmethod
    def setup_class(cls):
        cls.ORIGINAL_SETTINGS = override_settings(cls.OVERRIDE_SETTINGS)

    @classmethod
    def teardown_class(cls):
        override_settings(cls.ORIGINAL_SETTINGS)
        cls.OVERRIDE_SETTINGS.clear()
{'fraction_non_alphanumeric': 0.08748778103616814, 'fraction_numerical': 0.008308895405669599, 'mean_word_length': 3.263541666666667, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 29, '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': '1624426', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1142, 'n_words': 392}