Extending Python Questions .....

Nick Craig-Wood nick at craig-wood.com
Thu Feb 26 15:31:53 EST 2009


Ben <bnsilinux at gmail.com> wrote:
>  On Feb 24, 11:31?am, Nick Craig-Wood <n... at craig-wood.com> wrote:
> > So do you want to embed python into your code?
> >
> > I'm still not clear what you are trying to achieve with python, though
> > I have a better idea what SLAG is now!
> 
>  Actually no, I want to EXTEND python using the lower levels of S-lang
>  screen modules.

Ah!

>  My Modules are written in C and are a frame work for building pull-
>  down menus and data entry screens.
> 
>  Very nice for writing business applications. Think along the lines
>  of FoxPro and/or the "Screen" section in Cobol and you have a
>  pretty good idea of what i have done.

You've got several choices.

1) Write a python module in C which interfaces with your C code.

This is the traditional way, but it is quite time consuming and easy
to slip up with reference counting.  You need to learn the python API
to use this.

Will require compiling by your users (not a big hurdle if you make a
setup.py).

2) Write an interface with ctypes

If your C code presents itself as a shared object (dll/so) then this
is a really nice way of using it.  You don't have to write any C code
or worry about any reference counting - you do it all in python.

You'll need to learn how ctypes maps python onto C, but as you know C
well you won't find this difficult.

ctypes is included with python now.

3) Use Cython

Cython is a sort of hybrid C and python which can be used for
interfacing with C code.

This requires learning exactly what you can and can't do in Cython (it
is almost python but not quite).

This will require your users to install Cython and compile stuff.
Again easy if you make a setup.py but more stuff to install.

4) Use Swig

A solid approach, quicker than 1) but not as quick as 2) or 3)

This will require your users to install swig and a compiler.

...

My prefered approach is 2) ctypes at the moment.  I have used all 4 of
the above approaches in the past though!

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list