Python: FantaStat 1.1 Light
Si sta avvicinando il mercato di riparazione, quindi ho creato un piccolo tool con Python, per tutti i FantaAllenatori, che non vogliono arrivare impreparati all’asta invernale.
download:
eseguibile win
sorgenti python
Funziona come i fratelli maggiori FantaManager Full e
FantaManager Light, cioè si importano i file MCCxx.txt,
e si consultano le varie medie voto, gol total/bancaldo.wordpress.com/2011/08/05/lista-calciatori-gazzetta-2011-2012/”>file MCCxx.txt,
e si consultano le varie medie voto, gol totali, presenze,
di ogni singolo giocatore.
L’interfaccia è semplicissima (utilizzate le solite wx):
un bottone per importare il file MCC, un bottone per uscire,
una list-control per visualizzare i dati del database ed
un radiobox per visualizzare i calciatori, per ruolo.
Le colonne della list-control, ordinano in modo di, presenze,
di ogni singolo giocatore.
L’interfaccia è semplicissima (utilizzate le solite wx):
un bottone per importare il file MCC, un bottone per uscire,
una list-control per visualizzare i dati del database ed
un radiobox per visualizzare i calciatori, per ruolo.
Le colonne della list-control, ordinano in modo decrescente i
dati, eccetto nome e squadra (che francamente non incidono
sulla qualità della ricerca)
Il database è generato e gestito con Sqlite e SqlAlchemy.
Gli oggetti riferiti al db ed sqla (tabelle, campi ecc) sono definiti
in non-declarative perchè mi piace semplificare il porting su web con
Pylons (ho trovato difficoltà quando ho definito gli oggetti in
declarative)
Come sempre, ho cercato di utilizzare il pattern MVC,
spero riducendo al minimo gli errori di stile.
NOTA:
Ricordarsi di posizionare le dirs /images e /giornate
dove si troverà l’eseguibile o i sorgenti.
Qui i codici (sicuramente migliorabili):
Model:
# -*- coding: utf-8 -*-# # DataLight.py '''MODEL Module for FantaStat Application: Database engine: provided by Sqlite Database O.R.M: provided by SqlAlchemy (no_declarative) All data for fantapython manage are contained here. All the O.R.M. operation on data are defined here as Model-Class methods. ''' from sqlalchemy import create_engine, Column, Integer, Float from sqlalchemy import ForeignKey, Table from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import aliased from sqlalchemy.orm import sessionmaker from sqlalchemy import orm, func, desc, asc from sqlalchemy import MetaData from sqlalchemy.types import Unicode class Giocatore(object): '''class for sqla mapper''' pass class Voto(object): '''class for sqla mapper''' pass class Model(object): '''Model class with common application data''' base = declarative_base() engine = create_engine('sqlite:///fantastat.db', echo = False) metadata = MetaData() giocatore_table = Table('giocatori', metadata, Column('id', Integer(), primary_key=True), Column('idgaz', Integer()), Column('nome', Unicode(255)), Column('squadra', Unicode(255)), Column('valore', Integer()), Column('ruolo', Unicode(255)), ) voto_table = Table('voti', metadata, Column('id', Integer(), primary_key=True), Column('voto', Float()), Column('gg_', Integer()), Column('val', Integer()), Column('gol_fatti', Integer()), Column('pres', Integer()), Column('gol_sub', Integer()), Column('assist', Integer()), Column('ammo', Integer()), Column('esp', Integer()), Column('rig_tir', Integer()), Column('rig_ok', Integer()), Column('rig_no', Integer()), Column('voto_s', Float()), ) # M2M relation giocatori_voti_table = Table('giocatori_voti', metadata, Column('giocatori_id', Integer, ForeignKey('giocatori.id')), Column('voti_id', Integer, ForeignKey('voti.id')), ) # Map SQLAlchemy table definitions to python classes orm.mapper(Giocatore, giocatore_table, properties={ 'voti':orm.relation(Voto, secondary = giocatori_voti_table),}) orm.mapper(Voto, voto_table, properties={ 'giocatori':orm.relation(Giocatore, secondary = giocatori_voti_table),}) def __init__(self): Model.metadata.create_all(Model.engine) session = sessionmaker(bind = Model.engine) self.session = session() self.ruoli = ['portiere', 'difensore', 'ccampista', 'attaccante'] self.value_state, self.val_right_state = 'asc', 'asc' self.name_state, self.name_right_state = 'asc', 'asc' self.player_to_change = None self.team_a, self.ruolo = '', '' self.cols = {0: Giocatore.nome, 1: Giocatore.squadra, 2: Voto.voto, 3: Voto.pres, 4: (Voto.gol_fatti + Voto.rig_ok), 5: Voto.gol_sub, 6: Voto.assist, 7: Voto.ammo, 8: Voto.esp, 9: Voto.rig_tir, 10: Voto.rig_no, 11: Giocatore.valore} def get_data_players(self, ruolo, item = Voto.voto): '''get the avg of vote and all total data by role''' query = self.session.query(Giocatore).filter( Giocatore.ruolo == ruolo).subquery() adalias = aliased(Giocatore, query) query_p = self.session.query(adalias.nome, adalias.idgaz, func.avg(Voto.voto), func.sum(Voto.pres), func.sum(Voto.gol_fatti+Voto.rig_ok), func.sum(Voto.gol_sub), func.sum(Voto.assist), func.sum(Voto.ammo), func.sum(Voto.esp), func.sum(Voto.rig_tir), func.sum(Voto.rig_no), adalias.valore, adalias.squadra).join( # < JOIN adalias, Voto.giocatori).filter(Voto.pres > 0) .group_by(adalias).order_by( # < GROUP & SORT asc(func.avg(item))).all() return query_p def get_players_sort_name(self, ruolo): '''Get all available goalkeeper''' if self.name_state == 'asc': query = self.session.query(Giocatore).filter( Giocatore.ruolo == '%s' % ruolo ).order_by(desc(Giocatore.idgaz)).all() else: query = self.session.query(Giocatore).filter( Giocatore.ruolo == '%s' % ruolo ).order_by((Giocatore.idgaz)).all() return query def get_inserted_days(self): # for checking days '''get inserted votes-days for import-votes checking''' ggs = [item[0] for item in self.session.query(func.distinct(Voto.gg_)).all()] return ggs def get_player_by_idgaz(self, idgaz): '''Get the player by idgaz''' player = self.session.query(Giocatore).filter( Giocatore.idgaz == idgaz).first() return player def do_players_exist(self): # check at the first time '''Return True if players are already imported''' if self.session.query(Giocatore).count() != 0: print "Gazzetta players present in database" return True else: print "Gazzetta players must be imported first!" return False def import_new_player(self, idgaz, nome, squadra, valore, ruolo): '''Add new player on "giocatori" table''' player = Giocatore() player.idgaz = idgaz player.nome = nome player.squadra = squadra player.valore = valore player.ruolo = ruolo self.session.add(player) def upload_votes(self, item): '''Save the data-tuble of votes file into database''' print "UPLOAD", item rig_ok = int(item[10]) - int(item[11]) voto = Voto() voto.voto = item[1] voto.gg_ = item[2] voto.val = item[3] voto.gol_fatti = item[4] voto.pres = item[5] voto.gol_sub = item[6] voto.assist = item[7] voto.ammo = item[8] voto.esp = item[9] voto.rig_tir = item[10] voto.rig_ok = rig_ok voto.rig_no = item[11] voto.voto_s = item[12] self.session.add(voto) #m2m append voto.giocatori.append(self.get_player_by_idgaz(item[0])) # pylint: disable=E1101,C0301 return True def total_commit(self): '''external commit to use outside loop''' self.session.commit() def update_player_by_idgaz(self, idgaz, nome, squadra, valore, ruolo): '''Get the player by idgaz''' player = self.session.query(Giocatore).filter( Giocatore.idgaz == idgaz).first() player.idgaz = idgaz player.nome = nome player.squadra = squadra player.valore = int(valore) player.ruolo = ruolo def get_data_players_by_value(self, ruolo, jolly): '''generic query that uses subquery, alias and join function: return the sum of a Giocatore.value itering the votes, by idgaz''' query = self.session.query(Giocatore).filter( Giocatore.ruolo == ruolo).subquery() adalias = aliased(Giocatore, query) query_p = self.session.query(adalias.nome, adalias.idgaz, func.avg(Voto.voto), func.sum(Voto.pres), func.sum(Voto.gol_fatti+Voto.rig_ok), func.sum(Voto.gol_sub), func.sum(Voto.assist), func.sum(Voto.ammo), func.sum(Voto.esp), func.sum(Voto.rig_tir), func.sum(Voto.rig_no), adalias.valore, adalias.squadra).join( # < JOIN adalias, Voto.giocatori).filter(Voto.pres > 0) .group_by(adalias).order_by( # < GROUP & SORT asc(func.sum(jolly))).all() return query_p def get_data_players_by_valore(self, ruolo): '''generic query that uses subquery, alias and join function: return all total data player sorted by value''' query = self.session.query(Giocatore).filter( Giocatore.ruolo == ruolo).subquery() adalias = aliased(Giocatore, query) query_p = self.session.query(adalias.nome, adalias.idgaz, func.avg(Voto.voto), func.sum(Voto.pres), func.sum(Voto.gol_fatti+Voto.rig_ok), func.sum(Voto.gol_sub), func.sum(Voto.assist), func.sum(Voto.ammo), func.sum(Voto.esp), func.sum(Voto.rig_tir), func.sum(Voto.rig_no), adalias.valore, adalias.squadra).join( # < JOIN adalias, Voto.giocatori).filter(Voto.pres > 0) .group_by(adalias).order_by( # < GROUP & SORT asc(adalias.valore)).all() return query_p def main(): '''test starter''' mod = Model() if __name__ == '__main__': main()
Qui il codice relativo al View:
# -*- coding: utf-8 -*-# # ViewLight.py '''VIEW Module for FantaPython Application: Graphic library: provided by wx All the frames and their children widgets, are defined here. Binding are defined under CONTROLLER Module. Every Frame as a panel child and every widgets have panel as first parent. ''' import wx import os from wx import html as wxhtml from wx.lib.buttons import GenBitmapTextButton class FrameVotiPlayers(wx.Frame): '''Frame for votes consulting''' def __init__(self, *args, **kwargs): ruoli = ['portiere', 'difensore', 'ccampista', 'attaccante'] wx.Frame.__init__(self, *args, **kwargs) self.panel = wx.Panel(self, -1) self.panel.SetBackgroundColour('Pink') self.btn_import = GenBitmapTextButton(self.panel, wx.ID_NEW, wx.Bitmap('images\import_tr.png'), 'Import players/votes'.rjust(30), size = (350, 40), pos = (5, 425)) self.btn_exit = GenBitmapTextButton(self.panel, wx.ID_ANY, wx.Bitmap('images\quit.png'), 'Exit'.rjust(30), size = (335, 40), pos = (355, 425)) self.rboxruoli = wx.RadioBox(self.panel, wx.ID_ANY, "ruoli", (100, 100), wx.DefaultSize, ruoli, 4, wx.RA_SPECIFY_COLS) self.rboxruoli.Enable() self.listdati = wx.ListCtrl(self.panel, wx.NewId(), size = (690, 350), style = wx.LC_REPORT | wx.LC_HRULES | wx.LC_VRULES) self.listdati.Show(True) self.listdati.InsertColumn(0, "nome", wx.LIST_AUTOSIZE, 140) self.listdati.InsertColumn(1, "sq", wx.LIST_FORMAT_CENTER, 70) self.listdati.InsertColumn(2, "mv", wx.LIST_FORMAT_CENTER, 70) self.listdati.InsertColumn(3, "pr", wx.LIST_FORMAT_CENTER, 40) self.listdati.InsertColumn(4, "gf", wx.LIST_FORMAT_CENTER, 40) self.listdati.InsertColumn(5, "gs", wx.LIST_FORMAT_CENTER, 40) self.listdati.InsertColumn(6, "ass", wx.LIST_FORMAT_CENTER, 40) self.listdati.InsertColumn(7, "amm", wx.LIST_FORMAT_CENTER, 50) self.listdati.InsertColumn(8, "esp", wx.LIST_FORMAT_CENTER, 40) self.listdati.InsertColumn(9, "rt", wx.LIST_FORMAT_CENTER, 40) self.listdati.InsertColumn(10, "rs", wx.LIST_FORMAT_CENTER, 40) self.listdati.InsertColumn(11, "$", wx.LIST_FORMAT_CENTER, 35) # sizerize vbox = wx.BoxSizer(wx.VERTICAL) vbox.Add(self.rboxruoli, 0, wx.CENTER, 5) vbox.Add(wx.StaticLine(self.panel,), 0, wx.ALL|wx.EXPAND, 5) vbox.Add(self.listdati, 0, wx.ALL|wx.CENTER, 5) vbox.Add(wx.StaticLine(self.panel,), 0, wx.ALL|wx.EXPAND, 5) self.panel.SetSizer(vbox) 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 FileBrowser(wx.FileDialog): '''Class for file browser''' def __init__(self): self.fin = None wildcard = "File Gazzetta (*.txt)|*.txt|" "Tutti i files (*.*)|*.*" wx.FileDialog.__init__(self, None, "scegli il file", os.getcwd(), "", wildcard, wx.OPEN) if self.ShowModal() == wx.ID_OK: print(self.GetPath()) self.file = self.GetPath() self.fin = open(self.file, 'r') else: print "operazione apertura annullata" self.file = None self.Destroy() self.Destroy() def main(): '''app starter''' app = wx.PySimpleApp() FrameVotiPlayers(None, wx.ID_NEW, "FantaStat 1.1 light", size = (700, 500)) app.MainLoop() if __name__ == '__main__': main()
infine il codice relativo al COntroller:
# -*- coding: utf-8 -*-# # ControllerLight.py '''FantaStat v 1.1 Light Controller Module for FantaStat Application: Application pattern: MODEL | VIEW | CONTROLLER MODEL Module COntains all data for database querying VIEW Module Contains all GUI for viewing all database data CONTROLLER Module contains app logic. All the binding method to frame widget are defined here''' import wx from DataLight import Model from ProgressBar import BarController from ViewLight import FrameVotiPlayers, InfoMessage, ChoiceMessage, FileBrowser class Controller(object): '''Controller class for MVC-like pattern''' def __init__(self): self.model = Model() self.list_voti = [] self.ctrl = FrameVotiPlayers(None, wx.ID_NEW, "FantaStat 1.1 light", size = (700, 500)) self.ctrl.btn_exit.Bind(wx.EVT_BUTTON, _frame_exit) self.ctrl.btn_import.Bind(wx.EVT_BUTTON, self.import_votes) self.query = self.model.get_data_players(u'portiere') self.fill_list(self.query) self.ctrl.rboxruoli.Bind(wx.EVT_RADIOBOX, self.on_roles) self.ctrl.listdati.Bind(wx.EVT_LIST_COL_CLICK, self.on_list_column) def upload_players(self, file_in): '''upload/upgrade players in db via model''' def get_flag_role(idg): """get the role, by idgaz""" if idg < 200: role = 'portiere' elif idg > 200 and idg < 500: role = 'difensore' elif idg > 500 and idg < 800: role = 'ccampista' else: role = 'attaccante' return role if file_in != None: record = len(file_in.readlines()) #progress bar param barc = BarController(record) file_in.seek(0) for line in file_in.readlines(): val = line.split('|') try: idr = int(val[0]) except ValueError: idr = int(val[0].replace('xefxbbxbf', '')) role = get_flag_role(idr) nomev = val[2].decode('utf-8').replace('"', '') squadrav = val[3].replace('"', '') play_in = self.model.get_player_by_idgaz(idr) if play_in == None: print " <<<< assente giocatore con idgaz = ", idr self.model.import_new_player(idr, nomev, squadrav, int(val[27].rstrip()), role) else: print " ++++ aggiorno giocatore con idgaz = ", idr self.model.update_player_by_idgaz(idr, nomev, squadrav, int(val[27]), role) barc.update_progress_bar() self.model.session.commit() barc.destroy_progress_bar() print "core> dati inseriti con successo" else: print "core> Nessun dato da salvare" def import_votes(self, evt): '''Import Votes from Gazzetta.com''' parent = evt.GetEventObject().GetParent().GetParent() ggs = self.model.get_inserted_days() mes = "Inserire i voti nel database?" if ChoiceMessage(parent, mes).get_yes() == True: fbr = FileBrowser() file_in = fbr.fin path = fbr.file if file_in != None: choice = int(path.partition('MCC')[-1].partition('.txt')[0]) if choice not in ggs: try: self.upload_players(file_in) except ValueError: print "core> Operazione annullata" else: self.list_voti = [] file_in.seek(0) for line in file_in: self.list_voti.append((int(line.split("|")[0]), float(line.split("|")[7]), int(line.split("|")[1]), int((line.split("|")[27] ).rstrip()), int(line.split("|")[11]), int(line.split("|")[6]), int(line.split("|")[12]), int(line.split("|")[15]), int(line.split("|")[16]), int(line.split("|")[17]), int(line.split("|")[18]), int(line.split("|")[21]), float(line.split("|")[10]))) file_in.close() print "core> pre RECORD" record = len(self.list_voti) if record > 0: upbar = BarController(record) for item in self.list_voti: self.model.upload_votes(item) if True: upbar.update_progress_bar() self.model.total_commit() print "core> Dati Salvati con successo" upbar.destroy_progress_bar() role = self.ctrl.rboxruoli.GetStringSelection() self.ctrl.listdati.DeleteAllItems() self.fill_list(self.model.get_data_players(role)) else: mes = "errore file. Giornata gia' inserita!" InfoMessage(parent, mes).get_choice() else: print "core> nessun dato da salvare" def fill_list(self, sqla_query): '''Fill the ListControl with stats data from db''' self.ctrl.listdati.DeleteAllItems() for player in sqla_query: nome = player[0] row = self.ctrl.listdati.InsertStringItem(0, nome)#ROW sq = player[12] self.ctrl.listdati.SetStringItem(row, 1, str(sq)) #1st C m_v = player[2] self.ctrl.listdati.SetStringItem(row, 2, str("%.3f" % m_v)) pres = player[3] self.ctrl.listdati.SetStringItem(row, 3, str(pres)) g_f = player[4] self.ctrl.listdati.SetStringItem(row, 4, str(g_f)) g_s = player[5] self.ctrl.listdati.SetStringItem(row, 5, str(g_s)) ass = player[6] self.ctrl.listdati.SetStringItem(row, 6, str(ass)) ammo = player[7] self.ctrl.listdati.SetStringItem(row, 7, str(ammo)) esp = player[8] self.ctrl.listdati.SetStringItem(row, 8, str(esp)) r_t = player[9] self.ctrl.listdati.SetStringItem(row, 9, str(r_t)) r_s = player[10] self.ctrl.listdati.SetStringItem(row, 10, str(r_s)) val = player[11] self.ctrl.listdati.SetStringItem(row, 11, str(val)) def on_list_column(self, evt): '''get the column header for sorting database data''' header = evt.GetColumn() ruolo = self.ctrl.rboxruoli.GetStringSelection() if 2 < header < 11: jolly = self.model.cols[header] self.ctrl.listdati.DeleteAllItems() self.fill_list(self.model.get_data_players_by_value(ruolo, jolly)) elif header == 2: jolly = self.model.cols[header] self.ctrl.listdati.DeleteAllItems() self.fill_list(self.model.get_data_players(ruolo, jolly)) elif header == 11: self.ctrl.listdati.DeleteAllItems() self.fill_list(self.model.get_data_players_by_valore(ruolo)) def on_roles(self, evt): '''radio box "Ruolo" event handler''' role = evt.GetEventObject().GetStringSelection() players = self.model.get_data_players(role) self.ctrl.listdati.DeleteAllItems() self.fill_list(players) def _frame_exit(evt): '''exit from frame''' frame = evt.GetEventObject().GetParent().GetParent() frame.Close() def main(): '''starter''' app = wx.PySimpleApp() Controller() app.MainLoop() if __name__ == '__main__': main()
Ultimo oggetto estraneo al FantaStat, è la ControlBar delle importazioni:
# ProgressBar.py '''Module containing a Progress Bar class''' import wx class Data(object): '''Data class for M-V-C pattern''' def __init__(self): self.count = 0 self.max_v = 0 def add_count (self): '''increase the progress bar count for showing its progress''' self.count += 1 def get_max(self): '''Get the max value of the progress bar''' return self.max_v def get_count(self): '''Get the current count value of the progressbar in the loop''' return self.count def set_max(self, value): '''set the max value of the progress bar''' self.max_v = int(value) class ProgressBar(wx.ProgressDialog): '''Progress Dialog constructor''' def __init__(self, max_v): wx.ProgressDialog.__init__(self, "", "Update...", max_v, style = wx.PD_AUTO_HIDE | wx.PD_ELAPSED_TIME | wx.PD_REMAINING_TIME) def progress(self, progress): '''update the progress bar during the loops''' wx.MicroSleep(1) #for short processes use wx.MilliSleep(1) self.Update(progress) def destroy(self): '''Distruttore della progress bar''' self.Destroy() class BarController(object): '''Progress Bar Controller Constructor''' def __init__(self, value = 3): self.data = Data() self.data.set_max(value) self.max = self.data.get_max() self.count = self.data.get_count() self.progress_bar = ProgressBar(self.max) def update_progress_bar(self): '''Aggiorna la progressBar''' self.data.add_count() self.count = self.data.get_count() self.progress_bar.progress(self.count) # update progressbar def destroy_progress_bar(self): '''Destroy the wx.ProgressDialog instance''' self.progress_bar.destroy() def main(): '''app starter''' app = wx.PySimpleApp() bar_c = BarController(600) while bar_c.count < bar_c.max: bar_c.update_progress_bar() bar_c.destroy_progress_bar() app.MainLoop() if __name__ == "__main__": main()
Nella versione 1.2 implementerò l’ordinamento decrescente per colonna E per media_voto,
in modo da ordinare i giocatori di pari valore (es presenze), mettendo prima quello
con la media voto maggiore.
…continua
Commenti recenti