You are previewing Professional IronPython™.
O'Reilly logo
Professional IronPython™

Book Description

Thorough coverage of Microsoft's new dynamic programming language: IronPython

IronPython is a powerful and vital part of any .NET developer's toolbox, and although it is several years old, very little literature exists on the topic. This essential resource fills that void and provides you with an in-depth understanding of IronPython.

A brief introduction walks you through the installation, usage, and tools of IronPython and also explains what makes IronPython different from other programming languages. Coverage quickly moves on to explaining how to use and work with the IronPython language, and an in-depth look at its environment sheds light on how it can be stand alone or with the .NET Framework. You'll see how IronPython can be used to create either desktop or Web-based applications and you'll witness how it interacts with other existing technologies. In addition, coverage of advanced topics shares techniques for extending IronPython and making it a robust language.

  • Provides you with an in-depth look at IronPython, how it is different from other programming languages, what it is capable of, and how to maximize its potential

  • Explores how IronPython interacts with existing technologies and how it can perform administration tasks

  • Answers popular questions, such as how to extend IronPython and make it a more robust language

  • Tackles topics not addressed anywhere else, including executing IronPython using Mono

You'll want to devour every topic covered in Professional IronPython so you can get started working with this powerful programming language today.

Table of Contents

  1. Copyright
  2. CREDITS
  3. ABOUT THE AUTHOR
  4. ABOUT THE TECHNICAL EDITOR
  5. ACKNOWLEDGMENTS
  6. INTRODUCTION
    1. WHO THIS BOOK IS FOR
    2. WHAT THIS BOOK COVERS
    3. HOW THIS BOOK IS STRUCTURED
    4. WHAT YOU NEED TO USE THIS BOOK
    5. CONVENTIONS
    6. SOURCE CODE
    7. ERRATA
    8. P2P.WROX.COM
  7. I. Introducing IronPython
    1. 1. Discovering IronPython
      1. 1.1. AN OVERVIEW OF IRONPYTHON
      2. 1.2. GETTING IRONPYTHON
        1. 1.2.1. Understanding the IronPython Requirements
        2. 1.2.2. Getting the Software
        3. 1.2.3. Performing the Installation
        4. 1.2.4. Building the Binaries from Scratch
        5. 1.2.5. Using Third-Party Libraries
      3. 1.3. UNDERSTANDING THE DYNAMIC LANGUAGE RUNTIME
      4. 1.4. USING THE IRONPYTHON CONSOLE
        1. 1.4.1. Opening and Using the Default Console
        2. 1.4.2. Getting Help with Any Function
        3. 1.4.3. Understanding the IPY.EXE Command Line Syntax
          1. 1.4.3.1. Adding IPY.EXE to the Windows Environment
          2. 1.4.3.2. Executing an Application from the Command Prompt
          3. 1.4.3.3. Understanding the IPY.EXE Standard Command Line Switches
          4. 1.4.3.4. Working with the –X: Command Line Switches
          5. 1.4.3.5. Modifying the IPY.EXE Environment Variables
        4. 1.4.4. Exiting the IronPython Interpreter
      5. 1.5. USING THE IRONPYTHON WINDOWED ENVIRONMENT
      6. 1.6. CREATING YOUR FIRST APPLICATION
      7. 1.7. USING IRONPYTHON CONSTRUCTIVELY
    2. 2. Understanding the IronPython Basics
      1. 2.1. USING VISUAL STUDIO TO CREATE IRONPYTHON APPLICATIONS
        1. 2.1.1. Creating the Project
        2. 2.1.2. Adding Existing Files to the Project
        3. 2.1.3. Adding New Files to the Project
        4. 2.1.4. IronPython Project Limitations
        5. 2.1.5. Debugging the Project
      2. 2.2. UNDERSTANDING THE USE OF INDENTATION AND CAPITALIZATION IN IRONPYTHON
      3. 2.3. CONSIDERING DATA TYPES IN IRONPYTHON
      4. 2.4. EXPLORING THE IRONPYTHON MODULES
        1. 2.4.1. Considering Built-in and External Modules
        2. 2.4.2. Working with the Built-in Modules
          1. 2.4.2.1. Considering the Immediately Available Modules
          2. 2.4.2.2. Using sys.builtin_module_names
        3. 2.4.3. Using External Modules with IronPython
      5. 2.5. INTERACTING WITH THE IRONPYTHON ENVIRONMENT
        1. 2.5.1. Obtaining Version Information
        2. 2.5.2. Changing sys.path Values
        3. 2.5.3. Obtaining Command Line Arguments
      6. 2.6. USING IRONPYTHON CONSTRUCTIVELY
  8. II. Using the IronPython Language
    1. 3. Implementing Structured Language Elements
      1. 3.1. STRINGING STATEMENTS TOGETHER
      2. 3.2. SELECTING BETWEEN OPTIONS
        1. 3.2.1. Performing a Simple Decision Using if
        2. 3.2.2. Choosing between Two Options Using if . . . else
        3. 3.2.3. Creating a Decision Tree Using if . . . elif . . . else
      3. 3.3. CREATING LOOPS
        1. 3.3.1. Using for ... in
        2. 3.3.2. Using while
      4. 3.4. PERFORMING ENUMERATIONS
      5. 3.5. USING IRONPYTHON CONSTRUCTIVELY
    2. 4. Using Arrays and Collections
      1. 4.1. WORKING WITH TUPLES, LISTS, AND ARRAYS
        1. 4.1.1. Understanding Tuples, Lists, and Arrays
        2. 4.1.2. Creating and Accessing Single-Dimension Arrays
        3. 4.1.3. Manipulating Single-Dimension Arrays
        4. 4.1.4. Working with Multi-Dimension Arrays
        5. 4.1.5. Using the range() Function
        6. 4.1.6. Processing Arrays Using the break and else Clauses
        7. 4.1.7. Processing Arrays Using the continue Clause
        8. 4.1.8. Using the enumerate() Function
      2. 4.2. WORKING WITH THE COLLECTIONS MODULE
      3. 4.3. USING DICTIONARIES
      4. 4.4. USING IRONPYTHON CONSTRUCTIVELY
    3. 5. Interacting with Structures and Objects
      1. 5.1. WORKING WITH EXISTING OBJECTS
        1. 5.1.1. Discovering IronPython Objects
        2. 5.1.2. Working with String Objects
          1. 5.1.2.1. Performing Standard Tasks with Strings
          2. 5.1.2.2. Formatting String Output
        3. 5.1.3. Working with Numeric Objects
          1. 5.1.3.1. Considering Numeric Type Differences
          2. 5.1.3.2. Performing Standard Tasks with Numbers
        4. 5.1.4. Working with Boolean Objects
      2. 5.2. CREATING NEW OBJECTS
        1. 5.2.1. Defining the IronPython Class
        2. 5.2.2. Using Custom Objects in IronPython
        3. 5.2.3. Adding Documentation
      3. 5.3. USING IRONPYTHON CONSTRUCTIVELY
  9. III. Working with IronPython
    1. 6. Using the Python Standard Library
      1. 6.1. CONSIDERING THE STANDARD LIBRARY FEATURES
      2. 6.2. OBTAINING AND INSTALLING THE PYTHON STANDARD LIBRARY
      3. 6.3. ACCESSING THE STANDARD LIBRARY FROM IRONPYTHON
        1. 6.3.1. Manually Adding the Import Search Path
        2. 6.3.2. Modifying the Site.py File
      4. 6.4. USING THE STANDARD LIBRARY FEATURES
        1. 6.4.1. Using the Module Documentation
        2. 6.4.2. Using the Python Manuals
        3. 6.4.3. Working with IDLE
          1. 6.4.3.1. Configuring IDLE
          2. 6.4.3.2. Performing an Interactive Edit
          3. 6.4.3.3. Using the Path Browser
          4. 6.4.3.4. Using the Class Browser
          5. 6.4.3.5. Using the Debugger
        4. 6.4.4. Looking for Standard Library Issues
      5. 6.5. USING IRONPYTHON CONSTRUCTIVELY
    2. 7. Accessing the .NET Framework
      1. 7.1. IMPORTING THE .NET FRAMEWORK ASSEMBLIES
        1. 7.1.1. Performing a Standard Import
        2. 7.1.2. Importing an Assembly into the Global Namespace
        3. 7.1.3. Configuring the Console for .NET Help
        4. 7.1.4. Creating a Reference to .NET Assemblies
      2. 7.2. USING .NET DATA TYPES
      3. 7.3. INTERACTING WITH .NET FRAMEWORK ELEMENTS
        1. 7.3.1. Obtaining Assembly Information
        2. 7.3.2. Making Static Method Calls
        3. 7.3.3. Creating .NET Objects
        4. 7.3.4. Creating the Directory Example
      4. 7.4. WORKING WITH GENERICS
      5. 7.5. USING IRONPYTHON CONSTRUCTIVELY
    3. 8. Creating Windows Forms Applications
      1. 8.1. WORKING WITHOUT A VISUAL DESIGNER
        1. 8.1.1. Understanding How the Visual Designer Works
        2. 8.1.2. Understanding the Elements of a Window
        3. 8.1.3. Emulating the Visual Designer Results
      2. 8.2. DEFINING WINDOWS FORMS
        1. 8.2.1. Creating the Window Code
          1. 8.2.1.1. Creating the Basic Control Structure
          2. 8.2.1.2. Making the Application Easier to Use
          3. 8.2.1.3. Performing Quick Form Tests
        2. 8.2.2. Initializing the Application
        3. 8.2.3. Providing Handlers for the Control Events
        4. 8.2.4. Performing Some Useful Work as the Result of User Input
        5. 8.2.5. Running the Application
      3. 8.3. INTERACTING WITH PREDEFINED AND CUSTOM EVENTS
        1. 8.3.1. Handling Events Using Existing Classes
        2. 8.3.2. Developing Your Own Events
          1. 8.3.2.1. Creating the Event Class
          2. 8.3.2.2. Devising an Event Class Test Form
          3. 8.3.2.3. Running the Code
      4. 8.4. USING IRONPYTHON CONSTRUCTIVELY
    4. 9. Interacting with COM Objects
      1. 9.1. AN OVERVIEW OF COM ACCESS DIFFERENCES WITH PYTHON
      2. 9.2. CHOOSING A BINDING TECHNIQUE
        1. 9.2.1. Understanding Early and Late Binding
        2. 9.2.2. Using Early Binding
        3. 9.2.3. Using Late Binding
      3. 9.3. DEFINING AN INTEROP DLL
        1. 9.3.1. Accessing the Visual Studio .NET Utilities
        2. 9.3.2. Understanding the Type Library Import Utility
        3. 9.3.3. Understanding the ActiveX Import Utility
        4. 9.3.4. Creating the Windows Media Player Interop DLL
        5. 9.3.5. Exploring the Windows Media Player Interop DLL
          1. 9.3.5.1. Using the ILDasm Command Line
          2. 9.3.5.2. Working with ILDasm Symbols
          3. 9.3.5.3. Exploring ILDasm entries
        6. 9.3.6. Using the Windows Media Player Interop DLL
          1. 9.3.6.1. Working with ResGen
          2. 9.3.6.2. Creating the Media Player Form Code
          3. 9.3.6.3. Creating the Media Player Application Code
      4. 9.4. PERFORMING LATE BINDING USING ACTIVATOR.CREATEINSTANCE()
      5. 9.5. PERFORMING LATE BINDING USING MARSHAL.GETACTIVEOBJECT()
      6. 9.6. USING IRONPYTHON CONSTRUCTIVELY
    5. 10. Using IronPython for Administration Tasks
      1. 10.1. UNDERSTANDING THE COMMAND LINE
        1. 10.1.1. Understanding the Need for Command Line Applications
        2. 10.1.2. Reading Data from the Command Line
          1. 10.1.2.1. Using the Python Method
          2. 10.1.2.2. Using the .NET Method
        3. 10.1.3. Providing Command Line Help
          1. 10.1.3.1. Creating an Application Description
          2. 10.1.3.2. Describing the Application Calling Syntax
          3. 10.1.3.3. Documenting the Command Line Switches
          4. 10.1.3.4. Showing Usage Examples
          5. 10.1.3.5. Putting Everything Together
          6. 10.1.3.6. Including Other Elements
      2. 10.2. INTERACTING WITH THE ENVIRONMENT
        1. 10.2.1. Understanding Environment Variables
        2. 10.2.2. Using the Python Method
          1. 10.2.2.1. Reading the Environment Variables Using Python
          2. 10.2.2.2. Setting the Environment Variables Using Python
        3. 10.2.3. Using the .NET Method
          1. 10.2.3.1. Reading the Environment Variables Using .NET
          2. 10.2.3.2. Setting the Environment Variables Using .NET
        4. 10.2.4. Environment Variable Considerations
      3. 10.3. STARTING OTHER COMMAND LINE APPLICATIONS
      4. 10.4. PROVIDING STATUS INFORMATION
        1. 10.4.1. Reporting Directly to the User
        2. 10.4.2. Creating Log Files
        3. 10.4.3. Using the Event Log
      5. 10.5. USING IRONPYTHON CONSTRUCTIVELY
    6. 11. Developing ASP.NET Applications
      1. 11.1. CONSIDERING IRONPYTHON WEB APPLICATION COMPATIBILITY
      2. 11.2. OBTAINING ASP.NET DYNAMIC LANGUAGE SUPPORT
        1. 11.2.1. DLR Limitations
        2. 11.2.2. Getting DLR
        3. 11.2.3. Using the Sample Applications
      3. 11.3. CREATING A WEB SITE
      4. 11.4. BUILDING A BASIC ASP.NET SITE USING IRONPYTHON
        1. 11.4.1. Creating the Project
        2. 11.4.2. Defining the User Interface
        3. 11.4.3. Creating the Code Behind
        4. 11.4.4. Starting the Visual Studio Built-In Web Server
          1. 11.4.4.1. Understanding the WebDev.WebServer Command Line Syntax
          2. 11.4.4.2. Using the Built-In Web Server with a Batch File
          3. 11.4.4.3. Interacting with the Built-In Web Server
          4. 11.4.4.4. Considering the Built-In Web Server Limitations
        5. 11.4.5. Performing Basic Testing
        6. 11.4.6. Considering Debugging
      5. 11.5. USING IRONPYTHON CONSTRUCTIVELY
    7. 12. Debugging IronPython Applications
      1. 12.1. UNDERSTANDING IRONPYTHON WARNINGS
        1. 12.1.1. Working with Actions
        2. 12.1.2. Working with Messages
        3. 12.1.3. Working with Categories
      2. 12.2. OBTAINING ERROR INFORMATION
        1. 12.2.1. Using the sys Module
        2. 12.2.2. Using the traceback Module
      3. 12.3. DEBUGGING WITH THE PYTHON DEBUGGER
      4. 12.4. DEBUGGING WITH THE CLR DEBUGGER
      5. 12.5. USING VISUAL STUDIO FOR IRONPYTHON DEBUGGING
      6. 12.6. DEFINING AND USING EXCEPTIONS
        1. 12.6.1. Implementing Python Exceptions
        2. 12.6.2. Implementing .NET Exceptions
      7. 12.7. USING IRONPYTHON CONSTRUCTIVELY
  10. IV. Advanced IronPython Topics
    1. 13. Working with XML Data
      1. 13.1. USING THE .NET XML FUNCTIONALITY
        1. 13.1.1. Considering the System.Xml Namespace
        2. 13.1.2. Developing a Basic .NET XML Application
      2. 13.2. USING XMLUTIL
        1. 13.2.1. Loading and Viewing the XMLUtil Module
        2. 13.2.2. Using the XMLUtil Module to View XML Data
      3. 13.3. USING THE PYTHON MODULES
        1. 13.3.1. Working with xml.dom.minidom
        2. 13.3.2. Working with xml.sax
      4. 13.4. USING IRONPYTHON CONSTRUCTIVELY
    2. 14. Interacting with the DLR
      1. 14.1. OBTAINING DLR
        1. 14.1.1. Using the Direct Method
        2. 14.1.2. Downloading the Full DLR
        3. 14.1.3. Building the Full DLR
          1. 14.1.3.1. Performing a Command Line Build
          2. 14.1.3.2. Performing a Visual Studio 2008 Build
          3. 14.1.3.3. Performing a Visual Studio 2010 Build
        4. 14.1.4. Downloading the Documentation
        5. 14.1.5. Reporting Bugs and Other Issues
      2. 14.2. WORKING WITH HOSTING APIS
        1. 14.2.1. Using the Hosting APIs
        2. 14.2.2. Understanding the Hosting APIs Usage Levels
        3. 14.2.3. Considering the Host Application
        4. 14.2.4. Embedding IronPython as a Scripting Language
      3. 14.3. UNDERSTANDING THE EXTENSIONS TO LINQ EXPRESSION TREE
      4. 14.4. CONSIDERING DYNAMICSITE
      5. 14.5. WORKING WITH IDYNAMICOBJECT
      6. 14.6. UNDERSTANDING THE ACTIONBINDER
      7. 14.7. UNDERSTANDING THE OTHER DLR FEATURES
      8. 14.8. USING IRONPYTHON CONSTRUCTIVELY
    3. 15. Using IronPython from Other .NET Languages
      1. 15.1. UNDERSTANDING THE RELATIONSHIP BETWEEN DYNAMIC AND STATIC LANGUAGES
      2. 15.2. CREATING AN EXTERNALLY ACCESSIBLE IRONPYTHON MODULE
        1. 15.2.1. Considering Requirements for Externally Accessible Modules
        2. 15.2.2. Creating the IronPython Script
      3. 15.3. ACCESSING THE MODULE FROM C#
        1. 15.3.1. Adding the Required C# References
        2. 15.3.2. Adding the Required References to the Host Language
        3. 15.3.3. Understanding the Use of ScriptEngine
        4. 15.3.4. Using the dynamic Keyword
        5. 15.3.5. Working with the App.CONFIG File
      4. 15.4. ACCESSING THE MODULE FROM VISUAL BASIC.NET
        1. 15.4.1. Adding the Required Visual Basic.NET References
        2. 15.4.2. Creating the Visual Basic.NET Code
      5. 15.5. DEVELOPING TEST PROCEDURES FOR EXTERNAL MODULES
      6. 15.6. DEBUGGING THE EXTERNAL MODULE
      7. 15.7. USING IRONPYTHON CONSTRUCTIVELY
    4. 16. Extending IronPython Using C#
      1. 16.1. UNDERSTANDING THE REQUIREMENTS FOR AN EXTENSION
      2. 16.2. CONSIDERING IRONPYTHON AND STATIC LANGUAGE DIFFERENCES
        1. 16.2.1. Defining Why You Use a Static Language with IronPython
        2. 16.2.2. Understanding Line Noise
        3. 16.2.3. Considering Scoping Issues
      3. 16.3. CREATING THE SIMPLE C# EXTENSION
        1. 16.3.1. Creating the Project
        2. 16.3.2. Developing the C# Extension
        3. 16.3.3. Adding the IronPython Project
        4. 16.3.4. Creating the IronPython Application
      4. 16.4. USING C# FOR USER INTERFACE SUPPORT
        1. 16.4.1. Defining a Library of Dialog Boxes
        2. 16.4.2. Creating the Dialog Box Library in C#
          1. 16.4.2.1. Defining Simple Message Boxes
          2. 16.4.2.2. Using Enumerations with IronPython
          3. 16.4.2.3. Considering Developer Help
          4. 16.4.2.4. Defining Complex Forms
        3. 16.4.3. Accessing the Dialog Box Library from IronPython
          1. 16.4.3.1. An Alternative Method for Adding the IronPython Project
          2. 16.4.3.2. Performing the Message Box and Form Tests
      5. 16.5. USING C# FOR WIN32 SUPPORT
        1. 16.5.1. Creating the P/Invoke Code
        2. 16.5.2. Developing the IronPython Callable Methods
          1. 16.5.2.1. Defining Common Variables and the Constructor
          2. 16.5.2.2. Defining the GetCurrentDisplayMode() Method
          3. 16.5.2.3. Defining the GetConsoleWindowSize() Method
          4. 16.5.2.4. Defining the GetConsoleInfo() Method
        3. 16.5.3. Writing an IronPython Application to Use P/Invoke
      6. 16.6. USING IRONPYTHON CONSTRUCTIVELY
    5. 17. Extending IronPython Using Visual Basic.NET
      1. 17.1. CONSIDERING C# AND VISUAL BASIC.NET EXTENSION SIMILARITIES
      2. 17.2. CREATING THE SIMPLE VISUAL BASIC.NET EXTENSION
        1. 17.2.1. Creating the Project
        2. 17.2.2. Developing the Visual Basic.NET Extension
        3. 17.2.3. Adding the IronPython Project
        4. 17.2.4. Creating the IronPython Application
      3. 17.3. USING VISUAL BASIC.NET FOR USER INTERFACE SUPPORT
        1. 17.3.1. Creating the User Interface Library Module
          1. 17.3.1.1. Defining Simple Message Boxes
          2. 17.3.1.2. Defining Complex Forms
        2. 17.3.2. Accessing the User Interface Library Module from IronPython
          1. 17.3.2.1. An Alternative Method for Adding the IronPython Project
          2. 17.3.2.2. Performing the Message Box and Form Tests
      4. 17.4. USING VISUAL BASIC.NET FOR DATABASE SUPPORT
        1. 17.4.1. Obtaining and Configuring the Database
        2. 17.4.2. Creating the Database Support Module
          1. 17.4.2.1. Creating a Connection to the Database
          2. 17.4.2.2. Adding Database Manipulation Code
        3. 17.4.3. Accessing the Database Module through IronPython
      5. 17.5. USING IRONPYTHON CONSTRUCTIVELY
    6. 18. Using IronPython for Application Testing
      1. 18.1. UNDERSTANDING WHY YOU WANT TO USE IRONPYTHON FOR TESTING
      2. 18.2. CONSIDERING THE TEST ENVIRONMENT
        1. 18.2.1. Defining Access
        2. 18.2.2. Considering a Few Things IronPython Can't Test
        3. 18.2.3. Creating the Test Harness
      3. 18.3. TESTING DLLS
        1. 18.3.1. Creating the Test DLL
        2. 18.3.2. Creating the DLL Test Script
        3. 18.3.3. Performing the DLL Test
      4. 18.4. TESTING APPLICATIONS
        1. 18.4.1. Creating the Test Application
          1. 18.4.1.1. Defining the Form
          2. 18.4.1.2. Building the Code
        2. 18.4.2. Creating the Application Test Script
        3. 18.4.3. Performing the Application Test
      5. 18.5. PERFORMING COMMAND LINE TESTS
      6. 18.6. USING IRONPYTHON CONSTRUCTIVELY
    7. 19. Using IronPython with Mono
      1. 19.1. WHAT IS MONO?
        1. 19.1.1. An Overview of the Mono Family
        2. 19.1.2. Considering the Reasons for Using Mono
        3. 19.1.3. Understanding Mono Limitations
        4. 19.1.4. Using Mono on Windows Server 2008 Server Core
      2. 19.2. OBTAINING AND INSTALLING MONO
      3. 19.3. CREATING AN IRONPYTHON APPLICATION WITH MONO
        1. 19.3.1. Working at the Command Line
        2. 19.3.2. Defining the Project
        3. 19.3.3. Creating the Code
        4. 19.3.4. Running the Application from the IDE
        5. 19.3.5. Running the Application from the Command Line
      4. 19.4. INTERACTING WITH OTHER .NET LANGUAGES UNDER MONO
      5. 19.5. USING IRONPYTHON CONSTRUCTIVELY
  11. A. IronPython Differences with CPython
    1. A.1. IMPLEMENTING STANDARD TYPES, FUNCTIONS, AND BEHAVIORS
    2. A.2. MISSING CPYTHON LIBRARIES
    3. A.3. MISSING OR CHANGED EXTENSION MODULES
    4. A.4. USING CUSTOM CPYTHON EXTENSIONS
    5. A.5. INTERACTING WITH THE INTERPRETER AND ENVIRONMENT
    6. A.6. USING GARBAGE COLLECTION
  12. B. CPython Extensions for IronPython
    1. B.1. OBTAINING THE EXTENSIONS
    2. B.2. OVERCOMING POTENTIAL EXTENSION PROBLEMS
    3. B.3. OBTAINING THIRD-PARTY SOLUTIONS
      1. B.3.1. Considering Some of the Better Solutions
      2. B.3.2. Working with IronClad