[Tutor] integrating python and C

Chris Fuller cfuller084 at thinkingplanet.net
Sat Sep 24 06:18:48 CEST 2011


Sudoku is actually a pretty easy problem computationally, I'd do the whole 
thing in Python, unless you aren't as comfortable doing fancy algorithms 
(usually easier, anyway, unless you aren't used to the language, or really 
need static typing) in Python.. but then this would be a good opportunity to 
overcome that :)

Or you're doing large boards, generating puzzles in bulk, or something like 
that that multiplies the computational effort.  The GNOME Sudoku is written in 
Python, by the way.

There's tons of ways to interface C and Python.  You can access the Python API 
directly:  http://docs.python.org/extending/index.html; this is not for the 
faint of heart, but it provides maximum control and you learn a lot about  
Python internals on the way!

Write your C code as a library and use SWIG to wrap it up into a Python 
module:  http://www.swig.org/; this can get interesting if you have 
complicated call signatures, I sometimes avoid it and go right to the API, 
since I'm comfortable there :)

If your code is C++, you can do something similar with Boost::Python:    
http://www.boost.org/doc/libs/1_47_0/libs/python/doc/; I've never used it, but 
it's there.  Note that SWIG works fine with C++ libraries.

There's always the Python-C hybrid, Cython:  http://cython.org/; I haven't 
waded into its waters yet, either.

But I'd recommend doing it all in Python, for the exercise, if nothing else, 
unless you have a good reason not to.

GNOME Sudoku:
http://live.gnome.org/GnomeSudoku

Cheers

On Friday 23 September 2011, surya k wrote:
> Hi!
> 
> Actually, I'm writing sudoku solver. So, I'd write my logic in C but when
> it comes to GUI, I feel that python is much easier than doing in C.
> What I'm looking at is, take input from python program with graphic
> interface and passing it to my C program file then solve the puzzle and
> again passing the whole array to python code to display..
> 
> How should I do it.
> 
> Thanks.



More information about the Tutor mailing list