def player_stats(self):
        played = self.result_player.count()
        won = float(self.result_player.filter(result=9).count())
        lost = played - won  # more efficient than doing a count on the object

        # safe division (not by 0)
        if played != 0:
            win_rate = won / float(played) * 100.00
            # 2 points for winning, 1 for playing
            additional_points = ((won * 2) + played) / played
        else:
            return {
                'played': "-",
                'win_rate': "- %",
                'average': "-"
            }

        # work out the average with additional points
        average = list(self.result_player.aggregate(Avg('result')).values())[0]
        average_with_additional = average + additional_points

        leagues = self.league_set.filter(player=self)

        match_count = 0
        for league in leagues:
            # count other players in ladder minus the player to get games
            match_count += league.ladder.league_set.count() - 1

        completion_rate = float(played) / float(match_count) * 100.00

        return {
            'played': played,
            'win_rate': "{0:.2f} %".format(win_rate),
            'completion_rate': "{0:.2f} %".format(completion_rate),
            'average': "{0:.2f}".format(average_with_additional)
        }
Calculates stats about the players historical performance.
dataset/ETHPy150Open jzahedieh/django-tennis-ladder/ladder/models.py/Player.player_stats