May 17, 2018
Python Enhancement Proposal : list of guidelines, suggestions, ...
Â
Listed here : https://www.python.org/dev/peps/
Â
The ones tackled here :
You can get it in a Python shell with
import this
Â
There are a list of 19 "principles" for Python coding.
Â
List of coding conventions (spaces instead of tabs, line breaks, spaces around operators...)
Â
Why ? Because a code is read much more often than it is written.
Â
The list can be found here : https://www.python.org/dev/peps/pep-0008/
Â
And several tools help enforcing it :
Spaces around operators
a = 2 + 3
Except for showing priority or in function arguments
Â
No space before colon and comma, but one after.
Max 80 characters (as much as possible).
Â
If a line is too long use parenthesis:
foo = a_thing_with_a_very_long_long_name(
with a,
lot,
of,
arguments)
or backslash :
queryset = ModelDjangoALaNoix.objects\
.filter(foo=True)\
.exclude(bar=False)
Two lines between functions and classes, one between methods.
import os # import module from standard lib
import sys # group as same kind of stuff
from django.conf import settings # import from module
from django.shortcuts import redirect # grouped as same module
from my_project.my_module import thing # my project
MAX_SIZE = 100000
a_variable = 10
def a_function():
for x in range(10):
print(x)
return x
class NewClass:
def a_method(self):
pass
Use of "docstring" : """
def func(a,b):
"""
Docstring of the function : first a summary
Then a blank line followed by a potentially
long, very long, description.
"""
Module = a python file.
Â
Package
= namespaces which contain multiple packages and modules themselves
= directory with file init.py.
The main package repository : Pypi (https://pypi.org/)
Â
The classical way to install from Pypi : pip
pip install package
As soon as
Â
-> You will need an environment manager
Install :
pip install --user virtualenv
Create env :
virtualenv /path/to/project/env_name
Activate :
source /path/to/project/env_name/bin/activate
Install inside env with pip install package_name
Conda is a package/environment manager provided with the miniconda/anaconda Python distribution.
Â
Install miniconda (or the full anaconda distribution) : https://conda.io/miniconda.html
Create an environment with python 3 and numpy
conda create -n name_env python=3 numpy
List environments
conda env list
Activate the environment
conda activate name_env
Check what's happening : which python
Export and reinstall env
conda list --explicit > bio-env.txt
conda env create --file bio-env.txt
Install package from channel
conda install -c conda-forge numpy
Pros:
- language-agnostic package manager (python, r, external dependencies)
- one tool
Cons:
- a bit slow for dependency resolution
- not all packages are available/updated on default channel
Combine pip and virtualenv (similar to conda). Integrated in a package development workflow (lock versions, dev and prod envs...)
Install :
pip install --user pipenv
Add to path :
PYTHON_BIN_PATH="$(python3 -m site --user-base)/bin"
PATH="$PATH:$PYTHON_BIN_PATH"
Got to project directory and pipenv install numpy
Doc : https://docs.pipenv.org/
pip+virtualenv
- For Python-only
- project oriented
- two tools
conda
- for any package/dependencies
- environment oriented
- a bit slow/bloated ?
pipenv
- For Python-only
- Package development oriented
- The new standard way ?
A improved REPL (Read Eval Print Line) over the classic Python.
Â
Call it by typing ipython instead of python
Â
Lots of "magics"
%run myscript.py
%timeit L = [n ** 2 for n in range(1000)]
%prun L = [n ** 2 for n in range(1000)]
%matplotlib [inline/qt]
Install
conda install spyder
A scientific IDE, similar to Matlab/Rstudio :
First centered around the Jupyter notebook.
Now tries to be a full web IDE with JupyterLab (but still in Beta)
Install :
pip/conda install jupyterlab
Inside an environment (with ipykernel installed inside)
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
Then launch jupyter and select the kernel "Python (myenv)"
Vim, Pycharm, (Hydrogen)...