This document provides an introduction to using IPython and improving Python code through various techniques like list comprehensions and collections. It discusses features of IPython like interactive shells and Jupyter notebooks. It also covers enhancing for loops, dictionaries, and using namedtuple to organize data from collections. The goal is to help readers become more proficient in Python.
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
How To Train Your Python
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. Jordi Riera - Odoo Technical Consultant & Python Developer
7 ans d'expérience en pipeline dont :
- Pipeline développeur à MPC
- Pipeline TD à Sony Pictures Imageworks
6. ipython : le shell des vrais et durs!
voir des feignants...
Savoir-faire Linux | 6
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. En direct de votre Shell : > ipython
> pip install ipython
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. 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. 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. range
In [1]: for i in [0, 1, 2, 3, 4, 5]:
...: print i
In [2]: for i in range(6):
...: print i
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. 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. 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. 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. 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))
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. 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. Boucles dans un dict
In [1]: details = {
'sfl': ['John', 'Jonathan', 'Jordi'],
'nad': ['Christian']
}
In [2]: for key in details.keys():
...: print key
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. 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. 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. 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. Boucles dans un dict
Autres outils d'itérations dans un dictionnaire:
.keys() <-> .iterkeys()
.values() <-> .itervalues()
.items() <-> .iteritems()
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. 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. 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)