Home > python, wxPython > Python: creazione calendario Fantalega

Python: creazione calendario Fantalega

import wx
from random import shuffle


class Team(object):
    def __init__(self, name, home=True):
        self.name = name
        self.home = home

    def set_visit(self):
        self.home = False

    def set_home(self):
        self.home = True

    def __repr__(self):
        return "<Team %r>" % self.name


def get_teams():
    app = wx.App()
    app.MainLoop()
    teams = []
    dlg = wx.TextEntryDialog(None, "number of teams?", 'INFO', '')
    if dlg.ShowModal() == wx.ID_OK:
        nteams  = int(dlg.GetValue())
        team_no = 1
        while team_no <= nteams:
            dlgt = wx.TextEntryDialog(None, "team name n. %d?"  % (team_no),
                                      'INFO', '')
            if dlgt.ShowModal() == wx.ID_OK:
                name = dlgt.GetValue()
                if name in [t.name for t in teams]:
                    print "ERROR: team <%s> already exists" % name
                    dlgt.SetValue('')
                else:
                    teams.append(Team(name=name))
                    dlgt.SetValue('')
                    team_no += 1
    return teams


class Calendar(object):
    def __init__(self, teams, rounds=2):
        self.teams = teams
        self.max_rounds = rounds
        self.team1_home = True
        self.team2_home = True
        self.matrix = []
        self.first_round_cal = []

    def _first_step_season(self):
        """
        _first_step_season()

        create a matrix to fill with teams"""
        ## init matrix
        self.matrix = [[None]*len(teams) for team in teams]
        ## fill first matrix row with all teams (header)
        self.matrix[0] = self.teams

        # reversed header without the last team
        row2 = self.teams[:][:-1]
        row2.reverse()
        self.matrix[1][0:(len(self.teams) - 1)] = row2[0:(len(self.teams) - 1)]

        # Table composition: first step
        i = 1
        while i < len(self.teams):
            k = 1
            for item in self.matrix[i]:
                try:
                    self.matrix[i + 1][k] = item
                    self.matrix[i + 1][0] = self.matrix[i + 1][(
                        len(self.teams) - 1)]
                    self.matrix[i + 1][(len(self.teams) - 1)] = None
                    k += 1
                except IndexError:
                    break
            i += 1

        # Table composition: second step
        row_m = 1
        while row_m < len(self.teams):
            for item_a in self.matrix[0]:
                for item_b in self.matrix[row_m]:
                    if self.matrix[0].index(item_a) == \
                       self.matrix[row_m].index(item_b):
                        if item_a == item_b:
                            self.matrix[row_m][self.matrix[row_m].index(item_b)] = \
                                self.teams[-1]
                            self.matrix[row_m][(len(self.teams) - 1)] = item_b
            row_m += 1

        cal = []
        day = 1
        while day < len(self.teams):
            first_round = []
            for team1 in self.matrix[0]:
                for team2 in self.matrix[day]:
                    if self.matrix[0].index(team1) == \
                       self.matrix[day].index(team2):
                        if team2 not in first_round or team1 not in first_round:
                            if self.team1_home is True:
                                first_round.append(team1)
                                first_round.append(team2)
                                cal.append((day, team1, team2))
                                self.team1_home = False
                                self.team2_home = True
                            else:
                                first_round.append(team2)
                                first_round.append(team1)
                                cal.append((day, team2, team1))
                                self.team1_home = True
                                self.team2_home = False
            day += 1
        return cal


    def _second_step_season(self):
        """
        _second_step_season() -> list

        Create the second round for season
        """
        return [(int(n) + len(teams) - 1, team2, team1)
                for n, team1, team2 in self.first_round_cal]


    def build_season(self):
        """
        build_season() -> list
        """
        shuffle(self.teams)
        rounds = 1
        self.first_round_cal = self._first_step_season()
        while rounds < self.max_rounds:
            cal = self._second_step_season()
            self.first_round_cal += cal
            rounds += 1
        return self.first_round_cal


if __name__ == '__main__':
    teams = get_teams()
    if teams:
        cal = Calendar(teams=teams)
        season = cal.build_season()
        for match in season:
            print match
    else:
        print "WARNING: no teams saved"

Annunci
Categorie:python, wxPython Tag:,

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: