我已经安装了 Visual Studio Code 扩展Python 文档字符串生成器,它可以自动生成文档字符串片段。
我使用的自定义设置:
"autoDocstring.docstringFormat": "sphinx",
"autoDocstring.generateDocstringOnEnter": true,
"autoDocstring.includeExtendedSummary": true,
"autoDocstring.guessTypes": true,
"autoDocstring.customTemplatePath": ".vscode/sphinx.mustache",
Run Code Online (Sandbox Code Playgroud)
然而,它对于函数的文档字符串最有用。
我怎样才能生成:
__init__.py文件中在这方面有什么想法吗?
Python 3.12有这样type的说法。如何正确记录类型别名?
我试过:
type Number = int | float
"""Represents a scalar number that is either an integer or float"""
Run Code Online (Sandbox Code Playgroud)
但这似乎没有将文档字符串与别名关联起来。我尝试将文档字符串放在=别名定义之后和之前,但这生成了错误。
如果我只有文件的名称,有没有办法获取python文件的doc字符串?例如,我有一个名为a.py的python文件.我知道它有一个文档字符串(之前被强制执行)但不知道它的内部结构,即它是否有任何类或主要等?我希望我不会忘记一些非常明显的东西如果我知道它有一个主要功能我可以这样做使用导入
filename = 'a.py'
foo = __import__(filename)
filedescription = inspect.getdoc(foo.main())
Run Code Online (Sandbox Code Playgroud)
我不能这样做:
filename.__doc__ #it does not work
Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用狮身人面像,并愿意学习.
我想将我的各种函数分解到index.rst文件中的不同部分.所以每个函数都有自己的头.
因此,例如,如果我有一个名为test.py的python文件,并且在该文件中我有2个函数
def foo():
"""This prints bar"""
print("bar")
def bar():
"""This prints foo"""
print("foo")
Run Code Online (Sandbox Code Playgroud)
我怎么能在index.rst中将test.py文件中的2个函数分开.
:mod:`test` -- foo
.. automodule:: test.foo
:members:
:undoc-members:
:show-inheritance:
:mod:`test` -- bar
.. automodule:: test.bar
:members:
:undoc-members:
:show-inheritance:
Run Code Online (Sandbox Code Playgroud)
如果我能弄清楚如何分离这些函数,那么它在index.html中看起来更干净,那将是非常棒的!因为现在输出不是很干净,如果我只是运行以下:
:mod:`test` -- these are my functions
--------------------------------------------
.. automodule:: test
:members:
:undoc-members:
:show-inheritance:
Run Code Online (Sandbox Code Playgroud) 我创建了一系列节点,每个节点都有一组与之关联的属性对象.使用每个属性的描述和名称初始化属性对象.我希望这些属性及其描述能够在我的sphinx文档中显示,而不必在两个地方维护名称/描述,一次在类的doc字符串中,一次在属性的初始化中.
要说明问题,请考虑以下代码:
class Foo(object):
"""My doc string
"""
@classmethod
def default_attributes(cls):
return {'foo':'description of foo attribute',
'bar':'description of bar attribute'}
@classmethod
def attributes_string(cls):
attributes = cls.default_attributes()
result = '\nDefault Attributes:\n'
for key, value in attributes.iteritems():
result += '%s: %s\n' % (key, value)
return result
print Foo.__doc__
Run Code Online (Sandbox Code Playgroud)
我希望Foo.attributes_string的结果显示在Foo的doc字符串中,以便我得到这个:
My doc string
Default Attributes:
foo: description of foo attribute
bar: description of bar attribute
Run Code Online (Sandbox Code Playgroud)
首先我想"嘿,这很简单!我只是设置一个类装饰器!":
def my_decorator(cls):
doc = getattr(cls, '__doc__', '')
doc += cls.attributes_string()
cls.__doc__ = doc
return cls …Run Code Online (Sandbox Code Playgroud) 我只是想更好地感受Python文档字符串的布局(之间""" """)
我见过不同布局的docstrings ......比如...
"""
@DESCRIPTION
Ive seen tags STARTING with an at-sign
:DESCRIPTION:
Tags with colons
DESCRIPTION
And tags with nothing
"""
Run Code Online (Sandbox Code Playgroud)
这些都有功能吗?是@用药剂关联?或者这些只是开发者之间的偏好?我查看了docstrings的样式指南,但看不到它在哪里解决这些问题......
谢谢!
在Python中,注释函数时,可以采用一种使自动生成文档更加容易的方式来实现。他们将其称为文档字符串。
现在,我已经在Groovy中创建了一个我想传递的抽象类,是否还有一种标准的方式我应该对此进行注释?是否有任何Groovy工具可从代码注释生成基本文档?
我在项目中使用简单的视图,现在我的项目开始变得更大,我正在编写一个狮身人面像文档,并使用sphinx.ext.autodoc包含对象的文档字符串。
我的麻烦是:如何大幅格式化视图的文档字符串?
例如,我正在使用:
def my_view(request):
"""
Do something depending of HTTP method.
In GET: Returns a page
IN POST: Make something and redirect
GET parameters:
GET['next'] : URL to redirect to after submit form
POST parameters:
POST['action'] : Action
"""
Run Code Online (Sandbox Code Playgroud)
我在自问是否有更好的方法来做到这一点?是否对此有共识?
在spyderIDE中,可以ctrl+i在实例化类时按下以显示与该类关联的文档字符串.是否有类似的功能jupyter notebook?
help()进入类时,我已经在许多文档字符串中看到以下语句:“参见help(type(self))以获取准确的签名”。
值得注意的是,它是在help()为scipy.stats.binom.__init__和stockfish.Stockfish.__init__在最起码。因此,我认为这是某种股票信息。
无论如何,我不知道这到底意味着什么。这是有用的信息吗?请注意,可以说,在班级“之外”,我永远也无法访问self。此外,如果我无法访问__init__方法的签名,因此甚至无法执行,则无法实例化一个类help(type(my_object_instantiated))。这是一个陷阱。22.要使用它__init__,我需要签名__init__,但是为了读取它__init__,我需要使用实例化一个对象__init__。这一点在理论上是严格的,但是,即使我确实实例化了一个scipy.stats.binom,它实际上也会返回一个完全不同的类的对象,其对象中的rv_frozen消息完全相同__init__docstring,但其签名完全不同,用处不大。换句话说,help(type(self))实际上并没有给出准确的签名。这没用。
有谁知道此消息的来源或应该如何处理?仅仅是来自文档生成器的库存垃圾,还是我在误用户?
docstring ×10
python ×6
class-method ×1
comments ×1
django ×1
formatting ×1
groovy ×1
metaclass ×1
python-3.12 ×1
type-alias ×1
type-hinting ×1