我目前正在尝试使用openmp. 我把特拉维斯的旗帜挂'-fopenmp'在了身上。
我该如何解决这个问题?
在本地我刚刚brew install libopenmp解决了这个问题。但特拉维斯不行,有什么选择?
使用 cython 我得到以下“.travis.yml”
os: linux
dist: xenial
language: python
python:
- "3.7"
cache: pip
addons:
apt:
packages:
- patchelf
matrix:
include:
- os: osx
# No version of Python is available via virtualenv on OS X workers, see https://github.com/travis-ci/travis-ci/issues/2312
language: generic
env: TOXENV=py37
fast_finish: true
before_install:
brew install libomp
install:
- pip install --upgrade "pip < 19.1" -r CI/requirements.txt
- python setup.py develop
script:
- pytest
Run Code Online (Sandbox Code Playgroud)
Travis 在执行时失败: …
我已阅读在 Cython 中制作可执行文件和 BuvinJ 对如何有效地混淆 Python 代码的回答?并想测试编译后用 Cython 编译的源代码是否真的“不再存在”。确实流行使用 Cython 是一种保护 Python 源代码的方法,例如参见文章Protecting Python Sources With Cython。
让我们以这个简单的例子为例test.pyx:
import json, time # this will allow to see what happens when we import a library
print(json.dumps({'key': 'hello world'}))
time.sleep(3)
print(1/0) # division error!
Run Code Online (Sandbox Code Playgroud)
然后让我们使用 Cython:
cython test.pyx --embed
Run Code Online (Sandbox Code Playgroud)
这会产生一个test.c. 让我们编译它:
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
cl test.c /I C:\Python37\include /link C:\Python37\libs\python37.lib
Run Code Online (Sandbox Code Playgroud)
有用!它生成了一个 140KB 的test.exe可执行文件,不错! …
我有一小部分现有的C代码,我想用Cython包装.我希望能够设置一些numpy数组,然后将这些数组作为参数传递给C代码,其代码的函数采用标准的c数组(1d和2d).在弄清楚如何编写正确的.pyx代码以正确处理事情方面,我有点陷入困境.
有一些函数,但文件funcs.h中的典型函数类似于:
double InnerProduct(double *A, double **coords1, double **coords2, const int len)
Run Code Online (Sandbox Code Playgroud)
然后我有一个.pyx文件,其中包含相应的行:
cdef extern from "funcs.h":
double InnerProduct(double *A, double **coords1, double **coords2, int len)
Run Code Online (Sandbox Code Playgroud)
我摆脱了const,因为cython不支持它.我陷入困境的是包装器代码应该看起来像是将MxN numpy数组传递给**coords1和**coords2参数.
我一直在努力为这类问题找到正确的文档或教程.任何建议都将非常感激.
我有一个名为Foo的C++类.如果我遵循Cython C++教程,我将需要以不同的方式调用Python类,例如PyFoo.但是我真的需要调用Python类Foo.如何有效地做到这一点?
编辑:我正在尝试连接以前与Boost Python接口的现有C++库.出于不同的原因,我想测试Cython.因为使用Boost:使用与C++相同的名称调用Python Python类,我想继续使用这个命名约定.以不同方式调用类不是Python(CPython)的要求,但它似乎是由Cython强加的,至少在教程中是这样.
我当然可以使用纯python模块来定义一个调用PyFoo的Foo类,但这看起来既乏味又低效.
考虑一个简单的函数
def increment(self):
self.count += 1
Run Code Online (Sandbox Code Playgroud)
它通过Cython运行并编译成扩展模块.假设现在我想把这个函数作为一个类的方法.例如:
class Counter:
def __init__(self):
self.count = 0
from compiled_extension import increment
Counter.increment = increment
Run Code Online (Sandbox Code Playgroud)
现在这不起作用,因为C级别的调用约定将被打破.例如:
>>> c = Counter()
>>> c.increment()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: increment() takes exactly one argument (0 given)
Run Code Online (Sandbox Code Playgroud)
但是在Python 2中,我们可以通过执行以下操作将函数转换为未绑定的方法:
Counter.increment = types.MethodType(increment, None, Counter)
Run Code Online (Sandbox Code Playgroud)
我怎样才能在Python 3中完成同样的事情?
一种简单的方法是使用纤薄的包装:
from functools import wraps
def method_wraper(f):
def wrapper(*args, **kwargs):
return f(*args, **kwargs)
return wraps(f)(wrapper)
Counter.increment = method_wrapper(increment)
Run Code Online (Sandbox Code Playgroud)
有更有效的方法吗?
传递一个numpy数组的dtype np.float64_t工作正常(下图),但我无法传递字符串数组.
这是有效的:
# cython_testing.pyx
import numpy as np
cimport numpy as np
ctypedef np.float64_t dtype_t
cdef func1 (np.ndarray[dtype_t, ndim=2] A):
print A
def testing():
chunk = np.array ( [[94.,3.],[44.,4.]], dtype=np.float64)
func1 (chunk)
Run Code Online (Sandbox Code Playgroud)
但我无法做到这一点: 我找不到匹配的'类型标识符'为numpy字符串dtypes.
# cython_testing.pyx
import numpy as np
cimport numpy as np
ctypedef np.string_t dtype_str_t
cdef func1 (np.ndarray[dtype_str_t, ndim=2] A):
print A
def testing():
chunk = np.array ( [['huh','yea'],['swell','ray']], dtype=np.string_)
func1 (chunk)
Run Code Online (Sandbox Code Playgroud)
编译错误是:
Error compiling Cython file:
------------------------------------------------------------
ctypedef np.string_t dtype_str_t
^
------------------------------------------------------------
cython_testing.pyx:9:9: 'string_t' is …Run Code Online (Sandbox Code Playgroud) 有没有人有一个很好的例子,build_clib在distutils 中使用命令从setup.py构建一个外部(非python)C库?关于这个问题的文件似乎很少或根本不存在.
我的目标是构建一个非常简单的外部库,然后构建一个链接到它的cython包装器.我发现的最简单的例子就是这里,但这使用了system()对gcc 的调用,我无法想象这是最佳实践.
我想更快地进行cythonize.一个.pyx的代码是
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("MyFile.pyx")
)
Run Code Online (Sandbox Code Playgroud)
如果我想要cythonize怎么办?
有几个ext .pyx的文件,我会用他们的名字打电话
文件夹中的所有.pyx文件
在两种情况下,setup.py的python代码是什么?
我正在寻找解决方案来加速我编写的函数来循环遍历pandas数据帧并比较当前行和前一行之间的列值.
例如,这是我的问题的简化版本:
User Time Col1 newcol1 newcol2 newcol3 newcol4
0 1 6 [cat, dog, goat] 0 0 0 0
1 1 6 [cat, sheep] 0 0 0 0
2 1 12 [sheep, goat] 0 0 0 0
3 2 3 [cat, lion] 0 0 0 0
4 2 5 [fish, goat, lemur] 0 0 0 0
5 3 9 [cat, dog] 0 0 0 0
6 4 4 [dog, goat] 0 0 0 0
7 4 11 [cat] 0 0 …Run Code Online (Sandbox Code Playgroud) 我想Cython通过使用将一些额外的选项传递给编译器extra_compile_args.
我的setup.py:
from distutils.core import setup
from Cython.Build import cythonize
setup(
name = 'Test app',
ext_modules = cythonize("test.pyx", language="c++", extra_compile_args=["-O3"]),
)
Run Code Online (Sandbox Code Playgroud)
但是,当我跑步时python setup.py build_ext --inplace,我收到以下警告:
UserWarning: got unknown compilation option, please remove: extra_compile_args
Run Code Online (Sandbox Code Playgroud)
问题:如何extra_compile_args正确使用?
我用Cython 0.23.4下了Ubuntu 14.04.3.
cython ×10
python ×7
numpy ×2
arrays ×1
bigdata ×1
c++ ×1
compilation ×1
installation ×1
openmp ×1
pandas ×1
performance ×1
python-3.x ×1
setup.py ×1
travis-ci ×1