From wiki
Jump to: navigation, search


The Python style guide is in [PEP 8]

Python is very strict on indentation. Code blocks are kept together by their indent. Use either tabs or spaces (recommended) not both.

For readability long lines can be over more lines using () around a statement or \ to escape new-line's

(this = a(verylongline,
    so you can use parentheses)
you can also use \
    to escape the end-of-line character

We have our own template.


Modules need to be imported into your program by the import command.

To add the location of your own modules to the python search path put it in the PYTHONPATH see #sys below variable.

Import finds a file <modulename>.py or in directory <modulename>. <modulename>.py or are executed on import. Usually not many code is in modules to execute immediately, functions and classes are mostly in there.

import <module>
Import everything from the module, address components as <module>.<component>.
import <module> as <short>
Calls can have the short name. E.g. numpy is often imported as np
from <module> import *
Module components can be called without the module name. Beware of duplicates.
from <module> import <component>
Import a specific component from a modules, callable by just the component name.

We try to use modules that are available by default (on linux systems). If not it will be mentioned in this article. Only modules for which we use a very limited number of functions are listed here. More complex modules have there own article


Provides a number of system variables

List of everything on the commandline. sys.argv[0] is the program itself.
The python version you run
The directories python looks into when doing an import. The script location always in sys.path. Directories in the environment variable $PYTHONPATH are added to sys.path


Date and time functions

from datetime import datetime
timestamp ="%Y%m%d_%H%M%S")


Time functions

Sleep for 3 seconds
from time import sleep 


Module to execute shell commands

In python2

import subprocess
exitcode ="<any command>")
commandoutput = subprocess.check_output("<any command>")

Use ("command",shell=True) to have the call work like it would on the commandline

In python3

import subprocess
CompletedProcess ="<any command>")

The CompletedProcess returned has (args, returncode, stdout, stderr)


Generate random numbers.

Return a floating point in the range from 0.0 to 1.0 (including both)
Return an integer in the range from start to stop (including both)
Pick a random element from a list:


Enable parallel processing

t1=threading.Thread(target=<a function>)
Return a thread object to run <a function> in the background
Start the thread for the function targeted by t1
Wait until t1 is ready or until <timeout> has expired. Returns None always.
Return True if t1 is still running (usefull e.g. after join with timeout).


Module to parse the commandline arguments (sys.argv).


from collections import defaultdict
WARNING; In a dictionary created with defaultdict a key will be added when you try to read a non-existing key.
adict = defaultdict(<type>)
Create a dictionary key of the provided type automagically when it is used (WARNINIG, also when you try to read it). Use this to avoid checking if a key already exists before you populate it.
adict = defaultdict(lambda: defaultdict(lambda: defaultdict()))
Use lambda function to handle multilevel dictionaries


Everything is an object in python. Objects can be variables and functions.

Variables are always pointers to objects.
a = 2
b = 2

Both a and b point to the same object (the immutable integer '2')

Beware making variables point to each other when a represents a mutable object.
a = [1,2,3]
b = a

As b points to a and a has changed, b also returns [1,2,3,4]

a = row[0] or "0"
Set a to 0 if row[0] as a value that evaluates to False ( or None). Comes in handy for selections from databases where you expect a number but the field is empty.
Variables are local by default. If a routine has any assignment to a variable it is local. If you have defined a variable outside a routine and need assignments to it in the routine you have to declare it global explicitly.
a = 'a string'

def main():
    global a
    a = "This would fail with 'local variable 'a' referenced before assignment' if 'a' was not declared as global"


[Geeks for Geeks] has as good page about this.

Virtual environment

virtualenv --clear --always-copy -p <pythonbinary> venv
Create a virtual environment in the current directory. Clear the existing virtual environment, copy the files instead of symlinking them and install the <pythonbinary> in it.