guia (breve) de supervivencia a python 3
DESCRIPTION
Breve guía para ayudar a la migración de código desde Python 2.x a 3.xTRANSCRIPT
![Page 1: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/1.jpg)
![Page 2: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/2.jpg)
![Page 3: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/3.jpg)
Opciones para migrar a Python 3
OPCIÓN A
● Migramos el código a Python 3 y nos olvidamos de 2.7 y anteriores– Si te lo puedes permitir, adelante
– Se sincero, no te lo puedes permitir
![Page 4: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/4.jpg)
Opciones para migrar a Python 3
OPCION B
● Mantenemos el código de la 2.7 y con 2to3 o con six generamos el código para Python 3– Lento
– Código generado no pythonico
– No programamos en Python 3
– Aburrido
![Page 5: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/5.jpg)
![Page 6: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/6.jpg)
Existe un truco
● Escribir código que funcione a la vez en 2.x y 3.x es muy muy jodido difícil
● Pero hay un truco...
![Page 7: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/7.jpg)
![Page 8: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/8.jpg)
Soporte de versiones
● Las versiones anteriores a la 2.6 son anecdóticas
● las versiones 3.0, 3.1 y 3.2 se pueden obviar, ya que prácticamente no hay base instalada
● El subconjunto que queda es mucho más fácil de compatibilizar
![Page 9: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/9.jpg)
Unicode
● Las versiones 2.6, 2.7, 3.3 y 3.4 permiten especificar sin ambigüedades los tipos de string: – u'hola' siempre será unicode
– b'hola' será o una string en python 2.x o un bytestring en Python 3.x
![Page 10: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/10.jpg)
Excepciones
● Python 2.6 ya permite la nueva sintaxis de uso de las excepciones:
try: ...except Exception as err: ...
![Page 11: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/11.jpg)
función print
● Desde Python 2.6 podemos hacer una importacion del futuro:
from __future__ import print_function
Y usar print como una función en nuestro código
(El cambio podemos hacerlos con herramientas automáticas como 2to3 o python-modernize)
![Page 12: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/12.jpg)
Decoradores
● Desde Python 2.6 tenemos decoradores, que pueden ser muy útiles para adecuar las interfaces
● Por ejemplo, el cambio del método next() a la función y método __next()__
![Page 13: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/13.jpg)
Ejemplo (1/2)
# Definimos el decorador
if PY2:
def implements_iterator(cls):
cls.next = cls.__next__
del cls.__next__
return cls
else:
implement_iterator = lambda x:x
![Page 14: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/14.jpg)
Ejemplo (2/2)
# Usamos el decorador
@implements_iterator
class MyIterator(object):
def __init__(self):
self.lista = [1, 2, 3]
def __next__(self):
for i in self.lista:
yield i
![Page 15: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/15.jpg)
![Page 16: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/16.jpg)
Hay que escribir un módulo de compatibilidad
● No es tan difícil como puede parecer● Normalmente unas 200 o 300 líneas de código● Proyectos tan importantes como Jinja2, Numpy,
SciPy lo han conseguido
![Page 17: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/17.jpg)
Referencias
Porthing to Python 3 redux, de Armin Ronacherlucumr.pocoo.org/2013/5/21/porting-to-python-3-redux/
How to support both Python 2 and 3, de Ondřej Čertíkondrejcertik.blogspot.com.es/2013/08/how-to-support-both-python-2-and-3.html
Key differences between Python 2.7 and Python 3.x, de Sebastian Rashkasebastianraschka.com/Articles/2014_python_2_3_key_diff.html
![Page 18: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/18.jpg)
Herramientas
Six: Python 2 and 3 Compatibility Library¶pythonhosted.org/six/
Python-modernizegithub.com/mitsuhiko/python-modernize¶
Tox: virtualenv-based automation of test activitiespypi.python.org/pypi/tox
![Page 19: Guia (breve) de supervivencia a python 3](https://reader034.vdocuments.pub/reader034/viewer/2022042607/557bb7e0d8b42aac088b464b/html5/thumbnails/19.jpg)
Gracias
● Para dudas, preguntas, sugerencias, insultos (siempre que sea con cierta gracia), etc:
– Juan Ignacio Rodríguez de León
– Email: euribates (at) gmail.com
– Twitter: @jileon