IronPython in Action

Book description

IronPython in Action offers a comprehensive, hands-on introduction to Microsoft's exciting new approach for programming the .NET framework. It approaches IronPython as a first class .NET language, fully integrated with the .NET environment, Visual Studio, and even the open-source Mono implementation. You'll learn how IronPython can be embedded as a ready-made scripting language into C# and VB.NET programs, used for writing full applications or for web development with ASP. Even better, you'll see how IronPython works in Silverlight for client-side web programming.



About the Technology

In 2005, Microsoft quietly announced an initiative to bring dynamic languages to the .NET platform. The starting point for this project was a .NET implementation of Python, dubbed IronPython. After a couple years of incubation, IronPython is ready for real-world use. It blends the simplicity, elegance, and dynamism of Python with the power of the .NET framework.



About the Book

IronPython opens up exciting new possibilities. Because it's a dynamic language, it permits programming paradigms not easily available in VB and C#. In this book, authors Michael Foord and Christian Muirhead explore the world of functional programming, live introspection, dynamic typing and ‘duck typing’, metaprogramming, and more.

IronPython in Action explores these topics with examples, making use of the Python interactive console to explore the .NET framework with live objects. The expert authors provide a complete introduction for programmers to both the Python language and the power of the .NET framework. The book also shows how to extend IronPython with C#, extending C# and VB.NET applications with Python, using IronPython with .NET 3.0 and Powershell, IronPython as a Windows scripting tool, and much more.



What's Inside
  • An introduction to IronPython, including a Python tutorial
  • Overview of the .NET framework for Python programmers
  • Embedding IronPython in applications
  • Extending IronPython with C#
  • IronPython web programming with ASP, Silverlight, and the DLR
  • Structured application programming
  • Testing with IronPython
  • The power of dynamic languages through easy introspection, functional programming, and metaprogramming.


About the Reader

Whether you're a Python user exploring .NET or a .NET developer exploring the world of dynamic languages, IronPython in Action will get you started quickly and show you how to be productive with this powerful new tool.



About the Authors

Michael Foord and Christian Muirhead both work full time with IronPython for Resolver Systems; creating a highly programmable spreadsheet called Resolver One. They have been using IronPython since before version 1.0 was released.

Michael Foord has been developing with Python since 2002. He blogs and writes about Python and IronPython far more than is healthy for one individual and in 2008 was made the first Microsoft MVP for dynamic languages. As the Resolver Systems community champion he speaks internationally on Python and IronPython. He maintains the IronPython Cookbook and IronPython-URLs websites, and can also be found online at http://www.voidspace.org.uk/python/weblog/. In the real world he lives in Northampton, UK with his wife Delia.

Christian Muirhead began his career in a high-volume database environment, and for the last eight years has been building database-driven websites. He has five years of experience working with C#, the .NET framework, and ASP.NET. He has been using Python in most of his projects since discovering it in 1999, including building web applications for the BBC using Django. Christian is a New Zealander currently exiled in London with his wife Alice.



Quotes
... everything you need to get started with IronPython.
- Jim Hugunin, Creator of IronPython

The best book on one of the best implementations of Python.
- Alex Martelli, Author of Python in a Nutshell

A long-awaited guide.
- Keith J. Farmer, Idea Entity

... excellent explanations of unique and valuable features.
- Noah Gift, Author of Python for Unix and Linux System Administration

If you are a .NET programmer this book is for you... and if you are new to programming, it's for you too.
- Craig Murphy, Microsoft MVP

Table of contents

  1. Copyright
    1. Dedication
  2. Foreword
  3. Preface
  4. Acknowledgments
  5. About this Book
    1. Who should read this book?
    2. Roadmap
    3. Code conventions and downloads
    4. Author Online
    5. About the authors
    6. About the title
    7. About the cover illustration
  6. 1. Getting started with IronPython
    1. 1. A new language for .NET
      1. 1.1. An introduction to IronPython
        1. 1.1.1. What is IronPython?
        2. 1.1.2. A brief history of IronPython
        3. 1.1.3. IronPython for Python programmers
        4. 1.1.4. IronPython for .NET programmers
      2. 1.2. Python on the CLR
        1. 1.2.1. Dynamic languages on .NET and the DLR
        2. 1.2.2. Silverlight: a new CLR
        3. 1.2.3. The Python programming language
        4. 1.2.4. Multiple programming paradigms
      3. 1.3. Live objects on the console: the interactive interpreter
        1. 1.3.1. Using the interactive interpreter
        2. 1.3.2. The .NET framework: assemblies, namespaces, and references
        3. 1.3.3. Live objects and the interactive interpreter
        4. 1.3.4. Object introspection with dir and help
      4. 1.4. Summary
    2. 2. Introduction to Python
      1. 2.1. An overview of Python
        1. 2.1.1. Python datatypes
          1. Strings
          2. The CLR Module and IronPython Types
          3. Numbers
          4. Lists
          5. Tuples
          6. Dictionaries
          7. Sets
          8. None and the Booleans
        2. 2.1.2. Names, objects, and references
        3. 2.1.3. Mutable and immutable objects
      2. 2.2. Python: basic constructs
        1. 2.2.1. Statements and expressions
        2. 2.2.2. Conditionals and loops
        3. 2.2.3. Functions
        4. 2.2.4. Built-in functions
        5. 2.2.5. Classes
      3. 2.3. Additional Python features
        1. 2.3.1. Exception handling
        2. 2.3.2. Closures and scoping rules
        3. 2.3.3. List comprehensions
        4. 2.3.4. Modules, packages, and importing
        5. 2.3.5. Docstrings
        6. 2.3.6. The Python standard library
      4. 2.4. Summary
    3. 3. .NET objects and IronPython
      1. 3.1. Introducing .NET
        1. 3.1.1. Translating MSDN documentation into IronPython
        2. 3.1.2. The Form class
      2. 3.2. Structures, enumerations, and collections: .NET types
        1. 3.2.1. Methods and properties inherited from Control
        2. 3.2.2. Adding a Label to the Form: ControlCollection
        3. 3.2.3. Configuring the Label: the Color structure
        4. 3.2.4. The FormBorderStyle enumeration
        5. 3.2.5. Hello World with Form and Label
      3. 3.3. Handling events
        1. 3.3.1. Delegates and the MouseMove event
        2. 3.3.2. Event handlers in IronPython
      4. 3.4. Subclassing .NET types
      5. 3.5. Summary
  7. 2. Core development techniques
    1. 4. Writing an application and design patterns with IronPython
      1. 4.1. Data modeling and duck typing
        1. 4.1.1. Python and protocols
        2. 4.1.2. Duck typing in action
      2. 4.2. Model-View-Controller in IronPython
        1. 4.2.1. Introducing the running example
        2. 4.2.2. The view layer: creating a user interface
        3. 4.2.3. A data model
        4. 4.2.4. A controller class
      3. 4.3. The command pattern
        1. 4.3.1. The SaveFileDialog
        2. 4.3.2. Writing files: the .NET and Python ways
        3. 4.3.3. Handling exceptions and the system message box
        4. 4.3.4. The SaveCommand
        5. 4.3.5. The SaveAsCommand
      4. 4.4. Integrating commands with our running example
        1. 4.4.1. Menu classes and lambda
        2. 4.4.2. .NET classes: ToolBar and images
        3. 4.4.3. Bringing the GUI to life
      5. 4.5. Summary
    2. 5. First-class functions in action with XML
      1. 5.1. First-class functions
        1. 5.1.1. Higher order functions
        2. 5.1.2. Python decorators
        3. 5.1.3. A null-argument-checking decorator
      2. 5.2. Representing documents with XML
        1. 5.2.1. The .NET XmlWriter
        2. 5.2.2. A DocumentWriter Class
        3. 5.2.3. An alternative with an inner function
      3. 5.3. Reading XML
        1. 5.3.1. XMLReader
        2. 5.3.2. An IronPython XmlDocumentReader
      4. 5.4. Handler functions for MultiDoc XML
      5. 5.5. The Open command
      6. 5.6. Summary
    3. 6. Properties, dialogs, and Visual Studio
      1. 6.1. Document observers
        1. 6.1.1. Python properties
          1. Document Observers on the Mainform
          2. Making Document a Property
          3. Refactoring the TabController
        2. 6.1.2. Adding the OpenCommand
      2. 6.2. More with TabPages: dialogs and Visual Studio
        1. 6.2.1. Remove pages: OK and Cancel dialog box
          1. The Code Behind
        2. 6.2.2. Rename pages: a modal dialog
          1. Filling in the Dialog
          2. The RenameCommand
        3. 6.2.3. Visual Studio Express and IronPython
        4. 6.2.4. Adding pages: code reuse in action
        5. 6.2.5. Wiring the commands to the view
      3. 6.3. Object serializing with BinaryFormatter
      4. 6.4. Summary
    4. 7. Agile testing: where dynamic typing shines
      1. 7.1. The unittest module
        1. 7.1.1. Creating a TestCase
        2. 7.1.2. setUp and tearDown
        3. 7.1.3. Test suites with multiple modules
      2. 7.2. Testing with mocks
        1. 7.2.1. Mock objects
        2. 7.2.2. Modifying live objects: the art of the monkey patch
          1. Attribute Lookup Rules
          2. The Listener Class
        3. 7.2.3. Mocks and dependency injection
      3. 7.3. Functional testing
        1. 7.3.1. Interacting with the GUI thread
        2. 7.3.2. An AsyncExecutor for asynchronous interactions
        3. 7.3.3. The functional test: making MultiDoc dance
      4. 7.4. Summary
    5. 8. Metaprogramming, protocols, and more
      1. 8.1. Protocols instead of interfaces
        1. 8.1.1. A myriad of magic methods
          1. The Length of Containers
          2. Representing objects as strings
          3. Truth testing of objects
        2. 8.1.2. Operator overloading
        3. 8.1.3. Iteration
        4. 8.1.4. Generators
        5. 8.1.5. Equality and inequality
      2. 8.2. Dynamic attribute access
        1. 8.2.1. Attribute access with built-in functions
        2. 8.2.2. Attribute access through magic methods
        3. 8.2.3. Proxying attribute access
      3. 8.3. Metaprogramming
        1. 8.3.1. Introduction to metaclasses
        2. 8.3.2. Uses of metaclasses
        3. 8.3.3. A profiling metaclass
      4. 8.4. IronPython and the CLR
        1. 8.4.1. .NET arrays
          1. CreateInstance and multidimensional arrays
        2. 8.4.2. Overloaded methods
        3. 8.4.3. out, ref, params, and pointer parameters
        4. 8.4.4. Value types
        5. 8.4.5. Interfaces
        6. 8.4.6. Attributes
        7. 8.4.7. Static compilation of IronPython code
      5. 8.5. Summary
  8. 3. IronPython and advanced .NET
    1. 9. WPF and IronPython
      1. 9.1. Hello World with WPF and IronPython
        1. 9.1.1. WPF from code
        2. 9.1.2. Hello World from XAML
      2. 9.2. WPF in action
        1. 9.2.1. Layout with the Grid
        2. 9.2.2. The WPF ComboBox and CheckBox
        3. 9.2.3. The Image control
        4. 9.2.4. The Expander
        5. 9.2.5. The ScrollViewer
        6. 9.2.6. The TextBlock: a lightweight document control
        7. 9.2.7. The XamlWriter
      3. 9.3. XPS documents and flow content
        1. 9.3.1. FlowDocument viewer classes
          1. The FlowDocumentReader
          2. The FlowDocumentPageViewer and FlowDocumentScrollViewer
        2. 9.3.2. Flow document markup
          1. Basic Markup
        3. 9.3.3. Document XAML and object tree processing
          1. Handling Images
          2. Handling Links
      4. 9.4. Summary
    2. 10. Windows system administration with IronPython
      1. 10.1. System administration with Python
        1. 10.1.1. Simple scripts
        2. 10.1.2. Shell scripting with IronPython
          1. Parsing Command-line Arguments
          2. Reading Onfiguration Files
          3. Walking Directories
          4. Filtering Filenames
      2. 10.2. WMI and the System.Management assembly
        1. 10.2.1. System.Management
          1. Simple WQL Queries
          2. Monitoring Events
          3. WQL, WMI Classes, and Events
        2. 10.2.2. Connecting to remote computers
          1. Connection Authentication and Impersonation
          2. Querying Remote Computers
      3. 10.3. PowerShell and IronPython
        1. 10.3.1. Using PowerShell from IronPython
          1. The PowerShell Runspace
          2. Multiple Commands and the Pipeline
        2. 10.3.2. Using IronPython from PowerShell
          1. Embedding IronPython in PowerShell
          2. Creating STA Threads
          3. Asynchronous Events without Blocking
          4. Calling Python Code and Returning Results
      4. 10.4. Summary
    3. 11. IronPython and ASP.NET
      1. 11.1. Introducing ASP.NET
        1. 11.1.1. Web controls
        2. 11.1.2. Pages and user controls
        3. 11.1.3. Rendering, server code, and the page lifecycle
      2. 11.2. Adding IronPython to ASP.NET
        1. 11.2.1. Writing a first application
        2. 11.2.2. Handling an event
      3. 11.3. ASP.NET infrastructure
        1. 11.3.1. The App_Script folder
        2. 11.3.2. The Global.py file
        3. 11.3.3. The Web.config file
      4. 11.4. A web-based MultiDoc Viewer
        1. 11.4.1. Page structure
          1. Data Binding
        2. 11.4.2. Code-behind
          1. Page Lifecycle
      5. 11.5. Editing MultiDocs
        1. 11.5.1. Swapping controls
        2. 11.5.2. Handling view state
        3. 11.5.3. Additional events
      6. 11.6. Converting the Editor into a user control
        1. 11.6.1. View state again
        2. 11.6.2. Adding parameters
      7. 11.7. Summary
    4. 12. Databases and web services
      1. 12.1. Relational databases and ADO.NET
        1. 12.1.1. Trying it out using PostgreSQL
          1. Installing the Pieces
        2. 12.1.2. Connecting to the database
        3. 12.1.3. Executing commands
        4. 12.1.4. Setting parameters
        5. 12.1.5. Querying the database
        6. 12.1.6. Reading multirow results
        7. 12.1.7. Using transactions
        8. 12.1.8. DataAdapters and DataSets
      2. 12.2. Web services
        1. 12.2.1. Using a simple web service
        2. 12.2.2. Using SOAP services from IronPython
        3. 12.2.3. REST services in IronPython
          1. The Notes Service Client Module
          2. The Notes Service
      3. 12.3. Summary
    5. 13. Silverlight: IronPython in the browser
      1. 13.1. Introduction to Silverlight
        1. 13.1.1. Dynamic Silverlight
          1. Embedding the Silverlight Control
          2. The xap File
        2. 13.1.2. Your Python application
        3. 13.1.3. Silverlight controls
          1. Using Controls
          2. Using the Extended Controls
          3. Extended Controls from XAML
        4. 13.1.4. Packaging a Silverlight application
      2. 13.2. A Silverlight Twitter client
        1. 13.2.1. Cross-domain policies
        2. 13.2.2. Debugging Silverlight applications
        3. 13.2.3. The user interface
          1. The Grid, the ScrollViewer, and the HyperlinkButton
        4. 13.2.4. Accessing network resources
          1. The WebClient
          2. Parsing the XML from Twitter
          3. Posting With WebClient
        5. 13.2.5. Threads and dispatching onto the UI thread
        6. 13.2.6. IsolatedStorage in the browser
      3. 13.3. Videos and the browser DOM
        1. 13.3.1. The MediaElement video player
        2. 13.3.2. Accessing the browser DOM
      4. 13.4. Summary
  9. 4. Reaching out with IronPython
    1. 14. Extending IronPython with C#/VB.NET
      1. 14.1. Writing a class library for IronPython
        1. 14.1.1. Working with Visual Studio or MonoDevelop
        2. 14.1.2. Python objects from class libraries
        3. 14.1.3. Calling unmanaged code with the P/Invoke attribute
        4. 14.1.4. Methods with attributes through subclassing
          1. Compiling Assemblies Without Visual Studio
      2. 14.2. Creating dynamic (and Pythonic) objects from C#/VB.NET
        1. 14.2.1. Providing dynamic attribute access
        2. 14.2.2. Python magic methods
        3. 14.2.3. APIs with keyword and multiple arguments
      3. 14.3. Compiling and using assemblies at runtime
      4. 14.4. Summary
    2. 15. Embedding the IronPython engine
      1. 15.1. Creating a custom executable
        1. 15.1.1. The IronPython engine
        2. 15.1.2. Executing a Python file
          1. Setting the Command-line Arguments
          2. Setting the Search Path
          3. Handling Python Exceptions
      2. 15.2. IronPython as a scripting engine
        1. 15.2.1. Setting and fetching variables from a scope
        2. 15.2.2. Providing modules and assemblies for the engine
        3. 15.2.3. Python code as an embedded resource
      3. 15.3. Python plugins for .NET applications
        1. 15.3.1. A plugin class and registry
        2. 15.3.2. Autodiscovery of user plugins
        3. 15.3.3. Diverting standard output
        4. 15.3.4. Calling the user plugins
      4. 15.4. Using DLR objects from other .NET languages
        1. 15.4.1. Expressions, functions, and Python types
        2. 15.4.2. Dynamic operations with ObjectOperations
        3. 15.4.3. The built-in Python functions and modules
        4. 15.4.4. The future of interacting with dynamic objects
      5. 15.5. Summary
  10. A. A whirlwind tour of C#
    1. A.1. Namespaces
    2. A.2. Using directive
    3. A.3. Classes
    4. A.4. Attributes
    5. A.5. Interfaces
    6. A.6. Enums
    7. A.7. Structs
    8. A.8. Methods
      1. A.8.1. Virtual and override methods
      2. A.8.2. Other method modifiers
      3. A.8.3. Parameter passing
      4. A.8.4. Method overloading
    9. A.9. Delegates
    10. A.10. Events
    11. A.11. Operator overloading
    12. A.12. Properties and indexers
    13. A.13. Loops
      1. A.13.1. while loop
      2. A.13.2. do loop
      3. A.13.3. for loop
      4. A.13.4. foreach loop
    14. A.14. Casts
    15. A.15. if
    16. A.16. switch
    17. A.17. try/catch/finally and throw
    18. A.18. lock
    19. A.19. new
    20. A.20. null
    21. A.21. using statement
    22. A.22. Operators
    23. A.23. Generics
  11. B. Python magic methods
    1. B.1. Object creation
    2. B.2. Comparison
    3. B.3. Miscellaneous
    4. B.4. Containers and iteration
      1. B.4.1. Mapping and sequence protocol methods
      2. B.4.2. Generator expressions and conditional expressions
    5. B.5. Conversion to string
    6. B.6. Attribute access
    7. B.7. Numeric types
      1. B.7.1. Arithmetic operations
      2. B.7.2. Conversion between numeric types
    8. B.8. Context managers and the with statement
    9. B.9. The descriptor protocol
    10. B.10. Magic attributes
    11. B.11. Functions and modules
  12. C. For more information
    1. C.1. IronPython and Python language sites
    2. C.2. Mailing lists and newsgroups
    3. C.3. Python and IronPython code examples
    4. C.4. Learning Python
    5. C.5. Blogs
    6. C.6. IronPython team
    7. C.7. Silverlight
    8. C.8. .NET and Mono
    9. C.9. Dynamic languages on .NET
    10. C.10. IDEs and tools

Product information

  • Title: IronPython in Action
  • Author(s): Michael Foord, Christian J. Muirhead
  • Release date: March 2009
  • Publisher(s): Manning Publications
  • ISBN: 9781933988337