cython

Create package with cython so users can install it without having cython already installed

懵懂的女人 提交于 2020-05-25 22:03:46
问题 I have a question. I would like to distribute my cython-powered packages, but I see no easy way to build them in setup.py. I would like setup.py to: most importantly: install my package without cython (from pre-generated C files or by installing cython beforehand) rebuild (run cythonize) package on sdist not need to hard-code list of my cython modules (just use glob or something) be able to work without .c files (should not be stored in git) or .pyx (might not be distributed). at least one of

Python一键转Jar包,Java调用Python新姿势!

允我心安 提交于 2020-05-08 05:52:03
粉丝朋友们,不知道大家看故事看腻了没(要是没腻可一定留言告诉我^_^),今天这篇文章换换口味,正经的来写写技术文。言归正传,咱们开始吧! 本文结构: - 需求背景 - 进击的 Python - Java 和 Python - 给 Python 加速 - 寻找方向 - Jython? - Python->Native 代码 - 整体思路 - 实际动手 - 自动化 - 关键问题 - import 的问题 - Python GIL 问题 - 测试效果 - 总结 需求背景 进击的 Python 随着人工智能的兴起,Python 这门曾经小众的编程语言可谓是焕发了第二春。 以 tensorflow、pytorch 等为主的机器学习/深度学习的开发框架大行其道,助推了 python 这门曾经以爬虫见长(python 粉别生气)的编程语言在 TIOBE 编程语言排行榜上一路披荆斩棘,坐上前三甲的宝座,仅次于 Java 和 C,将 C++、JavaScript、PHP、C#等一众劲敌斩落马下。 当然,轩辕君向来是不提倡编程语言之间的竞争对比,每一门语言都有自己的优势和劣势,有自己应用的领域。 另一方面,TIOBE 统计的数据也不能代表国内的实际情况,上面的例子只是侧面反映了 Python 这门语言如今的流行程度。 Java 还是 Python 说回咱们的需求上来,如今在不少的企业中,同时存在

python3 Debug报错 Traceback (most recent call last)

百般思念 提交于 2020-05-04 00:37:06
记录一下,刚入门学习python3,模块可以run,但是debug报错 下面贴出报错信息 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- D:\Anaconda\python.exe "D:\jetbrains\PyCharm 2019.1.2\helpers\pydev\ pydevd.py " --multiproc --qt-support=pyside --client 127.0.0.1 --port 6835 --file D:/PythonDemo/code/ cal.py Traceback (most recent call last): File "D:\jetbrains\PyCharm 2019.1.2\helpers\pydev\_pydevd_bundle\ pydevd_cython_wrapper.py ", line 2, in <module> from _pydevd_bundle_ext.pydevd_cython

如何实现 C/C++ 与 Python 的通信?

大憨熊 提交于 2020-04-29 18:52:13
属于混合编程的问题。较全面的介绍一下,不仅限于题主提出的问题。 以下讨论中,Python指它的标准实现,即CPython(虽然不是很严格) 本文分4个部分 1. C/C++ 调用 Python (基础篇)— 仅讨论Python官方提供的实现方式 2. Python 调用 C/C++ (基础篇)— 仅讨论Python官方提供的实现方式 3. C/C++ 调用 Python (高级篇)— 使用 Cython 4. Python 调用 C/C++ (高级篇)— 使用 SWIG 练习本文中的例子,需要搭建Python扩展开发环境。具体细节见[搭建Python扩展开发环境 - 蛇之魅惑 - 知乎专栏](http://zhuanlan.zhihu.com/python-dev/20150730) **1 C/C++ 调用 Python(基础篇)** Python 本身就是一个C库。你所看到的可执行体python只不过是个stub。真正的python实体在动态链接库里实现,在Windows平台上,这个文件位于 %SystemRoot%\System32\python27.dll。 你也可以在自己的程序中调用Python,看起来非常容易: ``` //my_python.c #include <Python.h> int main(int argc, char *argv[]) { Py

How to represent inf or -inf in Cython with numpy?

不羁的心 提交于 2020-04-09 17:54:16
问题 I am building an array with cython element by element. I'd like to store the constant np.inf (or -1 * np.inf ) in some entries. However, this will require the overhead of going back into Python to look up inf . Is there a libc.math equivalent of this constant? Or some other value that could easily be used that's equivalent to (-1*np.inf) and can be used from Cython without overhead? EDIT example, you have: cdef double value = 0 for k in xrange(...): # use -inf here -- how to avoid referring

How to represent inf or -inf in Cython with numpy?

喜你入骨 提交于 2020-04-09 17:53:59
问题 I am building an array with cython element by element. I'd like to store the constant np.inf (or -1 * np.inf ) in some entries. However, this will require the overhead of going back into Python to look up inf . Is there a libc.math equivalent of this constant? Or some other value that could easily be used that's equivalent to (-1*np.inf) and can be used from Cython without overhead? EDIT example, you have: cdef double value = 0 for k in xrange(...): # use -inf here -- how to avoid referring

How to represent inf or -inf in Cython with numpy?

▼魔方 西西 提交于 2020-04-09 17:53:05
问题 I am building an array with cython element by element. I'd like to store the constant np.inf (or -1 * np.inf ) in some entries. However, this will require the overhead of going back into Python to look up inf . Is there a libc.math equivalent of this constant? Or some other value that could easily be used that's equivalent to (-1*np.inf) and can be used from Cython without overhead? EDIT example, you have: cdef double value = 0 for k in xrange(...): # use -inf here -- how to avoid referring

How to pass a function pointer to an external program in Cython

好久不见. 提交于 2020-04-06 02:45:55
问题 I am trying to write a wrapper around a C program so that I can call it from Python. I am using Cython to do this. The C function takes a callback function as an argument, but this call back function will only be known at run-time of the python program. I have been searching how to do this and it seems there is not simple solution but the following seems to work: #python.py libc = cdll.LoadLibrary("myfunc.so") #Callback function is defined in myfunc.so .... c_wrapper(libc.fun, ...) . #c

How to wrap a C pointer and length in a new-style buffer object in Cython?

南笙酒味 提交于 2020-04-06 02:39:09
问题 I'm writing a Python 2.7 extension module in Cython. How do I create a Python object implementing the new-style buffer interface that wraps a chunk of memory given to me by a C library? The chunk of memory is just a string of bytes, not a structure or multidimensional array. I'm given a const void * pointer and a length, and some details about how long the pointer stays valid. I can't copy the memory—that would kill performance for my application. With the old-style buffer objects I could

Passing multiprocessing.RawArray to a C++ function

旧巷老猫 提交于 2020-03-25 05:18:30
问题 My Python application creates an array shared between processes using multiprocessing.RawArray . Now to speed up computation I want to modify this array from within a C++ function. What is a safe way to pass a pointer to the underlying memory to a C++ function that accepts a void * argument? The function is defined in a pxd file as: cdef extern from 'lib/lib.hpp': void fun(void *buffer) My naive attempt so far: buffer = multiprocessing.RawArray(ctypes.c_ubyte, 10000) clib.fun(ctypes.cast(self