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 ...
随机推荐
- 2018 Multi-University Training Contest 1 - D Distinct Values (STL+双指针)
题意:数量为N的序列,给定M个区间,要求对每个区间Li,Ri,都有al..r (l≤i<j≤r), ai≠aj.构造这个序列使其字典序最小. 分析:如果对于每个所给区间都暴力扫一遍,1e5的数据 ...
- Unity,android和IOS 防止八门神器注入
八门神器主要是不断筛选,来获取关键属性(比如金币)在内存中的地址,再根据该地址来修改指向的数据就可以成功. 因此,我们需要在金币读取和设置的时候,使用一个偏移量,来达到干扰的目的就可以了 未经仔细测试 ...
- module.exports和exports得区别
对module.exports和exports的一些理解 可能是有史以来最简单通俗易懂的有关Module.exports和exports区别的文章了. exports = module.exports ...
- websocket redis实现集群即时消息聊天
websocket与redismq实现集群消息聊天 1.application.properties server.port=8081 #thymeleaf配置 #是否启用模板缓存. spring.t ...
- hadoop22---wait,notify
vv wait和notify,是要加syschronized的,是要获取锁的,wait是释放控制权,别的线程就可以执行了,notify和notifyall是通知其他线程执行.
- 编写Tesseract的Python扩展
Tesseract是一个开源的OCR(光学字符识别)引擎,用于识别并输出图片中的文字.虽然和商业软件比起来识别精度不算很高,但是如果你要寻找免费开源的OCR引擎,可能Tesseract就是唯一的选择了 ...
- Tomcat Connector 参数优化说明
默认参数 注: Connector 通常在%HOME_TOMCAT%/conf/servser.xml 文件内 # 正常参数 <Connector port=" protocol=&q ...
- 20145235李涛《网络对抗》Exp9 Web安全基础实践
基础问答 SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意SQL命令的目的. 对于SQL注入攻击的防范 ...
- mysql分库分表(一)
mysql分库分表 参考: https://blog.csdn.net/xlgen157387/article/details/53976153 https://blog.csdn.net/cleve ...
- JavaScript tips —— target与currentTarget的区别
定义 以下是红宝书的描述 属性/方法 类型 读/写 说明 currentTarget Element 只读 其事件处理程序当前正在处理事件的那个元素 target Element 只读 事件的目标 M ...