Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

How To Train Your Python

435 Aufrufe

Veröffentlicht am

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.

Veröffentlicht in: Software
  • Als Erste(r) kommentieren

How To Train Your Python

  1. 1. 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 .
  2. 2. Jordi Riera - Odoo Technical Consultant & Python Developer 7 ans d'expérience en pipeline dont : - Pipeline développeur à MPC - Pipeline TD à Sony Pictures Imageworks
  3. 3. Vous et python?
  4. 4. 1. Introduction à ipython 2. Pimp my code
  5. 5. ipython
  6. 6. ipython : le shell des vrais et durs! voir des feignants... Savoir-faire Linux | 6
  7. 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 Savoir-faire Linux | 7
  8. 8. En direct de votre Shell : > ipython > pip install ipython
  9. 9. ou du browser : > ipython notebook
  10. 10. Pimp my code
  11. 11. Loopers
  12. 12. 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
  13. 13. Index dans une boucle for In [1]: speakers = ['Christian', 'Eric', 'Dave', 'Jordi'] In [2]: for i in range(len(speakers)): ...: print i, speakers[i]
  14. 14. 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
  15. 15. range In [1]: for i in [0, 1, 2, 3, 4, 5]: ...: print i In [2]: for i in range(6): ...: print i
  16. 16. 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
  17. 17. 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]: a Out[3]: [0, 2, 4, 6, 8]
  18. 18. 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]: a Out[3]: [0, 2, 4, 6, 8] In [4]: a = [i for i in xrange(10) if not i % 2] In [5]: a Out[5]: [0, 2, 4, 6, 8]
  19. 19. 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)
  20. 20. 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))
  21. 21. The Great Dictionnary
  22. 22. 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] ...:
  23. 23. 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']}
  24. 24. Boucles dans un dict In [1]: details = { 'sfl': ['John', 'Jonathan', 'Jordi'], 'nad': ['Christian'] } In [2]: for key in details.keys(): ...: print key
  25. 25. 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
  26. 26. 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
  27. 27. 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
  28. 28. 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]
  29. 29. Boucles dans un dict Autres outils d'itérations dans un dictionnaire: .keys() <-> .iterkeys() .values() <-> .itervalues() .items() <-> .iteritems()
  30. 30. 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)
  31. 31. 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)
  32. 32. 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)
  33. 33. The Bone Collections
  34. 34. In [1]: {'john': 'sfl', 'jonathan': 'sfl', 'jordi':'sfl' , 'christian':'nad'} Out[1]: {'christian': 'nad', 'john': 'sfl', 'jonathan': 'sfl', 'jordi': 'sfl'}
  35. 35. 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]: d Out[7]: OrderedDict([('John', 'sfl'), ('Jonathan', 'sfl'), ('Jordi', 'sfl'), ('Christian', 'nad')])
  36. 36. 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)]
  37. 37. namedtuple In [1]: from collections import namedtuple In [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êmes méthodes qu'un tuple.
  38. 38. 1-877-735-4689 contact@savoirfairelinux.com http://www.savoirfairelinux.com
  39. 39. Nous recrutons! http://carrieres.savoirfairelinux.com/

×