cython

Cython has “first class function objects” - how efficient are they? Do they have Python overhead?

爱⌒轻易说出口 提交于 2020-01-07 03:11:09
问题 I have an external library that computes the optima, say minima, of a given function. Say its headers give me a function double[] minimizer(ObjFun f) where the headers define typedef double (*ObjFun)(double x[]) I have generated Cython wrappers for this library. I now want to give user parameterized functions to it, specifically, I want to write a function def getFunction(double q11, double q12, double q22): cdef f(double x[]): return x[0]*x[0]*q11 + 2*x[0]*x[1]*q12 + x[1]*x[1]*q22 return f

Convert ctypes code to cython

风格不统一 提交于 2020-01-07 02:25:26
问题 I'd like to convert some ctypes code to use cython instead, but I'm struggling. Essentially, the ctypes code: copies the contents (floats) of two lists into C-compatible structs sends the structs to my binary via FFI receives the structs back (the length is never modified) copies the contents to two new lists sends the structs back across the FFI boundary so their memory can be freed My ctypes code looks like this so far: rlib.h #ifndef RLIB_H #define RLIB_H typedef struct _FFIArray { void*

如何保护你的 Python 代码 cython

邮差的信 提交于 2020-01-06 21:02:32
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 如何保护你的 Python 代码 (一)—— 现有加密方案 https://zhuanlan.zhihu.com/p/54296517 0 前言 去年11月在PyCon China 2018 杭州站分享了 Python 源码加密 ,讲述了如何通过修改 Python 解释器达到加解密 Python 代码的目的。然而因为笔者拖延症发作,一直没有及时整理成文字版,现在终于战胜了它,才有了本文。 本系列将首先介绍下现有源码加密方案的思路、方法、优点与不足,进而介绍如何通过定制 Python 解释器来达到更好地加解密源码的目的。 由于 Python 的动态特性和开源特点,导致 Python 代码很难做到很好的加密。社区中的一些声音认为这样的限制是事实,应该通过法律手段而不是加密源码达到商业保护的目的;而还有一些声音则是不论如何都希望能有一种手段来加密。于是乎,人们想出了各种或加密、或混淆的方案,借此来达到保护源码的目的。 常见的源码保护手段有如下几种: 发行 .pyc 文件 代码混淆 使用 py2exe 使用 Cython 下面来简单说说这些方案。 1 发行 .pyc 文件 1.1 思路 大家都知道,Python 解释器在执行代码的过程中会首先生成 .pyc 文件,然后解释执行 .pyc 文件中的内容。当然了

My Cython code parses into C, but doesn't compile. First time trying to use external C code

倾然丶 夕夏残阳落幕 提交于 2020-01-06 12:56:30
问题 I am trying to compile this code: interp3d.pyx along with the library provided here: Interpolate3D It gets past the cython -> C stage without any errors, but there's a crapton of errors with gcc: Compile-time errors---dunno if this helps or not Excerpt from compile errors: gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -O2 -I/home/jordango/Desktop/epd-7.0-2-rh5-x86/include -fPIC -I/home/jordango/Desktop/epd-7.0-2-rh5-x86/include/python2.7 -c interp3d.c -o build/temp.linux-i686-2.7/interp3d

My Cython code parses into C, but doesn't compile. First time trying to use external C code

泪湿孤枕 提交于 2020-01-06 12:55:14
问题 I am trying to compile this code: interp3d.pyx along with the library provided here: Interpolate3D It gets past the cython -> C stage without any errors, but there's a crapton of errors with gcc: Compile-time errors---dunno if this helps or not Excerpt from compile errors: gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -O2 -I/home/jordango/Desktop/epd-7.0-2-rh5-x86/include -fPIC -I/home/jordango/Desktop/epd-7.0-2-rh5-x86/include/python2.7 -c interp3d.c -o build/temp.linux-i686-2.7/interp3d

How to call a cython cdef() function that contains as a parameter a function in python?

*爱你&永不变心* 提交于 2020-01-06 12:38:30
问题 I have a cdef function that has amongst its parameters a function. I am trying to generate a python 'wrapper' function that will call it. I know that defining a function as cpdef() I would be able to get access to a python version of the function. However, if I do this, I will get an error (as expected) that says that python cannot recognize the function definition I provided. Any suggestions? My original function is domain specific and quite long but I think that the following example

How to call a cython cdef() function that contains as a parameter a function in python?

丶灬走出姿态 提交于 2020-01-06 12:38:21
问题 I have a cdef function that has amongst its parameters a function. I am trying to generate a python 'wrapper' function that will call it. I know that defining a function as cpdef() I would be able to get access to a python version of the function. However, if I do this, I will get an error (as expected) that says that python cannot recognize the function definition I provided. Any suggestions? My original function is domain specific and quite long but I think that the following example

Cython throws an error when pass function with void * argument to dict

落花浮王杯 提交于 2020-01-06 05:38:26
问题 I have a function with void * parameter and I want to store it in the dict. What I do is: %%cython cdef void foo(void * bar): pass cdef dict foobar = {'foo': foo} But this code raises an Error: Cannot convert 'void (void *)' to Python object Any ways to overcome this problem? 回答1: The easiest solution is to create a cdef class that can wrap this function. Since the cdef class is a Python object it can be stored in a dictionary like any other Python object. ctypedef void (*void_func_ptr)(void*

Cython: How user-defined conversion of C++-classes can be used?

我是研究僧i 提交于 2020-01-06 04:37:52
问题 Cython's documentation seems to be silent about how user-defined conversion can be wrapped. For example, while the following c++-code prints 1 (i.e. true , here live): #include <iostream> struct X{ operator bool() const{ return true;} }; int main() { X x; std::cout << x << "\n"; } its "equivalent" in Cython: %%cython -+ cdef extern from *: """ struct X { //implicit conversion operator bool() const { return true; } }; """ cdef cppclass X: operator bool() # ERROR HERE def testit(): cdef X x;

Cython Cimport unresolved external symbol

ぃ、小莉子 提交于 2020-01-06 03:24:06
问题 My setup.py are : from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize import numpy as np extensions = [ Extension('_hmmc', ['_hmmc.pyx'], include_dirs = [np.get_include()]), ] setup( ext_modules = cythonize(extensions) ) and I'm experimenting with cimport to get it to work. from numpy.math cimport expl import numpy as np print(expl(5-2)) However, the erros are error LNK2001: unresolved external symbol _npy_expl Any idea? I have checked