41.html ( File view )
- By 2010-08-21
- View(s):12
- Download(s):0
- Point(s): 1
Safari | Python Developer's Handbook -> Python Services
See All Titles |
![]() ![]() Python ServicesThis first group of modules is known as Python Services. These modules provide access to services related to the interpreter and to Python's environment. sysThe sys module handles system-specific parameters, variables, and functions related to the interpreter. sys.argvThis object contains the list of arguments that were passed to a program. If you pass arguments to your program, for example, by saying, c:\ python program.py -a -h -c you are able to access those arguments by retrieving the value of sys.argv: >>> import sys >>> sys.argv ["program.py", "-a", "-h", "-c"] You can use this list to check whether certain parameters are transported to the interpreter. >>> If "-h" in sys.argv: >>> print "Sorry. There is no help available." sys.exit()This is a function used to exit a program. Optionally, it can have a return code. It works by raising the SystemExit exception. If the exception remains uncaught while going up the call stack, the interpreter shuts down. basic syntax: sys.exit([return_code]) >>> import sys >>> sys.exit(0) The return_code argument indicates the return code that should be passed back to the caller application. The sys module also contains three file objects that take care of the standard input and output devices (see Chapter 1, "Introduction," for more details about these objects).
Example: >>> import sys >>> data = sys.stdin.readlines() >>> str = "Counted %d lines." % len(data) >>> sys.stdout.write (str) Now, save the previous example in a file named countlines.py, and test it by typing the following instructions on your prompt: On Unix: cat coutlines.py | python countlines.py On DOS and Windows: type countlines.py | python countlines.py sys.modulesIt is a dictionary that contains the modules that were loaded by the current session. sys.platformsThis is a string that shows the current platform (for example, "win32", "mac", "linux-i386"). You can test which platform is running a program by doing something like this: if sys.platforms == "win32" <do something> elif sys.platform == "mac" <do something else> sys.pathThis is the list of directories that are searched to find the location of a module at the time of importing it. >>> import.sys >>> sys.path ['', 'C:\\Program Files\\Python\\Lib\\plat-win', 'C:\\Program Files\\Python\\Lib', 'C:\\Program Files\\Python\\DLLs', 'C:\\Program Files\\Python\\Lib\\lib-tk','C:\\PROGRAM FILES\\PYTHON\\DLLs', 'C:\\PROGRAM FILES\\PYTHON\\lib', 'C:\\PROGRAM FILES\\PYTHON\\lib\\plat-win', 'C:\\PROGRAM FILES\\PYTHON\\lib\\lib-tk', 'C:\\PROGRAM FILES\\PYTHON'] You can easily update this list to include your own directories. sys.builtin_module_namesThis is the list of modules that are not imported as files. >>> import sys >>> sys.builtin_module_names ('__builtin__', '__main__', '_locale', '_socket', 'array', 'audioop', 'binascii', 'cPickle', 'cStringIO', 'cmath', 'errno', 'imageop', 'imp', 'marshal', 'math', 'md5', 'msvcrt', 'new', 'nt', 'operator', 'pcre', 'regex', 'rgbimg', 'rotor', 'select', 'sha', 'signal', 'soundex', 'strop', 'struct', 'sys', 'thread', 'time', 'winsound') For all the next sys objects, see Chapter 4, "Exception Handling," for details. sys.exc_info()Provides information about the current exception being handled. sys.exc_type, sys.exc_value, sys.exc_tracebackIt is another way to get the information about the current exception being handled. sys.last_type, sys.last_value and sys.last_tracebackProvides information about the last uncaught exception. Python 2.0 contains a mode detailed version information function called sys.version_info. This function returns a tuple in the format (major, minor, micro, level, serial). For example, suppose the version number of your Python system is 3.0.4alpha1, the function sys.version_info() returns (3, 0, 4, 'alpha', 1). Note that the level can be one of the following values: alpha, beta, or final. Another set of functions added to Python 2.0 are: sys.getrecursionlimit() and sys.setrecursionlimit(). These functions are responsible for reading and modifing the maximum recursion depth for the routines in the system. The default value is 1000, and you can run the new script Misc/find_recursionlimit.py in order to know the maximum value suggested for your platform.
Sponsored links
Need
1 point
Your Point(s)
Your Point isn't enough. Get point immediately by PayPal More(Debit card / Credit card / PayPal Credit / Online Banking) |