ctypes. ctypes run!
Starts at page 36.
There is a new module that is available in Python2.5, and does a simple think like allowing to add "the standard c types to python: signed and unsigned bytes, shorts, ints and longs; as well as structs, unions, pointers and functions. At run-time it can load a shared library (DLL) and import its symbols, allowing a Python application to make function calls into the library without any special preparation".
So if this doesn't impress you, maybe a simple example will:
This is really a great thing, you don't need to use middle man tools like Boost.Python, SWIG or go for other not so intuitive methods you just need to do a simple import and make available all those C libraries.
>>> from ctypes.util import find_library
>>> find_library('SDL')
SDL is an opensource crossplatform media library.
'libSDL-1.2.so.0' On Linux.
'/Library/Frameworks/SDL.Framework/SDL' On OS X.
'C:\\WINDOWS\\system32\\SDL.dll' On Windows.
Once you have obtained the name of the library, you can load it. Symbols will be imported on
demand. For example, on Unix systems we can load libc to get the standard C functions:
>>> libc = cdll.LoadLibrary(find_library('c')) find_library returns ‘libc.so.6’ on Linux.
>>> libc.strcmp('alice', 'bob')
-1
So if you what more just read the referred article, or go to ctypes package home page you also find more links in the end of the article.
Sem comentários:
Enviar um comentário