Considering the Standard Library Feature

0
305

 

The Python Standard Library is the centerpiece of any IronPython you want to create of any complexity. If you want to ensure that your IronPython application will run on other platforms using other Python interpreters, then you need to stick with the functionality that the Standard Library provides and avoid the temptation to use .NET features in your application. Of course, the first thing you’ll want to do is discover what the Standard Library can do for you — perhaps it contains everything you need and falling back on .NET won’t be a problem.

Interestingly enough, IronPython does ship with a version of the Standard Library that’s been tuned for maximum compatibility with IronPython. Of course, the problem word in that previous sentence is “tuned.” If you want to ensure maximum compatibility outside the IronPython environment, you must download a copy of the Python Standard Library, install it on your machine, and use it in place of the IronPython equivalent. It’s also important to know that the IronPython version of the Standard Library isn’t complete. You won’t have all the functionality that other Python developers have unless you use the Python Standard Library.

Whichever version of the Standard Library you use, you’ll need to import modules into your application before you can use them. This chapter provides a few tricks and techniques you can use to make accessing the modules easier. It’ll also explain how IronPython locates the modules so you don’t spend a lot of time trying to figure out why a particular module is seemingly inaccessible.

Considering the Standard Library Features

The Standard Library comes with a number of features not found in IronPython. Of course, there are all those missing modules. If you look in Python26Lib you’ll find that the Python Standard Library contains 256 modules in the main library directory, while IronPython has only 186. (Table 6-1 shows a complete list of the missing modules — each module contains a header telling you about the task it performs.) In addition, the Python library contains 20 subdirectories, while IronPython has only 11. In short, the IronPython version of the Standard Library has a lot to offer; the Python version of the Standard Library has more. Here’s a list of the missing directories:

  • bsddb: Provides access to the Berkeley database library. This element is deprecated and will disappear in Python 3.0, but you can still use it today. See http://docs.python.org/ library/bsddb.html for details.
  • compiler: Contains elements that help you analyze your code. This element is deprecated and will disappear in Python 3.0, but you can still use it today. See http://docs.python.org/ library/compiler.html for details.
  • curses: Provides the means to handle character-cell displays. See http://docs.python.org/ library/curses.html for details.
  • hotshot: Gives you a high-performance logging profiler. See http://docs.python.org/ library/hotshot.html for details.
  • json: Implements the JavaScript Object Notation (JSON) encoder and decoder used as a lightweight data exchange format. See http://docs.python.org/library/json.html for details.
  • msilib: Reads and writes Microsoft Installer (MSI) files. The Microsoft Installer Library (MSILib) is an essential library for Windows systems where you need to work with .MSI files often. See http://docs.python.org/library/msilib.html for details.
  • multiprocessing: Helps you create multi-processing applications. This is an exciting new addition for Python 2.6. See http://docs.python.org/library/multiprocessing.html for details.
  • sqlite3: Provides access to the SQLite database library. See http://docs.python.org/ library/sqlite3.html for details. You can learn more about the SQLite database at http://www.sqlite.org/.
  • test: Contains a complete test package for the Python Standard Library. If you make any changes to the Standard Library to meet your specific needs, you want to perform regression testing to ensure the changes don’t introduce incompatibilities. See http://docs.python .org/library/test.html for details.
Table 6-1: Standard Library Modules Missing from IronPython
Table 6-1: Standard Library Modules Missing from IronPython

Getting all of the modules found in the original Python Standard Library would be reason enough to download, install, and use it. However, the Python Standard Library provides a number of additional features in the form of help files and utilities. Although you can’t use the utilities to perform .NET-specific work, you can use them to ensure your code is truly compatible with Python, which is a big deal if you plan to use your application on other platforms. With this in mind, here’s a list of the additional help files and utilities provided with the Python Standard Library.

  • IDLE (Integrated DeveLopment Environment): A graphical user interface to work with Python code. In many respects, this is a much nicer interface than the command line version of the IronPython interpreter. Most important, you can save your work, which means that you can possibly use IDLE as an alternative to Visual Studio if desired. Of course, IDLE doesn’t know anything about working with the .NET Framework and you can’t use it to write multi-language applications.
  • Module Docs: An HTML-formatted help file that provides complete documentation of the various Python Standard Library modules. Interestingly enough, you can use this help file with IronPython without modification because this is the documentation that IronPython’s Standard Library is written to support. For the most part, this help file is brief and not very detailed, but it’s good when you need a quick reference.
  • Python (command line): An application that works precisely like the IronPython command line. In fact, you’d be hard pressed to tell the two apart. The only reason you’d want to use the Python command line is to test the cross-platform compatibility of a module you write. Otherwise, you’ll want to use the IronPython command line to ensure your code runs properly with IronPython.
  • Python Manuals: This is the detailed help file you probably wanted with IronPython, but didn’t receive. You’ll definitely want to spend some time looking around this help file and discovering new things about the Python language.

Now that you have a better idea of what the Python Standard Library provides, you need to consider how you’ll use it or whether you want to use it at all. Just how the Python Standard Library affects your organization and you depends on your goals. For the most part, you’ll use these tools when you want to write pure Python applications or applications that rely heavily on Python (versus the mixed language applications found later in this book). It’s important to remember that IronPython has a lot to offer that Python doesn’t, so you need to decide which route your organization will take before you make a commitment to pure Python by using the Python Standard Library.