2011-05-24 30 views
9

我目前正在为Python开发一个基于C++的模块。我发现Boost :: Python对于我想要完成的工作非常有效。不过,我现在遇到了由Boost :: Python生成的docstring的一些问题。鉴于以下的boost :: Python的定义:如何覆盖Boost :: Python自动创建的文档字符串数据?

BOOST_PYTHON_MODULE(gcsmt) 
{ 
class_<gcsmt::Units>("Units", "Sets the units used as input.", no_init) 
    .def("PrintSupported", &gcsmt::Units::printSupported, "Print out all supported units.") 
    .def("SetDefault", &gcsmt::Units::setDefaultUnit, "Sets the default unit to be used for inputs/outputs.") 
    .staticmethod("PrintSupported") 
    .staticmethod("SetDefault") 
    .def(self_ns::str(self_ns::self)) 
    ; 
} 

如果我编译,加载我在Python模块,并获得了gscmt.Units类的帮助下,输出如下:

>>> help(gcsmt.Units) 

Help on class Units in module gcsmt: 

class Units(Boost.Python.instance) 
| Sets the units used as input. 
| 
| Method resolution order: 
|  Units 
|  Boost.Python.instance 
|  __builtin__.object 
| 
| Methods defined here: 
| 
| __reduce__ = <unnamed Boost.Python function>(...) 
| 
| __str__(...) 
|  __str__((Units)arg1) -> object : 
|  
|   C++ signature : 
|    _object* __str__(gcsmt::Units {lvalue}) 
| 
| ---------------------------------------------------------------------- 
| Static methods defined here: 
| 
| PrintSupported(...) 
|  PrintSupported() -> None : 
|   Print out all supported units. 
|  
|   C++ signature : 
|    void PrintSupported() 
| 
| SetDefault(...) 
|  SetDefault((UnitType)arg1, (str)arg2) -> None : 
|   Sets the default unit to be used for inputs/outputs. 
|  
|   C++ signature : 
|    void SetDefault(gcsmt::unitType,std::string) 
| 
| ---------------------------------------------------------------------- 
| Data and other attributes defined here: 
| 
| __init__ = <built-in function __init__> 
|  Raises an exception 
|  This class cannot be instantiated from Python 
| 
| ---------------------------------------------------------------------- 
| Data descriptors inherited from Boost.Python.instance: 
| 
| __dict__ 
| 
| __weakref__ 
| 
| ---------------------------------------------------------------------- 
| Data and other attributes inherited from Boost.Python.instance: 
| 
| __new__ = <built-in method __new__ of Boost.Python.class object> 
|  T.__new__(S, ...) -> a new object with type S, a subtype of T 

虽然输出的大部分文档对开发人员来说都很有价值,但大多数文档对于最终用户来说都是噪声,甚至更糟糕。 (例如,我的用户不关心给定方法的C++签名是什么,也不需要查看方法解析顺序或显示其他隐藏方法)。有什么方法可以重写,并减少Boost :: Python设置的文档的级别/详细程度?理想情况下,我想我的文档看起来是这样的:

>>> help(gcsmt.Units) 

Help on class Units in module gcsmt: 

class Units 
| Sets the units used as input. 
| 
| PrintSupported() -> None : 
|  Print out all supported units. 
| 
| SetDefault((UnitType)arg1, (str)arg2) -> None : 
|  Sets the default unit to be used for inputs/outputs. 

回答

19
  • 使用的boost ::蟒蛇:: docstring_options类来定义你自动创建的文档字符串选项。
  • 全部def函数将文档字符串作为最后一个参数。
  • 所有class_定义采取类文档字符串作为最后一个参数

即:

using boost::python; 
BOOST_PYTHON_MODULE(foo) 
{ 
    // This will enable user-defined docstrings and python signatures, 
    // while disabling the C++ signatures 
    docstring_options local_docstring_options(true, true, false); 

    class_<Bar>("Bar", init<>(), "Bar class" /* class docstring here */) 
    .def("foobar", &Bar::foobar, "foobar function" /* function docstring here */); 
} 
+1

感谢阿列克谢。 docstring_options让我获得了大部分途径。它看起来像Python中的帮助模块添加了一堆额外的信息类,但看起来这不是一个提升问题。 – MarkD 2011-05-24 18:44:37

+0

'docstring_options'是一个覆盖ctor中的选项并在dtor中恢复它们的类,有效地更改当前作用域中的选项。因此,创建一个临时的'docstring_options'是不够的,你必须创建一个局部变量来保持它的活性,例如, 'docstring_options doc_opts(true,true,false);'。 – 2011-10-21 12:09:21

+0

+1对于简短而有用的答案。谢谢! – 2011-11-22 15:43:20