Upload
jordi-riera
View
184
Download
4
Embed Size (px)
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.
Citation preview
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 .
Jordi Riera - Odoo Technical Consultant & Python Developer
7 ans d'expérience en pipeline dont : - Pipeline développeur à MPC - Pipeline TD à Sony Pictures Imageworks
Vous et python?
1. Introduction à ipython2. Pimp my code
ipython
Savoir-faire Linux | 6
ipython : le shell des vrais et durs! voir des feignants...
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
En direct de votre Shell : > ipython
> pip install ipython
ou du browser : > ipython notebook
Pimp my code
Loopers
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
Index dans une boucle for
In [1]: speakers = ['Christian', 'Eric', 'Dave', 'Jordi']
In [2]: for i in range(len(speakers)): ...: print i, speakers[i]
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
range
In [1]: for i in [0, 1, 2, 3, 4, 5]: ...: print i
In [2]: for i in range(6): ...: print i
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
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]
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]
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)
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))
The Great Dictionnary
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] ...:
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']}
Boucles dans un dict
In [1]: details = { 'sfl': ['John', 'Jonathan', 'Jordi'], 'nad': ['Christian'] }
In [2]: for key in details.keys(): ...: print key
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
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
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
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]
Boucles dans un dict
Autres outils d'itérations dans un dictionnaire:
.keys() <-> .iterkeys()
.values() <-> .itervalues()
.items() <-> .iteritems()
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)
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)
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)
The Bone Collections
In [1]: {'john': 'sfl', 'jonathan': 'sfl', 'jordi':'sfl' , 'christian':'nad'}Out[1]: {'christian': 'nad', 'john': 'sfl', 'jonathan': 'sfl', 'jordi': 'sfl'}
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')])
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)]
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.
Nous recrutons!
http://carrieres.savoirfairelinux.com/