stdlib.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. [[stdlib]]
  2. == Standard Library
  3. The Python Standard Library contains a huge number of useful modules and is part of every standard
  4. Python installation. It is important to become familiar with the Python Standard Library since many
  5. problems can be solved quickly if you are familiar with the range of things that these libraries
  6. can do.
  7. We will explore some of the commonly used modules in this library. You can find complete details
  8. for all of the modules in the Python Standard Library in the
  9. http://docs.python.org/2/library/['Library Reference' section] of the documentation that comes with
  10. your Python installation.
  11. Let us explore a few useful modules.
  12. CAUTION: If you find the topics in this chapter too advanced, you may skip this chapter. However, I
  13. highly recommend coming back to this chapter when you are more comfortable with programming using
  14. Python.
  15. === +sys+ module
  16. The `sys` module contains system-specific functionality. We have already seen that the `sys.argv`
  17. list contains the command-line arguments.
  18. Suppose we want to check the version of the Python software being used, the `sys` module gives us
  19. that information.
  20. --------------------------------------------------
  21. $ python
  22. >>> import sys
  23. >>> sys.version_info
  24. sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
  25. >>> sys.version_info.major == 2
  26. True
  27. --------------------------------------------------
  28. .How It Works
  29. The `sys` module has a `version_info` tuple that gives us the version information. The first entry
  30. is the major version. We can pull out this information to use it.
  31. [[logging]]
  32. === logging module
  33. What if you wanted to have some debugging messages or important messages to be stored somewhere so
  34. that you can check whether your program has been running as you would expect it? How do you "store
  35. somewhere" these messages? This can be achieved using the `logging` module.
  36. Save as `stdlib_logging.py`:
  37. [source,python]
  38. --------------------------------------------------
  39. include::programs/stdlib_logging.py[]
  40. --------------------------------------------------
  41. Output:
  42. --------------------------------------------------
  43. include::programs/stdlib_logging.txt[]
  44. --------------------------------------------------
  45. If you do not have the `cat` command, then you can just open the `test.log` file in a text editor.
  46. .How It Works
  47. We use three modules from the standard library - the `os` module for interacting with the operating
  48. system, the `platform` module for information about the platform i.e. the operating system and the
  49. `logging` module to *log* information.
  50. First, we check which operating system we are using by checking the string returned by
  51. `platform.platform()` (for more information, see `import platform; help(platform)`). If it is
  52. Windows, we figure out the home drive, the home folder and the filename where we want to store the
  53. information. Putting these three parts together, we get the full location of the file. For other
  54. platforms, we need to know just the home folder of the user and we get the full location of the
  55. file.
  56. We use the `os.path.join()` function to put these three parts of the location together. The reason
  57. to use a special function rather than just adding the strings together is because this function
  58. will ensure the full location matches the format expected by the operating system.
  59. We configure the `logging` module to write all the messages in a particular format to the file we
  60. have specified.
  61. Finally, we can put messages that are either meant for debugging, information, warning or even
  62. critical messages. Once the program has run, we can check this file and we will know what happened
  63. in the program, even though no information was displayed to the user running the program.
  64. [[motw]]
  65. === Module of the Week Series
  66. There is much more to be explored in the standard library such as
  67. http://docs.python.org/2/library/pdb.html[debugging],
  68. http://docs.python.org/2/library/argparse.html[handling command line options],
  69. http://docs.python.org/2/library/re.html[regular expressions] and so
  70. on.
  71. The best way to further explore the standard library is to read Doug Hellmann's excellent
  72. http://pymotw.com/2/contents.html[Python Module of the Week] series (also available as a
  73. http://amzn.com/0321767349[book]) and reading the http://docs.python.org/2/[Python documentation].
  74. === Summary
  75. We have explored some of the functionality of many modules in the Python Standard Library. It is
  76. highly recommended to browse through the http://docs.python.org/2/library/[Python Standard Library
  77. documentation] to get an idea of all the modules that are available.
  78. Next, we will cover various aspects of Python that will make our tour of Python more _complete_.