Python 编程核心 - Chapter9练习

# 9-8

mn = raw_input('Please Input Module Name:')
module = __import__(mn)
attr_list = dir(module)
# print attr_list

for obj in attr_list:
    print 'Name:', obj
    print 'Type:', type(getattr(module,obj))
    print 'Value:', getattr(module,obj)
    print 'Doc:', getattr(module, obj)
    print

<pre name="code" class="python"># 9-9
import os

PATH = r'C:\Python27\Lib'

def has_doc(file_name):
    """
    # check if the file of the module has doc string
    :param file_name: file name of the module, '*.py'
    :return: None-> No Doc string, DocString -> Has Doc String
    """
    f_path = PATH + os.sep + file_name
    fh = open(f_path)
    content = fh.read()
    fh.seek(0)
    doc = ''
    if content.startswith('"""'):
        for eachLine in fh:
            doc += eachLine
            if eachLine.endswith('"""\n'):
                break
        return doc
    else:
        return None

list_1 = []
list_2 = []
for file in os.listdir(PATH):
    # ignore all file other than *.py
    if not file.endswith('.py'):
        continue
    doc = has_doc(file)
    # print doc
    if doc is None:
        list_2.append(file)
    else:
        list_1.append((file, doc))
    

# print the result
for (file_name, doc_str) in list_1:
    print """************************************\n%s:\n************************************\n%s\n\n""" % (file_name, doc_str)

for file_name in list_2:
    print """************************************\n%s:\n************************************\n""" % file_name



9-10 没学GUI,暂缓


9-11 


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值