我是
靠谱客的博主
机智毛巾,最近开发中收集的这篇文章主要介绍
C语言扩展Python,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
Python具有很好的开发灵活性,最大的特点是C语言可以对Python进行扩展,目前工作中正在进行相关的开发,第一篇文章作为基础.
实现C函数,用Python API封装,实现俩个功能,1.say_hello,打印hello world! 2.calc_pv,做加法用算.
以下为使用方法:
01 | Python 2.7 . 3 (default, Nov 13 2012 , 11 : 17 : 50 ) |
02 | [GCC 4.1 . 2 20080704 (Red Hat 4.1 . 2 - 48 )] on linux2 |
03 | Type "help" , "copyright" , "credits" or "license" for more information. |
07 | >>> session.sayhello() |
以下为实现的步骤,核心的地方在代码里加了注释.
10 | static PyObject *say_hello(PyObject *self) |
12 | printf ( "%sn" , "hello world!" ); |
17 | static PyObject *calc_pv(PyObject *self,PyObject *args) |
26 | if (! PyArg_ParseTuple(args, "i|i" , &i_ct,&j_ct)) |
28 | return Py_BuildValue( "i" , (i_ct+j_ct)); |
44 | static PyMethodDef addMethods[] = |
47 | { "sayhello" ,(PyCFunction)say_hello,METH_NOARGS, "say hello!" }, |
49 | { "sum" ,(PyCFunction)calc_pv,METH_VARARGS, "calc pv" }, |
61 | PyMODINIT_FUNC initsession( void ) |
65 | module = Py_InitModule( "session" , addMethods); |
最后是编译的过程,首先创建setup.py,内容如下
1 | from distutils.core import setup, Extension |
3 | module1 = Extension( 'session' ,sources = [ 'cpython_eg1.c' ],language = 'c' ) |
5 | setup (name = 'emar_py_lib' , version = '1.0' , |
6 | description = 'This is a demo package' , |
7 | ext_modules = [module1]) |
编译过程为:
1.python setup.py build
2.sudo python setup.py install
转自:http://my.oschina.net/max1984/blog/89122?from=20121118
上次实践的是在工作中写的小程序,也是Python扩展的基础,在上一个例子C语言扩展Python(一)中其实核心在于对Python参数的解析,Python.org提供了很丰富的解析规则,几乎任何类型(基础类型、对象类型等).推荐官网链接说明:
http://docs.python.org/2/c-api/arg.html.
Python编写中,经常用到关键词参数传递,例如:
1 | def fun(arg0,arg1 = None ,arg2 = None ): |
2 | print "%s %s %s" % (arg0,arg1,arg2) |
5 | fun( 'hello' ,arg2 = '!' ,arg1 = 'world' ) |
7 | if __name__ = = '__main__' : |
输出:
C语言扩展也可以实现,参数解析我们使用PyArg_ParseTupleAndKeywords方法做解析.该方法定义的原型为:
6 | PyArg_ParseTupleAndKeywords(PyObject *args, |
下面是在例子
C语言扩展Python(一)
基础上增加,实现一个关键词参数传递的方法.
07 | static PyObject *print_kw(PyObject *self,PyObject *args,PyObject *kw) |
09 | static char *kwlist[] = { "arg0" , "arg1" ,NULL}; |
10 | char * hello= "default" ; |
11 | char * world= "default" ; |
12 | if (!PyArg_ParseTupleAndKeywords(args,kw, "s|s" ,kwlist,&hello,&world)) { |
16 | printf ( "%s %sn" ,hello,world); |
kwlist[] 定义出我们关键词参数key的名字,不难看出我们定义俩个关键词参数arg0与arg1,接下来就是向Python暴露该方法.
详细参考下方代码段.
01 | static PyMethodDef addMethods[] = |
04 | { "sayhello" ,(PyCFunction)say_hello,METH_NOARGS, "say hello!" }, |
06 | { "sum" ,(PyCFunction)calc_pv,METH_VARARGS, "calc pv" }, |
08 | { "print_args" ,(PyCFunction)print_kw,METH_KEYWORDS, "print argument keyword" }, |
METH_KEYWORDS代表该方法是关键词参数类型.接着我们按照C语言扩展Python(一)方法重新编译setup.py.并进行测试
01 | Python 2.7 . 3 (default, Nov 13 2012 , 11 : 17 : 50 ) |
02 | [GCC 4.1 . 2 20080704 (Red Hat 4.1 . 2 - 48 )] on linux2 |
03 | Type "help" , "copyright" , "credits" or "license" for more information. |
06 | Help on module session: |
12 | / usr / local / lib / python2. 7 / site - packages / session.so |
16 | print argument keyword |
25 | >>> session.print_args( 'hello' ) |
27 | >>> session.print_args(arg0 = 'hello' ) |
29 | >>> session.print_args(arg0 = 'hello' ,arg1 = 'world!' ) |
转自:http://my.oschina.net/max1984/blog/89435
最后
以上就是机智毛巾为你收集整理的C语言扩展Python的全部内容,希望文章能够帮你解决C语言扩展Python所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复