PyMaemo/Accessing APIs without Python bindings

Line 1: Line 1:
 +
<b style="color:red">This is an on-going document, and will be linked on the PyMaemo main page once finished</b>
 +
== Introduction ==
== Introduction ==
-
Sometimes you don't need a complete binding for some library, just a couple functions that will solve your problem. For these cases, you can use the <code>ctypes</code> module to cherry-pick the needed functions and use them inside Python, no further steps needed.
+
There are many libraries written in C that do not have native Python bindings yet. In Maemo, one of such libraries is libabook, used to manipulate the address book on Maemo devices.
-
This page is not meant to be a complete <code>ctypes</code> guide; there is some great documentation at http://docs.python.org/library/ctypes.html.  
+
While a full binding is still very useful, in most cases you need to use just a couple of functions and data structures to get your work done. Instead of waiting for a binding to be implemented, you can use Python's "ctypes" module, which allows to directly call functions and access data structures from C libraries.
-
== Usage ==
+
This article will explain how to do call C library functions using ctypes, using libabook as an example. Most of the code snippets were based on code found on the "Hermes"<ref> application
 +
 
 +
This page is not meant to be a complete <code>ctypes</code> guide; for that, be sure to read the documentation at http://docs.python.org/library/ctypes.html.
 +
 
 +
== Basic usage ==
Let's say you want to use libc's printf, for some reason. All you need in python is:
Let's say you want to use libc's printf, for some reason. All you need in python is:
Line 42: Line 48:
  >>> libc.printf("a integer: %d, a double: %f\n", 42, ctypes.c_double(3.14))
  >>> libc.printf("a integer: %d, a double: %f\n", 42, ctypes.c_double(3.14))
  a integer: 42, a double: 3.140000
  a integer: 42, a double: 3.140000
 +
 +
== References ==
 +
 +
<references/>

Revision as of 15:45, 29 January 2010

This is an on-going document, and will be linked on the PyMaemo main page once finished

Introduction

There are many libraries written in C that do not have native Python bindings yet. In Maemo, one of such libraries is libabook, used to manipulate the address book on Maemo devices.

While a full binding is still very useful, in most cases you need to use just a couple of functions and data structures to get your work done. Instead of waiting for a binding to be implemented, you can use Python's "ctypes" module, which allows to directly call functions and access data structures from C libraries.

This article will explain how to do call C library functions using ctypes, using libabook as an example. Most of the code snippets were based on code found on the "Hermes"[1]


Cite error: <ref> tags exist, but no <references/> tag was found