Tools for Python Coding

May 17, 2018

Outline

  • Good code writing practices (PEPs)
  • Package management
  • IDE

PEPs

What are PEPs

Python Enhancement Proposal : list of guidelines, suggestions, ...

 

Listed here : https://www.python.org/dev/peps/

 

The ones tackled here :

  • PEP20 : Python philosophy
  • PEP8 : coding style
  • PEP257 : Documentation

PEP20 (Zen of Python)

You can get it in a Python shell with

import this

 

There are a list of 19 "principles" for Python coding.

 

Ref : https://www.python.org/dev/peps/pep-0020/

PEP8 (Writing guidelines)

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 :

  • flake8
  • autopep8
  • integrated in IDE (options in Spyder, Pycharm, jupyterlab-flake8...)

PEP8 : Spaces

Spaces around operators

a = 2 + 3

Except for showing priority or in function arguments

 

No space before colon and comma, but one after.

PEP8 : Lines

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.

PEP8 : imports

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

PEP8 : Names

  • In loop and indices : lowercase letter
  • Modules, variables, functions and methods : lowercase_and_underscores
  • Class : CamelCase
  • Pseudo Constantes : UPPERCASE
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

PEP257 (Documentation)

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.
    """

Ref : https://www.python.org/dev/peps/pep-0257/

Package management

What is a package/module

Module = a python file.

 

Package

= namespaces which contain multiple packages and modules themselves

= directory with file init.py.

Install a package

The main package repository : Pypi (https://pypi.org/)

 

The classical way to install from Pypi : pip

pip install package

Create a package environment

As soon as

  • you are working on more than one project
  • you want to test an external package

 

-> You will need an environment manager

Virtualenv (+pip)

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

Ref : https://virtualenv.pypa.io/en/stable/userguide/

Conda

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

Conda

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

Conda (2/2)

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

A new "official" way : pipenv

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/

Summary

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 ?

Development environment

The iPython console

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]

Spyder

Install

conda install spyder

A scientific IDE, similar to Matlab/Rstudio :

Jupyter

First centered around the Jupyter notebook.

Now tries to be a full web IDE with JupyterLab (but still in Beta)

  • Allow multiple views/documents connected to the same kernel
  • File editor is not quite there yet.

Install :

pip/conda install jupyterlab

Demo : https://github.com/jupyterlab/jupyterlab-demo

Jupyter and multiple environments

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)"

And others IDE...

Vim, Pycharm, (Hydrogen)...