『Python CoolBook』C扩展库_其二_demo演示
C函数源文件
/* sample.c */
#include "sample.h" /* Compute the greatest common divisor */
int gcd(int x, int y) {
int g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
} /* Test if (x0,y0) is in the Mandelbrot set or not */
int in_mandel(double x0, double y0, int n) {
double x=0,y=0,xtemp;
while (n > 0) {
xtemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xtemp;
n -= 1;
if (x*x + y*y > 4) return 0;
}
return 1;
} /* Divide two numbers */
int divide(int a, int b, int *remainder) {
int quot = a / b;
*remainder = a % b;
return quot;
} /* Average values in an array */
double avg(double *a, int n) {
int i;
double total = 0.0;
for (i = 0; i < n; i++) {
total += a[i];
}
return total / n;
} /* Function involving a C data structure */
double distance(Point *p1, Point *p2) {
return hypot(p1->x - p2->x, p1->y - p2->y);
}
C函数头文件
/* sample.h */ #include <math.h> extern int gcd(int, int);
extern int in_mandel(double x0, double y0, int n);
extern int divide(int a, int b, int *remainder);
extern double avg(double *a, int n); typedef struct Point {
double x,y;
} Point; extern double distance(Point *p1, Point *p2);
Python API封装文件
pysample.c,本文件仅仅封装了前三个标量运算函数,复杂函数交由后续章节介绍
#include "Python.h"
#include "sample.h" /* int gcd(int, int) */
static PyObject *sample_gcd(PyObject *self, PyObject *args) {
int x, y, result; if (!PyArg_ParseTuple(args,"ii", &x, &y)) {
return NULL;
}
result = gcd(x,y);
return Py_BuildValue("i", result);
} /* int in_mandel(double, double, int) */
static PyObject *sample_in_mandel(PyObject *self, PyObject *args) {
double x0, y0;
int n;
int result; if (!PyArg_ParseTuple(args, "ddi", &x0, &y0, &n)) {
return NULL;
}
result = in_mandel(x0,y0,n);
return Py_BuildValue("i", result);
} /* int divide(int, int, int *) */
static PyObject *sample_divide(PyObject *self, PyObject *args) {
int a, b, quotient, remainder;
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
return NULL;
}
quotient = divide(a,b, &remainder);
return Py_BuildValue("(ii)", quotient, remainder);
} /* Module method table */
static PyMethodDef SampleMethods[] = {
{"gcd", sample_gcd, METH_VARARGS, "Greatest common divisor"},
{"in_mandel", sample_in_mandel, METH_VARARGS, "Mandelbrot test"},
{"divide", py_divide, METH_VARARGS, "Integer division"},
{ NULL, NULL, 0, NULL}
}; /* Module structure */
static struct PyModuleDef samplemodule = {
PyModuleDef_HEAD_INIT, "sample", /* name of module */
"A sample module", /* Doc string (may be NULL) */
-1, /* Size of per-interpreter state or -1 */
SampleMethods /* Method table */
}; /* Module initialization function */
PyMODINIT_FUNC
PyInit_sample(void) {
return PyModule_Create(&samplemodule);
}
启动文件setup.py
# setup.py
from distutils.core import setup, Extension setup(name='sample',
ext_modules=[
Extension('sample',
['pysample.c','sample.c'])]
)
安装命令
python setup.py install
『Python CoolBook』C扩展库_其二_demo演示的更多相关文章
- 『Python CoolBook』C扩展库_其一_用法讲解
不依靠其他工具,直接使用Python的扩展API来编写一些简单的C扩展模块. 本篇参考PythonCookbook第15节和Python核心编程完成,值得注意的是,Python2.X和Python3. ...
- 『Python CoolBook』C扩展库_其三_简单数组操作
点击进入项目 这里的数组要点在于: 数组结构,array.array或者numpy.array 本篇的数组仅限一维,不过基础的C数组也是一维 一.分块讲解 源函数 /* Average values ...
- 『Python CoolBook』C扩展库_其五_C语言层面Python库之间调用API
点击进入项目 一.C层面模块添加API 我们仍然操作如下结构体, #include <math.h> typedef struct Point { double x,y; } Point; ...
- 『Python CoolBook』C扩展库_其六_线程
GIL操作 想让C扩展代码和Python解释器中的其他进程一起正确的执行, 那么你就需要去释放并重新获取全局解释器锁(GIL). 在Python接口封装中去释放并重新获取全局解释器锁(GIL),此时本 ...
- 『Python CoolBook』C扩展库_其四_结构体操作与Capsule
点击进入项目 一.Python生成C语言结构体 C语言中的结构体传给Python时会被封装为胶囊(Capsule), 我们想要一个如下结构体进行运算,则需要Python传入x.y两个浮点数, type ...
- 『Python CoolBook』C扩展库_其六_从C语言中调用Python代码
点击进入项目 一.C语言运行pyfun的PyObject对象 思路是在C语言中提供实参,传给python函数: 获取py函数对象(PyObject),函数参数(C类型) 获取GIL(PyGILStat ...
- 『Python CoolBook』数据结构和算法_多变量赋值&“*”的两种用法
多变量赋值 a = [1,2,(3,4)] b,c,d = a print(b,c,d) b,c,(d,e) = a print(b,c,d,e) 1 2 (3, 4) 1 2 3 4 a = &qu ...
- 『Python CoolBook』数据结构和算法_字典比较&字典和集合
一.字典元素排序 dict.keys(),dict.values(),dict.items() 结合max.min.sorted.zip进行排序是个很好的办法,另外注意不使用zip时,字典的lambd ...
- 『Python CoolBook』Cython
github地址 使用Cython导入库的话,需要一下几个文件: .c:C函数源码 .h:C函数头 .pxd:Cython函数头 .pyx:包装函数 setup.py:python 本节示例.c和.h ...
随机推荐
- Ubuntu系统实现将Jupyter notebook项目发布到GitHub
一.准备 Ubuntu16.04.GitHub账户.Git.Jupyter Notebook项目 二.打开Terminal(用户和邮箱即为你的GitHub注册的账号和邮箱) git config -- ...
- Python3学习之路~5.10 PyYAML模块
Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation
- Fiddler修改图片显示
培训课讲修改请求值,记录一下操作步骤: 步骤如下: 1. 点击人人网图片另存为到桌面 2. 打开fiddler,找到图片发送的请求(单击图片的链接,点击右边面板的Inspectors.查看ImageV ...
- 【JVM】-NO.116.JVM.1 -【JDK11 HashMap详解-5-红黑树】
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...
- Python学习之高级数组(一)
1.Python基础学习之高级数组(一) 1.1视图:就是与较大数组共享相同数据的较小数组.Numpy包提供数据视图的概念是为了精确地控制内存的使用方式. 数组视图.切片视图.转置和重塑视图等 数组 ...
- 解决bootstrap 模态框 数据清除 验证清空
$("#switchModel").on("hidden.bs.modal", function () { $('#ware-form')[0].reset() ...
- linq 表分组后关联查询
测试linq,获取有教师名额的学校.比如学校有5个教师名额,teacher数量没超过5个,发现有空额 var query = (from teacher in _repositoryTeacher.T ...
- easy ui datatimebox databox 当前时间
databox 当前日期: class="easyui-datebox" var curr_time = new Date(); var strDate = curr_time. ...
- https://scrapingclub.com/exercise/basic_login/
遇到的问题:csrftoken cfduid 是在request.headers里面的,一直在找怎么在scrapy里get request.header,从scrapy shell ,then fet ...
- MongoDB运维心得(一)
问题:集群内部通信压力较大.出现在某一个节点创建普通表并插入数据,在其他点读的问题.会造成每次读表都要进行一次完整的数据传输. 前提: Mongodb处于Sharding Cluster状态. 造成原 ...