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. CAS单点登录的配置

    先说单点登录是个啥? 单点登录主要用于多系统集成,即在多个系统中,用户只需要到一个中央服务器登录一次即可访问这些系统中的任何一个,无须多次登录. 配置的步骤如下: 1.生成安全证书 Cas serve ...

  2. @RequestMapping映射请求,@PathVariable,@RequestParam,@RequestHeader的使用

    1.@RequestMapping Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请求,在控制器的类定义及方法定义处都可标注. @RequestMa ...

  3. JDK安装配置教程

    一.首先下载JDK的最新版本.可以去http://java.sun.com/javase/downloads/index.jsp下载最新版本JDK1.6.一切下载后选择安装路径,例如我选择安装在&qu ...

  4. SpringBoot实现文件上传功能

    新建maven项目,pom文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="htt ...

  5. python学习笔记(接口自动化框架 V2.0)

    这个是根据上次框架版本进行的优化 用python获取excel文件中测试用例数据 通过requets测试接口.并使用正则表达式验证响应信息内容 生成xml文件测试报告 版本更新内容: 1. 整理了Cr ...

  6. 自行实现Kinect 手势Demo踩的坑

    要将继承KinectGestures.GestureListenerInterface的脚本手动赋值给KinectManager脚本的手势监听列表

  7. sublime 常见问题

    问题一: There Are No Packages Available For Installation 原因:官方提供的Package Control不能用.将官方的那个Package Contr ...

  8. Ajax-07 基于Ajax实现跨域请求

    跨域 跨域名访问,如c1.com域名向c2.com域名发送请求 本质:浏览器存在同源策略机制,同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性. django服务端准备 url. ...

  9. Sunday算法--C#版

    public static int Sunday(string text, string pattern) { int i, j, m, k; i = j = 0; int tl, pl; int p ...

  10. eclipse启动Tomcat服务输入http://localhost:8080/报404解决方法

    其实如果Tomcat能够正常启动,而就算输入http://localhost:8080时出现404错误,也不会影响Tomcat作为服务器运行.通过eclipse来启动tomcat会碰到“访问http: ...