Take a swig of C with your python

From a tutorial on SWIG here, with some changes for my machine. The script is swigp.sh and needs to be chmod +x before use.

sudo apt-get install swig
locate -r "/Python\.h$"

The locate command uses option -r for regular expression and I am just a REGEX kind of guy so I used that.

#!/usr/bin/env bash
swig -python example.i
gcc -c example.c example_wrap.c   -I/usr/include/python2.6
ld -shared example.o example_wrap.o -o _example.so 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import example
print example.fact(6)
exit()

This is a python program to further automate the process. I could add the line "python examplec.py" to my shell script.

motey@motey-desktop:~/Documents$ ./swigp.sh
motey@motey-desktop:~/Documents$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> example.fact(5)
120
>>> exit()

This really makes it simple to add a little C code to the python. I was thinking about making a wxPython interface to my little ethernet / TCP / IP / AI spoofer and it seemed the lowest level interface would be messy in python and it just a small section anyway, so it seems reasonable to just code it in C and SWIG it. If I knew it was this easy, I would have done this long ago.

0 comments:

Contributors

Automated Intelligence

Automated Intelligence
Auftrag der unendlichen LOL katzen