用Python包装C库 [英] Wrapping a C library in Python

查看:57
本文介绍了用Python包装C库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C库,里面有大约50个调用,我想要包含在
Python中。我知道我可以使用像SWIG这样的工具,但这会给我一个

太字面的翻译;我想在整个过程中做一些修改

使界面更加Pythonic。


例如,所有这些函数都返回错误代码(通常只是

errno传递,但并非总是如此)。他们都接受一个指向某个地方来存储结果的

参数之一。我想

改变所有这些直接返回结果并抛出异常。


我也想改变一些返回类型。这些

函数返回一组值的常用方法是一对字符串数组,形成

键值对。在Python中,将它作为一个

字典返回是有意义的。


我知道我所描述的有点模糊,但是在那里

的工具可能有助于自动化(至少部分)这个翻译过程?

I''ve got a C library with about 50 calls in it that I want to wrap in
Python. I know I could use some tool like SWIG, but that will give me a
too-literal translation; I want to make some modifications along the way
to make the interface more Pythonic.

For example, all of these functions return an error code (typically just
errno passed along, but not always). They all accept as one of their
arguments a pointer to someplace to store their result. I want to
change all that to returning the result directly and throwing exceptions.

I also want to mutate some of the return types. A common way these
functions return a set of values is a pair of arrays of strings, forming
key-value pairs. In Python, it would make sense to return this as a
dictionary.

I know what I''m describing is kind of vague, but are there tools around
which might help automate (at least in part) this translation process?

推荐答案



仔细看看Pyrex


< http://nz.cosc.canterbury.ac.nz/~greg/python/Pyrex/>

/ Jean Brouwers

ProphICy Semiconductor,Inc。

文章< ro **************** *******@reader1.panix.com> ;, Roy Smith

< ro*@panix.com>写道:

Take a close look at Pyrex at

<http://nz.cosc.canterbury.ac.nz/~greg/python/Pyrex/>
/Jean Brouwers
ProphICy Semiconductor, Inc.
In article <ro***********************@reader1.panix.com>, Roy Smith
<ro*@panix.com> wrote:
我有一个C库,里面有大约50个调用,我想用Python包装。我知道我可以使用像SWIG这样的工具,但这会给我一个太直接的字面翻译;我想在此过程中做一些修改
使界面更加Pythonic。

例如,所有这些函数都返回错误代码(通常只是传递错误代码,但不总是)。他们都接受一个指向某个地方存储结果的指针作为他们的
参数之一。我想改变所有这些直接返回结果并抛出异常。

我也想改变一些返回类型。这些
函数返回一组值的常用方法是一对字符串数组,形成键值对。在Python中,将它作为一个字典返回是有意义的。

我知道我所描述的内容有点模糊,但是有没有可用的工具
帮助自动化(至少部分)这个翻译过程?
I''ve got a C library with about 50 calls in it that I want to wrap in
Python. I know I could use some tool like SWIG, but that will give me a
too-literal translation; I want to make some modifications along the way
to make the interface more Pythonic.

For example, all of these functions return an error code (typically just
errno passed along, but not always). They all accept as one of their
arguments a pointer to someplace to store their result. I want to
change all that to returning the result directly and throwing exceptions.

I also want to mutate some of the return types. A common way these
functions return a set of values is a pair of arrays of strings, forming
key-value pairs. In Python, it would make sense to return this as a
dictionary.

I know what I''m describing is kind of vague, but are there tools around
which might help automate (at least in part) this translation process?



[Roy Smith]
[Roy Smith]
我有一个C我想要在其中包含大约50个调用的库。
Python。我知道我可以使用像SWIG这样的工具,但这会给我一个太直接的字面翻译;我想在此过程中做一些修改
使接口更加Pythonic。
I''ve got a C library with about 50 calls in it that I want to wrap in
Python. I know I could use some tool like SWIG, but that will give me a
too-literal translation; I want to make some modifications along the way
to make the interface more Pythonic.




我使用Pyrex既快乐又成功包装C库

,同时为API提供更多Pythonic风格。我发现Pyrex是一个很容易做这些事情的好工具。


-

Fran?ois Pinard http://pinard.progiciels-bpi.ca

< br>

Roy Smith写道:
Roy Smith wrote:
我有一个C库,里面有大约50个调用,我想用Python包装。
I''ve got a C library with about 50 calls in it that I want to wrap in
Python.




我会推荐ctypes( http://starship.python.net/crew/theller/ctypes/)

它非常易于使用和多平台。


我有一个小项目,为ODBTP提供DB API 2.0接口

http://odbtp.sf.net )图书馆。请参阅 http://benjiyork.com/odbtp.html

(提防程序员 - 网络设计)代码(LGPL)。它可能会以
作为一个例子让你上路。


BTW,如果有人有兴趣,我希望有一个很好的版本

几周后准备好的包装器将被包含在官方ODBTP

发行中。

-

Benji约克
be***@benjiyork.com



I''d recommend ctypes (http://starship.python.net/crew/theller/ctypes/).
It is very easy to use and multi-platform.

I have a small project that provides a DB API 2.0 interface to the ODBTP
(http://odbtp.sf.net) library. See http://benjiyork.com/odbtp.html
(beware programmer-web-design ahead) for the code (LGPL). It might
serve as an example to get you on your way.

BTW, if anyone is interested, I hope to have a much improved version of
the wrapper ready in a few weeks to be included in the official ODBTP
distribution.
--
Benji York
be***@benjiyork.com


这篇关于用Python包装C库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆