博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
9.9 Python 文档字符串
阅读量:7002 次
发布时间:2019-06-27

本文共 2140 字,大约阅读时间需要 7 分钟。

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

 

import os#import pdbpath2search = '/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 modules2checkif __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 = []

  

转载于:https://www.cnblogs.com/iNeoWong/p/4763830.html

你可能感兴趣的文章
easyui实现datagrid列标题拖动
查看>>
CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境
查看>>
在Websphere上修改项目的web.xml中的配置后不起作用
查看>>
JAVA 数据计算、取整、+1、四舍五入
查看>>
wshell修改了upload功能,増加显示图片功能
查看>>
ERP中标准成本的差异分析控制
查看>>
linux 中断的上半部和下半部
查看>>
单例模式的七种写法
查看>>
好用到吐血!APP设计利器Sketch
查看>>
Android TensorFlow环境搭建
查看>>
【细品架构1/100】架构之缘起
查看>>
在js中获取后台传出的json数据
查看>>
Drools的JSR94实现形式
查看>>
oracle的nvl和nvl2
查看>>
hdfs 写orc
查看>>
1.9 xz压缩和解压缩
查看>>
IDEA如何自动提示并补全syso和main呢?
查看>>
9.数组和向量
查看>>
JXL读写Excel
查看>>
mysql自定义排序
查看>>