The Living Thing / Notebooks :

Python

A programming language whose remarkable and rare feature is working like 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 damn-well-supported language that goes on your computer right now, and requires you to reinvent few wheels, and which is transferrable 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 a bit closer and see all the horrid things that are wrong with it.

Debugging, profiling and testing

See Python debugging, profiling and testing.

Pro tip: dotenv

dotenv allows easy configuration thourgh 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.

Highlights:

## float precision
>>> print("{:.2f}".format(3.1415926))
3.14
## left padding
>>> print("{:0>2d}".format(5))
05
## power tip which the manual does not make clear:
## variable formatting
>>> pi = 3.1415926
>>> precision = 4
>>> print( "{:.{}f}".format( pi, precision ) )
3.1415

f-strings

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

timestamps

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
datetime.utcnow().isoformat(timespec='seconds')

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:

Compiling

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

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

Cython

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.

numba

More specialised than cython, uses llvm instead of c compiler. Numba make optimising inner numeric loops easy.

Highlights: jit-compiles plain python, so it's very 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. Practically, getting performance out of a GPU is trickier than working out you can optimise away one tedious matrix op. 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.

General

Anaconda

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 is 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/conda.fish

into ~/.config/fish/config.fish.

NB Conda will fill up your hard disk if not regularly disciplined. via conda clean to stop your disk filling up with obsolete versions of obscure dependencies for that package you tried out that one time.

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 disable it per request:

conda create -n pynomkl python nomkl

Note that the packagers claim this is a 100MB library. 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. MKL alone was using about 10GB total no my machine when I last checked, which is two orders of magnitude off what the unwarey might assume reading that manual page.

Reducing the harm of this kind of nonsense is one reason you should only ever install the minimalist miniconda as your base anaconda distribution, otherwise you might be tempted to try to use the base environment to do something, which will lead to even more bloat.

Python environment/version management

venv is now a builtin virtual python environment system in python 3. It doesn't support python 2 but fixes various problems, e.g. it supports framework python on OSX which is very important for GUIs, and is covered by the python docs in the python virtual environment introduction.

# Create venv
python3 -m venv ~/.virtualenvs/learning_gamelan_keras_2
# Use venv from fish
source ~/.virtualenvs/learning_gamelan_keras_2/bin/activate.fish
# 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.org python, os python, anaconda python etc.

BUT WHO MANAGES THE VIRTUALENV MANAGER MANAGER?

Asynchrony in python

See asynchronous python

Watching files for changes

Does this inotify solution work for non-linux? Because OSX 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

TLDR: 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 lots 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.

Typing

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 you are going to this trouble, why not just use julia, which takes type-hinting further, using it to JIT-compile optimised code?

Which command line parser is the good one?

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