我正在使用openbabel的 swig包装器(用C++编写,并通过swig提供python包装器)
下面我只是用它来读取分子结构文件并获得它的unitcell属性.
import pybel
for molecule in pybel.readfile('pdb','./test.pdb'):
unitcell = molecule.unitcell
print unitcell
|..>
|..>
<openbabel.OBUnitCell; proxy of <Swig Object of type 'OpenBabel::OBUnitCell *' at 0x17b390c0> >
Run Code Online (Sandbox Code Playgroud)
unitcell具有CellMatrix()功能,
unitcell.GetCellMatrix()
<22> <openbabel.matrix3x3; proxy of <Swig Object of type 'OpenBabel::matrix3x3 *' at 0x17b3ecf0> >
Run Code Online (Sandbox Code Playgroud)
OpenBabel :: matrix3x3是这样的:
1 2 3
4 5 6
7 8 9
Run Code Online (Sandbox Code Playgroud)
我想知道如何打印矩阵3*3的内容.我曾尝试__str__与__repr__它.
在python中使用swing包装的矩阵的内容的任何一般方法?
谢谢
小智 5
基于这个openbabel文档,看起来有一个很好的理由,Python绑定没有一个很好的打印方式matrix3x3 object.的matrix3x3C++类重载<<运算符,它SWIG将简单地忽略:
http://openbabel.org/api/2.2.0/classOpenBabel_1_1matrix3x3.shtml
这意味着你将需要修改你的SWIG接口文件(看http://www.swig.org/Doc1.3/SWIGPlus.html#SWIGPlus_class_extension)的添加__str__方法openbabel::matrix3x3在C++包装了<<运营商.你的方法可能看起来很像
std::string __str__() {
//make sure you include sstream in the SWIG interface file
std::ostringstream oss(std::ostringstream::out);
oss << (*this);
return oss.str();
}
Run Code Online (Sandbox Code Playgroud)
我相信SWIG会std::string在这种情况下正确处理C++的返回类型,但如果没有,你可能不得不回过头来返回一个字符数组.
此时,您应该能够重新编译绑定,并重新运行Python代码.现在,调用str()一个matrix3x3对象应该显示<<在C++中与操作符一起显示的内容.