how to train your python

39
How to train your Python! be a pythonista! Par Jordi Riera La marque de commerce Linux® est utilisée conformément à une sous-licence de LMI, licencié exclusif de Linus Torvalds, propriétaire de la marque au niveau mondial .

Upload: jordi-riera

Post on 02-Jul-2015

193 views

Category:

Software


4 download

DESCRIPTION

Talk à propos de python. La première partie parle de iPython alors que la seconde partie se concentre sur de bonnes pratiques pour optimiser du code Python.

TRANSCRIPT

Page 1: How To Train Your Python

How to train your Python!be a pythonista!

Par Jordi Riera

La marque de commerce Linux® est utilisée conformément à une sous-licence de LMI, licencié exclusif de Linus Torvalds, propriétaire de la marque au niveau mondial .

Page 2: How To Train Your Python

Jordi Riera - Odoo Technical Consultant & Python Developer

7 ans d'expérience en pipeline dont : - Pipeline développeur à MPC - Pipeline TD à Sony Pictures Imageworks

Page 3: How To Train Your Python

Vous et python?

Page 4: How To Train Your Python

1. Introduction à ipython2. Pimp my code

Page 5: How To Train Your Python

ipython

Page 6: How To Train Your Python

Savoir-faire Linux | 6

ipython : le shell des vrais et durs! voir des feignants...

Page 7: How To Train Your Python

Savoir-faire Linux | 7

● Powerful interactive shells (terminal and Qt-based).● A browser-based notebook with support for code, text, mathematical expressivons,

inline plots and other rich media.● Support for interactive data visualization and use of GUI toolkits.● Flexible, embeddable interpreters to load into your own projects.● Easy to use, high performance tools for parallel computing.

ipython.org

Page 8: How To Train Your Python

En direct de votre Shell : > ipython

> pip install ipython

Page 9: How To Train Your Python

ou du browser : > ipython notebook

Page 10: How To Train Your Python

Pimp my code

Page 11: How To Train Your Python

Loopers

Page 12: How To Train Your Python

for est un foreach

In [1]: speakers = ['Christian', 'Eric', 'Dave', 'Jordi']

In [2]: for i in range(len(speakers)): ...: print speakers[i]

In [3]: for speaker in speakers: ...: print speaker

Page 13: How To Train Your Python

Index dans une boucle for

In [1]: speakers = ['Christian', 'Eric', 'Dave', 'Jordi']

In [2]: for i in range(len(speakers)): ...: print i, speakers[i]

Page 14: How To Train Your Python

Index dans une boucle for

In [1]: speakers = ['Christian', 'Eric', 'Dave', 'Jordi']

In [2]: for i in range(len(speakers)): ...: print i, speakers[i]

In [3]: for i, speaker in enumerate(speakers): ...: print i, speaker

Page 15: How To Train Your Python

range

In [1]: for i in [0, 1, 2, 3, 4, 5]: ...: print i

In [2]: for i in range(6): ...: print i

Page 16: How To Train Your Python

range

In [1]: for i in [0, 1, 2, 3, 4, 5]: ...: print i

In [2]: for i in range(6): ...: print i

In [3]: for i in xrange(6): ...: print i

Page 17: How To Train Your Python

Compréhension à la portée de tous

In [1]: a = []

In [2]: for i in xrange(10): ...: if not i % 2: ...: a.append(i)

In [3]: aOut[3]: [0, 2, 4, 6, 8]

Page 18: How To Train Your Python

Compréhension à la portée de tous

In [1]: a = []

In [2]: for i in xrange(10): ...: if not i % 2: ...: a.append(i)

In [3]: aOut[3]: [0, 2, 4, 6, 8]

In [4]: a = [i for i in xrange(10) if not i % 2]

In [5]: aOut[5]: [0, 2, 4, 6, 8]

Page 19: How To Train Your Python

Almost all set

In [1]: a = range(10000) + range(20000) + range(30000)

In [2]: b = []

In [3]: for i in a: ...: if not i in b: ...: b.append(i)

Page 20: How To Train Your Python

Almost all set

In [1]: a = range(10000) + range(20000) + range(30000)

In [2]: b = []

In [3]: for i in a: ...: if not i in b: ...: b.append(i)

In [1]: a = range(10000) + range(20000) + range(30000)

In [2]: b = list(set(a))

Page 21: How To Train Your Python

The Great Dictionnary

Page 22: How To Train Your Python

Construire un dict à partir de listes

In [1]: companies = ['sfl', 'nad']

In [2]: people = (['John', 'Jonathan', 'Jordi'], ['Christian'])

In [3]: d = {}

In [4]: for i, company in enumerate(companies): ...: d[company] = people[i] ...:

Page 23: How To Train Your Python

Construire un dict à partir de listes

In [1]: companies = ['sfl', 'nad']

In [2]: people = (['John', 'Jonathan', 'Jordi'], ['Christian'])

In [3]: d = {}

In [4]: for i, company in enumerate(companies): ...: d[company] = people[i] ...:

In [5]: d = dict(izip(companies, people))Out[5]: {'nad': ['Christian'], 'sfl': ['John', 'Jonathan', 'Jordi']}

Page 24: How To Train Your Python

Boucles dans un dict

In [1]: details = { 'sfl': ['John', 'Jonathan', 'Jordi'], 'nad': ['Christian'] }

In [2]: for key in details.keys(): ...: print key

Page 25: How To Train Your Python

Boucles dans un dict

In [1]: details = { 'sfl': ['John', 'Jonathan', 'Jordi'], 'nad': ['Christian'] }

In [2]: for key in details.keys(): ...: print key

In [3]: for key in details: ...: print key

Page 26: How To Train Your Python

Boucles dans un dict

In [1]: details = { 'sfl': ['John', 'Jonathan', 'Jordi'], 'nad': ['Christian'] }

In [2]: for key in details.keys(): ...: print key

In [3]: for key in details: ...: print key

In [4]: for key in details.iterkeys(): ...: print key

Page 27: How To Train Your Python

Iteration mais pas tout le temps

In [1]: for k in details.iterkeys(): ....: if k == 'sfl': ....: del details[k]

RuntimeError: dictionary changed size during iteration

Page 28: How To Train Your Python

Iteration mais pas tout le temps

In [1]: for k in details.iterkeys(): ....: if k == 'sfl': ....: del details[k]

RuntimeError: dictionary changed size during iteration

In [2]: for k in details.keys(): ....: if k == 'sfl': ....: del details[k]

Page 29: How To Train Your Python

Boucles dans un dict

Autres outils d'itérations dans un dictionnaire:

.keys() <-> .iterkeys()

.values() <-> .itervalues()

.items() <-> .iteritems()

Page 30: How To Train Your Python

Remplir un dictionnaire

In [1]: people = (['John', 'sfl'], ['Jonathan', 'sfl'], ['Jordi', 'sfl'],['Christian', 'nad'])

In [2]: d = {}

In [3]: for p, company in peopls.iteritems(): ...: if company not in d: ...: d[company] = [] ...: d[company].append(p)

Page 31: How To Train Your Python

Remplir un dictionnaire

In [1]: people = (['John', 'sfl'], ['Jonathan', 'sfl'], ['Jordi', 'sfl'],['Christian', 'nad'])

In [2]: d = {}

In [3]: for p, company in people.iteritems(): ...: if company not in d: ...: d[company] = [] ...: d[company].append(p)

In [4]: for p, company in people.iteritems(): ....: d.setdefault(company, []).append(p)

Page 32: How To Train Your Python

Remplir un dictionnaire

In [5]: from collections import defaultdict

In [6]: d = defaultdict(list)

In [7]: for p, company in people.iteritems(): ....: d[company].append(p)

Page 33: How To Train Your Python

The Bone Collections

Page 34: How To Train Your Python

In [1]: {'john': 'sfl', 'jonathan': 'sfl', 'jordi':'sfl' , 'christian':'nad'}Out[1]: {'christian': 'nad', 'john': 'sfl', 'jonathan': 'sfl', 'jordi': 'sfl'}

Page 35: How To Train Your Python

OrderedDict

In [1]: {'john': 'sfl', 'jonathan': 'sfl', 'jordi':'sfl' , 'christian':'nad'}Out[1]: {'christian': 'nad', 'john': 'sfl', 'jonathan': 'sfl', 'jordi': 'sfl'}

In [2]: d = OrderedDict()In [3]: d['John'] = 'sfl'In [4]: d['Jonathan'] = 'sfl'In [5]: d['Jordi'] = 'sfl'In [6]: d['Christian'] = 'nad'In [7]: dOut[7]: OrderedDict([('John', 'sfl'), ('Jonathan', 'sfl'), ('Jordi', 'sfl'),('Christian', 'nad')])

Page 36: How To Train Your Python

namedtuple

In [1]: get_points()Out[1]: [(65, 28, 45, 255, 255, 87), (255, 255, 87, 65, 28, 45)]

In [2]: get_points()Out[2]: [Coord_color(x=65, y=28, z=45, r=255, g=255, b=87), Coord_color(x=255, y=255, z=87, r=65, g=28, b=45)]

Page 37: How To Train Your Python

namedtuple

In [1]: from collections import namedtupleIn [2]: Coord_color = namedtuple('Coord_color', ['x', 'y', 'z', 'r', 'g', 'b'])

In [3]: [Coord_color(65, 28, 45, 255, 255, 87), Coord_color(255, 255, 87, 65,28, 45)]Out[3]: [Coord_color(x=65, y=28, z=45, r=255, g=255, b=87), Coord_color(x=255, y=255, z=87, r=65, g=28, b=45)]

Namedtuple est une sous classe de tuple, lui donnant les mêmesméthodes qu'un tuple.

Page 38: How To Train Your Python

1-877-735-4689

[email protected]

http://www.savoirfairelinux.com

Page 39: How To Train Your Python

Nous recrutons!

http://carrieres.savoirfairelinux.com/