Part I TE ⊲⊲ CHAPTER 1: Discovering IronPython RI AL Introducing IronPython CO PY RI GH TE D MA ⊲⊲ CHAPTER 2: Understanding the IronPython Basics
1 Discovering IronPython WHAT’S IN THIS CHAPTER? ➤➤ Understanding why you want to add IronPython to your developer toolbox ➤➤ Obtaining and installing IronPython on your machine ➤➤ Understanding some underlying basics of how IronPython works ➤➤ Using IronPython at the console and within a window ➤➤ Designing and building a simple application IronPython: It sounds like some kind of metal snake infesting your computer, but it isn’t. IronPython is the .
❘ Chapter 1 Discovering IronPython Finally, this chapter takes you through the process of creating a simple application. No, this isn’t going to be the next great Windows application. It will be a little better than Hello World, but not much. The idea is to get you started doing something useful with IronPython. Don’t worry; the examples will become a lot more interesting as the book progresses.
An Overview of IronPython environmental conditions. (You’ll discover many other dynamic language advantages as the chapter progresses.) Unfortunately, you often pay for runtime flexibility with poorer performance — there’s always a tradeoff between flexibility and performance. Performance is a combination of three factors: speed, reliability, and security. When an application has a performance hit, it means a decrease in any of these three factors.
❘ Chapter 1 Discovering IronPython ➤➤ An extensive array of standard libraries ➤➤ Full library support using hierarchical packages (a concept that is already familiar to every .NET developer) ➤➤ Robust third-party libraries that support just about every need ➤➤ Support for writing both extensions and modules in both C and C++ ➤➤ Support for writing extensions and modules using third-party solutions for both .
Getting IronPython ❘7 An interesting use of IronPython is as an application testing tool. In fact, some developers use IronPython exclusively for this purpose. Chapter 18 tells you more about this exciting use of IronPython and demonstrates that using IronPython for this purpose really does make application testing considerably easier. Don’t get the idea that IronPython is going to restrict your use of familiar technologies.
❘ Chapter 1 Discovering IronPython ➤➤ (Optional) Visual Studio 2005 or Visual Studio 2008 (your system must meet the prerequisites for this software) ➤➤ (Optional) .NET Framework 2.0 Software Development Kit (SDK) You need only the optional requirements if you plan to build IronPython 2.6 from the source code. Here are the requirements for the .NET Framework 4.0 version (again, the optional requirements are there if you want to build IronPython from source code). ➤➤ The .NET Framework 4.
Getting IronPython 2. ❘9 Read the licensing agreement, check I Accept the Terms in the License Agreement, and then click Next. You’ll see the Custom Setup dialog box shown in Figure 1-1 where you can select the IronPython features you want to install. At a minimum, you must install the Runtime. The Documentation, Standard Library, and Tools features are also strongly recommended. This book assumes that you’ve installed all the features.
❘ Chapter 1 Discovering IronPython Building the Binaries from Scratch You may eventually want to build the IronPython binaries from scratch. The normal reason to perform this task is to create a special version of IronPython that meets specific needs. A company may want to add extensions or special features to IronPython.
Understanding the Dynamic Language Runtime ❘ 11 You should be able to use some third-party libraries with IronPython. At the time of this writing, you won’t actually find any usable third-party libraries. However, you should check http://www .ironpython.info/index.php/Third-Party_Library_Compatibility from time-to-time to discover whether there are any third-party libraries that do work with IronPython.
❘ Chapter 1 Discovering IronPython ➤➤ Creates an environment where sharing of objects and libraries between languages is possible ➤➤ Makes it possible to perform fast dynamic dispatch and invocation of objects This section provides a good overview of DLR. You’ll discover additional details about DLR as the book progresses. However, if you’d like to delve into some of the architectural details of DLR, check out the article at http://msdn.microsoft.com/library/dd233052.aspx.
Using the IronPython Console ❘ 13 Notice that the top of the window tells you which version of IronPython you’re using and which version of the .NET Framework it’s running on. This is important information because it helps you understand the IronPython environment and what limitations you have when working with IronPython. Below this first line, you’ll see some commands that Microsoft thought you might find useful.
❘ Chapter 1 Discovering IronPython Let’s say you have no idea of what you want to find. Console help provides you with a list of words you can type to get general help. These terms are: ➤➤ Modules ➤➤ Keywords ➤➤ Topics Type any of these terms and press Enter. You’ll see a list of additional words you can type, as shown in Figure 1-6 for modules. Using this technique, you can drill down into help and locate anything you want.
Using the IronPython Console Figure 1-6: Drill down into help to find topics of interest.
❘ Chapter 1 Discovering IronPython Figure 1-7: The console also provides the means to obtain precise help about any module, keyword, or topic. 4. Select the end of the string that appears in the Variable Value field. Type ;C:\Program Files\IronPython 2.6 and click OK. Make sure you modify this path to match your IronPython configuration. 5. Click OK three times to close the Edit System Variable, Environment Variables, and System Properties dialog boxes.
Using the IronPython Console ❘ 17 displayed at each click point, as shown in Figure 1-8. If you look at the command prompt window at this point, you’ll see that the mouse cursor is blinking but you can’t type anything because the command prompt is waiting for the IronPython interpreter to end. When you click the Close button, the application ends and you can again type something at the command prompt. Understanding the IPY.EXE Standard Command Line Switches Sometimes you need to provide IPY.
❘ Chapter 1 Discovering IronPython –s: Specifies that the interpreter shouldn’t add the user site directory to sys.path. –S: Specifies that the interpreter shouldn’t imply that it should execute the import site command on initialization. –t: Outputs warnings about inconsistent tab usage, which can lead to code interpretation problems. –tt: Outputs errors for inconsistent tab usage. Inconsistent tab usage can lead to code interpretation problems, which can result in hard-to-locate bugs.
Using the IronPython Console ❘ 19 Working with the –X: Command Line Switches In addition to the standard command line switches, you also have access to the –X: command line switches, which configure the IronPython interpreter. The following list describes each of the configuration options: –X:AutoIndent: Enables auto-indenting in the read-evaluation-print loop (REPL). –X:ColorfulConsole: Enables ColorfulConsole support. –X:Debug: Enables application debugging.
❘ Chapter 1 Discovering IronPython Modifying the IPY.EXE Environment Variables IPY also supports a number of environment variables. The following list describes each of these environment variables. IRONPYTHONPATH: Specifies the path to search for modules used within an application IRONPYTHONSTARTUP: Specifies the name and location of the startup module Exiting the IronPython Interpreter Eventually, you’ll want to leave the console. In order to end your session, simply type exit() and press Enter.
Creating Your First Application ❘ 21 Creating Your First Application After all this time, you might have started wondering whether you would get to write any code at all in this chapter. The first application won’t be very fancy, but it’ll be more than a simple Hello World kind of application. You can use any editor that outputs pure text in this section. Notepad will work just fine. Listing 1-1 shows the code you should type in your editor.
❘ Chapter 1 Discovering IronPython Figure 1-10: The output of the example is a simple equation. Using IronPython Constructively This chapter has introduced you to IronPython. You should have a good understanding of why you want to use IronPython and how it differs from other, static .NET languages. Dynamic languages have a special place in your toolbox. They aren’t the answer to every need, but they can address specific needs — just as other languages address the needs for which they were built.