During 2008, dynamic languages picked up a lot of momentum. Indeed, in IronPython In Action, Foord and Muirhead describe this momentum somewhat amusingly: “dynamic languages are trendy; all the alpha-geeks are using them!” With what is the largest known IronPython codebase forming a large part of their careers, the authors are well-placed to provide us with accurate and authorative information coupled with practical code examples.

Should I Buy This Book?

You should buy this book if you wish to understand where the Dynamic Language Runtime fits into your .NET toolkit. If you are looking for better ways to test your code, dynamic languages and duck typing lend themselves to mock objects: you should buy this book to discover the gems of chapter 7. If you are searching for the Holy Grail that is an extensible application architecture, you need this book for chapter 15 alone.

If you are an existing .NET programmer, this book is for you: it will carry you through your first steps into the dynamic language runtime and will provide you with a good grounding in the Python language. If you are new to programming and are unsure where to start, this is also the book for you: it provides a high-level overview at the start of each chapter followed by a reasonably detailed explanation. It may be 400 pages, however it’s free from lengthy introductions and reams of source code. Extensive use of footnotes provide links to further information if it’s required.

Overview

Spread over 15 chapters and some 400 or so pages, IronPython In Action takes advantage of Manning’s relaxed reading layout. The chapters themselves are grouped logically into four main areas:

Part 1: Getting Started With Iron Python. Three chapters that cover an introduction to the Python language, its origins and its structure. We’re also given our first foray into using .NET objects in the IronPython implementation.

Part 2: Core Development Techniques. Five solid chapters covering the post-introduction territory. Design patterns, first class functions, properties, dialogs, agile testing, metaprogramming and protocols are covered in some detail.

Part 3: IronPython and Advanced .NET. A further five chapters that focus solidly on using IronPython in the .NET environment. Windows Presentation Foundation, ASP.NET, database access, web services and, very importantly, working with Silverlight using IronPython.

Part 4: Reaching Out With IronPython. Two chapters finish the main content: using C# (and, if you must, Visual Basic) with IronPython and incorporating the IronPython langauge into your own application.

It’s worth noting that this book is very well organised. The chapters lead into each very nicely, however they can be read in isolation. The authors do make fine use of each chapter’s summary to provide this lead-in to the next chapter.

In Detail

What you get – chapter by chapter

Part 1: Getting Started With Iron Python

1. A new language for .NET

Positioning IronPython in the plethora of languages now available for .NET and the Common Language Runtime, this chapter provides an excellent and waffle-free explanation of IronPython’s history/background – it may be a new language for .NET, however the Python language has considerable heritage. It also covers how it integrates with the CLR and what we might expect to be able to achieve with IronPython. Indeed, being able to use IronPython within client-side web applications built using Silverlight is introduced to us early in this chapter – this is a point well-made by the authors.

Python fans of old will appreciate this chapter’s coverage of the productivity gains offered by IronPython, particularly if you are hoping to target multiple processors and to introduce multi-threading into your application. Similarly, existing .NET programmers will be pleased to see that there is an explanation of how the CLR handles dynamic languages. If you have been focusing on the CLR using C#, Visual Basic, Delphi, etc. you may have missed or chosen to miss the arrival of dynamic languages and the Dynamic Language Runtime (DLR). Over the course of a few pages, this chapter covers many of the salient points that dynamic languages have to offer – it covers a lot of ground very quickly, which is a good thing in this reviewer’s opinion.

In preparation for Chapter 2, the authors spend the latter 10 or so pages positioning [Iron]Python as multi-paradigm programming (procedural, functional and metaprogramming). We’re also treated to our first look at some Python code. Of course, in order to use IronPython, there are some prerequisites: the authors provide download links to all that is required to get started with ipy.exe, the interactive interpreter. The examples themselves are very elementary, but that is to be expected this early on in the book. Actually, the examples are only elementary on the surface, they do serve to remind us about the inner workings of a .NET application.

The authors make extensive use of footnotes; there are 42 in chapter one alone, an unintentional reference to Douglas Adams’ question to “Life The Universe and Everything”: whilst IronPython In Action is a good book, I’m afraid to report it that it doesn’t answer that question! Footnotes are a good thing, the keep the main text free from interruptions yet provide us with a place to look for further and/or related information.

2. Introduction to Python

Chapter 1 set the scene very well, we now have an understanding of where Python came from, how it works and what we can expect to do with IronPython in .NET. For the sake of completeness, this chapter’s 36 or so pages cover the features and constructs that make up the Python programming language. As .net programmers, you may well be familiar with most of this chapter. However, as newcomers to programming or newcomers to the Python language, this chapter is essential reading.

In a nutshell, you’ll be reading about Python datatypes, strings, numbers, lists, tuples, dictionaries, sets, “none and the booleans”, names and object references, mutable and immutable objects, statements and expressions, conditionals and loops, functions including callable objects, built-in functions, classes, exception handling closures and scoping rules, list comprehensions, “modules, packaging and importing”, docstrings and the Python Standard Library. Once again, the authors cover these in enough detail such that you don’t find yourself tiring or getting bored reading through page after page…29 footnotes provide you with places to go for more information.

3. .NET objects and IronPython

Chapter 1 introduced us to IronPython and touched on .NET. Chapter 2 explained the Python language. Chapter 3 brings that learning together by providing a full example of IronPython in action [sic].

If you are familiar with a .NET application built using C# or Visual Basic, and you are comfortable reading MSDN documentation, you’ll find this chapter easy reading. It’s in this chapter that the use of “highlighted keypoints”, indented paragraphs of text that contain pertinent information, becomes obvious. The authors have clearly made good decisions to include certain items of information in-line rather than the possible use of footnotes.

It’s a very granular chapter, the authors take us through code line by line, property by property, with careful interjection of explanatory text. As a seasoned .NET programmer, I found this chapter a good read and a reminder of my university days looking at functional programming languages. 16 or so pages take us through the creation of a .NET “Hello World” application built using IronPython. Expect to see coverage of the Form class, .NET types: structures, enumerations and collections, event handling, delegates, event handlers in IronPython and subclassing .NET types.

This chapter does make mention of a few topics that suggest “some experience is required”, however it is by no means expected. It provides a good overview of how IronPython interacts with the .NET framework and its inherent types. Experienced .NET programmers will be very comfortable after reading this chapter. Indeed, if you too leave this chapter feeling that some experience of design patterns is assumed, fear not, it is covered in the next chapter.

Part 2: Core Development Techniques

4. Writing an application and design patterns with IronPython

In part 2 of IronPython In Action, we can expect to read about many of the development techniques that are central to any programming language, in this case with a Python slant. Chapter 4 kicks off a look at building an application that makes use of the Model View Controller design pattern, a far cry from the “Hello World” example we saw earlier. This chapter introduces a grander and more practical application, a multi-page document editor called MultiDoc.

Spread over 30 pages, we are introduced to data modelling, duck typing, the model view controller (MVC), the command pattern, savefiledialog, writing files (from .NET and Python), more on exception handling, the save and saveas commands, menu classes and lambda functions, .NET classes for toolbars and images, and GUI operations.

The explanation of the MVC is concise and sets the scene very well indeed. Other books dedicate more space to a similar explanation, however this is a book about IronPython not design patterns! I found the ground-up code examples, again demonstrating very granular development, laid the foundations for a solid understanding of IronPython, its usage and its nuances.

5. First class functions “in action” with XML

Building an application in today’s Internet-aware world will ultimately require the use of XML at some stage in its development. .NET itself provides extensive support for the creation and consumption of XML documents and data from a vast array of sources. IronPython, as chapter 1 touched on, can be used to build client-side web applications built using Silverlight. For that reason, amongst many more, we should expect IronPython to share .NET’s love for XML. This chapter, spanning around 24 pages, provides us with a grounding in IronPythons XML support. It also explains the concept of a first class function, an important concept in the world of functional programming languages.

Of course, if XML has managed to pass you by, you’ll be pleased to know that the authors provide a single page overview of what XML looks like and they touch on a few of its nuances. With that introduction out of the way, it’s straight into the .NET XmlWriter class. The authors fine-grained approach is starting to become very “all inclusive”: as I’m reading about, for example, XmlWriter I’m thinking about all the things I might want to be able to do with an XmlWriter object – the authors, very timely, then start discussing the properties of XmlWriterSettings.

The line by line approach of coding/explanation continues throughout this chapter. The authors build up MultiDoc using the creation of an XML document as the basis of the code samples. Useful points and gotchas, such as XML document conformance levels are touched on as and when required. Expect to read about XmlWriter, a custom DocumentWriter class, reading XML using XmlReader and a custom XmlDocumentReader.

6. Properties, dialogs, and Visual Studio

So far IronPython In Action has introduced us to the MVC design pattern. This chapter extends that knowledge by discussing how the Observer pattern needs to be integrated into the MultiDoc application. It’s clear that the authors have considerable knowledge that they wish to pass on to us, particularly the importance of application design decisions and the kind of problems that design patterns can either solve or alleviate. That being said, this is not “Design Patterns In Action”, so don’t expect a deep dive into the inner workings of the MVC or Observer patterns, but do expect to see them implemented using IronPython.

This chapter demonstrates the need for a visual development environment. The code samples, whilst kept to the essentials, do make reference to the positioning of controls on a form. It is for that reason that the latter pages, of the 26 that make up this chapter, cover the use of Visual Studio Express and IronPython, the Visual Studio Forms Designer particularly. In keeping with IronPython (ipy.exe), Visual Studio Express is also free; despite earlier references to a commercial Python for .NET Integrated Development Environment (IDE), you can develop IronPython applications without spending any money.

Using IronPython and C# (or Visual Basic for that matter) is the covered in a later chapter. However, the Visual Studio Express coverage in this chapter does suggest that there is some element of cross-language work taking place. Indeed there is: the authors are making use of the code generated from the Visual Studio Express Forms Designer, they then make reference to it in MultiDoc. This avoids the need to use write IronPython code that handles the creation and design of a user interface…just let use the Forms Designer to create the user interface, then let IronPython subclass from the generated code. An interesting approach and an approach that has served these authors well.

7. Agile testing: where dynamic typing shines

In today’s software development discipline, design patterns are considered best practice. Equally, it’s difficult to avoid mention of “agile” and “unit testing”. Chapter 7 covers testing of your IronPython code, particularly automated testing using the Python test framework called “unittest”.

This chapter does keep its explanation of unit testing to a minimum, presumably because so much has been written about it elsewhere (including this reviewer’s own content). Indeed, the reader is referred to the footnotes for further information.

Python, being a dynamic language, lends itself to different testing techniques, a point not missed by the authors when they discuss Testing with Mocks. Statically typed languages, where the compiler expects types to match, means that testing is limited to real objects of an expected type. Dynamic languages can take advantage of duck typing, whereby objects need only implement attributes/methods that are present in test cases. This chapter explains the differences between mock objects and stubs, and makes good and appropriate reference to definitive sources on the subject.

Spanning 30 pages, this chapter covers a lot of testing material, applying it to the MultiDoc application. Expect to read about unit tests, functional tests, regression tests, setUp and tearDown, test suits with multiple modules, testing with mocks, the art of the monkey patch (modifying live objects), the Listener design pattern, mocks and dependency injection, interacting with the GUI thread and asynchronous interactions.

8. Getting deeper into IronPython: metaprogramming, protocols, and more

As you might expect at this stage in the book (over half way through) and with a chapter title that includes the words metaprogramming and protocols, the “hello world” days are well behind us. This is 32-page chapter takes us on a deep dive into the Python programming language and demonstrates some of the true “magic” that is on offer.

The authors make no secret of the fact that they work for Resolver Systems and that they have a very large IronPython codebase. Something that caught my eye in this chapter was the syntax: worksheet.A1 = 3 as a means of setting the value of a spreadsheet’s cell A1. In static programming languages we would have to provide a property definition for A1. Implementing A2,A3, etc. would of course be a laborious exercise, never mind the unreadable code that would follow. Using a combination of attributes and magic methods the authors present us with the six lines of IronPython that are required to handle this scenario in a dynamic language: very impressive, very tidy, very concise and very readable.

Expect to read about protocols vs interfaces, magic methods, representing string as objects, truth testing of objects, operator overloading, iteration, generators, equality and inequality, dynamic attribute access, metaprogramming, IronPython and the CLR, .NET Arrays, overloading methods,parameter accessors, value types, .NET interfaces and attributes and the static compilation of IronPython code.

Part 3: IronPython and Advanced .NET

9. WPF and IronPython

This chapter takes IronPython right into the heart of .NET with discussion about the Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), Windows Workflow Foundation (WWF) and Windows Cardspace. By virtue of the discussion about WPF, the authors also spend some time introducing XAML, the eXtensible Application Markup Language.

As with the previous chapters, we are presented with a line by line example of how to go about creating a “hello world” application using WPF. This is actually rather good as it gives the authors a chance to introduce the salient WPF namespaces. In contrast to the “WPF via code”, we’re also given the same example using XAML, as mark up. With the introduction to WPF over, this chapter then dives into dealing with grid layouts, WPF controls, ComboBox and CheckBox, Image control, the Expander control, the ScrollViewer, TextBlock and the XamlWriter class: it’s worth noting that we’re given sample code that demonstrates each these controls.

Chapter 9 goes on to take a very interesting direction by discussing XML Paper Specification (XPS) Documents and the management of flowing content. The remaining 6 or so pages out this chapter’s 30 pages discuss the use of the FlowDocument classes for presenting flowing content in a WPF application. The reason that I think this is an interesting direction, is because WPF and XAML, coupled with XPS, are meshing the work of developers and designers in ways that were could never have been imagined even a few years ago.

10. Windows system administration with IronPython

With its roots as an interpreted language, it should come as little surprise that many folks see Python as a scripting language. Indeed, Python is an excellent scripting language but it’s also a good language for developing complete systems. However, that’s not the focus of this chapter, instead we’re treated to 30 pages of nitty-gritty system admin-type operations. Expect to read about: Shell Scripting with IronPython, reading configuration files, traversing directories/folders and filtering filenames.

Sitting nicely inside the world of scripting, Windows Management Instrumentation (WMI) is given some 10 pages of coverage. Since WMI is at the heart of the Microsoft Windows operating system, it’s excellent to see it being covered and with copious code examples written using IronPython.

However, no mention of scripting in a Microsoft environment would be complete without a mention of PowerShell. Indeed, the remaining 14 or so pages cover: using PowerShell from IronPython, Using IronPython from PowerShell, Embedding IronPython in PowerShell,creating STA threads, Asynchronous Events without Blocking and Calling Python and Returning Results. There’s a lot of good detail presented in this section, much of it demonstrating the heavy lifting work that you might find yourself doing inside the PowerShell environment.

11. IronPython and ASP.NET

This chapter contains a thankfully brief introduction to ASP.NET’s predecessor, ASP. More space is given to an explanation of ASP.NET and IronPython: this is a good thing as it allows developers who are new to ASP.NET to understand why web applications are built the way they are. It’s pretty much in keeping with other chapters of IronPython In Action, the introduction to ASP.NET is just enough to ensure the reader will understand the remainder of the chapter.

Adding IronPython to ASP.NET necessitates the installation of Visual Studio Express and IronPython for ASP.NET, the URLs are provided in this chapter. The standard ASP.NET compilation and execution model and the differences that are required for use with dynamic languages. So much so, Microsoft’s DLR team have built a new “compilation” model, as covered by David Ebbo’s whitepaper.

The bulk of this chapter’s 28 pages covers the implementation of a web-based version of the MultiDoc application that was introduced earlier. To do this, the authors cover such things as: handling [ASP.NET] events in IronPython, the ASP.NET infrastructure, the App_Script folder, Global.py, Web.config, page structure, databinding, code-behind, page lifecycle, handling viewstate and conversion MultiDoc into a user control.

12. Databases and web services

32 pages provide an overview of database technologies and web services. By now you will know that the authors like to provide a high-level overview of the chapter contents, chapter 12 is no different. We are treated to a brief foray into the relational model, with gratuitous reference to the man himself, Edgar Codd. We’re also presented with an introduction to ADO.NET and all that it entails: DataSets, Data Providers, Connections, Commands, etc. Interestingly, the authors have chosen to use PostGreSQL as the underlying database for this chapter’s example.

On the whole, this chapter is standard stuff. You’ll learn how to connect to a database, execute commands, set parameters, query the database, read multi-row result sets, work with transactions, and use DataAdaptors and DataSets. If you’re comfortable using ADO.NET in a statically-typed language such as C#, little has changed in the dynamic world.

Today’s connected world wouldn’t be the same without web services. This section provides us with a very good overview of such things as: SOAP, HTTP Methods (GET, POST, PUT and DELETE), WDSL, and REST. A simple note taking web application makes up the code sample. It presents the IronPython required to implement note addition, deletion, update and list. When the second edition of this book is commissioned, I would like to see the web services content given a chapter of its own.

13. Silverlight: IronPython in the browser

If I had to pick one chapter to call my favourite chapter, this is it. Why? Well it mentions three words that appeal to me: Silverlight, Tetris and Twitter. I tell a lie, it also mentioned Deep Zoom which is another watch word for me. I was a little disappointed to learn that the Tetris cover was minimal, however there’s some good stuff in the Twitter coverage so my disappointment was short lived.

Early in this chapter we are shown how to load some XAML using an IronPython Silverlight application, it’s only a few lines of code but it’s nonetheless impressive. We’re then treated to coverage of a handful of the common Silverlight controls, along with some code to work with them.

I was pleased to read the section about Packaging a Silverlight application: it covers enough of what you need to know in order to package a Silverlight application and does so without going into too much detail. It’s a great overview of what required and enough to make this section entirely readable.

A Twitter application is a relatively simple thing to develop. However, it does require using a variety of technologies. We are, after all, retrieving selected data from a web site then presenting it to the user. The user may also interact with that data, which adds a little complexity. The authors understand this and provided a great explanation of the underlying issues that we might have implementing such an application. I was pleased to read about: cross domain policies and debugging Silverlight applications – both of which are supported by code samples.

The Twitter application is developed over the course of 13 of this chapter’s 30 pages. Accessing network resources, parsing XML, working with threads (particularly the infamous UI thread) and using isolated storage are covered in order to take the application to completion.

Part 4: Reaching Out With IronPython

14. Extending IronPython with C#/VB

This chapter should not be a surprise to any reader. The CLR allows multi-language development in the statically-typed world, so it stands to reason that IronPython has some means of cross-language interaction. No time is wasted, within the first few pages of this penultimate chapter’s 26 pages, we’re presented with C# and Visual Basic code that is callable from IronPython.

It’s good to see that calling unmanaged code with the P/Invoke attribute warrants some 5 pages of this attention. Whilst the code samples are themselves short, some of the lines of code are a little too long, however this isn’t a criticism, it’s just the way C# and Visual Basic are: in comparison to Python, they are somewhat “verbose”.

Creating Python classes that can be consumed by C# or Visual Basic is another matter as we are essentially mixing a dynamic language with a static language. That being said, the authors are very quick to point out that it is possible using the .NET attribute [SpecialName]. We’re given 6 pages that cover using dynamic (or “Pythonic”) objects from a C# or Visual Basic perspective. I’m grateful for the simplicity the authors go to in order to keep their example easy to read, this is potentially sore head territory.

The remainder of this chapter covers Python’s magic methods, APIs with keyword arguments and/or multiple arguments and compiling/using assemblies at runtime.

15. Embedding the IronPython engine

Spanning some 34 pages this chapter is by far the most technical chapter in this book. And as you might expect, it does what it says on the tin, it discusses in some detail how we might incorporate the IronPython engine inside a C# or a Visual Basic application.

The ability to run Python scripts from within your CLR-based .NET application is frankly astounding, the authors present us with everything we need in order to achieve this in just a few lines of code. Similarly, the ability to treat Python code as part of your application’s “plug in” architecture makes extending and customising your applications without altering the primary codebase so much easier. Given that such functionality, extensible application architectures, are becoming commonplace, this chapter serves us well. It’s examples are to-the-point and are supported by sensible quantities of explanatory narrative.

This chapter closes with coverage of how we might use DLR objects from .NET languages. Of course, and I am stealing the authors’ thunder here, it’s possible to modify Python code without the need for a re-compile (Python code can even be generated on-the-fly).

Resources

Michael Foord’s blog

This book’s web-site, including source code

This book’s publisher

Jim Hugunin’s Thinking Dynamic