9.9 Python 文档字符串. 进入 Python 标准库所在的目录. 检查每个 .py 文件看是否有__doc__ 字符串, 如果有, 对其格式进行适当的整理归类. 你的程序执行完毕后, 应该会生成一个漂亮的清单. 里边列出哪些模块有文档字符串, 以及文档字符串的内容. 清单最后附上那些没有文档字符串模块的名字.

import os
#import pdb
path2search = '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7'
modules2check = []
def findPythonFiles(path2search):
for eachItem in os.listdir(path2search):
pathOfEachItem = os.path.join(path2search, eachItem)
if os.path.isdir(pathOfEachItem):
p = os.path.join(path2search, eachItem)
#pdb.set_trace()
findPythonFiles(pathOfEachItem)
else:
if os.path.splitext(eachItem)[1] == '.py':
modules2check.append(os.path.join(path2search,eachItem))
return modules2check
if __name__ == '__main__':
tempList = []
modules2check = findPythonFiles(path2search)
for eachPyModule in modules2check:
f = open(eachPyModule)
isDoc = False
for eachLine in f:
# the 4 if or elif below can't be reordered.
# check __doc__ like r"""sdfdsf"""
if (eachLine.startswith('r"""') or eachLine.startswith('"""')) and len(eachLine) > 5 and eachLine.rstrip().endswith('"""'):
tempList.append(eachLine)
break
# check the 1st line of __doc__ like r"""sdfsdf
elif (eachLine.startswith('r"""') or eachLine.startswith('"""')) and isDoc == False:
isDoc = True
tempList.append(eachLine)
# check the last line of __doc__ like sdfsdf"""
elif eachLine.rstrip().endswith('"""') and isDoc == True:
tempList.append(eachLine)
isDoc = False
break
# check content within r""" and """"
elif not (eachLine.startswith('r"""') or eachLine.startswith('"""')) and isDoc == True:
tempList.append(eachLine)
# write name of module that doesn't have __doc__ into file hasnodoc.txt
else:
f2 = open("hasnodoc.txt","a+")
f2.write('Name: ' + eachPyModule + '\n\n')
f2.close()
tempList = []
# write name and content of module that has __doc__ into file hasdoc.txt
if tempList != []:
f1 = open("hasdoc.txt","a+")
f1.write('Name: ' + eachPyModule + '\n\n')
f1.writelines(tempList)
f1.close()
tempList = []

  

9.9 Python 文档字符串的更多相关文章

  1. 第8.19节 使用__doc__访问Python文档字符串(DocStrings )

    __doc__特殊变量用于查看类.函数.模块的帮助信息,这些帮助信息存放在文档字符串中. 一. 关于文档字符串 关于文档字符串前面很多章节提到过,DocStrings 文档字符串用于程序的文档说明,并 ...

  2. python文档字符串(函数使用说明)

    关键字: 函数说明.help()函数 1.效果图: 2.代码: # 文档字符串( doc str) 是 函数使用说明 # 用法: 在函数第一行写一个字符串 def fn(*nums): ''' 函数的 ...

  3. python文档字符串

    #coding=utf-8 #文档字符串def d(i,j): """这个函数实现了一个乘法运算. 函数会返回一个乘法运算的结果.""" k ...

  4. [python]文档字符串

    文档字符串可以在运行时访问,也可以用来自动生成文档. 输入: def foo(): print "This is a doc string" return True foo() 运 ...

  5. Python常用函数--文档字符串DocStrings

    Python 有一个甚是优美的功能称作python文档字符串(Documentation Strings),在称呼它时通常会使用另一个短一些的名字docstrings.DocStrings 是一款你应 ...

  6. Python中的文档字符串作用

    文档字符串是使用一对三个单引号 ''' 或者一对三个双引号 """来包围且没有赋值给变量的一段文字说明(如果是单行且本身不含引号,也可以是单引号和双引号), 它在代码执行 ...

  7. Python中定义文档字符串__doc__需要注意格式对齐的处理

    Python中的文档字符串是个很不错的提升代码交付质量.编写文档方便的特征,但是需要注意在使用文档字符串时,将文档字符串标识的引号对必须遵守缩进的规则,否则Python语法检查时会无法通过,而引号内的 ...

  8. Python基础(2):__doc__、文档字符串docString、help()

    OS:Windows 10家庭中文版,Python:3.6.4 Python中的 文档字符串(docString) 出现在 模块.函数.类 的第一行,用于对这些程序进行说明.它在执行的时候被忽略,但会 ...

  9. python 函数的文档字符串 docstrings

    函数体的第一行可以是一个可选的字符串文本:此字符串是该函数的文档字符串,或称为docstring.(更多关于 docstrings 的内容可以在 文档字符串一节中找到.)有工具使用 docstring ...

随机推荐

  1. React Native之持久化存储(AsyncStorage、react-native-storage)的使用

    AsyncStorage是一个简单的.异步的.持久化的Key-Value存储系统,它对于App来说是全局性的.这是官网上对它的介绍.可以知道,这个asyncstorage也是以键值对的形式进行存储数据 ...

  2. 02-JZ2440裸机学习之MMU内存管理单元【转】

    本文转载自:https://blog.csdn.net/fengyuwuzu0519/article/details/66479248 1.MMU定义: MMU是Memory Management U ...

  3. SSD: Single Shot MultiBox Detector 编译方法总结

    SSD是一个基于单网络的目标检测框架,它是基于caffe实现的,所以下面的教程是基于已经编译好的caffe进行编译的. caffe的编译可以参考官网 caffe Installation Instal ...

  4. LeetCode——Diameter of Binary Tree

    LeetCode--Diameter of Binary Tree Question Given a binary tree, you need to compute the length of th ...

  5. 高亮显示UILabel中的子串

    I. 用户在搜索框中,输入关键字进行检索时,APP对搜索结果进行显示,有以下两种情况: 1. 匹配一次,如检索关键字为人名 这种情况,实现比较容易.写一个UILabel的category, 用rang ...

  6. thinkphp Composer安装指南

    1.首先我们去composer的官网下载composer,当然也可以用命令行的形势下下载,我是在windows安装的.https://www.phpcomposer.com/ 2.下载以后进行安装,一 ...

  7. XMLHTTP.readyState

    XMLHTTP.readyState的五种就绪状态: 0:请求未初始化(还没有调用 open()). 1:请求已经建立,但是还没有发送(还没有调用 send()). 2:请求已发送,正在处理中(通常现 ...

  8. Red Hat OpenStack 10的新特性

    这是Red Hat有史以来最好的版本,同时也是第一个长生命周期版本(最长五年支持),这篇文章会介绍为什么这是你私有云最好的礼物. 由于要使用命令行,以前安装OpenStack是很繁重的工作.这个版本提 ...

  9. 远程调用之RMI、Hessian、Burlap、Httpinvoker、WebService的比较

    一.综述 本文比较了RMI.Hessian.Burlap.Httpinvoker.WebService5这种通讯协议的在不同的数据结构和不同数据量时的传输性能. RMI是java语言本身提供的远程通讯 ...

  10. 地图Legend控件的使用

    http://support.esrichina-bj.cn/2011/0318/1150.html