cythonize

How to apply decorators to Cython cpdef functions

﹥>﹥吖頭↗ 提交于 2020-01-22 12:34:10
问题 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

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

Make executable file from multiple pyx files using cython

落花浮王杯 提交于 2020-01-09 03:56:05
问题 I am trying to make one unix executable file from my python source files. I have two file, p1.py and p2.py p1.py :- from p2 import test_func print (test_func()) p2.py :- def test_func(): return ('Test') Now, as we can see p1.py is dependent on p2.py . I want to make an executable file by combining two files together. I am using cython. I changed the file names to p1.pyx and p2.pyx respectively. Now, I can make file executable by using cython, cython p1.pyx --embed It will generate a C source

distribution : how to build a compiled module for multiple python version and platform

我的梦境 提交于 2020-01-05 04:30:48
问题 I have build a python 3 module for my own process. I use cython to compile and wrap C++ sources. I have a linux (Debian Jessie) machine with python 3.4 and so cythonize make me a Processing.cpython-34m.so and copy it to /usr/local/lib/python3.4/dist-packages . But when I use it on another machine which has python3.5, I have to recompile everything. How can I build a linux or pip package from my machine for all python 3 version and multiple platforms (here, just Jessie and Stretch, which might

Cython “Not allowed in a constant expression”, boundscheck False doesn't work

不羁的心 提交于 2019-12-29 06:28:31
问题 I am relatively new to Cython and have encountered an error that my research has failed me on (I am using Python3 in spyder and my Sython version is 0.26) I tried this: import cython @cython.boundscheck(False) def boundtest(): cdef int r=4 cdef double l[3] and it works fine. But then I tried this: import cython @cython.boundscheck(False) def boundtest(): cdef int r=4 cdef double l[r] and I receive the error [1/1] Cythonizing test.pyx Error compiling Cython file: ------------------------------

Singleton is not working in Cython

我与影子孤独终老i 提交于 2019-12-23 17:19:12
问题 This is how i define Singleton. class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] Then I have my classes defined as: class MyClass(object): __metaclass__ = Singleton def __init__(self): pass a = MyClass() b = MyClass() "a is b" will return True However, cdef class MyCythonClass(object): __metaclass__ = Singleton def __cinit__(self): pass c =

How to compile my python code in cython with external python libs like pybrain

瘦欲@ 提交于 2019-12-23 16:30:06
问题 I need more perfomance running my neural network, so I thinked that building it with cython will be good idea. I am building my code like this: from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("my_code.pyx") ) But will it build external python files that I use? Like pybrain, skimage and PIL in my case. If not, how to force cython to build them. 回答1: No, external python files will not be cythonized and compiled unless you specifically add them

How to grant read-only (non-copy) access to an object in another process in Cython?

馋奶兔 提交于 2019-12-23 04:44:32
问题 How to grant read-only (non-copy) access to an object in one process to another process? For example, in the diagram below, how to grant Process 2 and Process 3 access to the class Tasks which is in Process 1 ? Process 1 is the only process which will write to the class Tasks and it is not important to maintain any state about which tasks have been processed etc. Process 2 Process 1 Process 3 +-------------------+ +-------------------+ +-------------------+ | | | | | | | Perform job A for | <

How to build cython pyx to pyd in anaconda(python3.6) in windows 8.1?

感情迁移 提交于 2019-12-21 19:52:51
问题 I have referred some websites to build pyx to pyd in Windows 8.1.I 'm using Anaconda Distribution with Spyder IDE, I have developed pyx file and unable to build in "Anaconda Command Prompt" Anaconda> python setup.py build --inplace --compiler=mingw32 and tried python setup.py build_ext --inplace --compiler=mingw32 getting following error: File "C:\ProgramData\Anaconda3\lib\distutils\cygwinccompiler.py", line 129 in __init__ if self.ld_version >= "2.10.90": TypeError: '>=' not supported

Declaring a numpy boolean mask in Cython

牧云@^-^@ 提交于 2019-12-13 03:47:21
问题 How should I declare the type of a boolean mask in Cython? Do I actually need to declare it? Here is the example: cpdef my_func(np.ndarray[np.double_t, ndim = 2] array_a, np.ndarray[np.double_t, ndim = 2] array_b, np.ndarray[np.double_t, ndim = 2] array_c): mask = ((array_a > 1) & (array_b == 2) & (array_c == 3) array_a[mask] = 0. array_b[mask] = array_c[mask] return array_a, array_b, array_c 回答1: You need to "cast" np.uint8_t to bool via np.ndarray[np.uint8_t, ndim = 2, cast=True] mask = ...