如何覆盖Boost :: Python自动创建的docstring数据?

Mar*_*rkD 9 c++ python documentation boost boost-python

我目前正在为Python开发一个基于C++的模块.我发现Boost :: Python对我想要完成的工作非常有效.但是,我现在遇到了由Boost :: Python生成的docstring的一些问题.给出以下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))
    ;
}
Run Code Online (Sandbox Code Playgroud)

如果我编译,在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
Run Code Online (Sandbox Code Playgroud)

虽然输出的大部分文档对我来说都很有价值,但对于最终用户来说,大部分文档都会产生噪音,甚至更糟.(例如,我的用户不关心给定方法的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.
Run Code Online (Sandbox Code Playgroud)

Ale*_*kiy 19

  • 使用boost :: python :: docstring_options类来定义自动创建的文档字符串选项.
  • 所有def函数都将docstring作为最后一个参数.
  • 所有class_ definitions都将类docstring作为最后一个参数

即:

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 */);
}
Run Code Online (Sandbox Code Playgroud)