`
zuroc
  • 浏览: 1290112 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
社区版块
存档分类
最新评论

pyrex学习笔记

阅读更多
0.
easy_install pyrex

1.写pyrex文件
~/pyrex $ ls
__init__.py  primes.pyx

~/pyrex $ cat primes.pyx
#coding:utf-8

#一个函数
def primes(int kmax):
    cdef int n, k, i
    cdef int p[1000]
    result = []
    if kmax > 1000:
        kmax = 1000
    k = 0
    n = 2
    while k < kmax:
        i = 0
        while i < k and n % p[i] <> 0:
            i = i + 1
        if i == k:
           p[k] = n
           k = k + 1
           result.append(n)
        n = n + 1
    return result


#一种类型
"""
1.
Finalization method: __dealloc__
The counterpart to the __cinit__ method is the __dealloc__ method, which should perform the inverse of the __cinit__ method. Any C data structures that you allocated in your __cinit__ method should be freed in your __dealloc__ method.

2.
http://blog.donews.com/limodou/archive/2004/08.aspx?PageNumber=2
在 Python 中,在一个类定义中引用一个类方法,得到的是一个一般的函数对象。但在 pyrex 中得到的是一个未与类绑定的对象。这样导致的结果是通常使用的classmethod和staticmethod这样的用法在pyrex中不可用。例如,在Python中可以:

    class spam:
        def method(self):
            ...
        method = classmethod(method)

但在 pyrex 中不能使用。你可以在类的外面定义这个方法,然后在类的定义中,将classmethod和staticmethod的结果赋给类的变量。如:

    def Spam_method(cls):
        ...
    class spam:
        method = classmethod(Spam_method)
"""
cdef class Spam:

  cdef int amount

  def __cinit__(self):
    self.amount = 0

  def get_amount(self):
    return self.amount

  def set_amount(self, new_amount):
    self.amount = new_amount

  def describe(self):
    print self.amount, "tons of spam!"

2.编译
pyrexc -r primes.pyx
生成了 primes.c
gcc -O2 -fPIC -shared -o primes.so -I/home/zuroc/include/python2.6  -L/home/zuroc/lib primes.c

3.运行
>>> import primes
>>> primes.primes(10)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
>>> spam=primes.Spam()
>>> spam.get_amount()
0

4.封装现有c函数

"""
http://blog.donews.com/limodou/archive/2004/08.aspx?PageNumber=2

cdef extern from "spam.h":
    int spam_counter
    void order_spam(int tons)

这个cdef extern from子句做了三件事:

   1. 它告诉pyrex设置一个#include语句在生成的C代码中,使用指定的头文件
   2. 它阻止pyrex对下面的代码缩近块中存在的声明生成C的声明代码
   3. 它会认为代码块中所有的声明都是以cdef extern开始的

也就是说,pyrex本身不会去读取指定的头文件,自动分析哪些在头文件中有声明,这个工作需要你去做,你要指定哪些变量或函数已经在头文件中声明了(就是在代码块中进行原型的声明),告诉pyrex不要再为这些变量或函数产生声明C代码了。

每一个pyrex模块都为Python和C名字提供一个模块级的名字空间。不过当你想要封装一些外部的C函数,并以相同的名字提供给用户Python函数时,这样就不方便了。

一种方法是使用C名字规范(c name specification, 就是在一个名字后面跟着一个字符串)来对一个C函数给出不同的python和C名字。例如:

    cdef extern void c_eject_tomto "eject_tomato" (float speed)

...........



通过使用public关键字,可以使得在pyrex内部声明的C变量和函数被外部的C代码(或pyrex模块)使用,例如:
cdef public int spam #公共变量声明


"""
另外一种是

Using cimport to resolve naming conflicts

The cimport mechanism provides a clean and simple way to solve the problem of wrapping external C functions with Python functions of the same name. All you need to do is put the extern C declarations into a .pxd file for an imaginary module, and cimport that module. You can then refer to the C functions by qualifying them with the name of the module. Here's an example:

c_lunch.pxd
cdef extern from "lunch.h":
    void eject_tomato(float) cimport c_lunch

lunch.pyx
def eject_tomato(float speed):
    c_lunch.eject_tomato(speed)

You don't need any c_lunch.pyx file, because the only things defined in c_lunch.pxd are extern C entities. There won't be any actual c_lunch module at run time, but that doesn't matter; the c_lunch.pxd file has done its job of providing an additional namespace at compile time.
分享到:
评论

相关推荐

    Pyrex-0.9.9.tar.gz

    pyrex安装包,linux系统亲测可用,windows和mac没有试,下载之后直接安装就可以

    pyrex:无缝容器设置,用于使用OpenEmbeddedYocto项目进行开发

    Pyrex具有以下系统要求: docker或podman python 3.6或更高版本 注意:如果将docker作为snap安装,Pyrex将无法正常运行,因为它将绑定不允许快照访问的安装文件。 请安装非快照版本的apt install docker.io ,例如...

    pyrex-tools-开源

    一组用于Pyrex的工具,包括以下内容:pyximport-允许实时编译Python代码中包含的Pyrex模块。 pyxtest-独立于平台的Pyrex分发测试工具。

    使用Pyrex来扩展和加速Python程序的教程

    主要介绍了使用Pyrex来扩展和加速Python程序的教程,来自IBM官方技术文档,需要的朋友可以参考下

    水辅助激光诱导等离子体背部刻蚀Pyrex7740玻璃

    提出了水辅助激光诱导等离子体背部刻蚀Pyrex7740玻璃的方法, 制备了缩小型十字通道微流控芯片。通过理论分析和加工试验, 研究了激光能量密度和激光加工次数对平均刻蚀深度的影响, 以及刻蚀过程中去离子水的作用。...

    Pyrex玻璃的ICP刻蚀技术研究* (2008年)

    以SF6/Ar为刻蚀气体,采用感应耦合等离子体(ICP)刻蚀Pyrex玻璃,研究气体流量、射频功率对刻蚀速率及刻蚀面粗糙度的影响。采用正交实验方法找出优化的实验参数,得到Pyrex玻璃刻蚀速率为106.8nm/min,表面粗糙度为...

    pxcap - Python libpcap wrapper in Pyrex-开源

    Pyrex中的Python libpcap包装器和TCP / IP解码器

    python-spidermonkey

    安装pyrex:sudo apt-get install python-pyrex 安装g++:sudo apt-get install g++ 安装libjs.so: $ tar zxvf js-1.7.0.tar.gz $ cd js/src $ make -f Makefile.ref $ cp Linux_All_DBG.OBJ/libjs.so /...

    使用最广泛的Python到C编译器-Python开发

    Cython基于Pyrex,但支持更多的尖端功能和优化。 Cython语言非常c欢迎使用Cython! Cython是一种使Python编写C扩展与Python本身一样容易的语言。 Cython基于Pyrex,但支持更多的尖端功能和优化。 Cython语言与Python...

    用于评估钢琴练习表现的 Python工具_python_代码_下载

    记录器目录有一个围绕 OS X 的核心 MIDI 库的 pyrex 包装器和 用于输出来自 MIDI 键盘的事件的 Python 脚本。 示例目录有一些初始记录和“分数” Hanon 的第 21 个练习的右手(以及用于生成的 Python 代码) 这个...

    python-cython:https

    Cython基于Pyrex,但支持更多的尖端功能和优化。 Cython语言与Python语言非常接近,但是Cython还支持调用C函数以及在变量和类属性上声明C类型。 这使编译器可以从Cython代码生成非常有效的C代码。 这使Cython成为...

    Expert Python Programming(PACKT,2ed,2016)

    Moving on, you will learn to effectively create Python extensions with C, C++, cython, and pyrex. The important factors while writing code such as code management tools, writing clear documentation, ...

    论文研究-一种用于光学器件封装的圆片级玻璃微透镜阵列的制备 .pdf

    一种用于光学器件封装的圆片级玻璃微透镜阵列的制备,徐超,罗新虎,本文介绍了一种使用Pyrex7740玻璃制备圆片级二元微透镜阵列的方法,它可以应用于红外探测器、LED等光学器件。本工艺基于硅微加工工艺

    c#毕业设计源码下载-cython:使用最广泛的Python到C编译器

    Cython基于Pyrex,但支持更多的尖端功能和优化。 Cython语言与Python语言非常接近,但是Cython还支持调用C函数以及在变量和类属性上声明C类型。 这使编译器可以从Cython代码生成非常有效的C代码。 这使Cython成为...

    Pex-开源

    Pex为Pyrex或Cython添加了使用numpy.ndarray编写C快速数字的功能,使您摆脱了Makefile和头文件的麻烦,并使Pyrex类可通过酸洗和更快的方案进行序列化。

    论文研究 - Co3O4助催化剂对InTaO4的影响在可见光下可将CO2光催化还原为CH3OH

    在Pyrex反应器中,在可见光照射下,用KHCO3或NaOH水溶液鼓泡超纯CO2气体进行光催化还原。 SEM显微照片显示InTaO4表面上有许多小的Co3O4颗粒。 Co3O4-InTaO4的带隙为2.7 eV,证实这些催化剂具有将CO2还原为甲醇的...

    一种高精细度MEMS光纤F-P压力传感器

    该传感器基于MEMS技术,将硅片与Pyrex#7740玻璃片阳极键合并镀上高反介质膜构成一个高精细度的F-P腔。当外界压力发生变化时,F-P腔长会发生变化;采用高灵敏度光纤白光干涉测量技术,通过测量F-P腔长就可获得被测压力。...

    基于流体纳米通道的新型泄漏组件

    制作并设计了尺寸约为280 nm的流体纳米通道,并将其设计为泄漏组件,其中纳米通道形成在硅片上并用Pyrex玻璃封闭。 通过扫描电子显微镜对几何尺寸进行表征,并测量氦气和其他重气体(N2,O2和Ar)的气体流导,并通过...

Global site tag (gtag.js) - Google Analytics