The Living Thing / Notebooks :


A programming language whose remarkable feature is working how you imagine, if not how it should

A Swiss army knife of coding tools. Good matrix library, general scientific tools, statistics tools, web server, art tools, but, most usefully, interoperation with everything else - It wraps C, C++, Fortran, includes HTTP clients, parsers, API libraries, and all the other fruits of a thriving community. Fast enough, easy to debug, garbage-collected. If some bit is too slow, you compile it, otherwise, you relax. A good default if you’d rather get stuff done than write code.

I typically do my stats and graphs in R, my user interface in javascript, my linear algebra library is fortran, and I use julia for my other scientific calculations, but python is the thread that stitches this Frankensteinisch monster together.

Of course, it could be better. clojure is more elegant, scala is easier to parallelise, julia prioritises science more highly…

But in terms of using a well-supported language that goes on your computer right now, and requires you to reinvent few wheels, and which is transferable across number crunching, web development, UIs, text processing, graphics and sundry other domains, and does not require heavy licensing costs, this one is a good starting point.

What a pitch! Now, let’s look closer and see all the horrid things that are wrong with it.

Debugging, profiling and testing

See Python debugging, profiling and testing.

ipython, the interactive python upgrade

The python-specific part of jupyter, which can also run without jupyter. Long story.

The main thing I forget here is

Pretty display of objects

ipython display protocol

Check out the Rich display protocol which allows you to render objects as arbitrary graphics.

How to use this? The display api docs explain that you should basically implement methods such as _repr_svg_.

I made a thing called latex_fragment which leverages this to display arbitrary latex inline. This is how you use it:

def _figure_data(self, format):
       fig, ax = plt.subplots()
       ax.plot(, 'o')
       data = print_figure(fig, format)
       # We MUST close the figure, otherwise IPython’s display machinery
       # will pick it up and send it as output, resulting in a double display
       return data

    # Here we define the special repr methods that provide the IPython display protocol
    # Note that for the two figures, we cache the figure data once computed.

    def _repr_png_(self):
       if self._png_data is None:
           self._png_data = self._figure_data('png')
       return self._png_data

For a non-graphical non-fancy terminal, you probably simply want nice formatting of dictionaries etc:

from pprint import pprint, pformat
pprint(obj)  # display it
print(pformat(obj))  # get a nicely formatted representation

Wait, you want to write your own pretty-printer, with correct indentation? Use tiles.

Pro tip: dotenv

dotenv allows easy configuration through OS environment variables or text files in the parent directory. You should probably use this. PRO-TIP: there are lots of packages with similar names. Make sure you install using this one

pip install python-dotenv

String formatting

String formatting things that are unnecessarily hard to discern from the manual

What a nightmare is that manual for the string formatting. While all the information you need is in there, it is arranged in perverse inversion of some mixture of the frequency and the priority with which you use it. See Marcus Kazmierczak’s cookbook instead.


## float precision

>>> print("{:.2f}".format(3.1415926))
## left padding

>>> print("{:0>2d}".format(5))
## power tip which the manual does not make clear:

## variable formatting
>>> pi = 3.1415926
>>> precision = 4
>>> print( "{:.{}f}".format( pi, precision ) )


f-strings make things somewhat easier for python 3.6+ because they don’t need to mess around with naming things for the .format(foo=foo) call:

>>> name = "Fred"
>>> f"He said his name is {name!r}."
"He said his name is 'Fred'."


Why is a now timestamp in UTC not the first line in any academic research? because it’s tedious to look up the different bits.

Here you go:

from datetime import datetime

Rendering HTML output

You have a quick and dirty chunk o’ HTML you need to output? You aren’t writing some damnable webapp with a nested hierarchy of template rendering and CSS integration into some design framework? you just want to pump out some markup?

I recommend yattag which is fast, simple, good and has a 1-page manual. It works real good.

Rendering markdown as HTML

from markdown import markdown
html_string = markdown("""
# Title

Paragraph. *emphasis*.

Which Foreign Function Interface am I supposed to be using now?

Want to call a a function in C+, C++, FORTRAN etc from python?

If you are just talking to C, ctypes is a python library to translate python objects to c with minimal fuss, and no compiler requirement. See the ctypes tutorial.

And of course, if you have your compiler lying about, Python was made to talk to other languages and has a normal C API.

If you want something closer to python for your development process, Cython allows python compilation using a special syntax, and easy calling of foreign functions in one easy package. SWIG wraps function interfaces between various languages, but looks like a PITA; (See a comparison on stackoverflow).

There is also Boost.python if you want to talk to C++. Boost comes with lots of other fancy bits, like numerical libraries.

There are many other options, but in practice I’ve never needed to go further than cython, so I can’t even talk about all the options listed here knowledgeably.

Related, overlapping:


There are too many options for interfacing with external libraries and/or compiling python code.

FFI, ctypes, Cython, Boost-Python, numba, SWIG…


Lowish-friction, well tested, well-document works everywhere that Cpython extensions can be compiled. Compiles most python code (apart from generators and inner functions). Optimises python code using type defs and extended syntax. Here, read Max Burstein’s intro.

Highlights: It works seamlessly with numpy. It makes calling C-code easy

Problems: No generic dispatch. Debugging is nasty, like debugging C with extra crap in your way.


More specialised than cython, uses LLVM instead of the generic C compiler. Numba make optimising inner numeric loops easy.

Highlights: jit-compiles plain python, so it’s easy to use normal debuggers then switch on the compiler for performance improvements using the @jit Generic dispatch using the @generated_jit decorator. Compiles to multi-core vectorisations as well as CUDA. In principle this means you can do your calculations on the GPU.

Problems: LLVM is a shifty beast and sensitive version dependencies are annoying. Documentation is a bit crap, or at least unfriendly to outsiders. Practically, getting performance out of a GPU is trickier than working out you can optimise away one tedious matrix op, and doing it at this level is hard. There is too much messing with details of how many processors to allocate what to.

You might find it easier to use julia if a well-maintained and documented LLVM infrastructure is a real selling point for you.

Packaging and environments

Not so hard, but confusing and chaotic due to many long-running disputes only lately resolving.



The distribution you use if you want to teach a course in numerical python without dicking around with a 5 hour install process.

Has a slightly different packaging workflow. See, e.g. Tim Hoppper’s workflow which explains this environment.yml malarkey, or the creators’ rationale.

The upshot for the end user is that if you want to install something with tricky dependencies like ViTables, you do this:

conda install pytables=3.2
conda install pyqt=4

Aside: Do you use fish shell? You need to do some extra setup. Specifically, add the line

source (conda info --root)/etc/fish/conf.d/

into ~/.config/fish/

NB Conda will fill up your hard disk if not regularly disciplined. via conda clean.

conda clean -pt

You might also want to not have the gigantic MKL library installed, of which I am in any case not a fan. You can (usually) disable it per request:

conda create -n pynomkl python nomkl

Note that the packagers claim MKL is a 100MB library, which is am error so large than it should raise concerns about their competence to distribute a numerical calculations package. 100MB? Poppycock. The package alone is 800MB, and it’s even bigger installed. That’s not counting the fact that conda will keep many many versions around if you are not assiduous in your clonda clean habits. MKL alone was using about 10GB total on my machine when I last checked, which is two orders of magnitude off what the unwary might assume.

Reducing the harm of this kind of nonsense is one reason you should only ever install the minimalist miniconda as your base anaconda distribution, cautiously adding things as you need them.

Python environment/version management

venv is now a built-in python virtual environment system in python 3. It doesn’t support python 2 but fixes various problems, e.g. it supports framework python on macOS which is very important for GUIs, and is covered by the python docs in the python virtual environment introduction. It has a higher-level, er, …wrapper (?) called pipenv.

# Create venv

python3 -m venv ~/.virtualenvs/learning_gamelan_keras_2
# Use venv from fish

source ~/.virtualenvs/learning_gamelan_keras_2/bin/
# Use venv from bash

source ~/.virtualenvs/learning_gamelan_keras_2/bin/activate

Python environment management management

One suggestion I’ve has is to use pyenv. which eases and automates switching between all the other weird python environments created by virtualenv, python, os python, anaconda python etc.


Asynchrony in python

See asynchronous python.

Watching files for changes

Does this inotify solution work for non-Linux? Because macOS uses FSEvents and Windows uses I-don’t-even-know.

watchdog asserts that it is cross-platform. (source)

Python 2 vs 3

Are you old? New to python 3?

Sebastian Raschka, The key differences between Python 2.7.x and Python 3.x with examples

Neat python 3 stuff

Alex Rogozhnikov, Python 3 with pleasure highlights some new tricks which landed recently.

Useful for us is a friendlier python struct-like thing, the data class Geir Arne Hjelle explains.

Override module accessors.

Asynchrony is less awful.

Python 2 and 3 compatibility

TL;DR: I am no employee of giant enterprise-type business with a gigantic legacy code base, and so I don’t use python 2. My code is not python 2 compatible. Python 3 is more productive, and no-one is paying me to be less productive right now. Python 2 code is usually easy to port to python 3. It is possible to write code which is compatible with python 2 and 3, but then I would miss out on some of the work that has gone into making python 3 easier and better, and waste time porting elegant easy python 3 things to hard boring python 2 things.

Writing python 2 and 3 compatible code.

TODO: six versus future.


Python 3.6+ includes type hinting, and projects such as mypy support static analysis using type hints. There are not yet many tutorials on the details of this. Here’s one.

Short version: you go from this:

def fib(n):
    a, b = 0, 1
    while a < n:
        yield a
        a, b = b, a+b

to this:

def fib(n: int) -> Iterator[int]:
    a, b = 0, 1
    while a < n:
        yield a
        a, b = b, a+b

However, if I am going to that much trouble, I would in fact rather julia, which takes type-hinting further, using it to JIT-compile optimised code.

Command line parser

Another bike-shedding danger zone is command-line parsing, leading to the need to spend too much time parsing command line parsers rather than parsing command lines.

Miscellaneous stuff I always need to look up

Misc recommendations