我想为我的C++项目提供Python接口.从技术上讲,我决定使用Cython来包装C++代码.随着时间的推移,整个项目将成为一个Python扩展模块,但首先,这是高度实验性的.渐渐地,C++类需要暴露给Python.
我的问题是如何最好地组织文件和构建配置,以便Cython生成的和人工编写的C++代码不会混合,并且Python扩展模块与其他目标完全分开构建.
我想象一个源文件的目录结构,以及Cython的一些构建目录.
Project/
src/
*.h
*.cpp
cython/
Project.pyx
setup.py
Run Code Online (Sandbox Code Playgroud) 应用于pandas.to_numeric包含表示数字(以及可能的其他不可解析字符串)的字符串的数据帧列会导致出现如下错误消息:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-66-07383316d7b6> in <module>()
1 for column in shouldBeNumericColumns:
----> 2 trainData[column] = pandas.to_numeric(trainData[column])
/usr/local/lib/python3.5/site-packages/pandas/tools/util.py in to_numeric(arg, errors)
113 try:
114 values = lib.maybe_convert_numeric(values, set(),
--> 115 coerce_numeric=coerce_numeric)
116 except:
117 if errors == 'raise':
pandas/src/inference.pyx in pandas.lib.maybe_convert_numeric (pandas/lib.c:53558)()
pandas/src/inference.pyx in pandas.lib.maybe_convert_numeric (pandas/lib.c:53344)()
ValueError: Unable to parse string
Run Code Online (Sandbox Code Playgroud)
看看哪个值无法解析会不会有帮助?
我输入hg init了一个目录,但后来改变了主意:我宁愿在版本控制下没有这个.那么如何撤消hg init?
我想还有另一个解决方案.hg,而不是删除目录,但我找不到命令.或者mercurial不可能从项目中删除自己?
我正在尝试构建我的第一个Boost.Python示例.
#include <iostream>
#include <boost/python.hpp>
using namespace boost::python;
class Hello {
public:
std::string greet() {
std::cout << "Hello World" << std::endl;
}
};
BOOST_PYTHON_MODULE(hello)
{
class_<Hello>("Hello")
.def("greet", &Hello::greet);
}
int main() {
std::cout << "Boost.Python Test" << std::endl;
Hello hello;
hello.greet();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑:缺少Python开发标题,正如@cdhowie所指出的那样.我找到并包含了所需的头文件.现在链接器抱怨:
10:43:58 **** Build of configuration BoostPythonTest-DPar for project BoostPythonTest
****
make all
Building file: ../src/BoostPythonTest.cpp
Invoking: GCC C++ Compiler
/usr/local/bin/g++-4.7 -I/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -I/usr/include -I/usr/local/Cellar/gcc/4.7.2/gcc/include/c++/4.7.2 -O0 -g3 -p -pg -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/BoostPythonTest.d" -MT"src/BoostPythonTest.d" …Run Code Online (Sandbox Code Playgroud) 如何在Cython中的(Python)列表上并行迭代?
考虑以下简单功能:
def sumList():
cdef int n = 1000
cdef int sum = 0
ls = [i for i in range(n)]
cdef Py_ssize_t i
for i in prange(n, nogil=True):
sum += ls[i]
return sum
Run Code Online (Sandbox Code Playgroud)
这会产生很多编译器错误,因为没有GIL的并行部分显然无法与任何Python对象一起使用:
Error compiling Cython file:
------------------------------------------------------------
...
ls = [i for i in range(n)]
cdef Py_ssize_t i
for i in prange(n, nogil=True):
sum += ls[i]
^
------------------------------------------------------------
src/parallel.pyx:42:6: Coercion from Python not allowed without the GIL
Error compiling Cython file:
------------------------------------------------------------
...
ls = [i for …Run Code Online (Sandbox Code Playgroud) 我注意到交互式小部件在我的 Jupyter Lab 笔记本中不起作用。
以下代码应生成交互式滑块,但不会:
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
def f(x):
return x
interact(f, x=10);
Run Code Online (Sandbox Code Playgroud)
这里有什么问题,我怎样才能让小部件工作?
新的Jupyter Lab很棒,但我缺少将单元格转换为幻灯片的选项。在经典的 Jupyter Notebooks 中,它位于“视图 > 单元格工具栏 > 幻灯片”下:
功能怎么了?有没有办法在 Jupyter Lab 中编辑幻灯片?
对于调试,我需要查看地图(std::unordered_map或std::map)的内容,Eclipse CDT调试器没有以可读的形式提供给我,例如{1: 2, 3:4, ...}
调试时检查地图内容的最佳方法是什么?
我是否必须使用print-statements返回调试?如果是的话,简洁的宏或函数如何将任何地图的内容打印为字符串?
假设我们将一些数据读入pandas数据框:
data1 = pd.read_csv("data.csv", "\t")
Run Code Online (Sandbox Code Playgroud)
内容如下:

然后定义一个函数,它应该给我们一个水平条形图,其中条形长度代表值,条形用键标记.
def barchart(data, labels):
pos = arange(len(data))+.5 # the bar centers on the y axis
barh(pos, data, align='center', height=0.25)
yticks(pos, labels)
Run Code Online (Sandbox Code Playgroud)
然后我们调用这个绘图函数:
barchart(data1["val"], data1["key"])
Run Code Online (Sandbox Code Playgroud)
这给了我们以下情节:

现在,是什么决定了酒吧的顺序?
假设我们希望条形图按特殊顺序排列,比方说[C, A, D, F, E, B],我们如何强制执行此操作?
我想在开发模式下安装我的Python模块.正如我在许多例子中看到的python setup.py develop那样,应该这样做.但是develop我的setup.py文件不存在该命令:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
import os
src = ["_NetworKit.pyx"] # list of source files
modules = [Extension("_NetworKit",
src,
language = "c++",
extra_compile_args=["-fopenmp", "-std=c++11", "-O3", "-DNOGTEST"],
extra_link_args=["-fopenmp", "-std=c++11"],
libraries=["NetworKit-Core-O"],
library_dirs=["../"])]
for e in modules:
e.cython_directives = {"embedsignature" : True}
setup(name="_NetworKit",
cmdclass={"build_ext": build_ext},
ext_modules=modules,
py_modules = ["NetworKit.py"])
Run Code Online (Sandbox Code Playgroud)
(注意Cython扩展模块).
我错过了什么?我需要修改setup.py吗?
python ×8
c++ ×3
cython ×3
jupyter ×2
jupyter-lab ×2
pandas ×2
boost ×1
boost-python ×1
build ×1
data-science ×1
debugging ×1
distutils ×1
map ×1
matplotlib ×1
mercurial ×1
openmp ×1
plot ×1
python-3.x ×1
setup.py ×1