Commit 2a43f63a authored by paugier's avatar paugier
Browse files

Add notebooks Python language

parent dbf67fcf
*.pyd*
*.pyc
*~
*tmp
**/tmp
*.so
*.o
*.pstats
......
......@@ -31,6 +31,7 @@ help:
clean:
rm -f ipynb/*.slides.html
rm -f ipynb/index.html
rm -rf ipynb/tmp
lab:
jupyter-lab
......
%% Cell type:markdown id: tags:
# Python for High Performance Computing
**A training to scale up Python code **
Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)
- Day 0: Python language
- Day 1: Numpy / Scipy / Pandas + Visualization
- Day 2: Packaging, documentation, intro HPC, profiling
- Day 3 and 4: Wrapping, Python accelerators, parallel computing + personal projects
%% Cell type:markdown id: tags:
# Python training UGA 2017
**A training to acquire strong basis in Python to use it efficiently**
Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)
# First steps
%% Cell type:markdown id: tags:
## Why Python ?
- A very nice and well-thought programming language.
- **Generalist**: good for several applications, [used with success in many fields](http://brochure.getpython.info/).
- For academic purposes (teaching, research): **great scientific ecosystem**.
%% Cell type:markdown id: tags:
## Python: a language, an interpreter
Python is a programming language.
It is first an **interpreted** language (like Bash, Matlab, and in contrast with Fortran, C or C++), so we need an interpreter, i.e. a binary parsing Python code and running computer instructions.
Open a terminal. There are usually different python commands: `python`, `python2` and `python3`.
%% Cell type:markdown id: tags:
### Remark Python 2 / Python 3
2 languages, 2 interpreters, 2 commands
%% Cell type:markdown id: tags:
## Python distributions
- On Linux, there are the system python binaries.
- On Windows, Mac (and Linux), you can install and use
* https://www.python.org/downloads/
* [Anaconda or Miniconda](https://www.continuum.io/downloads)
([advice on how to install a good scientific Python environment](http://fluiddyn.readthedocs.io/en/latest/get_good_Python_env.html))
- In some systems (cluster), there are modules:
```bash
module load python
```
%% Cell type:markdown id: tags:
### Do it yourself: execute a script with the command `python3`
Go into the root directory of the repository of the training.
Run a script with `python3` (pyfiles/helloworld.py is *just* a text file):
```bash
cat pyfiles/helloworld.py
python3 pyfiles/helloworld.py
```
(`cat` is a Unix command that print the content of a text file.)
%% Cell type:markdown id: tags:
### Do it yourself: work interactively with [ipython](https://ipython.org/)
The command `ipython3` launch the program [ipython](https://ipython.org/), which is used for interactive python.
- In ipython, you can execute a first interactive instruction:
%% Cell type:code id: tags:
``` python
2 + 2
```
%% Output
4
%% Cell type:code id: tags:
``` python
3 / 4
```
%% Output
0.75
%% Cell type:markdown id: tags:
- Run the script from ipython.
%% Cell type:code id: tags:
``` python
run ../pyfiles/helloworld.py
```
%% Output
Hello world
My name is Pierre
%% Cell type:code id: tags:
``` python
# name is defined in ../pyfiles/helloworld.py
print(name)
```
%% Output
Pierre
%% Cell type:markdown id: tags:
- Help on an object can be displayed with the question mark (try it):
```ipython
name?
```
- Try to type "name." and to press on tab... The tab key is used to tell you how what you typed can be completed.
- Try to use the top and bottom arrows...
%% Cell type:markdown id: tags:
### Python in an IDE ([Spyder](https://pythonhosted.org/spyder/))
Launch the application Spyder, a Python IDE (Integrated development environment).
- a quite good code editor with:
- syntax coloring,
- code analysis powered by pyflakes and pylint,
- introspection capabilities such as code completion.
- Ipython console
- variable inspector
- ...
Remarks:
- it is very important to use a good editor to code in Python (for example Spyder, emacs or vi with [a good setup](https://bitbucket.org/fluiddyn/fluid_emacs.d), or [Visual Studio Code](https://code.visualstudio.com/docs/languages/python)).
%% Cell type:markdown id: tags:
### Python in the browser ([Jupyter](https://jupyter.readthedocs.io))
The presentations of this python training are made with Jupyter (demonstration).
This is a very powerful tool to present results (see these [examples](http://nbviewer.jupyter.org/)).