这是自己做的练习,可能有错误,欢迎讨论和各种优化重构方案。
根据反馈,或者code review,对本篇文章答案或者相关内容的更新补充,一般会被添加在本篇博客的评论中。
将尽量保证每题的答案代码是完整的,不仅仅是函数,类或者只是个大概,力求打开Python 2.7的IDLE,将代码完整拷贝进去,就能调试运行。
欢迎访问Balian在博客园的家。 http://www.cnblogs.com/balian

【这篇博文可能让人失望,对不起】

14-9.
Shells。创建shell(操作系统接口)程序。给出接受操作系统命令的命令行接口(任意平台)。
附加题1:支持管道(见os模块中的dup(),dup2()和pipe()函数)。管道过程允许进程的标准输入连接到另一个进程的标准输入。
附加题2:用括号支持逆序的管道,给shell一个函数式编程接口。换句话说,支持更加函数式风格,如sort(grep(ps (-ef), root), -n, +1),而不是ps -ef | grep root | sort -n +1这样的命令。
【未完】
感觉有难度,暂时押后。

14-10.
fork()/exec*()和spawn*()的比较。使用fork()-exec*()对和spawn*()家族函数有什么不同?那一组的功能更强?
【未完】
感觉有难度,暂时押后。但可以参考书第429页表14.6以及
http://blog.csdn.net/small_tina/article/details/8071791

14-11.
生成和执行Python代码。用funcAttrs.py脚本(例14.4-实际应该是14.2,书第425页)加入测试代码到已有程序的函数中。创建一个测试框架,每次遇到你特殊的函数属性,它都会运行你的测试代码。
【未完】
感觉有难度,暂时押后。funcAttrs.py脚本代码如下:

def foo():
    return True

def bar():
    'bar() does not do much'
    return True

foo.__doc__ = 'foo() does not do much'

foo.tester='''
if foo():
    print 'PASSED'
else:
    print 'FAILED'
'''

for eachAttr in dir():
    obj = eval(eachAttr)
    if isinstance(obj, type(foo)):
        if hasattr(obj, '__doc__'):
            print '\nfunction "%s" has a doc string:\n\t%s' % (eachAttr, obj.__doc__)
        if hasattr(obj, 'tester'):
            print 'function "%s" has a tester... executing' % eachAttr
            exec obj.tester
        else:
            print 'Function "%s" has no tester... skip ping' % eachAttr
    else:
        print '"%s" is not a function' % eachAttr


【执行结果】

"__builtins__" is not a function
"__doc__" is not a function
"__file__" is not a function
"__name__" is not a function
"__package__" is not a function

function "bar" has a doc string:
	bar() does not do much
Function "bar" has no tester... skip ping

function "foo" has a doc string:
	foo() does not do much
function "foo" has a tester... executing
PASSED
posted on 2013-04-10 10:12  balian  阅读(808)  评论(2编辑  收藏  举报