码迷,mamicode.com
首页 > 编程语言 > 详细

9.9 Python 文档字符串

时间:2015-08-27 18:05:56      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

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 文档字符串

标签:

原文地址:http://www.cnblogs.com/iNeoWong/p/4763830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!