我很确定python脚本可以在所有三个中运行,但我想确定.我已经在这里和那里阅读过可以编写CPython,Jython,IronPython的编辑,我希望我能够深入了解这一区别.
我的情况是我有3种不同的api,我想测试.每个api执行相同的功能代码,但它们在实现上是不同的.我正在为每种语言的api编写包装器.每个包装器都应该使用Boost :: python,Jython和IronPython向python公开完全相同的功能和实现.
我的问题是,使用这些公开的方法编写的python脚本(每种语言都很常见)是否适用于Python的所有三种"风格"?
就像我说的那样,我很确定答案是'当然',但我需要确保在花费太多时间进行研究之前.
我最近使用MacPorts安装了Boost,意图在C++中进行一些Python嵌入.然后,我决定使用Python网站上的示例检查我是否正确配置了Xcode:
#include <boost/python.hpp>
using namespace boost::python;
int main( int argc, char ** argv )
{
try
{
Py_Initialize();
object main_module(handle<>(borrowed(PyImport_AddModule("__main__"))));
object main_namespace = main_module.attr("__dict__");
handle<> ignored(PyRun_String("print \"Hello, World\"",
Py_file_input,
main_namespace.ptr(),
main_namespace.ptr()));
}
catch( error_already_set )
{
PyErr_Print();
}
}
Run Code Online (Sandbox Code Playgroud)
它正确编译,但是当我启动它时,对attr()的调用会抛出异常,并且生成的错误消息是"TypeError:属性名称必须是字符串,而不是'str'".哪个可疑听起来像一个占位符.
我在Google上查了一下,但没有运气.
我在Leopard上使用Boost v1.39,Python 2.5和GCC 4.0.
我有一个用C++编写的类,它也使用了cuda_runtime.h中的一些定义,这是一个名为ADOL-C的开源项目的一部分,你可以看看这里!
这在我使用CUDA-C时有效,但我想以某种方式在PyCUDA中导入这个类,如果有可能的话.因此,我将在内核(而不是'main')中使用此类来定义用于计算函数派生的特定变量.有没有办法将这个类传递给PyCUDA的SourceModule?
我问了一个类似的问题,但在这里我想再解释一下.所以,有一个解决方案使用nvcc -cubin编译我的C代码(感谢talonmies)然后用driver.module_from_file()导入它,但是,我想使用SourceModule并在.py文件中写入这些内核,所以它可能更加用户友好.我的例子看起来像这样:
from pycuda import driver, gpuarray
from pycuda.compiler import SourceModule
import pycuda.autoinit
kernel_code_template="""
__global__ void myfunction(float* inx, float* outy, float* outderiv)
{
//defining thread index
...
//declare dependent and independet variables as adoubles
//this is a part of my question
adtl::adouble y[3];
adtl::adouble x[3];
// ...
}
"""
Run Code Online (Sandbox Code Playgroud)
......这只是一个想法,但是SourceModule不知道什么是"adouble",因为它们是在类定义adoublecuda.h中定义的,所以我希望你现在更好地理解我的问题.有谁知道我应该从哪里开始?如果没有,我将在CUDA-C中编写这个内核,并使用nvcc -cubin选项.
感谢帮助!
我有从python序列中提取数值的代码,在大多数情况下它运行良好,但对于numpy数组则不行.
当我尝试提取unsigned char时,我会执行以下操作
unsigned char val = boost::python::extract<unsigned char>(sequence[n]);
Run Code Online (Sandbox Code Playgroud)
其中sequence是任何python序列,n是索引.我收到以下错误:
TypeError: No registered converter was able to produce a C++ rvalue of type
unsigned char from this Python object of type numpy.uint8
Run Code Online (Sandbox Code Playgroud)
如何在C++中成功提取unsigned char?我是否必须为numpy类型编写/注册特殊转换器?我宁愿使用我用于其他python序列的相同代码,也不必编写使用该代码的特殊代码PyArrayObject*.
使用boost python我需要创建嵌套的命名空间.
假设我有以下cpp类结构:
namespace a
{
class A{...}
namespace b
{
class B{...}
}
}
Run Code Online (Sandbox Code Playgroud)
明显的解决方案不起作用:
BOOST_PYTHON_MODULE( a ) {
boost::python::class_<a::A>("A")
...
;
BOOST_PYTHON_MODULE(b){
boost::python::class_<a::b::B>("B")
...
;
}
}
Run Code Online (Sandbox Code Playgroud)
它会导致编译时错误: linkage specification must be at global scope
有没有办法声明可以从Python访问的B类a.b.B?
我正在使用Boost Python来包装我创建的一些C++函数.我的一个C++函数包含22个参数.当我尝试用这个函数编译我的解决方案时Boost抱怨,我试图弄清楚它是否只是因为这个函数有太多的参数.
有谁知道这样的限制是否存在?我已经复制了我在下面的错误,而不是代码,因为我认为某人要么知道答案还是没有 - 如果没有限制,那么我只是试着弄清楚自己.首先十分感谢!
这是我收到的错误消息开头的副本...
1>main.cpp
1>c:\cpp_ext\boost\boost_1_47\boost\python\make_function.hpp(76): error C2780: 'boost::mpl::vector17<RT,most_derived<Target,ClassT>::type&,T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14> boost::python::detail::get_signature(RT (__thiscall ClassT::* )(T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14) volatile const,Target *)' : expects 2 arguments - 1 provided
1>c:\cpp_ext\boost\boost_1_47\boost\python\signature.hpp(236) : see declaration of 'boost::python::detail::get_signature'
Run Code Online (Sandbox Code Playgroud)
最终我得到了大约一百个与此类似的错误消息:
1>c:\cpp_ext\boost\boost_1_47\boost\python\make_function.hpp(76): error C2784: 'boost::mpl::vector17<RT,ClassT&,T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14> boost::python::detail::get_signature(RT (__thiscall ClassT::* )(T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14) volatile const)' : could not deduce template argument for 'RT (__thiscall ClassT::* )(T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14) volatile const' from 'std::string (__cdecl *)(const std::string &,jal::date::JULIAN_DATE,const std::string &,const std::string &,int,const std::string &,const std::string &,const std::string &,const std::string &,const std::string &,const std::string &,int,const std::string &,const std::string …Run Code Online (Sandbox Code Playgroud) 所以,我有一个简单的事件库,用C++编写并使用Boost库.我想把这个库暴露给Python,所以很自然地我转向了Boost :: Python.我最终得到了编译代码,但现在我遇到了相当严重的问题:我的库使用了高阶编程技术.例如,库由三个主要类组成:事件类,事件管理器类和事件侦听器类.事件监听器类带来了问题.码:
class listener{
public:
listener(){}
void alert(cham::event::event e){
if (responses[e.getName()])
responses[e.getName()](e.getData());
}
void setResponse(std::string n, boost::function<void (std::string d)> c){responses.insert(make_pair(n, c));}
void setManager(_manager<listener> *m){manager = m;}
private:
std::map<std::string, boost::function<void (std::string d)> > responses;
_manager<listener> *manager;
Run Code Online (Sandbox Code Playgroud)
如您所见,功能setResponse就是问题所在.它需要一个函数传递给它,不幸的是,在这种情况下,Boost :: Python不会应用它的转换器魔法.如下所示调用时:
>>> import chameleon
>>> man = chameleon.manager()
>>> lis = chameleon.listener()
>>> def oup(s):
... print s
...
>>> lis.setResponse("event", oup)
Run Code Online (Sandbox Code Playgroud)
它给出了这个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
listener.setResponse(listener, …Run Code Online (Sandbox Code Playgroud) python boost functional-programming boost-python higher-order-functions
我使用Boost.Python包装了一个C++类.这些对象在C++方面具有强引用(boost :: shared_ptr),并且Python中也可能存在间歇性强引用.到目前为止,一切运作良好.但是,如果我从其中一个强引用创建python弱引用,则只要最后一个python强引用消失,就会删除此弱引用.我希望这个弱引用能够保持活着,直到C++方面的最后一个强引用也消失了.是否有可能实现这一目标?
换句话说:如果特定的C++对象(由Boost.Python包装)仍然存在,有没有办法从python中找到?
我正在尝试使用boost :: python库在C++中为Python3创建一个helloWorld模块.
这是一个CmakeList.txt:
set(Python_ADDITIONAL_VERSIONS 3.4)
find_package( PythonLibs 3.4 REQUIRED )
include_directories( ${PYTHON_INCLUDE_DIRS} )
find_package( Boost 1.56.0 EXACT COMPONENTS python3 REQUIRED )
include_directories( ${Boost_INCLUDE_DIR} )
# Define the wrapper library that wraps our library
add_library( hello SHARED main.cpp )
target_link_libraries( hello ${Boost_LIBRARIES} ${PythonLibs_LIBRARIES} )
# don't prepend wrapper library name with lib
set_target_properties( hello PROPERTIES PREFIX "" OUTPUT_NAME hello)
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <boost/python.hpp>
char const* greet( )
{
return "Hello world";
}
BOOST_PYTHON_MODULE(mymodule)
{
using namespace boost::python;
def( "greet", …Run Code Online (Sandbox Code Playgroud) 我有一个Boost Python对象
py::object obj = whatever();
Run Code Online (Sandbox Code Playgroud)
我想用普通的python规则打印它.
// I want the effect of print 'My object is ', obj
std::cout << "My object is " << obj << std::endl;
Run Code Online (Sandbox Code Playgroud)
这不会使用巨大的编译器转储进行编译.我该怎么做呢?
boost-python ×10
python ×9
c++ ×5
boost ×4
cmake ×1
cuda ×1
ironpython ×1
jython ×1
numpy ×1
pycuda ×1
python-3.x ×1
testing ×1