views:

23

answers:

2

I keep seeing people writing wrappers for, say a module written in X language to use it in Y language. I wanted to know the basics of writing such wrappers. Where does one start from? My question here is more specific for libgnokii, how do I begin to write python bindings for it.

+2  A: 

You can start with reading this: extending python with c or c++ And then when you decide that it's too much hassle, you can check out swig or possibly Boost.Python. ctypes may also be useful.

I've done manual wrapping of c++ classes and I've used swig. swig was much easier to use, but in the end I wanted to do stuff that wasn't easily done (or I was just too lazy to figure out how). So i ended up doing manual wrapping. It's a bit of work but if you know a bit of C, it's very doable.

Mattias Nilsson
+2  A: 

You can start by looking here for information on extending Python with C. You'll probably want to think about how to translate libgnokii's API into something Pythonic while you're at it. If you don't want to do a lot of work, you can just write a thin wrapper that translates all the gnokii API calls into Python functions.

Nathon
+1, I agree about making things more pythonic. Sometimes a thin wrapper is just awkward to use in python.
Mattias Nilsson
SO making things Pythonic would mean writing the whole library as a module from the scratch?
tsudot
No, not necessarily. One example from what I did: I had a c++ class that I needed to make an object of before I could use a function on it (in this case, the function should have been static). Instead of making the python module user go through the same steps, I just built in an object of that class in my python extension and exposed the function as a function in a python module, hiding the fact that it belonged to a class, that wasn't really necessary. So, sort of tidying up the interfaces a bit.
Mattias Nilsson