Monday, February 16, 2009

IronPython in Action

IronPython in Action is an upcoming book from Manning Publications by Michael Foord and Christian Muirhead. It describes the intersection of the .Net Framework and IronPython with a large number of examples, covering a reasonably large swath of the .Net Framework: WinForms, WPF, ASP.NET, Silverlight, XML processing, and more.

IronPython in Action is exactly what it says on the tin: this book is a large collection of real-world(ish) examples showing how the .Net Framework can be used in conjunction with the Python language. To go with this book, I'd recommend a Python language reference and a reference for whichever .Net technologies you are working with. This book doesn't try to by the definitive reference for either Python or .Net; it focuses on the intersection of the two. For the most part there is very little mismatch between the two; C# (or VB) can be translated into Python and everything will (mostly) work fine.

The first part of the book gives a very quick overview of the history of Python, .Net, and IronPython; this is followed by a brief introduction to the Python language and how to access various .Net features (classes, enums, events, etc.) from IronPython.

The second part of the book is the creation of a simple WinForms application that is used to demonstrate some .Net framework classes and some Python design techniques and idioms. In particular, it covers the basics of WinForms programming and XML processing, along with some insights into patterns and unit testing in Python. The final chapter of this section (Ch. 8) covers Python metaprogramming and protocols, and some gory details about interacting with the CLR – in particular, arrays and reference parameters are tricky to use from the Python side.

Part 3 of the book covers a wide range of topics: WPF, systems administration, ASP.NET, database access, web services, and Silverlight. Again, none of these subjects are covered in detail – each could (and has!) fill a couple of volumes. Instead, the authors focus on getting everything going and covering some of the issues that may come up. The ASP.NET demonstration is particularly clever as it is simply a recasting of the example editor from Part 2 as a web site; the WinForms code had to be swapped for WebForms code, but none of the rest of the code (the controllers and the model) had to be changed. The database section uses PostgreSQL as the  database target; the only major difference to SQL Server would be the connection string (and some edge cases in the SQL).

The final section of the book covers embedding and extending IronPython. It is a nice, simple introduction to building a C# (or VB) IronPython extension, followed by demonstrating how to embed the IronPython engine into a .Net program. The embedding section, in particular, has some tricks that are hard to learn otherwise – the functionality is a bit spread out, and the current documentation is only passable. Thankfully, Ch. 15 will be unnecessary (or at least very different) with C# 4.0.

I wasn't really sure what to expect when given this book. Having read it, I now see that it is exactly what it says it is – IronPython, in action. For any serious work with IronPython (more than just using it as an alternative Python interpreter), than this book will save you a ton of learning time. The examples are clear, concise, and – most importantly – realistic. Even if you already know Python and .Net inside and out, but are new to IronPython, this book will save you some headaches learning about how they interact.