quote start I just wanted to say excellent work on Nathive quote end

C-extensions with Python syntax


(slide-7a) Python is a great multipurpose tool, but it lacks doing massive mathematical process, this is not a secret, two or three talks in the Libre Graphics Meeting of this year are about this, the most used solution is to extend Python with C-extensions (slide-7b), this allow us to get low-level performace fully integrated with Python, but this implies some handicaps, first, we are forced to know how to code in C, second, we must handle manually the input and output variable types conversion, and finally we must config it to work as a Python extension.

Inspired by Cython, -not CPython-, (slide-7c) which is a language abstraction layer, Nathive has its own syntax abstraction (slide-7d), it's like a lightweight version of Cython, but here the scope is smaller, while Cython has almost full Python features, here we just need to manipulate pixel buffers, and manage the input and ouput typing in a simplest way, the Python-like syntax was not necessary, but it's cool.

But, let me highlight something (slide-7e), this is not for manipulate strings or other stuff, this is just for pixel or other low-level operations, the high-level code must stay in real Python.

This is an example of .cy code (slide-7f), the picker extension, as you can see, it's almost Python, but there are two differences, the last argument is to define the input and output typing (slide-7g), so as input arguments we have a pixel buffer pointer and three ints, which are buffer width and coordinates, and after process its return a list of three ints, RGB. The other addition (slide-7h) is the variable definition, because after all we are in C, in the picker example (type long index) and (type char r g b).

Now, this is the equivalent C code (slide-7i), it's six times longer and much harder to read... so imagine the time we can save up using this in a more complex extensions.

Previous page | Next page