<commit_before>from django.conf import settings
from django.contrib import messages
from django.core import mail
from django.shortcuts import _get_queryset
from django.utils.translation import gettext as _

import stored_messages


def get_object_or_none(klass, *args, **kwargs):
    """
    Uses get() to return an object, or None if the object
    does not exist.

    klass may be a Model, Manager, or QuerySet object. All other passed
    arguments and keyword arguments are used in the get() query.

    Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
    object is found.
    """
    queryset = _get_queryset(klass)
    try:
        return queryset.get(*args, **kwargs)
    except queryset.model.DoesNotExist:
        return None


def get_list_or_none(klass, *args, **kwargs):
    """
    Uses filter() to return a list of objects, or return None if
    the list is empty.

    klass may be a Model, Manager, or QuerySet object. All other passed
    arguments and keyword arguments are used in the filter() query.
    """
    queryset = _get_queryset(klass)
    object_list = list(queryset.filter(*args, **kwargs))
    if not object_list:
        return None
    return object_list


class Emissary:
    def __init__(self, recipient):
        self.recipient = recipient

    def info(self, message):
        self.inform(messages.INFO, message)

    def success(self, message):
        self.inform(messages.SUCCESS, message)

    def warn(self, message):
        self.inform(messages.WARNING, message)

    def error(self, message):
        self.inform(messages.ERROR, message)

    def debug(self, message):
        self.inform(messages.DEBUG, message)

    def inform_mail(self, subject, mail_body, warn_if_no_email=True):
        email_address = getattr(self.recipient, 'email', None)
        if email_address:
            mail.send_mail(
                '[OSMAXX] ' + subject,
                mail_body,
                settings.DEFAULT_FROM_EMAIL,
                [email_address]
            )
        if warn_if_no_email and not email_address:
            self.warn(
                _("There is no email address assigned to your account. You won't be notified by email!")
            )

    def inform(self, message_type, message):
        stored_messages.api.add_message_for(
            users=[self.recipient],
            level=message_type,
            message_text=message
        )
<commit_msg>trying is better than iffing<commit_after>from django.conf import settings
from django.contrib import messages
from django.core import mail
from django.shortcuts import _get_queryset
from django.utils.translation import gettext as _

import stored_messages


def get_object_or_none(klass, *args, **kwargs):
    """
    Uses get() to return an object, or None if the object
    does not exist.

    klass may be a Model, Manager, or QuerySet object. All other passed
    arguments and keyword arguments are used in the get() query.

    Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
    object is found.
    """
    queryset = _get_queryset(klass)
    try:
        return queryset.get(*args, **kwargs)
    except queryset.model.DoesNotExist:
        return None


def get_list_or_none(klass, *args, **kwargs):
    """
    Uses filter() to return a list of objects, or return None if
    the list is empty.

    klass may be a Model, Manager, or QuerySet object. All other passed
    arguments and keyword arguments are used in the filter() query.
    """
    queryset = _get_queryset(klass)
    object_list = list(queryset.filter(*args, **kwargs))
    if not object_list:
        return None
    return object_list


class Emissary:
    def __init__(self, recipient):
        self.recipient = recipient

    def info(self, message):
        self.inform(messages.INFO, message)

    def success(self, message):
        self.inform(messages.SUCCESS, message)

    def warn(self, message):
        self.inform(messages.WARNING, message)

    def error(self, message):
        self.inform(messages.ERROR, message)

    def debug(self, message):
        self.inform(messages.DEBUG, message)

    def inform_mail(self, subject, mail_body, warn_if_no_email=True):
        try:
            email_address = self.recipient.email
            mail.send_mail(
                '[OSMAXX] ' + subject,
                mail_body,
                settings.DEFAULT_FROM_EMAIL,
                [email_address]
            )
        except AttributeError:
            if warn_if_no_email:
                self.warn(
                    _("There is no email address assigned to your account. You won't be notified by email!")
                )

    def inform(self, message_type, message):
        stored_messages.api.add_message_for(
            users=[self.recipient],
            level=message_type,
            message_text=message
        )

{'fraction_non_alphanumeric': 0.08073394495412844, 'fraction_numerical': 0.0, 'mean_word_length': 2.5244252873563218, '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': '3091749', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1353, 'n_words': 442}