nntp2http.com
Posting
Suche
Optionen
Hilfe & Kontakt

Primo programma realizzato in Python 2.4 SO Mac 10.5.4

Von: LaGuNa (scozzaro@gmail.com) [Profil]
Datum: 05.07.2008 18:29
Message-ID: <488a7990-278b-49d1-a4c9-e88cb16621e8@m36g2000hse.googlegroups.com>
Newsgroup: it.comp.lang.python
Grazie al vostro prezioso aiuto sono riuscito ha realizzare il mio
primo programma in python.

Ho utilizzato
- python 2.4 in quanto ho visto che py2app esiste solo per questa
versione
- wxpython
- pysqlite

Elaborazione è corretta

il files del db è reperibile da quì
www.megaupload.com/it/?d=178BOMW0

Il programma non ha:
- funzione stampa
- validate input textctrl

Ecco il codice
#!/bin/env python
import wx
import string
from pysqlite2 import dbapi2 as sqlite


# Assegnamento id
FILE_NEW_ID = wx.NewId()
FILE_OPEN_ID = wx.NewId()
FILE_RECENT_ID = wx.NewId()
FILE_QUIT_ID = wx.NewId()

NAVKEYS = (wx.WXK_END, wx.WXK_LEFT, wx.WXK_RIGHT,
wx.WXK_UP, wx.WXK_DOWN, wx.WXK_PRIOR, wx.WXK_NEXT)

class MyRec:
Cognome = 'ROSSI'
Nome = 'MARIO'
NatoA = 'ROMA'
NatoPV = 'RM'
NatoGG = '18'
NatoMM = '7'
NatoAA = '1971'
Sesso='M'
CodComune=''
CF = ''



class CharValidator(wx.PyValidator):
def __init__(self, flag):
wx.PyValidator.__init__(self)
self.flag = flag
self.Bind(wx.EVT_CHAR, self.OnChar)
def Clone(self):
return CharValidator(self.flag, self.key)
def Validate(self, win):
return True
def TransferToWindow(self):
return True
def TransferFromWindow(self):
return True
def OnChar(self, evt):
key = chr(evt.GetKeyCode())
if self.flag == "no-alpha" and key in string.letters:   return
if self.flag == "no-digit" and key in string.digits:  return
evt.Skip()

class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "My Frame", size=(500, 300))

# Creazione barra del menu
menubar = wx.MenuBar()

# Creazione menu File
file = wx.Menu()
# Inserimento elementi nel menu File
nuovo = wx.MenuItem(file, FILE_NEW_ID, '&Nuovo\tCtrl+N',
'Nuova storia')
file.AppendItem(nuovo)
file.AppendSeparator()

open = wx.MenuItem(file, FILE_OPEN_ID, '&Apri...\tCtrl+O',
'Apri File')
file.AppendItem(open)


quit = wx.MenuItem(file, FILE_QUIT_ID, '&Quit\tCtrl+Q', 'Quit
the Application')
file.AppendItem(quit)


menubar.Append(file, '&File')

self.SetMenuBar(menubar)
self.Centre()

# wx.PyEventBinder
#    Bind(self, target, id1, id2, function)
#self.Bind(wx.EVT_MENU, self.OnNew, id = FILE_NEW_ID)
#self.Bind(wx.EVT_MENU, self.OnQuit, id = FILE_QUIT_ID)

panel = wx.Panel(self, -1)

"""
panel.Bind(wx.EVT_MOTION, self.OnMove)
wx.StaticText(panel, -1, "Pos:", pos=(10, 120))
self.posCtrl = wx.TextCtrl(panel, -1, "", pos=(40, 120))
"""
wx.StaticText(panel, -1, "Cognome:", pos=(10, 10))
MyRec.Cognome = wx.TextCtrl(panel,  wx.NewId(), MyRec.Cognome,
size=(400, -1),   pos=(80, 10))

wx.StaticText(panel, -1, "Nome:", pos=(10, 40))
MyRec.Nome = wx.TextCtrl(panel, wx.NewId(), MyRec.Nome,
size=(420, -1),   pos=(60, 40))

wx.StaticText(panel, -1, "Nato A:", pos=(10, 70))
MyRec.NatoA = wx.TextCtrl(panel, wx.NewId(), MyRec.NatoA,
size=(170, -1),   pos=(60, 70))

wx.StaticText(panel, -1, "PV", pos=(240, 70))
MyRec.NatoPV = wx.TextCtrl(panel, wx.NewId(), MyRec.NatoPV,
size=(30, -1),   pos=(260, 70))

#Nato il gg/mm/aa
wx.StaticText(panel, -1, "Il", pos=(300, 70))
MyRec.NatoGG = wx.TextCtrl(panel, wx.NewId(), MyRec.NatoGG,
size=(20, -1),   pos=(310, 70))
wx.StaticText(panel, -1, "/", pos=(330, 70))
MyRec.NatoMM = wx.TextCtrl(panel, wx.NewId(), MyRec.NatoMM,
size=(20, -1),   pos=(340, 70))
wx.StaticText(panel, -1, "/", pos=(360, 70))
MyRec.NatoAA = wx.TextCtrl(panel, wx.NewId(), MyRec.NatoAA,
size=(40, -1),   pos=(370, 70))

wx.StaticText(panel, -1, "Sesso", pos=(420, 70))
MyRec.Sesso = wx.TextCtrl(panel, wx.NewId(), MyRec.Sesso,
size=(20, -1),   pos=(460, 70))

wx.StaticText(panel, -1, "Codice Comune:", pos=(10, 100))
MyRec.CodComune = wx.TextCtrl(panel, wx.NewId(),
MyRec.CodComune, size=(230, -1),   pos=(160, 100))

MyRec.CF = wx.TextCtrl(panel, wx.NewId(), MyRec.CF, size=(230,
-1),   pos=(160, 130))

elabora = wx.Button(panel, label="Elabora", pos=(55,
180),size=(110, 50))
self.Bind(wx.EVT_BUTTON, self.OnElabora, elabora)

stampa = wx.Button(panel, label="Stampa", pos=(190,
180),size=(110, 50))
self.Bind(wx.EVT_BUTTON, self.OnStampa, stampa)

uscita = wx.Button(panel, label="Uscita", pos=(330,
180),size=(110, 50))
self.Bind(wx.EVT_BUTTON, self.OnUscita, uscita)

def OnStampa(self, event):
print "Stampa"

def OnUscita(self, event):
print "Uscita"


def OnElabora(self, event):
nome = MyRec.Nome.GetValue()
cognome = MyRec.Cognome.GetValue()
giornonascita = MyRec.NatoGG.GetValue()
mesenascita = int(MyRec.NatoMM.GetValue())
annonascita = MyRec.NatoAA.GetValue()
sesso = MyRec.Sesso.GetValue()
cittanascita = MyRec.NatoA.GetValue()
codice=MyRec.CodComune.GetValue()
pv = MyRec.NatoPV.GetValue()

#cittanascita = cittanascita.upper()
print nome
print cognome
print giornonascita
print mesenascita
print annonascita
print sesso
print cittanascita
print pv


mesi = ['A', 'B', 'C', 'D', 'E', 'H', 'L', 'M', 'P', 'R', 'S',
'T']
consonanti = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l',
'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z']
codicicomuni = {}
regolecontrollo = {}
regolecontrollo['A']=[0,1]
regolecontrollo['B']=[1,0]
regolecontrollo['C']=[2,5]
regolecontrollo['D']=[3,7]
regolecontrollo['E']=[4,9]
regolecontrollo['F']=[5,13]
regolecontrollo['G']=[6,15]
regolecontrollo['H']=[7,17]
regolecontrollo['I']=[8,19]
regolecontrollo['J']=[9,21]
regolecontrollo['K']=[10,2]
regolecontrollo['L']=[11,4]
regolecontrollo['M']=[12,18]
regolecontrollo['N']=[13,20]
regolecontrollo['O']=[14,11]
regolecontrollo['P']=[15,3]
regolecontrollo['Q']=[16,6]
regolecontrollo['R']=[17,8]
regolecontrollo['S']=[18,12]
regolecontrollo['T']=[19,14]
regolecontrollo['U']=[20,16]
regolecontrollo['V']=[21,10]
regolecontrollo['W']=[22,22]
regolecontrollo['X']=[23,25]
regolecontrollo['Y']=[24,24]
regolecontrollo['Z']=[25,23]
regolecontrollo['0']=[0,1]
regolecontrollo['1']=[1,0]
regolecontrollo['2']=[2,5]
regolecontrollo['3']=[3,7]
regolecontrollo['4']=[4,9]
regolecontrollo['5']=[5,13]
regolecontrollo['6']=[6,15]
regolecontrollo['7']=[7,17]
regolecontrollo['8']=[8,19]
regolecontrollo['9']=[9,21]

resti = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z']

for i in consonanti:
consonanti = consonanti + [i.upper()]

vocali = ['a', 'e', 'i', 'o', '']
for i in vocali:
vocali = vocali + [i.upper()]

chars = list()

#PRIME 3 LETTERE
cons = list()
voc = list()
for cgn in cognome:
if cgn in consonanti:
cons = cons + [cgn.upper()]
if cgn in vocali:
voc = voc + [cgn.upper()]

if len(cons) > 2:
chars = chars + cons[0:3]
if len(cons) == 2:
chars = chars + cons[0:2] + voc[0:1]

if (len(cons) == 1) and (len(voc) == 2):
chars = chars + cons[0:1] + voc[0:2]
if (len(cons) == 1) and (len(voc) == 1):
chars = chars + cons[0:1] + voc[0:1] + ['X']
if (len(cons) == 0) and (len(voc) == 2):
chars = chars + voc[0:2] + ['X']

#SECONDE 3 LETTERE
cons = list()
voc = list()
for nom in nome:
if nom in vocali:
voc = voc + [nom.upper()]
if nom in consonanti:
cons = cons + [nom.upper()]


if len(cons) > 3:  chars = chars + cons[0:1] + cons[2:3] +
cons[3:4]
if len(cons) == 3:  chars = chars + cons[0:1] + cons[1:2] +
cons[2:3]
if len(cons) == 2:  chars = chars + cons[0:2] + voc[0:1]
if (len(cons) == 1) and (len(voc) == 2):  chars = chars +
cons[0:1] + voc[0:2]
if (len(cons) == 1) and (len(voc) == 1):   chars = chars
+
cons[0:1] + voc[0:1] + ['X']
if (len(cons) == 0) and (len(voc) == 2):  chars = chars +
voc[0:2] + ['X']

chars = chars + [annonascita[2], annonascita[3]]

chars = chars + [mesi[mesenascita-1]]

if sesso=='F' or sesso=='f':
chars = chars + [unicode(atoi(giornonascita) + 40)]
else:
if len(giornonascita)<2:
chars = chars + ['0', giornonascita]
else:
chars = chars + [giornonascita[0], giornonascita[1]]

#codice = CodiceCM(cittanascita,pv)
#print codice

TABLE_NAME = 'CodFis'         #nome tabella
m= str(cittanascita)
t=(pv)
print m
print t

# dalla tabella dobbiamo scegliere la riga del codice fiscale
dove sappiamo la citta' e la provincia
SELECT= "select ccat_cod  from CodFis where desc_cod='"+m+"'
and prov_cod='"+t+"'"
print SELECT
con = sqlite.connect("/Users/enzoscozzaro/cf/codfis.db3") # ci
colleghiamo al db
cur = con.cursor()
cur.execute(SELECT)
fieldIndices = range(len(cur.description))
for row in cur:
for fieldIndex in fieldIndices:
fieldValue = str(row[fieldIndex])
codice= str(fieldValue)

MyRec.CodComune.SetValue(codice)


chars = chars + [codice[0], codice[1], codice[2], codice[3]]

sommone = 0
for i in range(1,16):
if not i % 2:
sommone = sommone + regolecontrollo[chars[i-1]][0]
else:
sommone = sommone + regolecontrollo[chars[i-1]][1]
resto = sommone % 26
chars = chars + [resti[resto]]

codicefinale = str()
for i in chars:
codicefinale = codicefinale + i

MyRec.CF.SetValue(codicefinale)
print "Il tuo codice fiscale e\': %s" % codicefinale
print "Elabora"
"""
def OnMove(self, event):
pos = event.GetPosition()
self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
"""


if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show(True)
app.MainLoop()

P.S. se qualcuno mi da una mano per migliorare il validator glie ne
sarei grado

IL validator deve consntire la possibilità di gestire i tasti cursore
(freccia) ecco dove mi sono blocato

Ciao by Enzo

[ Auf dieses Posting antworten ]