简体   繁体   English

如何在python中向用户显示模块和子类的文档

[英]How to show documentation of a module and sub class to a user in python

I am trying to code up a module which has two classes. 我正在尝试编写具有两个类的模块。 First class is called as TextProcessing 第一类称为TextProcessing

class TextProcessing(object):

    """ To carry out text processing 
    """

    def __init__(self,):
        pass

It has various methods in there for pre-processing text. 它具有用于预处理文本的各种方法。

Similary other class is for other data wrangling on pre-processed data. 类似地,其他类用于处理预处理数据的其他数据。

I am saving these two classes in a python file to make it a module. 我将这两个类保存在python文件中以使其成为模块。

Now lets say a user downloads this python module and would now want to run the various methods of each class. 现在,假设用户下载了这个python模块,现在想运行每个类的各种方法。

I wanted to provide some sort of documentation about the module, methods of each class to a user when she imports the module so that she is aware of which function to call and what parameters to pass. 我想为用户在导入模块时向其提供有关该模块,每个类的方法的某种文档,以便她知道要调用的函数以及要传递的参数。

Think of how a scikit learn documentation is on their documentation page. 考虑一下scikit如何在其文档页面上学习文档。

http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfTransformer.html http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfTransformer.html

Even the documentation we get to see when we do 甚至当我们这样做时,我们也会看到文档

help(some_python_module) 

is fine too. 也可以

Issue is I don't have a documentation page like sklearn to show documentation. 问题是我没有像sklearn这样的文档页面来显示文档。 And I wanted a user to know documentation of various methods she can use once she imports the module in python console. 而且我希望用户知道将其导入python控制台后可以使用的各种方法的文档。

Is there a way I can print that documentation info to the console when a user imports the module? 用户导入模块时,有什么方法可以将文档信息打印到控制台?

It can show the doc string of each Class and Method. 它可以显示每个类和方法的文档字符串。

This is a very weird thing to do, but it's definitely possible . 这是一件很奇怪的事情,但是绝对有可能


The easiest thing to do is just to call help . 最简单的方法就是致电help While it's intended to be called from the interactive prompt, there's nothing stopping you from calling it from your own code. 虽然打算从交互式提示中调用它,但没有什么能阻止您从自己的代码中调用它。

Of course you could instead extract the docstrings (they're stored as __doc__ on every module, class, and function), textwrap them yourself, and print them out, but if you're trying to reproduce the same thing help does, that's a lot of work for no real benefit. 当然,您可以提取文档字符串(它们以__doc__形式存储在每个模块,类和函数中),自己进行自动textwrap ,然后将其print输出,但是如果您要复制与help相同的内容,那大量工作没有任何实际好处。


The only tricky bit is that the thing you want to invoke the help system on is "this current module". 唯一棘手的一点是,您要在其上调用帮助系统的是“当前模块”。 How do you refer to that? 您如何指代呢? It's a bit clunky, but you have this current module's name as __name__ , so you can look it up in sys.modules . 这有点笨拙,但是您将当前模块的名称命名为__name__ ,因此可以在sys.modules进行查找。

So: 所以:

"""Helpful module"""

import sys

class Spam:
    """Classy class"""
    def eggs(self):
        "Functional function"
        return 2

help(sys.modules[__name__])

Now, when you import helpful for the first time in a session, it will print out the help . 现在,在会话中首次import helpful时,它将打印出help


Of course that will be pretty odd if someone's trying to run a script that does an import helpful , rather than doing it from an interactive session. 当然,如果有人尝试运行对import helpful的脚本,而不是从交互式会话中进行import helpful ,那将是很奇怪的。 So you may want to only do this in interactive sessions, by checking sys.flags : 因此,您可能只想通过检查sys.flags在交互式会话中执行此sys.flags

if sys.flags.interactive:
    help(sys.modules[__name__])

What if someone does an import otherthing , and that otherthing does an import helpful ? 如果有人做了import otherthing ,而otherthing做的import helpful You'll get the same help, which may be confusing. 您将获得相同的帮助,这可能会造成混淆。

If that's a problem, the only real option I can think of is to check whether the calling frame comes from the top-level script (and that the flags are interactive). 如果这是一个问题,我唯一想到的真正的选择就是检查调用框架是否来自顶级脚本(并且这些标志是交互式的)。 That's pretty hacky, and something you shouldn't even consider unless you really need to, so I'll just direct you to the inspect module and hope you don't need it. 那是很棘手的事情,除非真正需要,否则您甚至都不应该考虑这一点,因此,我将指导您进入inspect模块,并希望您不需要它。

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

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