cython

How to apply decorators to Cython cpdef functions

怎甘沉沦 提交于 2020-01-22 12:33:44
问题 I've been playing around with Cython lately and I came across this error when applying a decorator to a Cython function Cdef functions/classes cannot take arbitrary decorators Here is the code I was tinkering with: import functools def memoize(f): computed = {} @functools.wraps(f) def memoized_f(main_arg, *args, **kwargs): if computed.get(main_arg): return computed[main_arg] computed[main_arg] = f(main_arg, *args, **kwargs) return computed[main_arg] return memoized_f @memoize cpdef int fib

c++ class in fused type

强颜欢笑 提交于 2020-01-21 12:13:26
问题 I wish to implement python wrapper for a bunch of c++ classes. Somewhere in pxd I have: cdef cppclass FooImpl1: FooImpl1() int foo() cdef cppclass FooImpl2 FooImpl2() int foo() I wonder if I can write something like this in pyx python wrapper: ctypedef fused FooImpl: FooImpl1* FooImpl2* cdef class Foo: cdef FooImpl impl def __cinit__(self, int selector): if selector == 1: self.impl = new FooImpl1() else: self.impl = new FooImpl2() def func(self): # depending on the object stored in impl

c++ class in fused type

心已入冬 提交于 2020-01-21 12:13:08
问题 I wish to implement python wrapper for a bunch of c++ classes. Somewhere in pxd I have: cdef cppclass FooImpl1: FooImpl1() int foo() cdef cppclass FooImpl2 FooImpl2() int foo() I wonder if I can write something like this in pyx python wrapper: ctypedef fused FooImpl: FooImpl1* FooImpl2* cdef class Foo: cdef FooImpl impl def __cinit__(self, int selector): if selector == 1: self.impl = new FooImpl1() else: self.impl = new FooImpl2() def func(self): # depending on the object stored in impl

What's the purpose of dictproxy?

雨燕双飞 提交于 2020-01-20 13:58:17
问题 The __dict__ of a type is a dictproxy object that is read only. I want to know what's the purpose of it. Is it only for "don't allow modify builtin types"? I found a method that can walk around this. I know it's not a good idea to modify builtin types. But I am trying to modify cdef class of Cython on fly. I want to know are there any dangerous to modify the __dict__ of cdef class this way? Here is the code: import gc gc.get_referents(float.__dict__)[0]["square"] = lambda self: self*self (3

What is pyximport and how should I use it?

落爺英雄遲暮 提交于 2020-01-19 05:37:08
问题 I am using cython to generate faster code for a mathematical model. I am having a hard time compiling the code, but somehow I managed to do so using a .bat: setlocal EnableDelayedExpansion CALL "C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\SetEnv.cmd" /x64 /release set DISTUTILS_USE_SDK=1 C:\Python27\python.exe C:\gcsilve\trunk\myproject\myproject\cythonsetup.py build_ext --inplace PAUSE It runs ok... My question is regarding pyximport. I have old code written by someone else that uses

Using function pointers to methods of classes without the gil

我的梦境 提交于 2020-01-18 10:50:27
问题 Part of my works requires a lot of calculations, but they are often fairly straight-forward and can in principle quite easily be done in parallel with Cython's prange, requiring nogil. However, given that I tried to write my Cython code with a focus on having cdef classes as building blocks I encountered a problem. Let's say I got an numerical integration routine or similar which takes a function pointer as an input ctypedef double (* someFunctionPointer) (double tt) nogil cdef double

pyx的Cython编译和连接的问题

主宰稳场 提交于 2020-01-16 15:17:10
pyx 模板的运行和测试 如果要测试 pyx 模板,可以通过编译或者动态导入: 编译: setup.py,生成动态库(linux 为so文件, window pyd),可以直接被 import 引入到一个Python会话中 from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize setup( ext_modules = cythonize([Extension("select_by_kp", ["select_by_kp.pyx"])]) ) 静态链接,比如链接使用 numpy: 在 select_by_kp.pyx 中链接 # distutils: include_dirs = /Users/yangshujun/self/cython_build/venv/lib/python3.7/site-packages/numpy/core/include/ import numpy as np cimport numpy as np from libc.stdio cimport printf # from cython.parallel import prange, parallel, threadid #

Cython buildable (Python Binded) C code to download file via HTTP (REST API)

落爺英雄遲暮 提交于 2020-01-16 11:34:42
问题 I have been googling and searching some good example(s) and so far somewhat closest was this one: Architecting a RESTFul C++ api to interface with Python In a nutshell: Essentially, I am building .so files using cython (via building Python Extension module with Bindings from *.c source file). Scope/Requierments: As first prio - I need to add codebase in *.c file that lets to download an file from certain endpoint (lets say some cloud storage) and as bonus would be able to communicate with

Cython buildable (Python Binded) C code to download file via HTTP (REST API)

早过忘川 提交于 2020-01-16 11:32:08
问题 I have been googling and searching some good example(s) and so far somewhat closest was this one: Architecting a RESTFul C++ api to interface with Python In a nutshell: Essentially, I am building .so files using cython (via building Python Extension module with Bindings from *.c source file). Scope/Requierments: As first prio - I need to add codebase in *.c file that lets to download an file from certain endpoint (lets say some cloud storage) and as bonus would be able to communicate with

Segmentation-fault with PyObject_Call() in shared library for iTunes

我与影子孤独终老i 提交于 2020-01-16 02:06:22
问题 I'm experimenting with the iTunes SDK and Cython. The DLL entry-point seems to work, but using any "real Python" causes iTunes to crash. The following code compiles fine and the plugin-dll is loaded successfully by iTunes. cimport libc.stdio as stdio cdef extern from "iTunesAPI/iTunesAPI.h": ctypedef int OSType ctypedef int OSStatus ctypedef struct PluginMessageInfo: pass int unimpErr cdef public OSStatus iTunesPluginMain( OSType message, PluginMessageInfo* messageInfo, void* refCon ): cdef