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 文档字符串的更多相关文章

  1. 第8.19节 使用__doc__访问Python文档字符串(DocStrings )

    __doc__特殊变量用于查看类.函数.模块的帮助信息,这些帮助信息存放在文档字符串中. 一. 关于文档字符串 关于文档字符串前面很多章节提到过,DocStrings 文档字符串用于程序的文档说明,并 ...

  2. python文档字符串(函数使用说明)

    关键字: 函数说明.help()函数 1.效果图: 2.代码: # 文档字符串( doc str) 是 函数使用说明 # 用法: 在函数第一行写一个字符串 def fn(*nums): ''' 函数的 ...

  3. python文档字符串

    #coding=utf-8 #文档字符串def d(i,j): """这个函数实现了一个乘法运算. 函数会返回一个乘法运算的结果.""" k ...

  4. [python]文档字符串

    文档字符串可以在运行时访问,也可以用来自动生成文档. 输入: def foo(): print "This is a doc string" return True foo() 运 ...

  5. Python常用函数--文档字符串DocStrings

    Python 有一个甚是优美的功能称作python文档字符串(Documentation Strings),在称呼它时通常会使用另一个短一些的名字docstrings.DocStrings 是一款你应 ...

  6. Python中的文档字符串作用

    文档字符串是使用一对三个单引号 ''' 或者一对三个双引号 """来包围且没有赋值给变量的一段文字说明(如果是单行且本身不含引号,也可以是单引号和双引号), 它在代码执行 ...

  7. Python中定义文档字符串__doc__需要注意格式对齐的处理

    Python中的文档字符串是个很不错的提升代码交付质量.编写文档方便的特征,但是需要注意在使用文档字符串时,将文档字符串标识的引号对必须遵守缩进的规则,否则Python语法检查时会无法通过,而引号内的 ...

  8. Python基础(2):__doc__、文档字符串docString、help()

    OS:Windows 10家庭中文版,Python:3.6.4 Python中的 文档字符串(docString) 出现在 模块.函数.类 的第一行,用于对这些程序进行说明.它在执行的时候被忽略,但会 ...

  9. python 函数的文档字符串 docstrings

    函数体的第一行可以是一个可选的字符串文本:此字符串是该函数的文档字符串,或称为docstring.(更多关于 docstrings 的内容可以在 文档字符串一节中找到.)有工具使用 docstring ...

随机推荐

  1. Asp.Net中OnClientClick与OnClick的区别

    当我们当击这个按钮时,自动先执行的客户端,再执行服务器端的.如果客户端返回的是false,那么服务器端对应的方法永远不会执行.这样就达到检测,只有通过才去执行服务器端的方法.

  2. 【Java】流与文件(端口 & 文件读写对象)

    概述: 1.input和output是相对于内存而言的.输入(input)就是写入到内存里,输出(output)就是把内存里的东西写到外面. 2.操作内存里的东西非常便利,要么声明变量,要么new对象 ...

  3. ping主机脚本

    #!/bin/bash #ping net='172.16.1' uphosts=0 downhosts=0 for i in {1..254};do ping -c 1 -w 1 ${net}.${ ...

  4. 《React-Native系列》3、RN与native交互之Callback、Promise

    接着上一篇<React-Native系列>RN与native交互与数据传递,我们接下来研究另外的两种RN与Native交互的机制 一.Callback机制 首先Calllback是异步的, ...

  5. 华为S5700系列交换机使用高级ACL限制不同网段的用户互访

    组网图形 图1 使用高级ACL限制不同网段的用户互访示例 组网需求 如图一所示,某公司通过Switch实现各部门之间的互连.为方便管理网络,管理员为公司的研发部和市场部规划了两个网段的IP地址.同时为 ...

  6. zabbix 邮件报警

    一.设置sendmail 安装sendmail yum install -y sendmail systemctl start sendmail.service 配置 cd  /usr/lib/zab ...

  7. MyEclipse 为xml添加本地的dtd文件

    在使用Eclipse或MyEclipse编辑XML文件的时候经常会碰到编辑器不提示的现象,这常常是因为其xml文件需要参考的DTD文件找不到,还有因为网络的问题不能及时提示而产生的.Eclipse/M ...

  8. 不常用的gcd公式

    gcd(a^m-b^m,a^n-b^n)=a^(gcd(m,n))-b^(gcd(m,n))

  9. 内存中加载DLL DELPHI版

    //从内存中加载DLL DELPHI版 unit MemLibrary; interface uses Windows; function memLoadLibrary(pLib: Pointer): ...

  10. mysql应该看的blog

    一个比较系统的学习mysql的网站:http://www.runoob.com/mysql/mysql-data-types.html