zoukankan      html  css  js  c++  java
  • 9.9 Python 文档字符串

    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 + '
    
    ')
                           	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 + '
    
    ')
    			f1.writelines(tempList)
    			f1.close()
    			tempList = []
    

      

  • 相关阅读:
    浏览器渲染页面
    递归求1-100之和
    border属性
    ES6 Class
    数组去重
    get、post请求
    对象冒充继承
    原型链继承
    实现JS数据拷贝
    【转】centos升级curl版本
  • 原文地址:https://www.cnblogs.com/iNeoWong/p/4763830.html
Copyright © 2011-2022 走看看