在django RestFramework中,是否有任何"官方"方式来生成"Api Root"的文档?
在查看RestFramework的源代码之后,我通过继承DefaultRouter找到了一个解决方法:
from rest_framework import routers
class MyRouter(routers.DefaultRouter):
def get_api_root_view(self):
api_root_view = super(MyRouter, self).get_api_root_view()
ApiRootClass = api_root_view.cls
class MyAPIRoot(ApiRootClass):
"""My API Root documentation"""
pass
return MyAPIRoot.as_view()
router = MyRouter()
Run Code Online (Sandbox Code Playgroud)
有更干净或更好的方式吗?
从文档中可以看出,双引号用于文字,而单引号则在有代码文本被解释时使用.
这将导致我为f()下面的方法编写docstring :
class A(B):
def f(arg1, arg2):
return B(arg1 + arg2 + self.index)
Run Code Online (Sandbox Code Playgroud)
如:
Takes two arguments, ``arg1` and ``arg2``, which are assumed to be objects
of type (or duck-type) `NiceClass`, and returns a new object of class `B`
with `B.something` assigned some hash of ``arg1`` and ``arg2``.
Run Code Online (Sandbox Code Playgroud)
这是正确的吗?
在许多代码示例中,Sphinx和其他方面,我看到相当于B并NiceClass用双引号括起来("``B``"和"``NiceClass``").
是否有打印输出帮助('myfun')的选项.我看到的行为是输出打印到std.out并且脚本等待用户输入(即输入'q'继续).
必须有一个设置来设置它只是转储文档字符串.
或者,如果我可以转储文档字符串PLUS"def f(args):"行也可以.
搜索"python帮助功能"是滑稽的.:)也许我错过了一些不错的pydoc页面那里解释了一切?
IntelliJ IDEA允许链接到Java文档注释中的其他方法.这允许我将光标移动到符号上并使用键盘快捷键跳转到定义,以及在鼠标悬停时按住ctrl,鼠标在可点击的符号下显示下划线.例如:
/**
* This is a link to a method {@link #setBalance}
*/
Run Code Online (Sandbox Code Playgroud)
我试图用PyCharm在Python中实现这一点.我从其他答案中尝试了各种各样的事情而没有任何运气.以下是一些尝试:
def my_func(my_arg):
'''
Convert a S{decimal.Decimal} to an :ref:`int`.
This method imports a module called :mod:``django``.
Sometimes we might call :func:`~utils.error` to raise an {@link Exception}.
'''
Run Code Online (Sandbox Code Playgroud)
这些都不是自动完成或创建超链接.
是不是PyCharm还没有实现这个功能呢?
这个问题类似于python docstring中的链接类方法,但答案似乎不适用于PyCharm.
在python中有一些docstrings的标签,比如@param和@return:
def my_method(a_param):
''' @param a_param: Description of this param
@return: The return value of the method
'''
return int(a_param) * (other or 1)
Run Code Online (Sandbox Code Playgroud)
我可以用什么来记录发电机?特别是yield关键字,如:
def my_generator(from=0):
''' @param from: The initial value
@yield: A lot of values
'''
yield a_value
Run Code Online (Sandbox Code Playgroud)
我知道@return an iterator可以在这里使用,但我不知道它是否正确,因为生成器也可以返回值.
谢谢.
我想使用docstring或类似的东西记录我的fortran例程,这些例程可以与python help命令一起使用.由f2py生成的自动生成的文档字符串是不够的,我需要添加更多细节,就像我们使用python函数docstring一样.
在我的想法中,它应该看起来像:
mymod.f:
subroutine foo()
! This is my function
end subroutine
Run Code Online (Sandbox Code Playgroud)
并在python会话中:
>>> import mymod
>>> help(mymod.foo)
Run Code Online (Sandbox Code Playgroud) 我想用Jedi为我的Python代码添加一些自动完成支持.这可以通过使用函数docstrings或类型提示(或两者)来完成.
def function_with_types_in_docstring(param1, param2):
"""Example function with types documented in the docstring.
:type param1: int
:type param2: str
:rtype: bool
"""
def function_with_pep484_type_annotations(param1: int, param2: str) -> bool:
"""Example function with PEP 484 type annotations."""
Run Code Online (Sandbox Code Playgroud)
记录类型的哪种方法在内存使用和运行时间方面增加了更少的开销?我首先对Python代码本身的效率感兴趣,然后是Jedi.
在Python文档字符串中,如何记录:rtype:可以返回多种可能数据类型的函数?
例如,如果函数可以返回defaultdictOR dictOR list,则根据函数参数,您如何记录这个?
代码示例:
from collections import defaultdict
def read_state(state_file, state_file_type='defaultdict'):
"""Deserialize state file or create empty state and return it.
:param state_file: The path and file name of state file to read.
:type state_file: str
:param state_file_type: Data type in which state is stored.
:type state_file_type: str
:return: Current or new empty state.
:rtype: ?????
"""
if os.path.exists(state_file):
with open(state_file) as conf_fo:
state = json.load(conf_fo)
elif state_file_type == 'defaultdict':
state = defaultdict(lambda: …Run Code Online (Sandbox Code Playgroud) 我目前已经安装了 VS Code 作为 Jupyterlab 的替代编辑器,用于 Python 数据科学开发。现在我想知道如何显示函数的文档字符串或签名?
我没有发现任何有关需要更改设置的快捷方式的信息。
docstring visual-studio-code jupyter-notebook vscode-settings
我使用VS Code作为编辑器,使用autoDocstring作为Python的自动文档字符串生成器扩展。有时我会通过引入新参数或修改它们的名称、默认值等来修改函数的参数。在这种情况下,我需要对文档字符串进行手动更改。
当我更改函数签名时,有什么方法可以自动更新文档字符串吗?至少在文档字符串部分引入了新参数。
docstring ×10
python ×8
django ×1
f2py ×1
fortran ×1
ide ×1
pycharm ×1
python-3.x ×1
python-jedi ×1
return-type ×1
type-hinting ×1