C always provide light-weight processing among all high-level programming language. Using python give advantages which we can extend into C as python modules. There are several workflow we should know before starting.
1. Naming Convention
Preferable we create C files always followed with module to indicating it’s a “Module”.
Eg:
1 | temperaturemodule.c |
2. Using Python Dev API
We should have “python-dev” installed in our Linux or Ubuntu.
Then we can call Python API to wrap C codes as python modules.
3. Use setup.py
Because we will treat C extension as python modules, we should provide setup.py installation.
At this example, I will show how to write “temperature” module in C and extend into python as module.
First, we create temperaturemodule.c. Then we build design by :
1 2 3 4 5 6 7 8 9 | #INCLUDE HEADER #CREATE METHOD fahrenheit_to_celcius #CREATE METHOD celcius_to_fahrenheit #REGISTER ALL METHODS #INITIALIZE |
1. #INCLUDE HEADER
We should call python header “Python.h”
2. #CREATE METHOD fahrenheit_to_celcius and #CREATE METHOD celcius_to_fahrenheit
We create to function to convert fahrenheit to celcius and vice-versa.
3. #REGISTER ALL METHODS
Then we should register all methods
4. #INITIALIZE
The last thing, we should initialize this modules.
The codes :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <Python.h> static PyObject* fahrenheit_to_celcius(PyObject* self, PyObject* args) { double fahr, celcius; if(PyArg_ParseTuple(args, "d", &fahr)) { celcius = 5 * (fahr-32) / 9; return PyFloat_FromDouble(celcius); } else { return NULL; } } static PyObject* celcius_to_fahrenheit(PyObject* self, PyObject* args) { double celcius, fahr; if(PyArg_ParseTuple(args, "d", &celcius)) { fahr = (9 * celcius / 5) + 32; return PyFloat_FromDouble(fahr); } else { return NULL; } } static PyMethodDef TemperatureMethods[] = { {"celcius_fahrenheit", celcius_to_fahrenheit, METH_VARARGS, ""}, {"fahrenheit_celcius", fahrenheit_to_celcius, METH_VARARGS, ""}, }; PyMODINIT_FUNC inittemperature(void) { (void) Py_InitModule("temperature", TemperatureMethods); } |
Then, we should create “setup.py” for installation :
1 2 3 4 5 6 7 8 9 | from distutils.core import setup, Extension module1 = Extension("temperature", sources=["temperaturemodule.c"]) setup (name = "FahrenheitCelcius", version = "1.0", description = "Convert Fahrenheit value into Celcius and vice-versa", ext_modules = [module1] ) |
After we have “temperaturemodule.c” and “setup.py”, we can start build this C into python module :
1 2 | python build setup.py cd build/<lib>/ |
On folder which contain “temperature.so”, we can execute python intepreter and start import this module by :
1 2 3 4 5 6 7 8 9 10 11 | Python 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import temperature >>> f = 100 >>> c = temperature.fahrenheit_celcius(f) >>> c 37.77777777777778 >>> f = temperature.celcius_fahrenheit(c) >>> f 100.0 |
All codes can be easily grab at my github in :
https://github.com/yodiaditya/C