Some months ago I finally caught up with modern development practices and switched to Git for new software projects. I even now use my GitHub and Bitbucket accounts more often, e.g. for collaboration projects, though I host my own projects on my own server with the help of Trac and gitosis.
One thing I do like about GitHub though, are Gists. Like many good ideas this one makes one wonder, why nobody came up with it before. Basically Gists are code snippets you publish on the web (like in a pastebin) but they are under version control and each Gist is its own mini git repository. They let you share programming ideas and nifty little tools with the rest of the world with ease and others can easily take them and comment, expand or correct them. The revision control makes it clear, what developed from where and who did what. Finally, your Gist page on GitHub serves as a central place, where you can browse, search and present all these digital scrap notes.
It is also very easy to include Gists in your blog, which is what I’m starting with this post.
Running Cython source files
Recently, on the German mailing list of the pyCologne User Group, someone was pointing out Python’s poor performance at an implementation of the fibonacci series using recursion and showed some Scala code that profited from a few added type declarations. I showed that a similar performance gain could be achieved using Cython, with very few changes to the original pure Python code.
Cythons primary purpose is to write C-extension modules for Python, either to speed things up or to interface with C/C++ code. This means that to run Cython code you usually have to a) compile and link it first to build an extension module and b) write some Python code that imports the extension module and runs some callable from it. The first step can be made transparent by using the pyximport module, which automatically compiles any Cython modules as soon as you import them in your Python code. But you still need an extra Python script, even if you just want to run one function from the Cython module.
This is where the
--embed option of the Cython compiler/preprocessor comes
in. When given, Cython generates code to set the module name to
run as a standalone program. Which means you can use the usual Python idiom
__name__ == '__main__': to run a “main” function in this case. You can then
compile the resulting C-code like any normal program and run it directly
without the Python interpreter. The binary still depends on libpython and any
Python modules used in the code (e.g. from the standard library).
I created a small shell function to automate the process of compiling a Cython
source file with the
--embed option and turning it it into a binary. The
function then runs the resulting binary, so you can use this function to run a
*.pyx file directly, provided that the compilation and linking step doesn’t
depend on any libraries besides
libpython. For example:
1 2 3 4 5
$ pyxrun -t fibonacci.pyx 165580141 real 1.43 user 1.37 sys 0.01
So, that’s the gist of it….