为什么我无法使用Sphinx Python软件包打印出docstring?
我正在使用狮身人面像记录我的功能。但是,它没有阅读我的Doc字符串,以便使用装饰器读取功能。 sphinx只需用<魔术替换函数doc字符串。魔术在这里发生>
我的功能是用装饰器以这种方式编写的。如何使狮身人面像检测功能中的DOC字符串。
文件夹处于此格式
- 项目
- 功能 - function.py
- 文档
- 功能 - function.py
def wrap(pre, post):
""" Wrapper """
def decorate(func):
""" Decorator """
def call(*args, **kwargs):
""" The magic happens here """
pre(func)
result = func(*args, **kwargs)
post(func)
return result
return call
return decorate
def entering(func, *args):
""" Pre function logging """
logging.debug("Entered {}".format(func.__name__))
# logging.info(func.__doc__)
logging.debug("Function at line {} in {}".format(func.__code__.co_firstlineno, func.__code__.co_filename))
try:
logging.debug("The argument {} is {}".format(func.__code__.co_varnames[0], *args))
except IndexError:
logging.debug("No arguments")
def exiting(func):
""" Post function logging """
logging.debug("Exited {}".format(func.__name__))
@wrap(entering, exiting)
def function(a, b):
"""
Function to execute
:param a:
:param b:
:return: Sum of a+b
"""
return a+b
I am using Sphinx to document out my functions. However, it does not read my doc strings for function with a decorator.
Sphinx just replace the function doc strings with <The magic happens here>
My functions are written in this manner with a decorator. How do i make Sphinx detect the doc strings in the function.
Folder is in this format
- Project
- Functions
- function.py- docs
- Functions
def wrap(pre, post):
""" Wrapper """
def decorate(func):
""" Decorator """
def call(*args, **kwargs):
""" The magic happens here """
pre(func)
result = func(*args, **kwargs)
post(func)
return result
return call
return decorate
def entering(func, *args):
""" Pre function logging """
logging.debug("Entered {}".format(func.__name__))
# logging.info(func.__doc__)
logging.debug("Function at line {} in {}".format(func.__code__.co_firstlineno, func.__code__.co_filename))
try:
logging.debug("The argument {} is {}".format(func.__code__.co_varnames[0], *args))
except IndexError:
logging.debug("No arguments")
def exiting(func):
""" Post function logging """
logging.debug("Exited {}".format(func.__name__))
@wrap(entering, exiting)
def function(a, b):
"""
Function to execute
:param a:
:param b:
:return: Sum of a+b
"""
return a+b
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
装饰
函数才能将docstring从func
将其复制到调用
之前。否则,Sphinx只能获取呼叫
的DocString,而不是原始功能的docstring。您可以通过直接执行
致电。__doc__ = func .__ doc __
,也可以使用function> function> wrapss.wrapss.wraps
decortor 默认情况下还要复制其他一些属性,并且可以进行很多定制)。我会尝试:
You need your
decorate
function to copy the docstring fromfunc
ontocall
before returning it. Otherwise Sphinx can only get the docstring ofcall
, not the docstring of the original function.You can do this yourself by directly doing
call.__doc__ = func.__doc__
, or you could use thefunctools.wraps
decorator from the standard library to do it for you (which also copies a few other attributes by default, and can be customized quite a bit).I'd try: