python: calendario fantalega
1 Settembre 2014
Ecco una applicazione per creare un calendario con n squadre Pari.
Non tiene conto della giornata di riposo in caso di squadre dispari!
Testandola, dovrebbe tenere conto anche del “casa”, “fuoricasa”.
codice view:
# -*- coding: utf-8 -*-# '''VIEW Module for FantaCalendario: Graphic library: wx Bindings are defined under CONTROLLER Module. ''' import wx, os class FrameCal(wx.Frame): '''Main frame for FantaPython application''' def __init__(self, *args, **kwargs): self.nsq = kwargs.pop('nsq') wx.Frame.__init__(self, *args, **kwargs) self.panel = wx.Panel(self) self.panel.SetBackgroundColour('Pink') sq_cont = 1 xpos, ypos = 100, 20 while sq_cont <= self.nsq: wx.StaticText(self.panel, wx.ID_NEW, 'squadra%s' % sq_cont, pos = ((xpos - 85), ypos), size = (70, 25)) wx.TextCtrl(self.panel, wx.ID_NEW, pos = (xpos, ypos), size = (140, 25)) ypos += 25 sq_cont += 1 self.btn_create = wx.Button(self.panel, wx.ID_NEW, 'Crea Calendario', size = (240, 25), pos = (0, (ypos))) self.SetSizeWH(250, (ypos + 65)) self.Centre() self.Show() class InfoMessage(wx.MessageDialog): '''Simple message Dialog''' def __init__(self, parent, message): wx.MessageDialog.__init__(self, parent, message, 'core info', wx.OK | wx.ICON_EXCLAMATION) def get_choice(self): '''get the state of the user choice''' if self.ShowModal() == wx.ID_OK: self.Destroy() class ChoiceMessage(wx.MessageDialog): '''Simple choice message Dialog''' def __init__(self, parent, message): wx.MessageDialog.__init__(self, parent, message, 'Core question', wx.YES_NO | wx.ICON_QUESTION) def get_yes(self): '''get True if YES is clicked''' if self.ShowModal() == wx.ID_YES: return True else: self.Destroy() class EntryDialog(wx.TextEntryDialog): '''Simple Text Entry Dialog''' def __init__(self, parent, msg, value): wx.TextEntryDialog.__init__(self, parent, msg, 'Core request', defaultValue = value, style = wx.OK) def get_choice(self): '''get the state of the user choice''' if self.ShowModal() == wx.ID_OK: response = self.GetValue() self.Destroy() return response class FileSaver(wx.FileDialog): '''Class for file Saver''' def __init__(self): self.f_save = None wildcard = "File Calendario (*.txt)|*.txt|" \ "Tutti i files (*.*)|*.*" wx.FileDialog.__init__(self, None, "salva il file", os.getcwd(), "", wildcard, wx.SAVE) if self.ShowModal() == wx.ID_OK: print "...salvo il file in: %s" % (self.GetPath()) self.output = self.GetPath() self.f_save = open(self.output, 'w') else: print "operazione salvataggio annullata" self.f_save = None self.Destroy() self.Destroy() def main(): '''app starter''' app = wx.PySimpleApp() n_sq = 8 # number of teams FrameCal(None, wx.ID_NEW, "Calendario 1.1", nsq = n_sq) app.MainLoop() if __name__ == '__main__': main()
codice controller:
'''FantaCalendario v 2.0 Controller Module for FantaCalendario Application: All the binding method to frame widget are defined here''' import wx from View import (FrameCal, EntryDialog, InfoMessage, FileSaver) from copy import copy class Team(object): '''A team class''' def __init__(self, name, status=True): self.name = name self.casa = status def set_casa(self): """set the status 'casa' to True""" self.casa = True def set_fuori(self): """set the status 'fuori' to True""" self.casa = False class Controller(object): '''Controller class for MVC-like pattern''' def __init__(self): n_sq = int(EntryDialog(None, "Quante Squadre partecipano?", '8').get_choice()) self.gui = FrameCal(None, wx.ID_NEW, "Calendario 2.0", nsq=n_sq) self.gui.Bind(wx.EVT_BUTTON, self.on_create) def on_create(self, evt): '''create every "team_x vs team_y" combinations possible''' panel = evt.GetEventObject().GetParent() teams = [] err = 0 for widget in panel.GetChildren(): if isinstance(widget, wx.TextCtrl): if widget.GetValue() != '': teams.append(Team(widget.GetValue())) else: err += 1 if err > 0: msg = "mancano uno o piu' nomi squadra" InfoMessage(panel.GetParent(), msg).get_choice() else: print teams crea_calendario(teams) self.gui.Close() def crea_calendario(lista): '''crea gli accoppiamenti da una lista''' matrix = [] cont = 0 while cont < len(lista): matrix.append([None] * len(lista)) cont += 1 matrix[0] = lista # intestazione # Riga Intestazione invertita meno l'ultima squadra row2 = copy(lista) row2.pop() row2.reverse() matrix[1][0:(len(lista) - 1)] = row2[0:(len(lista) - 1)] # Composizione tabella prima FASE i = 1 while i < len(lista): k = 1 for item in matrix[i]: try: matrix[i + 1][k] = item matrix[i + 1][0] = matrix[i + 1][(len(lista) - 1)] matrix[i + 1][(len(lista) - 1)] = None k += 1 except IndexError: break i += 1 # Composizione tabella seconda FASE row_m = 1 while row_m < len(lista): for item_a in matrix[0]: for item_b in matrix[row_m]: if matrix[0].index(item_a) == matrix[row_m].index(item_b): if item_a == item_b: matrix[row_m][matrix[row_m].index(item_b)] = lista[-1] matrix[row_m][(len(lista) - 1)] = item_b row_m += 1 # Stampa giornata cont = 1 fbs = FileSaver() outputfile = fbs.f_save while cont < len(lista): outputfile.write("\nGiornata n. " + str(cont) + "\n") andata = [] for sq_a in matrix[0]: for sq_b in matrix[cont]: if matrix[0].index(sq_a) == matrix[cont].index(sq_b): if sq_b not in andata or sq_a not in andata: if sq_a.casa == True: andata.append(sq_a) andata.append(sq_b) outputfile.write(sq_a.name + "-" + sq_b.name + "\n") sq_a.set_fuori() sq_b.set_casa() else: andata.append(sq_b) andata.append(sq_a) outputfile.write(sq_b.name + "-" + sq_a.name + "\n") sq_a.set_casa() sq_b.set_fuori() cont += 1 outputfile.close() def main(): '''starter''' app = wx.PySimpleApp() Controller() app.MainLoop() if __name__ == '__main__': main()
il funzionamento è semplicissimo:
si avvia, si sceglie il numero di squadre:
si mettono i nomi delle squadre:
si preme il pulsante crea e si sceglie un nome per la destinazione in txt, che poi potrà essere importata ad esempio in excel.
Giornata n. 1 A-C B-D Giornata n. 2 D-A C-B Giornata n. 3 A-B D-C
link binario (exe)
Commenti recenti