<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
from operator import attrgetter
from itertools import groupby
from collections import defaultdict, OrderedDict

from colorama import Fore, init, Style
from sqlalchemy import cast, String

from db.common import session_scope
from db.team import Team
from db.game import Game
from db.team_game import TeamGame
from db.division import Division


STREAK_REGEX = re.compile(R"(\w)\1*")
MAX_LINE_LENGTH = 76


def get_data(season):
    """
    Retrieves necessary data for specified season.
    """
    # retrieving all team games for specified season
    with session_scope() as session:
        team_games = session.query(
            TeamGame).filter(cast(
                TeamGame.game_id, String).like("%d02%%" % season)).all()

        divisions = session.query(Division).filter(
            Division.season == season).all()

        last_game_date = max(map(
            attrgetter('date'),
            session.query(Game).filter(Game.season == season).all()))

    return team_games, divisions, last_game_date


def compile_records(team_games):
    """
    Compiles team records from specified team-per-game items.
    """
    records = dict()

    for tg in sorted(team_games):
        if tg.team_id not in records:
            records[tg.team_id] = defaultdict(int)
        # aggregating games played
        records[tg.team_id]['gp'] += 1
        # aggregating official goals for and against
        records[tg.team_id]['ogf'] += tg.score
        records[tg.team_id]['oga'] += tg.score_against
        # aggregating actual goals for and against
        records[tg.team_id]['gf'] += tg.goals_for
        records[tg.team_id]['ga'] += tg.goals_against
        # aggregating official wins and losses
        records[tg.team_id]['ow'] += tg.win
        records[tg.team_id]['ol'] += tg.regulation_loss
        if any([tg.shootout_loss, tg.overtime_loss]):
            records[tg.team_id]['ootl'] += 1
        # aggregating regulation wins
        records[tg.team_id]['w'] += tg.regulation_win
        # aggregating official regulation or overtime wins (row)
        if any([tg.regulation_win, tg.overtime_win]):
            records[tg.team_id]['orow'] += 1
        # *regulation* rows are just regulation wins
        records[tg.team_id]['row'] = records[tg.team_id]['w']
        # aggregating ties, i.e. games that went to overtime or shootout
        if any([
                tg.overtime_win, tg.shootout_win,
                tg.overtime_loss, tg.shootout_loss]):
                    records[tg.team_id]['t'] += 1
        # decreasing actual goals scored by one for an overtime win
        if tg.overtime_win:
            records[tg.team_id]['gf'] -= 1
        # decreasing actual goals allowed by one for an overtime loss
        if tg.overtime_loss:
            records[tg.team_id]['ga'] -= 1
        # decreasing actual goal totals by empty-net goals for and against
        records[tg.team_id]['gf'] -= tg.empty_net_goals_for
        records[tg.team_id]['ga'] -= tg.empty_net_goals_against
        # calculating goal differentials
        records[tg.team_id]['ogd'] = (
            records[tg.team_id]['ogf'] - records[tg.team_id]['oga'])
        records[tg.team_id]['gd'] = (
            records[tg.team_id]['gf'] - records[tg.team_id]['ga'])
        # calculating points
        records[tg.team_id]['opts'] = (
            records[tg.team_id]['ow'] * 2 + records[tg.team_id]['ootl'])
        records[tg.team_id]['pts'] = (
            records[tg.team_id]['w'] * 2 + records[tg.team_id]['t'])
        # calculating point percentages
        records[tg.team_id]['oppctg'] = round(
            records[tg.team_id]['opts'] / (
                records[tg.team_id]['gp'] * 2.0), 3)
        records[tg.team_id]['ppctg'] = round(
            records[tg.team_id]['pts'] / (
                records[tg.team_id]['gp'] * 2.0), 3)
        # registering sequence of official game outcomes, denoting overtime/
        # shootout losses separately
        if not records[tg.team_id]['osequence']:
            records[tg.team_id]['osequence'] = ''
        if tg.win:<commit_msg>Disregard goals scored in overtime for regulation standings, fixes #20<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
from operator import attrgetter
from itertools import groupby
from collections import defaultdict, OrderedDict

from colorama import Fore, init, Style
from sqlalchemy import cast, String

from db.common import session_scope
from db.team import Team
from db.game import Game
from db.team_game import TeamGame
from db.division import Division


STREAK_REGEX = re.compile(R"(\w)\1*")
MAX_LINE_LENGTH = 76


def get_data(season):
    """
    Retrieves necessary data for specified season.
    """
    # retrieving all team games for specified season
    with session_scope() as session:
        team_games = session.query(
            TeamGame).filter(cast(
                TeamGame.game_id, String).like("%d02%%" % season)).all()

        divisions = session.query(Division).filter(
            Division.season == season).all()

        last_game_date = max(map(
            attrgetter('date'),
            session.query(Game).filter(Game.season == season).all()))

    return team_games, divisions, last_game_date


def compile_records(team_games):
    """
    Compiles team records from specified team-per-game items.
    """
    records = dict()

    for tg in sorted(team_games):
        if tg.team_id not in records:
            records[tg.team_id] = defaultdict(int)
        # aggregating games played
        records[tg.team_id]['gp'] += 1
        # aggregating official goals for and against
        records[tg.team_id]['ogf'] += tg.score
        records[tg.team_id]['oga'] += tg.score_against
        # aggregating actual goals for and against
        records[tg.team_id]['gf'] += tg.goals_for
        records[tg.team_id]['ga'] += tg.goals_against
        # aggregating official wins and losses
        records[tg.team_id]['ow'] += tg.win
        records[tg.team_id]['ol'] += tg.regulation_loss
        if any([tg.shootout_loss, tg.overtime_loss]):
            records[tg.team_id]['ootl'] += 1
        # aggregating regulation wins
        records[tg.team_id]['w'] += tg.regulation_win
        # aggregating official regulation or overtime wins (row)
        if any([tg.regulation_win, tg.overtime_win]):
            records[tg.team_id]['orow'] += 1
        # *regulation* rows are just regulation wins
        records[tg.team_id]['row'] = records[tg.team_id]['w']
        # aggregating ties, i.e. games that went to overtime or shootout
        if any([
                tg.overtime_win, tg.shootout_win,
                tg.overtime_loss, tg.shootout_loss]):
                    records[tg.team_id]['t'] += 1
        # decreasing actual goals scored by one for an overtime win
        if tg.overtime_win:
            records[tg.team_id]['gf'] -= 1
        # decreasing actual goals allowed by one for an overtime loss
        if tg.overtime_loss:
            records[tg.team_id]['ga'] -= 1
        # decreasing regulation goal totals by empty-net goals for and against
        records[tg.team_id]['gf'] -= tg.empty_net_goals_for
        records[tg.team_id]['ga'] -= tg.empty_net_goals_against
        # decreasing regulation goal totals by overtime goals for and against
        if tg.overtime_win:
            records[tg.team_id]['gf'] -= 1
        if tg.overtime_loss:
            records[tg.team_id]['ga'] -= 1
        # calculating goal differentials
        records[tg.team_id]['ogd'] = (
            records[tg.team_id]['ogf'] - records[tg.team_id]['oga'])
        records[tg.team_id]['gd'] = (
            records[tg.team_id]['gf'] - records[tg.team_id]['ga'])
        # calculating points
        records[tg.team_id]['opts'] = (
            records[tg.team_id]['ow'] * 2 + records[tg.team_id]['ootl'])
        records[tg.team_id]['pts'] = (
            records[tg.team_id]['w'] * 2 + records[tg.team_id]['t'])
        # calculating point percentages
        records[tg.team_id]['oppctg'] = round(
            records[tg.team_id]['opts'] / (
                records[tg.team_id]['gp'] * 2.0), 3)
        records[tg.team_id]['ppctg'] = round(
            records[tg.team_id]['pts'] / (
                records[tg.team_id]['gp'] * 2.0), 3)
        # registering sequence of official game outcomes, denoting overtime/
        # shootout losses separately
        if not records[tg.team_id]['osequence']:
            records[tg.team_id]['osequence'] = ''
        if tg.win:
{'fraction_non_alphanumeric': 0.12835786031301097, 'fraction_numerical': 0.005138986218173324, 'mean_word_length': 2.534048699958729, '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': '3337713', 'n_tokens_mistral': 2695, 'n_tokens_neox': 2556, 'n_words': 604}