cython

Cython memoryviews: wrapping c function with array parameter to pass numpy array

一个人想着一个人 提交于 2020-07-09 07:12:05
问题 I am trying to use Cython to wrap c function with an array parameter ( quick_sort() ), so I can pass a numpy array to it. I've searched the documentation, SO and web for a working, minimal example but didn't find it. I've tried several possibilities but without any progress, so please help me to figure it out. Here are my files: quicksort.c #include <stdio.h> void quick_sort (int* a, int n) { int i, j, p, t; if (n < 2) return; p = a[n / 2]; for (i = 0, j = n - 1;; i++, j--) { while (a[i] < p)

Python multiprocessing shared memory issues with C objects involved

拥有回忆 提交于 2020-07-09 04:54:14
问题 I am working on a program that uses an external C library to parse data from external sources and a Python library to run some optimisation problem on it. The optimisation is very time consuming so using several CPU would be a significant plus. Basically, I wrapped the C(++) structures with Cython as follows: cdef class CObject(object): cdef long p_sthg cdef OBJECT* sthg def __cinit__(self, sthg): self.p_sthg = sthg self.sthg = <OBJECT*> self.p_sthg def __reduce__(self): return (rebuildObject

Real-time audio signal processing using python

蹲街弑〆低调 提交于 2020-07-04 13:51:31
问题 I have been trying to do real-time audio signal processing using 'pyAudio' module in python. What I did was a simple case of reading audio data from microphone and play it via headphones. I tried with the following code(both Python and Cython versions). Thought it works but unfortunately it is stalls and not smooth enough. How can I improve the code so that it will run smoothly. My PC is i7, 8GB RAM. Python Version import pyaudio import numpy as np RATE = 16000 CHUNK = 256 p = pyaudio.PyAudio

Minimal set of files required to distribute an embed-Cython-compiled code and make it work on any machine

不羁的心 提交于 2020-06-30 10:19:06
问题 TL;DR: how to use Cython as a distribution method instead of Py2exe, cx_freeze, pyinstaller, etc. Following Making an executable in Cython, I'd like to see how it could be possible to distribute a Python program to any Windows user (who doesn't have Python already installed on his machine) by compiling it first with Cython --embed . Let's use a test.py : import json print(json.dumps({'key': 'hello world'})) and compile it: cython test.py --embed call "C:\Program Files (x86)\Microsoft Visual

Best choice for passing float arrays in Cython

时光毁灭记忆、已成空白 提交于 2020-06-29 06:43:44
问题 Say there is a C++ class in which we would like to define a function to be called in python. On the python side the goal is being able to call this function with: Input : of type 2D numpy-array(float32), or list of lists, or other suggestions Output : of type 2D numpy-array(float32), or list of lists, or other suggestions and if it helps latency/simplicity 1D array is also ok. One would for example define a function in the header with: bool func(const std::string& name); which has string type

Cython Compiled EXE needs Python Installation to RUN

▼魔方 西西 提交于 2020-06-23 19:38:50
问题 I have compiled my python code (in Windows) with the following cython and gcc commands:- cython --embed -o hello.c hello.py gcc -municode -mthreads -Wall -O -IC:\Python37\include -LC:\Python37\libs hello.c -lpython37 -Wl,--subsystem,windows -o hello.exe (I am using modules such as request, pysimpleguiqt and fdb in hello.py) The resulting exe requires python installation to execute. Is there any way to compile it without the need of python installation at runtime? 回答1: Is there any way to

In C++/Cython it possible to only declare relevant attributes to be visible in Python?

五迷三道 提交于 2020-06-23 12:45:05
问题 Say I have a .h file with the following code: class MyClass { public: int Attribute1; int Attribute2; MyClass(){}; virtual ~MyClass(){}; virtual void Method1(string var1); virtual float Method2(float var2); }; And a related .pyx file with: cdef class PyClass: cdef MyClass *classptr [standard __cinit__ and __dealloc__ declarations ] cdef int Attribute1; def Method1(self, var1): self.classptr.Method1(var1) ... and pxd file with: cdef extern from "mycode.h": cdef cppclass MyClass: MyClass()

Does compiled standalone Cython executable still contain all original source code?

风格不统一 提交于 2020-06-17 03:43:41
问题 I'm experimenting with Cython and possibilities of code obfuscation (article). In that article especially noted: When the compilation is done there’s no way to reverse compiled libraries back to readable Python source code! I use this question info to compile my code in standalone executable. In my understanding and as mentioned in article 1, Cython translates Python code into C code, with correspond calls of Python library (is this correct?). In other wolds, we have only C file as output,

Does compiled standalone Cython executable still contain all original source code?

女生的网名这么多〃 提交于 2020-06-17 03:42:52
问题 I'm experimenting with Cython and possibilities of code obfuscation (article). In that article especially noted: When the compilation is done there’s no way to reverse compiled libraries back to readable Python source code! I use this question info to compile my code in standalone executable. In my understanding and as mentioned in article 1, Cython translates Python code into C code, with correspond calls of Python library (is this correct?). In other wolds, we have only C file as output,

When would the python tracemalloc module allocations statistics not match what's shown in ps or pmap?

二次信任 提交于 2020-06-14 04:57:08
问题 I'm trying to track down a memory leak, so I've done import tracemalloc tracemalloc.start() <function call> # copy pasted this from documentation snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno') print("[ Top 10 ]") for stat in top_stats[:10]: print(stat) This shows no major allocations, all memory allocations are pretty small, while I'm seeing 8+ GB memory allocated in ps and pmap (checking before and after running the command, and after running garbage