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 + '\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 文档字符串的更多相关文章
- 第8.19节 使用__doc__访问Python文档字符串(DocStrings )
__doc__特殊变量用于查看类.函数.模块的帮助信息,这些帮助信息存放在文档字符串中. 一. 关于文档字符串 关于文档字符串前面很多章节提到过,DocStrings 文档字符串用于程序的文档说明,并 ...
- python文档字符串(函数使用说明)
关键字: 函数说明.help()函数 1.效果图: 2.代码: # 文档字符串( doc str) 是 函数使用说明 # 用法: 在函数第一行写一个字符串 def fn(*nums): ''' 函数的 ...
- python文档字符串
#coding=utf-8 #文档字符串def d(i,j): """这个函数实现了一个乘法运算. 函数会返回一个乘法运算的结果.""" k ...
- [python]文档字符串
文档字符串可以在运行时访问,也可以用来自动生成文档. 输入: def foo(): print "This is a doc string" return True foo() 运 ...
- Python常用函数--文档字符串DocStrings
Python 有一个甚是优美的功能称作python文档字符串(Documentation Strings),在称呼它时通常会使用另一个短一些的名字docstrings.DocStrings 是一款你应 ...
- Python中的文档字符串作用
文档字符串是使用一对三个单引号 ''' 或者一对三个双引号 """来包围且没有赋值给变量的一段文字说明(如果是单行且本身不含引号,也可以是单引号和双引号), 它在代码执行 ...
- Python中定义文档字符串__doc__需要注意格式对齐的处理
Python中的文档字符串是个很不错的提升代码交付质量.编写文档方便的特征,但是需要注意在使用文档字符串时,将文档字符串标识的引号对必须遵守缩进的规则,否则Python语法检查时会无法通过,而引号内的 ...
- Python基础(2):__doc__、文档字符串docString、help()
OS:Windows 10家庭中文版,Python:3.6.4 Python中的 文档字符串(docString) 出现在 模块.函数.类 的第一行,用于对这些程序进行说明.它在执行的时候被忽略,但会 ...
- python 函数的文档字符串 docstrings
函数体的第一行可以是一个可选的字符串文本:此字符串是该函数的文档字符串,或称为docstring.(更多关于 docstrings 的内容可以在 文档字符串一节中找到.)有工具使用 docstring ...
随机推荐
- 【leetcode刷题笔记】Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- JDK1.8之Stream
为什么需要 Stream Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 ...
- 20145201 实验四 Andoid开发基础
20145201 实验四 Andoid开发基础 AndroidStudio安装 首先设置环境变量: Windows环境下Android Studio v1.0安装: 安装完毕. 运行Andriod S ...
- JVM调优总结(一)
数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本身, ...
- 安装MySQL 5.6
记录安装mysql 5.6的全过程 下载安装包(尝试过使用mysql的yum源去安装--如果你的网络够好的话...) 注:我的系统是Centos 7.2的 如下,根据自己的需求去下载 CentOS L ...
- IOS 出现错误 :Reason: image not found
把Build Phases 里HyphenateLite.framework后边的选项修改成为Optional就可以了 dyld 后面有提示 HyphenateLite.framework
- [BZOJ1018]堵塞的交通traffic
Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个2行C列的矩形网格,网格上的每个点代表一个城市,相邻的城市之间有一 ...
- Javascript -- 示例:多选下拉选框
1. 示例:多选下拉选框 <html> <head> <meta http-equiv="Content-Type" content="te ...
- Spring scope解惑
在2.0之前只有两种singleton和prototype(网上说的,没去验证),后面增加了session.request.global session三种专门用于web应用程序上下文的Bean Si ...
- 使用SpringMVC报错 Error creating bean with name 'conversionService' defined in class path resource [springmvc.xml]
使用SpringMVC报错 Error creating bean with name 'conversionService' defined in class path resource [spri ...