Commit 81d1bed5 authored by paugier's avatar paugier
Browse files

Add base files

parent ac6611b8
*.pyd*
*.pyc
*~
*tmp
\#*\#
ipynb/*.slides.html
.ipynb_checkpoints
**/Untitled.ipynb
**/*.egg-info/
ipynb/index.html
.vscode
TP/TP2_package/mypackuga/build
TP/TP2_package/mypackuga/dist
\ No newline at end of file
Copyright (c) 2020, Pierre Augier
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IPYNBDIR := ipynb
IPYNBFILES := $(shell find $(IPYNBDIR) -name '*.ipynb' | grep -v ipynb_checkpoints)
IPYNBPRES = $(addsuffix .slides.html, $(basename $(IPYNBFILES)))
define STR_HELP
This makefile can be used for
help: print this help.
lab: run jupyter-lab.
presentations: build the html presentations.
serve: launch a small server to display the notebooks with reveal.js
endef
export STR_HELP
.PHONY: help lab serve presentations
help:
@echo "$$STR_HELP"
clean:
rm -f ipynb/*.slides.html
rm -f ipynb/index.html
lab:
jupyter-lab
ipynb/index.html: ipynb/index.rst
cd $(IPYNBDIR) && rst2html5 index.rst > index.html
%.slides.html: %.ipynb ipynb/slides_reveal_wide.tpl
jupyter-nbconvert $< --reveal-prefix='./reveal.js' --to slides --template ipynb/slides_reveal_wide.tpl
presentations: $(IPYNBPRES) ipynb/index.html
serve: $(IPYNBPRES) ipynb/index.html
cd $(IPYNBDIR) && python3 -m http.server 9000
import sys
if sys.version_info[:2] < (3, 6):
raise RuntimeError("Python version >= 3.6 required.")
try:
import conda
except ImportError:
print(
"Problem: the conda package not importable.\n"
"Please run this script in the base conda environment"
)
sys.exit(1)
else:
print("Good! conda package importable!")
try:
import numpy, scipy, matplotlib, pandas
except ImportError:
print(
"Problem: numpy, scipy, matplotlib, pandas not importable\n"
"Maybe run:\nconda install numpy scipy matplotlib pandas"
)
else:
print("Good! numpy, scipy, matplotlib, pandas importable!")
## To be done before the first day of the training session
We will have to use Python 3 (with Miniconda3), a good Python IDE (either
Spyder or VSCode), Jupyter and a version-control tool (Mercurial, or Git if you
know it and really like it).
In the following, we give indications about how to install these tools and how
to get the repository of this training locally on your computer. Please, try to
do this before the training and tell us if you encounter problems. You can fill
an issue
[here](https://gricad-gitlab.univ-grenoble-alpes.fr/python-uga/training-hpc/issues)
to explain what you did and what are the errors (please copy / paste the error
log).
Moreover, let's add that the best OS for HPC (and HPC with Python) is
Linux/GNU. Windows (at least without
[WLS](https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux)) and even
macOS are less adapted for this particular application. Python is a
cross-platform language but nevertheless, you will get a better experience for
HPC with Python on Linux.
#### Install Python and utilities
The first step is to install miniconda3 (see
[here](https://docs.conda.io/en/latest/miniconda.html))
```bash
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
```
You have to answer "yes" to the last question about the initialization of
conda. When it's done, close the terminal (with `ctrl-d`) and reopen a new
terminal (with `ctrl-alt-t`). The world "(base)" should be in the line in the
terminal.
You will need to activate the conda channel `conda-forge` with:
```conda config --add channels conda-forge```
#### Install and setup Mercurial
To install and setup Mercurial, you can do:
```bash
pip install conda-app --no-cache-dir
conda-app install mercurial
```
Close the terminal (`ctrl-d`) and reopen a new terminal (`ctrl-alt-t`)!
```hg config --edit```
You have to write something like:
```
[ui]
username=myusername <email@adress.org>
editor=nano
tweakdefaults = True
[extensions]
hgext.extdiff =
# only to use Mercurial with GitHub and Gitlab
hggit =
[extdiff]
cmd.meld =
```
By default, Mercurial will open the program `vi`. It's highly probable that you
won't like it! Just exit and save the file by writing `:x` and pressing the
enter key. Then reopen the file `~\.hgrc` with another program (for example
using gedit with the command `gedit ~\.hgrc`).
Warning: Mercurial **has to be correctly setup**! Since we will use [the Gitlab
instance of UGA](https://gricad-gitlab.univ-grenoble-alpes.fr), the Mercurial
extension hg-git has to be activated so the line `hggit =` in the configuration
file is mandatory.
#### Clone this repository
Once everything is installed and setup, you should be able to clone the
repository of the training with:
```hg clone https://gricad-gitlab.univ-grenoble-alpes.fr/augierpi/intro-python.git```
Please tell us before the training if it does not work.
#### Install few packages in your base conda environment
```
cd intro-python
conda install --file requirements.txt
```
#### Check your environment
Once the repository is cloned you should have a new directory `training-hpc`
and you should be able to enter into it with `cd training-hpc`.
Finally, you can check that your Python environment is all fine with:
```python check_env.py```
#### Build the presentations
One needs Jupyter, rst2html5 (installable with `pip install rst2html5`), plus
`make` and few other Unix tools. Therefore, it is not easy to build the website
on Windows.
On Unix-like systems, `make presentations` should build the presentations and
`make serve` should start a server to visualize them in a browser.
#### Bonus
You can also install VSCode (see [here](https://code.visualstudio.com/download)).
# 2 different syntaxes for importing a module
import util
from util import myvar1, print_variables
util.myvar0 = 100
myvar1 += 100
print(f'in prog.py, util.myvar0 = {util.myvar0}; myvar1 = {myvar1}')
print_variables()
print('begin of util.py')
myvar0 = 0
myvar1 = 1
def print_variables():
print(f'in function print_variables: myvar0 = {myvar0}; myvar1 = {myvar1}')
# 2 different syntaxes for importing a module
import util
from util import myvar1, print_variables
util.myvar0 = 100
myvar1 += 100
print(f'in prog.py, util.myvar0 = {util.myvar0}; myvar1 = {myvar1}')
print_variables()
print('in prog.py, __name__ =', __name__)
# __name__ is a special variable always defined.
# its value depends on how the file is called (directly executed or imported)
if __name__ == '__main__':
print('end of prog.py')
print('begin of util.py')
myvar0 = 0
myvar1 = 1
def print_variables():
print(f'in function print_variables: myvar0 = {myvar0}; myvar1 = {myvar1}')
print('in util.py, __name__ =', __name__)
# __name__ is a special variable always defined.
# its value depends on how the file is called (directly executed or imported)
if __name__ == '__main__':
# this code is executed only in the file is directly executed
print('the module util.py has been directly executed')
print_variables()
print('end of util.py')
else:
print('the module util.py has been imported')
print('Hello world')
name = 'Pierre'
print('My name is ' + name)
var0=1
var2 = 2;
print(mystr)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment