<commit_before>from __future__ import unicode_literals

from smartmin.models import SmartModel
from django.db import models
from django.db.models import Avg, Sum, Q

WEEK_AVG = 8
AVG_GAMES = 48

class Player(SmartModel):
    name = models.CharField(max_length=128,
                            help_text="The player's name")

    def get_games(self):
        scores = list(PlayerScore.objects.filter(player=self).order_by('-match__date'))

        # create a dict of match / game to score
        match_games = dict()
        for score in scores:
            match_games['%d_%d' % (score.match_id, score.game)] = [score]

        q = Q(match_id__lt=0)
        for score in scores:
            q |= (Q(match=score.match, game=score.game) & ~Q(player=self))

        for opp_score in PlayerScore.objects.filter(q):
            match_games['%d_%d' % (opp_score.match_id, opp_score.game)].append(opp_score)

        games = []
        for score in scores:
            opp_score = match_games['%d_%d' % (score.match_id, score.game)][1]
            games.append(dict(date=score.match.date, score=score.score, opponent=opp_score.player.name, opp_score=opp_score.score))

        return games

    def get_week_averages(self):
        scores = list(PlayerScore.objects.filter(player=self).order_by('match__date'))

        weeks = list()
        current_week = dict()

        for score in scores:
            if not current_week or score.match.date != current_week['date']:
                if current_week:
                    current_week['avg'] = float(current_week['total']) / current_week['games']
                    weeks.append(current_week)

                current_week = dict(date=score.match.date, total=score.score, games=1)

            else:
                current_week['total'] += score.score
                current_week['games'] += 1

        if current_week:
            current_week['avg'] = float(current_week['total']) / current_week['games']
            weeks.append(current_week)

        # now build the week avg for each week
        for i in range(len(weeks)):
            game_count = sum(w['games'] for w in weeks[max(0, i-WEEK_AVG):i+1])
            game_total = sum(w['total'] for w in weeks[max(0, i-WEEK_AVG):i+1])
            weeks[i]['running_avg'] = game_total / float(game_count)

        return weeks

    def avg(self, before_date):
        last = list(PlayerScore.objects.filter(match__date__lt=before_date, player=self).order_by('-match__date')[:AVG_GAMES])

        # less than 4 games? default to average
        if len(last) < 6:
            return 7
        else:
            # build up our scores and sort them
            scores = sorted([ps.score for ps in last])

            # figure out our quartile size
            quartile = last.count() / 4

            # strip off the top and bottom quartile
            scores = scores[quartile:-quartile]

            return float(sum(scores)) / len(scores)

    def set_season(self, season):
        self.season = season

        # get last games
        last = list(PlayerScore.objects.filter(player=self).order_by('-match__date')[:AVG_GAMES])

        self.wins = sum([1 if ps.score == 10 else 0 for ps in last])
        self.losses = sum([1 if ps.score < 10 else 0 for ps in last])

        self.games = len(last)
        self.points = sum([ps.score for ps in last])

        # build up our scores and sort them
        scores = sorted([ps.score for ps in last])

        # figure out our quartile size
        quartile = len(scores) / 4 if len(scores) > 0 else 0

        # strip off the top and bottom quartile
        scores = scores[quartile:-quartile]
        self.avg = float(sum(scores)) / len(scores) if self.games > 0 else 0

        q = Q(match_id__lt=0)
        for score in last:
            q |= (Q(match=score.match, game=score.game) & ~Q(player=self))

        last_opp = list(PlayerScore.objects.filter(q))

        self.opponent_points = sum([ps.score for ps in last_opp])
        self.mpg = float(self.points - self.opponent_points) / self.games if self.games > 0 else 0

    def games(self):
        return PlayerScore.objects.filter(match__season=self.season, player=self).count()

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ('name',)



class Season(SmartModel):
    name = models.CharField(max_length=128,
                            help_text="Season Name")
    start_date = models.DateField(help_text="When the season started")
    end_date = models.DateField(help_text="When the season ended")
<commit_msg>72 games for average top 8th and bottom 8th dropped<commit_after>from __future__ import unicode_literals

from smartmin.models import SmartModel
from django.db import models
from django.db.models import Avg, Sum, Q

WEEK_AVG = 8
AVG_GAMES = 72

class Player(SmartModel):
    name = models.CharField(max_length=128,
                            help_text="The player's name")

    def get_games(self):
        scores = list(PlayerScore.objects.filter(player=self).order_by('-match__date'))

        # create a dict of match / game to score
        match_games = dict()
        for score in scores:
            match_games['%d_%d' % (score.match_id, score.game)] = [score]

        q = Q(match_id__lt=0)
        for score in scores:
            q |= (Q(match=score.match, game=score.game) & ~Q(player=self))

        for opp_score in PlayerScore.objects.filter(q):
            match_games['%d_%d' % (opp_score.match_id, opp_score.game)].append(opp_score)

        games = []
        for score in scores:
            opp_score = match_games['%d_%d' % (score.match_id, score.game)][1]
            games.append(dict(date=score.match.date, score=score.score, opponent=opp_score.player.name, opp_score=opp_score.score))

        return games

    def get_week_averages(self):
        scores = list(PlayerScore.objects.filter(player=self).order_by('match__date'))

        weeks = list()
        current_week = dict()

        for score in scores:
            if not current_week or score.match.date != current_week['date']:
                if current_week:
                    current_week['avg'] = float(current_week['total']) / current_week['games']
                    weeks.append(current_week)

                current_week = dict(date=score.match.date, total=score.score, games=1)

            else:
                current_week['total'] += score.score
                current_week['games'] += 1

        if current_week:
            current_week['avg'] = float(current_week['total']) / current_week['games']
            weeks.append(current_week)

        # now build the week avg for each week
        for i in range(len(weeks)):
            game_count = sum(w['games'] for w in weeks[max(0, i-WEEK_AVG):i+1])
            game_total = sum(w['total'] for w in weeks[max(0, i-WEEK_AVG):i+1])
            weeks[i]['running_avg'] = game_total / float(game_count)

        return weeks

    def avg(self, before_date):
        last = list(PlayerScore.objects.filter(match__date__lt=before_date, player=self).order_by('-match__date')[:AVG_GAMES])

        # less than 6 games? default to average
        if len(last) < 6:
            return 7
        else:
            # build up our scores and sort them
            scores = sorted([ps.score for ps in last])

            # figure out our eighth size
            eighth = len(scores) / 8

            # strip off the top and bottom quartile
            scores = scores[eighth:-eighth]

            return float(sum(scores)) / len(scores)

    def set_season(self, season):
        self.season = season

        # get last games
        last = list(PlayerScore.objects.filter(player=self).order_by('-match__date')[:AVG_GAMES])

        self.wins = sum([1 if ps.score == 10 else 0 for ps in last])
        self.losses = sum([1 if ps.score < 10 else 0 for ps in last])

        self.games = len(last)
        self.points = sum([ps.score for ps in last])

        # build up our scores and sort them
        scores = sorted([ps.score for ps in last])

        # figure out our quartile size
        eighth = len(scores) / 8 if len(scores) > 0 else 0

        # strip off the top and bottom quartile
        scores = scores[eighth:-eighth]
        self.avg = float(sum(scores)) / len(scores) if len(scores) > 0 else 0

        q = Q(match_id__lt=0)
        for score in last:
            q |= (Q(match=score.match, game=score.game) & ~Q(player=self))

        last_opp = list(PlayerScore.objects.filter(q))

        self.opponent_points = sum([ps.score for ps in last_opp])
        self.mpg = float(self.points - self.opponent_points) / self.games if self.games > 0 else 0

    def games(self):
        return PlayerScore.objects.filter(match__season=self.season, player=self).count()

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ('name',)



class Season(SmartModel):
    name = models.CharField(max_length=128,
                            help_text="Season Name")
    start_date = models.DateField(help_text="When the season started")
    end_date = models.DateField(help_text="When the season ended")

{'fraction_non_alphanumeric': 0.11363636363636363, 'fraction_numerical': 0.008522727272727272, 'mean_word_length': 2.3576669112252384, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4081293', 'n_tokens_mistral': 2915, 'n_tokens_neox': 2703, 'n_words': 697}