16-standard-library.pd 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # Standard Library
  2. The Python Standard Library contains a huge number of useful modules and is part of every standard Python installation. It is important to become familiar with the Python Standard Library since many problems can be solved quickly if you are familiar with the range of things that these libraries can do.
  3. We will explore some of the commonly used modules in this library. You can find complete details for all of the modules in the Python Standard Library in the ['Library Reference' section](http://docs.python.org/3.0/library/) of the documentation that comes with your Python installation.
  4. Let us explore a few useful modules.
  5. Note
  6. : If you find the topics in this chapter too advanced, you may skip this chapter. However, I highly recommend coming back to this chapter when you are more comfortable with programming using Python.
  7. ## sys module
  8. The `sys` module contains system-specific functionality. We have already seen that the `sys.argv` list contains the command-line arguments.
  9. Suppose we want to check the version of the Python command being used so that, say, we want to ensure that we are using at least version 3. The `sys` module gives us such functionality.
  10. ~~~
  11. >>> import sys
  12. >>> sys.version_info
  13. (3, 0, 0, 'beta', 2)
  14. >>> sys.version_info[0] >= 3
  15. True
  16. ~~~
  17. How It Works:
  18. The `sys` module has a `version_info` tuple that gives us the version information. The first entry is the major version. We can check this to, for example, ensure the program runs only under Python 3.0:
  19. ~~~python
  20. #!/usr/bin/python
  21. # Filename: versioncheck.py
  22. import sys, warnings
  23. if sys.version_info[0] < 3:
  24. warnings.warn("Need Python 3.0 for this program to run",
  25. RuntimeWarning)
  26. else:
  27. print('Proceed as normal')
  28. ~~~
  29. Output:
  30. ~~~
  31. $ python2.5 versioncheck.py
  32. versioncheck.py:6: RuntimeWarning: Need Python 3.0 for this program to run
  33. RuntimeWarning)
  34. $ python3 versioncheck.py
  35. Proceed as normal
  36. ~~~
  37. How It Works:
  38. We use another module from the standard library called `warnings` that is used to display warnings to the end-user. If the Python version number is not at least 3, we display a corresponding warning.
  39. ## logging module
  40. What if you wanted to have some debugging messages or important messages to be stored somewhere so that you can check whether your program has been running as you would expect it? How do you "store somewhere" these messages? This can be achieved using the `logging` module.
  41. ~~~python
  42. #!/usr/bin/python
  43. # Filename: use_logging.py
  44. import os, platform, logging
  45. if platform.platform().startswith('Windows'):
  46. logging_file = os.path.join(os.getenv('HOMEDRIVE'), os.getenv('HOMEPATH'), 'test.log')
  47. else:
  48. logging_file = os.path.join(os.getenv('HOME'), 'test.log')
  49. print("Logging to", logging_file)
  50. logging.basicConfig(
  51. level=logging.DEBUG,
  52. format='%(asctime)s : %(levelname)s : %(message)s',
  53. filename = logging_file,
  54. filemode = 'w',
  55. )
  56. logging.debug("Start of the program")
  57. logging.info("Doing something")
  58. logging.warning("Dying now")
  59. ~~~
  60. Output:
  61. ~~~
  62. $python use_logging.py
  63. Logging to C:\Users\swaroop\test.log
  64. ~~~
  65. If we check the contents of `test.log`, it will look something like this:
  66. ~~~
  67. 2008-09-03 13:18:16,233 : DEBUG : Start of the program
  68. 2008-09-03 13:18:16,233 : INFO : Doing something
  69. 2008-09-03 13:18:16,233 : WARNING : Dying now
  70. ~~~
  71. How It Works:
  72. We use three modules from the standard library - the `os` module for interacting with the operating system, the `platform` module for information about the platform i.e. the operating system and the `logging` module to *log* information.
  73. First, we check which operating system we are using by checking the string returned by `platform.platform()` (for more information, see `import platform; help(platform)`). If it is Windows, we figure out the home drive, the home folder and the filename where we want to store the information. Putting these three parts together, we get the full location of the file. For other platforms, we need to know just the home folder of the user and we get the full location of the file.
  74. We use the `os.path.join()` function to put these three parts of the location together. The reason to use a special function rather than just adding the strings together is because this function will ensure the full location matches the format expected by the operating system.
  75. We configure the `logging` module to write all the messages in a particular format to the file we have specified.
  76. Finally, we can put messages that are either meant for debugging, information, warning or even critical messages. Once the program has run, we can check this file and we will know what happened in the program, even though no information was displayed to the user running the program.
  77. ## urllib and json modules
  78. How much fun would it be if we could write our own program that will get search results from the web? Let us explore that now.
  79. This can be achieved using a few modules. First is the `urllib` module that we can use to fetch any webpage from the internet. We will make use of Yahoo! Search to get the search results and luckily they can give us the results in a format called JSON which is easy for us to parse because of the built-in `json` module in the standard library.
  80. **TODO** Use some other example that doesn't use YUI API, maybe twitter firehose, etc.
  81. ~~~python
  82. #!/usr/bin/python
  83. # Filename: yahoo_search.py
  84. import sys
  85. if sys.version_info[0] != 3:
  86. sys.exit('This program needs Python 3.0')
  87. import json
  88. import urllib, urllib.parse, urllib.request, urllib.response
  89. # Get your own APP ID at http://developer.yahoo.com/wsregapp/
  90. YAHOO_APP_ID = 'jl22psvV34HELWhdfUJbfDQzlJ2B57KFS_qs4I8D0Wz5U5_yCI1Awv8.lBSfPhwr'
  91. SEARCH_BASE = 'http://search.yahooapis.com/WebSearchService/V1/webSearch'
  92. class YahooSearchError(Exception):
  93. pass
  94. # Taken from http://developer.yahoo.com/python/python-json.html
  95. def search(query, results=20, start=1, **kwargs):
  96. kwargs.update({
  97. 'appid': YAHOO_APP_ID,
  98. 'query': query,
  99. 'results': results,
  100. 'start': start,
  101. 'output': 'json'
  102. })
  103. url = SEARCH_BASE + '?' + urllib.parse.urlencode(kwargs)
  104. result = json.load(urllib.request.urlopen(url))
  105. if 'Error' in result:
  106. raise YahooSearchError(result['Error'])
  107. return result['ResultSet']
  108. query = input('What do you want to search for? ')
  109. for result in search(query)['Result']:
  110. print("{0} : {1}".format(result['Title'], result['Url']))
  111. ~~~
  112. Output:
  113. **TODO**
  114. How It Works:
  115. We can get the search results from a particular website by giving the text we are searching for in a particular format. We have to specify many options which we combine using `key1=value1&key2=value2` format which is handled by the `urllib.parse.urlencode()`> function.
  116. So for example, open [this link in your web browser](http://search.yahooapis.com/WebSearchService/V1/webSearch?query=byte+of+python&appid=jl22psvV34HELWhdfUJbfDQzlJ2B57KFS_qs4I8D0Wz5U5_yCI1Awv8.lBSfPhwr&results=20&start=1&output=json) and you will see 20 results, starting from the first result, for the words "byte of python", and we are asking for the output in JSON format.
  117. We make a connection to this URL using the `urllib.request.urlopen()` function and pass that file handle to `json.load()` which will read the content and simultaneously convert it to a Python object. We then loop through these results and display it to the end-user.
  118. ## Module of the Week Series
  119. There is much more to be explored in the standard library such as [debugging](http://docs.python.org/dev/library/pdb.html), [handling command line options](http://docs.python.org/3.0/library/getopt.html), [regular expressions](http://www.diveintopython.org/regular_expressions/index.html) and so on.
  120. The best way to further explore the standard library is to read Doug Hellmann's excellent [Python Module of the Week](http://www.doughellmann.com/projects/PyMOTW/) series or reading the [Python documentation](http://docs.python.org/py3k/).
  121. ## Summary
  122. We have explored some of the functionality of many modules in the Python Standard Library. It is highly recommended to browse through the [Python Standard Library documentation](http://docs.python.org/py3k/library/index.html) to get an idea of all the modules that are available.
  123. Next, we will cover various aspects of Python that will make our tour of Python more *complete*.