Section 14.10. Exercises


14.10. Exercises

14-1.

Callable Objects. Name Python's callable objects.exec versus eval(). What is the difference between the exec statement and the eval() BIF?

14-2.

input() versus raw.input(). What is the difference between the BIFs input() and raw_input()?

14-3.

Execution Environment. Create a Python script that runs other Python scripts.

14-4.

os.system(). Choose a familiar system command that performs a task without requiring input and either outputs to the screen or does not output at all. Use the os.system() call to run that program. Extra credit: Port your solution to subprocess.call().

14-5.

commands.getoutput(). Solve the previous problem using commands.getoutput().

14-6.

popen() Family. Choose another familiar system command that takes text from standard input and manipulates or otherwise outputs the data. Use os.popen() to communicate with this program. Where does the output go? Try using popen2.popen2() instead.

14-7.

subprocess Module. Take your solutions from the previous problem and port them to the subprocess module.

14-8.

Exit Function. Design a function to be called when your program exits. Install it as sys.exitfunc(), run your program, and show that your exit function was indeed called.

14-9.

Shells. Create a shell (operating system interface) program. Present a command-line interface that accepts operating system commands for execution (any platform).

Extra credit 1: Support pipes (see the dup(), dup2(), and pipe() functions in the os module). This piping procedure allows the standard output of one process to be connected to the standard input of another.

Extra credit 2: Support inverse pipes using parentheses, giving your shell a functional programming-like interface. In other words, instead of piping commands like ...

ps -ef | grep root | sort -n +1


... support a more functional style like...

sort(grep(ps -ef, root), -n, +1)


14-10.

fork()/exec*() versus spawn*(). What is the difference between using the fork()-exec*() pairs vs. the spawn*() family of functions? Do you get more with one over the other?

14-11.

Generating and Executing Python Code. Take the funcAttrs.py script (Example 14.4) and use it to add testing code to functions that you have in some of your existing programs. Build a testing framework that runs your test code every time it encounters your special function attributes.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net