<commit_before>"""
byceps.services.user_badge.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2017 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from collections import defaultdict
from typing import Dict, Optional, Set

from ...database import db
from ...typing import BrandID, UserID

from .models.awarding import BadgeAwarding, BadgeAwardingTuple, \
    QuantifiedBadgeAwardingTuple
from .models.badge import Badge, BadgeID, BadgeTuple


def create_badge(label: str, image_filename: str, *, brand_id: BrandID=None,
                 description: str=None) -> BadgeTuple:
    """Introduce a new badge."""
    badge = Badge(label, image_filename, brand_id=brand_id,
                  description=description)

    db.session.add(badge)
    db.session.commit()

    return badge.to_tuple()


def find_badge(badge_id: BadgeID) -> Optional[BadgeTuple]:
    """Return the badge with that id, or `None` if not found."""
    badge = Badge.query.get(badge_id)

    if badge is None:
        return None

    return badge.to_tuple()


def get_badges(badge_ids: Set[BadgeID]) -> Set[BadgeTuple]:
    """Return the badges with those IDs."""
    if not badge_ids:
        return []

    badges = Badge.query \
        .filter(Badge.id.in_(badge_ids)) \
        .all()

    return {badge.to_tuple() for badge in badges}


def get_badges_for_user(user_id: UserID) -> Set[BadgeTuple]:
    """Return all badges that have been awarded to the user."""
    badges = Badge.query \<commit_msg>Fix return value (discovered by mypy)<commit_after>"""
byceps.services.user_badge.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2017 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from collections import defaultdict
from typing import Dict, Optional, Set

from ...database import db
from ...typing import BrandID, UserID

from .models.awarding import BadgeAwarding, BadgeAwardingTuple, \
    QuantifiedBadgeAwardingTuple
from .models.badge import Badge, BadgeID, BadgeTuple


def create_badge(label: str, image_filename: str, *, brand_id: BrandID=None,
                 description: str=None) -> BadgeTuple:
    """Introduce a new badge."""
    badge = Badge(label, image_filename, brand_id=brand_id,
                  description=description)

    db.session.add(badge)
    db.session.commit()

    return badge.to_tuple()


def find_badge(badge_id: BadgeID) -> Optional[BadgeTuple]:
    """Return the badge with that id, or `None` if not found."""
    badge = Badge.query.get(badge_id)

    if badge is None:
        return None

    return badge.to_tuple()


def get_badges(badge_ids: Set[BadgeID]) -> Set[BadgeTuple]:
    """Return the badges with those IDs."""
    if not badge_ids:
        return set()

    badges = Badge.query \
        .filter(Badge.id.in_(badge_ids)) \
        .all()

    return {badge.to_tuple() for badge in badges}


def get_badges_for_user(user_id: UserID) -> Set[BadgeTuple]:
    """Return all badges that have been awarded to the user."""
    badges = Badge.query \
{'fraction_non_alphanumeric': 0.13205678441729943, 'fraction_numerical': 0.005282271376691978, 'mean_word_length': 3.4558823529411766, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '1065658', 'n_tokens_mistral': 1017, 'n_tokens_neox': 956, 'n_words': 268}