点击进入项目

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演示的更多相关文章

  1. 『Python CoolBook』C扩展库_其一_用法讲解

    不依靠其他工具,直接使用Python的扩展API来编写一些简单的C扩展模块. 本篇参考PythonCookbook第15节和Python核心编程完成,值得注意的是,Python2.X和Python3. ...

  2. 『Python CoolBook』C扩展库_其三_简单数组操作

    点击进入项目 这里的数组要点在于: 数组结构,array.array或者numpy.array 本篇的数组仅限一维,不过基础的C数组也是一维 一.分块讲解 源函数 /* Average values ...

  3. 『Python CoolBook』C扩展库_其五_C语言层面Python库之间调用API

    点击进入项目 一.C层面模块添加API 我们仍然操作如下结构体, #include <math.h> typedef struct Point { double x,y; } Point; ...

  4. 『Python CoolBook』C扩展库_其六_线程

    GIL操作 想让C扩展代码和Python解释器中的其他进程一起正确的执行, 那么你就需要去释放并重新获取全局解释器锁(GIL). 在Python接口封装中去释放并重新获取全局解释器锁(GIL),此时本 ...

  5. 『Python CoolBook』C扩展库_其四_结构体操作与Capsule

    点击进入项目 一.Python生成C语言结构体 C语言中的结构体传给Python时会被封装为胶囊(Capsule), 我们想要一个如下结构体进行运算,则需要Python传入x.y两个浮点数, type ...

  6. 『Python CoolBook』C扩展库_其六_从C语言中调用Python代码

    点击进入项目 一.C语言运行pyfun的PyObject对象 思路是在C语言中提供实参,传给python函数: 获取py函数对象(PyObject),函数参数(C类型) 获取GIL(PyGILStat ...

  7. 『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 ...

  8. 『Python CoolBook』数据结构和算法_字典比较&字典和集合

    一.字典元素排序 dict.keys(),dict.values(),dict.items() 结合max.min.sorted.zip进行排序是个很好的办法,另外注意不使用zip时,字典的lambd ...

  9. 『Python CoolBook』Cython

    github地址 使用Cython导入库的话,需要一下几个文件: .c:C函数源码 .h:C函数头 .pxd:Cython函数头 .pyx:包装函数 setup.py:python 本节示例.c和.h ...

随机推荐

  1. kafka8 编写简单消费者

    1.eclipse运行消费者代码.代码如下 package cn.test.mykafka; import java.util.Arrays; import java.util.Properties; ...

  2. java框架之SpringBoot(10)-启动流程及自定义starter

    启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...

  3. centos7.5图形界面与命令行界面转换

    查看当前状态下的显示模式: # systemctl get-default 转换为图形界面: # systemctl set-default graphical.target 转换为命令行界面: # ...

  4. git----------git:如何让git识别我修改了文件夹名字和文件名字的大小写问题。

    修改每个项目里面的隐藏的.git文件里面的config文件.将箭头指的原本是true改成false.

  5. IE 浏览器background image 属性问题

    background-size 如果以百分比的形式设置参数,第一个参数是宽度,第二页参数是高度.“如果只设置第一个参数,则第二个参数为auto”.这样设置,在Firefox chrome 浏览器中,图 ...

  6. ASP.NET页面之间传值的方式之Application(个人整理)

    Application  Application变量在整个应用程序生命周期中都是有效的,类似于使用全局变量一样,所以可以在不同页面中对它进行存取.它和Session变量的区别在于,前者是所有的用户共用 ...

  7. 为什么一个java源文件中只能有一个public类

    问题:一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 答案:可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致.一个文件 ...

  8. Java正则表达式草稿程序*2

    1.成绩统计. 输入文件input.txt: 张三 语文12 数学31 英语11 李四 语文22 数学22 英语22 王五 语文33 数学33 英语33 期待输出output.txt: 张三 语文12 ...

  9. K8S学习笔记之k8s日志收集实战

    0x00 简介 本文主要介绍在k8s中收集应用的日志方案,应用运行中日志,一般情况下都需要收集存储到一个集中的日志管理系统中,可以方便对日志进行分析统计,监控,甚至用于机器学习,智能分析应用系统问题, ...

  10. 安装Joomla!3

    在日常测试中搭建一个web 站点进行进行linux 相关功能测试,只是不喜欢httpd 或者nginx 的默认界面: 安装Joomla!3: 系统:centos 7 软件:  连接: https:// ...