Compiling mixed Sage and Python code with Cython

At the moment I’m working on some maths code written in a combination of standard Python and its mathematical superset, Sage. The code is fairly computationally expensive (it’s essentially a brute force search for chemical reaction networks that are monotone). Consequently, after getting the code working as a proof-of-concept, I wanted to compile it to C using Cython to get a bit more speed out of it. I found the documentation on converting Sage and Python scripts to Cython a bit vague, particularly when it comes to combining the two, so this article gives step by step instructions based on my findings.