9.9 Python Document String

Source: Internet
Author: User

9.9 Python Document String. Enter the directory where the Python standard library resides. Check each. py file to see if there is a __doc__ string, and if so, make a proper collation of its format. After your program has finished executing, you should generate a nice list. It lists which modules have the document string, and the contents of the document string. The list is appended with the name of the module without the document string.

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):p Athofeachitem = 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 = FalseFor EachLine in f:# the 4 If or elif below can ' t be reordered.# check __doc__ like R "" "SDFDSF" "" if (Eachline.startswith (' R "" "') or EACHLINE.STARTSW ITH (' "" ') ") and Len (Eachline) > 5 and Eachline.rstrip (). EndsWith ('" ""): Templist.append (eachline) break# Check the 1st line of __doc__ like R "" "Sdfsdfelif (Eachline.startswith (' R" "" ') or Eachline.startswith ('"" ")) and Isdoc = = False:isdoc = Truetemplist.append (eachline) # Check the last line of __doc__ like Sdfsdf" "" Elif Eachlin E.rstrip (). EndsWith (' "" "') and Isdoc = = True:tempList.append (eachline) Isdoc = falsebreak# 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 has __doc__ into file Hasnodoc.txtelse:f2 = open ("Hasnodoc.txt", "A +") F2.write (' Name: ' + eachpymodule + ' \ n ') f2.close () templist = []# writ e name and content of module that have __doc__ into file hasdoc.txtif templist! = []:F1 = open ("Hasdoc.txt", "A +") f1.write (' Name: ' + eachpymodule + ' \ n ') f1.writelines (templist) f1.close () templist = []

  

9.9 Python Document String

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.