One of the most useful things that i've come acrossed in Python is
Cython a C extension for python, this tool was derived from
Pyrex which is developed by Greg Ewing.
- cdef extern from "xine_internal.h"
- ctypedef extern struct xine_t
cdef extern from "xine_internal.h"
ctypedef extern struct xine_t
The above is an example from cython, it shares the same general syntax as python but has added many keywords that are specific to it.
cdef is the keyword that is pretty much the same as
def but it is declaring that the function is a C function.
In the example above
cdef extern from "...", it is telling the interpreter to include the specified file as an
extern.The next thing that is going on is
ctypedef extern struct xine_t, this is declaring the C struct xine_t as an extern.
As soon as you have declared all the necessary datatypes in Cython, you can create a class and call those external functions from that class.
-
- cdef extern const_char_ptr xine_get_version_string()
-
-
- cdef class libxine:
- .....
- def xineGetVersionString(self):
- return xine_get_version_string()
# forward declaration of the external function
cdef extern const_char_ptr xine_get_version_string()
cdef class libxine:
.....
def xineGetVersionString(self):
return xine_get_version_string()
You can combine datatypes into the Cython code and use it like the code below. The code
is a cast which is the same a C cast. You can also set class objects that are structs or external objects or types and them pass those through functions.
- def xineGetMetaInfo(self,int Info):
- MetaTable = {
- 0: "Title",
- 1: "Comment",
- 2: "Artist",
- 3: "Genre",
- 4: "Album",
- 5: "Year"
- }
-
- cdef char *GetMeta
- GetMeta = <char *>xine_get_meta_info(self.xineStream, Info)
- if GetMeta == NULL:
- raise Exception('MetaFail')
- return MetaTable[Info], GetMeta
def xineGetMetaInfo(self,int Info):
MetaTable = {
0: "Title",
1: "Comment",
2: "Artist",
3: "Genre",
4: "Album",
5: "Year"
}
cdef char *GetMeta
GetMeta = <char *>xine_get_meta_info(self.xineStream, Info)
if GetMeta == NULL:
raise Exception('MetaFail')
return MetaTable[Info], GetMeta
Original source (pyxine-ng)