#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    10

    My first step into a larger world...


    Well. As you'll have noticed, I'm new to this forum. I am new too, to GNU and open source software development. On January I first played with PHP and PHP-nuke and made some modifications to some scripts and got some very useful results. From later on, I love programming, but I'm all new to this.
    This is my first python program:
    Upload file did not seemed to work, so here it is:
    Code:
    CREATE TABLE `agenda` (
      `id` tinyint(5) NOT NULL auto_increment,
      `nombre` varchar(50) NOT NULL default '',
      `celular` varchar(10) NOT NULL default '',
      `telefono` varchar(10) NOT NULL default '',
      `email` varchar(50) NOT NULL default '',
      PRIMARY KEY  (`id`)
    ) TYPE=MyISAM PACK_KEYS=0;
    Code:
    # -*- coding: iso-8859-1 -*-
    import MySQLdb,sys,smtplib, string,os,time,output
    #######################
    host = "localhost"
    user = "root"
    passwd = "pass" #Put your password here
    db = "python"
    #######################
    # Database conection.
    connect = MySQLdb.connect(host,user,passwd,db)
    cursor = connect.cursor()
    
    # Doesn't have too much sense, but I'm just beginning to learn classes
    class dirty:
    	def limpiar(self,op):
    		time.sleep(op)
    		os.system("clear")
    	
    	def mail(self,host,desde,hacia,body):
    		self.host = host
    		self.desde = desde
    		self.hacia = hacia
    		self.body = body
    		server = smtplib.SMTP(self.host)
    		server.sendmail(self.desde, [self.hacia], self.body)
    		server.quit()
    	
    	def str(self,op):
    		if op == "cancel":
    			print output.red("	action has been canceled")
    		if op == "satisfactorio":
    			print(output.red("	action has been completed"))
    		if op == "nofound":
    			print output.red("	No results found")
    		if op == "found":
    			print output.red("	Results found!")
    		if op == "malinput":
    			print output.red("	I dont understand that.")
    		if op == "adios":
    			print output.red("	Good bye!")
    	
    	def raw(self,op):
    		if op == "busqueda":
    			global nombre
    			nombre = raw_input(output.turquoise("	Name to search is: "))		
    		if op == "continue":
    			raw_input(output.turquoise("	Press Enter to continue"))
    		if op == "opcion":
    			global accion
    			accion = raw_input(output.yellow("	And your choice is: "))	
    		if op == "eliminar":
    			global eliminar
    			eliminar = raw_input(output.turquoise("	Do you want to erase this data? yes/no: "))
    		if op == "eliminars":
    			global eliminars
    			eliminars = raw_input(output.turquoise("	Do you want to erase some data? yes/no: "))
    		if op == "idborra":
    			global idborra
    			idborra = raw_input(output.turquoise("	Enter the ID to erase data: "))
    		if op == "nuevo":
    			global nuevo
    			nuevo = raw_input(output.turquoise("	Name (and second name): "))
    		if op == "celular":
    			global celular
    			celular = raw_input(output.turquoise("	Cell Phone: "))
    		if op == "telefono":	
    			global telefono
    			telefono = raw_input(output.turquoise("	Phone: "))
    		if op == "email":	
    			global email
    			email =raw_input(output.turquoise("	Email: "))
    		if op == "sendmailexist":
    			global sendmailexist
    			sendmailexist = raw_input(output.turquoise("	Send mail to existing user? yes/no: "))
    		if op == "sendmail":
    			global sendmail
    			sendmail = raw_input(output.turquoise("	Do you want to send a mail? yes/no: ")	)
    	
    
    
    def consulta(var=0):
    	global hay,result
    	if var == 1:
    		cursor.execute("SELECT nombre FROM agenda ORDER by nombre")
    	else:
    		cursor.execute("SELECT * FROM agenda WHERE nombre LIKE '%s%%' " % nombre)
    	result = cursor.fetchall( )
    	hay = cursor.rowcount		
    
    def mostrar(var=0):
    	global record,cardinal
    	cardinal = 0
    	for record in result:
    		cardinal = cardinal + 1
    		if var == 1:
    			print "	=>",record[0]
    		else:
    			print "	*** ID:",record[0],"==>",record[1]
    			print "				Cell Phone:",record[2]
    			print "				Phone:",record[3]
    			print "				Email:",record[4]
    
    def resultado():
    	if hay == 0:
    		nana.str("nofound")	
    	else:
    		nana.str("found")
    		print
    		mostrar()
    
    nana = dirty()
    nana.limpiar(0)
    
    # Bloque de codigo que mantiene al programa ejecutandose hasta el quit
    while True:
    	print output.red("\n			Python Agenda\n")
    	print output.green('''		(1) Add new contact
    		(2) Search for a contact
    		(3) Erase a contact
    		(4) List my contacts
    		(5) Send a Mail
    		(q) Quit
    ''')
    	nana.raw("opcion")
    	
    	# Bloque de codigo para salir del programa
    	if accion == "q":
    		break
    
    	# Bloque de codigo para aņadir un contacto
    	elif accion == "1":
    		nana.raw("nuevo")
    		if nuevo == "":
    			nana.str("cancel")
    			nana.limpiar(2)
    		else:
    			nana.raw("celular")
    			nana.raw("telefono")
    			nana.raw("email")
    			cursor.execute('''INSERT into agenda (nombre,celular,telefono,email) 
    						VALUES (%s, %s, %s,%s)''',(nuevo,celular,telefono,email))
    			print
    			nana.str("satisfactorio")
    			nana.raw("continue")
    			nana.limpiar(0)
    			
    	# Bloque de codigo para buscar un contacto
    	elif accion == "2":
    		nana.raw("busqueda")
    		consulta()
    		resultado()
    		nana.raw("continue")
    		nana.limpiar(0)
    
    	# Bloque de codigo para borrar un contacto:
    	elif accion == "3":
    		nana.raw("busqueda")
    		consulta()
    		resultado()
    		while True:
    			if cardinal == 1:
    				nana.raw("eliminar")
    				if eliminar == "":
    					nana.str("cancel")
    					nana.limpiar(2)
    					break
    				elif eliminar == "yes":
    					idborra = record[0]
    					cursor.execute("DELETE FROM agenda WHERE id=%s",idborra)
    					nana.str("satisfactorio")
    					nana.limpiar(2)
    					break
    				elif eliminar == "no":
    					nana.str("cancel")
    					nana.limpiar(2)
    					break
    				else:
    					nana.str("malinput")
    			if cardinal > 1:
    				nana.raw("eliminars")
    				if eliminars == "yes":
    					nana.raw("idborra")
    					if idborra == "":
    						nana.str("cancel")
    						nana.limpiar(2)
    						break
    					else:				
    						cursor.execute("DELETE FROM agenda WHERE id=%s",idborra)
    						nana.str("satisfactorio")
    						nana.raw("continue")
    						break
    				elif eliminars == "no":
    					nana.str("cancel")
    					nana.limpiar(2)
    					break
    				else:
    					nana.str("malinput")
    		nana.limpiar(0)
    	
    	# Bloque de codigo para mandar un mail
    	elif accion == "5":
    		nana.raw("sendmailexist")
    		if sendmailexist == "":
    			nana.str("cancel")
    			nana.limpiar(2)
    		if sendmailexist == "yes":
    			nana.raw("busqueda")
    			consulta()
    			resultado()	
    			while True:
    				if hay == 0:
    					nana.limpiar(2)
    					break
    				nana.raw("sendmail")
    				if sendmail == "yes":
    					desde = raw_input("	Where is your mail going?")
    					if desde == "":
    						nana.str("cancel")
    						nana.limpiar(2)
    						break
    					else:
    						host = 'localhost'
    						hacia = record[4]
    						subject = raw_input("	Subject? ")
    						msg = raw_input("	Write the message:\n ")
    						body = string.join(("From: %s" % desde,"To: %s" % hacia,"Subject: %s" % subject,"",msg), "\r\n")
    						nana.mail(host,desde,hacia,body)
    						print
    						nana.str("satisfactorio")
    						nana.limpiar(2)
    						break
    				elif sendmail == "no":
    					nana.str("cancel")
    					nana.limpiar(2)
    					break
    				else:
    					nana.str("malinput")
    		if sendmailexist == "no":
    			while True:	
    				host = 'localhost'
    				hacia = raw_input("	Where is your mail going? ")
    				if hacia == "":
    					nana.str("cancel")
    					nana.limpiar(2)
    					break
    				else:	
    					desde = raw_input("	Put your mail here, so they can reach you ")
    					subject = raw_input("	Subject? ")
    					msg = raw_input("	Write the message:\n ")
    					body = string.join(("From: %s" % desde,"To: %s" % hacia,"Subject: %s" % subject,"",msg), "\r\n")
    					nana.mail(host,desde,hacia,body)
    					print
    					nana.str("satisfactorio")
    					nana.limpiar(2)
    					break
    	
    	# Bloque de codigo para listar a todos los contactos
    	elif accion == "4":
    		consulta(1)
    		nana.str("found")
    		mostrar(1)
    		nana.raw("continue")
    		nana.limpiar(0)
    		
    	else:
    		nana.str("malinput")
    		nana.limpiar(2)
    
    # Fin del ciclo while
    nana.str("adios")
    nana.limpiar(1)
    Sorry for the long post... but, well... It's incomplete, but I couldn't wait to get some feedback... so there you have some alpha... or beta, i dont know, its my first program in python.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    10
    Oh, i forgot... the program uses de output.py (import output) posted in this forum... I dont seem able to attach it here, so just search for it... or download it from here:
    http://www.opticasqueirolo.cl/output.py
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I'm a bit confused on your actual problem, but for uploading files, look into ftplib .
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    10
    Oh thanks, but the "upload" problem was with this forum (for attaching files, and not with my program)
    I did put this program in here to get some user feedback, since its my first program...

IMN logo majestic logo threadwatch logo seochat tools logo