zoukankan      html  css  js  c++  java
  • #小练习 输出模块中方法及其docstring 分类: python 小练习 divide into python 2013-11-05 18:17 451人阅读 评论(0) 收藏

    divide into python 中例子,输出模块的函数及docstring文档内容。

    以下例子是通过调用getdoc模块中的main方法,输出imp_main模块中方法及docstring

    imp_main.py :

    #coding:utf-8
    
    #运行此模块时,调用此函数
    def runself():
        'execute this method when running this module'
        print "Running self to be used"
    
    #此模块被导入时,调用此函数
    def outUse():
        print "When imported to be used"
    
    if __name__ == '__main__':
        runself()
    else:
        outUse()
    
    


    getdoc.py

    #coding:utf-8
    
    import imp_main
    
    def main(module,space=15,collapse=1):
        methodlist = [method for method in dir(module) if callable(getattr(module,method))]
        docFunc = collapse and (lambda s:" ".join(s.split())) or (lambda s:s)
        print '
    '.join(
        "%s --> %s" % (method.ljust(space),docFunc(str(getattr(module,method).__doc__))) for method in methodlist)
    
    
    if __name__ == '__main__':
        main(imp_main)
        print '*'*60
        main([])
    



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    13 数据库主从
    12 数据备份
    11 锁机制
    12 日志
    10 索引(二)
    09 索引
    update kernel 3.10-3.12
    haproxy para config
    mysql slave to master
    storage disk
  • 原文地址:https://www.cnblogs.com/think1988/p/4628030.html
Copyright © 2011-2022 走看看