简体   繁体   English

如何为 python 项目生成文档,包括使用 pdoc 的私有/受保护类

[英]How to generate documentation for python project including private/protected classes with pdoc

I am trying to generate documentation for my python project with pdoc module by following this article .我正在尝试按照本文使用pdoc模块为我的 python 项目生成文档。

I noticed that all the classes, methods, etc starting with __ (private/protected ones) are missing in the generated documentation.我注意到生成的文档中缺少以__ (私有/受保护的)开头的所有类、方法等。

This is the sample.py这是sample.py

class __Members:
    def __test():
        pass

class seedoc:
    ''' see me '''
    pass

This is how I generated the documentation with pdoc这就是我使用 pdoc 生成文档的方式

$pdoc --html sample.py
html/sample.html

I see only public class in the generated documentation as per this screenshot:根据此屏幕截图,我在生成的文档中只看到 public class : 在此处输入图像描述

Can someone please help me figure out a way to get over this limitation and generate the documentation with all the private/protected members?有人可以帮我想办法克服这个限制并与所有私人/受保护成员一起生成文档吗? Thanks in advance.提前致谢。

Pdoc only extracts public API members (not prefixed with an underscore) by convention. Pdoc 仅按惯例提取公共 API 成员(不带下划线前缀)。

You may be able to override this behavior, on a module level, by defining __all__ , or, more generally, by specifying overrides in the __pdoc__ dict , either manually for a few members, or automatically yet hackish, somewhat like:您可以在模块级别通过定义__all__或更一般地通过在__pdoc__ dict中指定覆盖来覆盖此行为,可以手动为一些成员,也可以自动但有点骇人听闻,有点像:

# Specified at the end of each module that contains private
# classes/methods that need to be exposed
__pdoc__ = {name: True
            for name, klass in globals().items()
            if name.startswith('_') and isinstance(klass, type)}
__pdoc__.update({f'{name}.{member}': True
                 for name, klass in globals().items()
                 if isinstance(klass, type)
                 for member in klass.__dict__.keys()
                 if member not in {'__module__', '__dict__', 
                                   '__weakref__', '__doc__'}})

Alternatively, you should just rename your members if they are part of your public API.或者,如果您的成员是您的公共 API 的一部分,您应该只重命名他们。

Also note, Python by default defines dozens of dunder members on objects, most of which have standard meaning or are internal:另请注意,Python 默认在对象上定义了几十个 dunder 成员,其中大多数具有标准含义或内部含义:

>>> class Cls:
...     pass

>>> dir(Cls)
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM