V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  herozem  ›  全部回复第 7 页 / 共 17 页
回复总数  329
1 ... 3  4  5  6  7  8  9  10  11  12 ... 17  
:joy: 再等我两年
@lyricorpse 试了一下直接声明全局变量没成功,编译不过。但是我想可以用曲折的办法就是给全局变量写上 getter/setter 然后把这几个函数包装一下,在 python 层面再做成 property 。
py_area.pxd 不是必须的,可以把声明_square 类型那一行移到 py_area.pyx 里。我是看了一遍官方文档,然后看了一下 cython : a guide to python programmers 前几章,又跳回去把官方文档看了一遍。我也是这几天才开始看 cython 的😅如果有 python 和 c 基础的话,仔细读读这两个文档,然后一边看一边自己验证应该不用太久的
我研究了一下,贴下代码吧。
```
root@arch area: # 首先是 area.h 和 area.c
root@arch area: cat area.h
#ifndef _AREA_H
#define _AREA_H

struct Square {
float length;
float width;
};

typedef struct Square *pSquare;

float calc_area(pSquare);

#endif /* area.h */
root@arch area: cat area.c
#include <stdio.h>
#include <stdlib.h>

#include "area.h"


float calc_area(pSquare s) {
return s->length * s->width;
}

int main(void) {
pSquare s = (pSquare)malloc(sizeof(struct Square));
if (s == NULL) {
printf("memory not enough");
exit(1);
}
s->length = 10.0;
s->width = 3.0;
printf("area of the square: %f\n", calc_area(s));
free(s);
}
root@arch area: # 运行一下
root@arch area: cc area.c && ./a.out && rm a.out
area of the square: 30.000000
root@arch area: # 为 area.c 定义包裹的 Cython 头文件
root@arch area: cat carea.pxd
cdef extern from "area.h":
cdef struct Square:
float length
float width

ctypedef Square *pSquare;

cdef float calc_area(pSquare);
root@arch area: # 为 python 版本 Square 定义 Cython 头文件
root@arch area: cat py_area.pxd
cimport carea

cdef class Square:
cdef carea.pSquare _square

cpdef float calc_area(Square)
root@arch area: # 为 python 版本 Square 包裹一下
root@arch area: cat py_area.pyx
from cpython.mem cimport PyMem_Malloc, PyMem_Free

cimport carea

cdef class Square:
def __cinit__(self, length, width):
self._square = <carea.pSquare>PyMem_Malloc(sizeof(carea.Square))
if not self._square:
raise MemoryError("Memory not enough")

self._square.length = length
self._square.width = width

def __dealloc__(self):
PyMem_Free(self._square)

cpdef float calc_area(self):
return carea.calc_area(self._square)
root@arch area: # 调用一下
root@arch area: # 哦不,先写好 setup.py ,然后编译
root@arch area: cat setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

ext_modules = cythonize([
Extension("py_area", ["py_area.pyx", "area.c"])
])

setup(ext_modules=ext_modules)
root@arch area: python3 setup.py build_ext --inplace
running build_ext
building 'py_area' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fPIC -I/usr/include/python3.6m -c py_area.c -o build/temp.linux-x86_64-3.6/py_area.o
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fPIC -I/usr/include/python3.6m -c area.c -o build/temp.linux-x86_64-3.6/area.o
gcc -pthread -shared -Wl,-O1,--sort-common,--as-needed,-z,relro -Wl,-O1,--sort-common,--as-needed,-z,relro build/temp.linux-x86_64-3.6/py_area.o build/temp.linux-x86_64-3.6/area.o -L/usr/lib -lpython3.6m -o /root/tests/area/py_area.cpython-36m-x86_64-linux-gnu.so
root@arch area: # 调用
root@arch area: ipython
Python 3.6.0 (default, Dec 24 2016, 08:03:08)
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: import py_area

In [2]: a = py_area.Square(10, 20)

In [3]: a.calc_area()
Out[3]: 200.0

In [4]:
Do you really want to exit ([y]/n)?
root@arch area:
```
2017-01-12 13:09:45 +08:00
回复了 lyricorpse 创建的主题 问与答 一个简单的 C 程序 bus error,以及如何用 Cython 进行封装
因为,你的 c ,写错了。

#include <stdio.h>
#include <string.h>

struct Square {
float length;
float width;
};

typedef struct Square *sq;

float calc_area(const sq a) {
float s;
s = a->length * a->width;
return s;
}

int main() {
struct Square a;
a.length = 10.0;
a.width = 3.0;

printf("%f\n", calc_area(&a));
}
2016-12-10 17:01:03 +08:00
回复了 aiqier 创建的主题 Python 关于 python 大整数对象池
https://github.com/jiajunhuang/cpython/blob/python_source_analysis/Objects/intobject.c#L83-L117

我也在看。不过很奇怪的是,这段代码里并没有根据值去寻找已存在的大整数。所以按照代码来看应该是就算是数值
相同的两个大整数,应该是每次都返回不同的地址才对。
2016-12-08 09:44:39 +08:00
回复了 xlvecle 创建的主题 问与答 有一个会写代码的女朋友是一种什么样的体验?
@Lucups 听歌识曲
2016-12-06 13:00:59 +08:00
回复了 ansheng 创建的主题 程序员 如何学习 Tornado 异步非阻塞?
2016-12-02 10:02:20 +08:00
回复了 herozem 创建的主题 问与答 更新了一下 tornado 写的小博客
@Kilerd 我把博客撤掉了[捂脸]
2016-11-09 20:00:09 +08:00
回复了 herozem 创建的主题 问与答 更新了一下 tornado 写的小博客
@roychan 👏
2016-10-28 12:54:54 +08:00
回复了 herozem 创建的主题 Raspberry Pi 送一个树莓派 2B,有人要么
送给一楼了~~你们速度真快
2016-09-12 19:13:48 +08:00
回复了 herozem 创建的主题 Flask flask 中的 endpoint 的存在的意义是什么?
@zhangmiaoCHN 嗯,看过了 stackoverflow 上的这篇回答。但是我仍然觉得有疑问就是,直接写 `/admin/greeting` 和 `/user/greeting` 有什么区别吗?另外函数重名其实也是可以避免的,例如不要 `from admin import greeting` 而用 `admin.greeting` 代替就可以了。 说到底,我就是在寻找 endpoint 的存在必要性。不过目前看来,也许只是为了写大项目的时候方便点,并没有绝对的存在必要性。
2016-09-09 16:15:23 +08:00
回复了 herozem 创建的主题 Flask flask 中的 endpoint 的存在的意义是什么?
@xcv58 尴尬

@zeayes
@zhangmiaoCHN 所以为什么不直接用 url -> func 的形式?


@guyskk 能避免匿名函数的问题?能举个例子吗?


@WordCount stackoverflow 上的我看到过了,但是还是没有充分的理由啊,就是为了 url_for 一个函数就加一层么。。


@aec4d 所以说到底,就是为了 url_for 这个函数,所以要在 url 和函数之间加一层么
@im67 准迁证不是只有落户地公安分局才能办理的吗
@Felldeadbird 谢谢
@Tink 单位不解决。我准备挂靠亲戚家。公安分局电话打了,但是没人接。所以问问看有没有过来人:)
2016-06-18 21:54:30 +08:00
回复了 herozem 创建的主题 Python 用 Tornado 写了一个用 rst 纯文本写作的博客
@aristotll 历史问题我也不太清楚,好像在 md 流行之前 python 社区就在用 rst 了
2016-06-18 17:22:32 +08:00
回复了 herozem 创建的主题 Python 用 Tornado 写了一个用 rst 纯文本写作的博客
@TrustyWolf DO 现在国内巨慢。以前用锐速还好点。现在完全不行了。以后 vps 只能买日本的了
2016-06-01 17:52:55 +08:00
回复了 herozem 创建的主题 DNS 学校网络访问不了 alicdn,求解决
@adaofu123 昨天就设置了 223.5.5.5 和 223.6.6.6 ,但是还是没有用
1 ... 3  4  5  6  7  8  9  10  11  12 ... 17  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2730 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 28ms · UTC 14:35 · PVG 22:35 · LAX 07:35 · JFK 10:35
Developed with CodeLauncher
♥ Do have faith in what you're doing.