About this Book

IronPython is a radical project for Microsoft. It is the first project to be released under their Ms-PL (Microsoft Public License) open source license. It is also a radically different language from the ones that Microsoft has traditionally promoted for the .NET framework. IronPython is an implementation of the popular programming language Python for .NET. Python is an open source, object-oriented, dynamically typed language in use by organizations like Google, NASA and Pixar. Python is a multi-paradigm language, and brings new possibilities to .NET programmers: not just the added flexibility of dynamic typing, but programming styles such as functional programming and metaprogramming. For Python programmers the powerful runtime, with its JIT compiler and huge range of .NET libraries, also presents new opportunities.

The goal of IronPython in Action is not just to teach the mechanics of using IronPython, but to demonstrate the power and effectiveness of object-oriented programming in the Python language. To this end we cover best practices in API design, testing, and the use of design patterns in structured application development. In part this is to dispel the myth that dynamic languages are merely scripting languages; but mostly it is to help you make the best of the language and the platform on which it runs.

The addition of Python to the range of languages available as first-class citizens in .NET reflects the changes happening in the wider world of programming. No one says it better than Anders Hejlsberg, the architect of C#, when asked by Computer World[5] what advice he had for up-and-coming programmers:

Go look at dynamic languages and meta-programming: those are really interesting concepts. Once you get an understanding of these different kinds of programming and the philosophies that underlie them, you can get a much more coherent picture of what’s going on and the different styles of programming that might be more appropriate for you with what you’re doing right now.

Anyone programming today should check out functional programming and meta-programming as they are very important trends going forward.

Who should read this book?

IronPython in Action is particularly aimed at two types of programmers: Python programmers looking to take advantage of the power of the .NET framework or Mono for their applications, and .NET programmers interested in the flexibility of dynamic languages. It assumes no experience of either Python or .NET, but does assume some previous programming experience. If you have some programming experience, but have never used either of these systems, you should find IronPython in Action an accessible introduction to both Python and .NET.

Just as Python is suited to an enormous range of problem domains, so is IronPython. The book covers a range of different uses of IronPython: from web development to application development, one-off scripting to system administration, and embedding into .NET applications for extensible architectures or providing user scripting.

Roadmap

This book contains 15 chapters organized into four parts.

Part 1 Getting started with IronPython—The first part of the book introduces the fundamental concepts behind developing with IronPython and the .NET framework. Chapter 1 introduces IronPython along with key points of interest for both Python and .NET programmers. It finishes by diving into IronPython through the interactive interpreter; a powerful tool for both Python and IronPython. Chapter 2 is a Python tutorial, including areas where IronPython is different from the standard distribution of Python known as CPython. Where chapter 2 is particularly valuable to programmers who haven’t used Python before, chapter 3 is an introduction to the .NET framework. As well as covering the basic .NET types (classes, enumerations, delegates, and the like), this chapter shows how to use them from IronPython, ending with a more fully featured “Hello World” program than created in chapter 1.

Part 2 Core development techniques—The next part extends your knowledge of the Python language and the classes available in the .NET framework. It does this by demonstrating a structured approach to Python programming by developing the MultiDoc application using several common design patterns. Figure 1 shows MultiDoc as it looks by the end of chapter 6. Along the way we’ll work with Windows Forms, lambdas, properties, decorators, XML, first-class functions, and using C# class libraries created in Visual Studio.

This part finishes by covering testing techniques, to which dynamic languages are especially suited, and some more advanced Python programming techniques such as metaprogramming. The end of chapter 8 contains valuable information about how IronPython interacts with aspects of the Common Language Runtime, information that neither experience with Python nor another .NET framework language alone will furnish you with.

Part 3 IronPython and advanced .NET—The third part takes IronPython into practical and interesting corners of .NET. Each chapter in this part takes an area of .NET programming and shows how best to use it from IronPython.

  • Chapter 9Writing desktop applications using the Windows Presentation Foundation user interface library

  • Chapter 10System administration, including shell scripting, WMI, and PowerShell

  • Chapter 11Web development with ASP.NET

  • Chapter 12Databases and web services

  • Chapter 13Silverlight

Part 4 Reaching out with IronPython—The final part of this book takes IronPython out into the wilds of a polyglot programming environment. Chapter 14 shows how to create classes in C# and VB.NET for use from IronPython. Of special importance here is creating APIs that feel natural when used from Python, or even giving your objects dynamic behavior. Chapter 15 reverses the situation and embeds IronPython into .NET applications. It tackles the interesting and challenging problem of using dynamic objects from statically typed languages like C# and VB.NET. For many .NET programmers, being able to embed IronPython into applications, to provide a ready-made scripting solution, is the main use case for IronPython.

The MultiDoc application as it appears in part 2

Figure 1. The MultiDoc application as it appears in part 2

There are also three appendixes. Appendix A covers the basics of C# and explains the core concepts of the language. Appendix B shows how to create your own objects in Python by implementing its protocol methods. Appendix C has a list of online resources with more information about IronPython and dynamic languages on the .NET framework.

Code conventions and downloads

This book includes copious numbers of examples in Python, C#, and VB.NET. Source code in listings, or in text, is in a fixed-width font to separate it from ordinary text. Additionally, method names in text are also presented using fixed-width font.

C# and VB.NET can be quite verbose, but even Python is not immune to the occasional long line. In many cases, the original source code (available online) has been reformatted, adding line breaks to accommodate the available page space in the book. In rare cases, even this was not enough, and listings will include line continuation markers. Additionally, comments in the source code have been removed from the listings.

Code annotations accompany many of the source code listings, highlighting important concepts. In some cases, numbered bullets link to explanations that follow the listing.

IronPython is an open source project, released under the very liberal Ms-PL software license. IronPython is available for download, in source or binary form, from the IronPython home page: www.codeplex.com/IronPython.

The source code for all examples in this book is available from Manning’s web site: www.manning.com/foord. It is also available for download from the book’s website: www.ironpythoninaction.com/.

Author Online

The purchase of IronPython in Action includes free access to a private web forum run by Manning Publications, where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/ironpythoninaction. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions lest their interest stray! The Author Online forum and the archives of previous discussions will be accessible from the publisher’s web site as long as the book is in print.

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[6] and IronPython-URLs[7] 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.

About the title

By combining introductions, overviews, and how-to examples, the In Action books are designed to help learning and remembering. According to research in cognitive science, the things people remember are things they discover during self-motivated exploration.

Although no one at Manning is a cognitive scientist, we are convinced that for learning to become permanent it must pass through stages of exploration, play, and, interestingly, re-telling of what is being learned. People understand and remember new things, which is to say they master them, only after actively exploring them. Humans learn in action. An essential part of an In Action guide is that it is example-driven. It encourages the reader to try things out, to play with new code, and explore new ideas.

There is another, more mundane, reason for the title of this book: our readers are busy. They use books to do a job or solve a problem. They need books that allow them to jump in and jump out easily and learn just what they want just when they want it. They need books that aid them in action. The books in this series are designed for such readers.

About the cover illustration

The caption of the figure on the cover of IronPython in Action reads “An Ironworker.” The illustration is taken from a French book of dress customs, Encyclopedie des Voyages by J. G. St. Saveur, published in 1796. Travel for pleasure was a relatively new phenomenon at the time and illustrated guides such as this one were popular, introducing both the tourist as well as the armchair traveler to the inhabitants of other regions of the world, as well as to the regional costumes and uniforms of French soldiers, civil servants, tradesmen, merchants, and peasants.

The diversity of the drawings in the Encyclopedie des Voyages speaks vividly of the uniqueness and individuality of the world’s towns and provinces just 200 years ago. This was a time when the dress codes of two regions separated by a few dozen miles identified people uniquely as belonging to one or the other, and when members of a social class or a trade or a profession could be easily distinguished by what they were wearing.

Dress codes have changed since then and the diversity by region and social status, so rich at the time, has faded away. It is now often hard to tell the inhabitant of one continent from another. Perhaps, trying to view it optimistically, we have traded a cultural and visual diversity for a more varied personal life. Or a more varied and interesting intellectual and technical life.

We at Manning celebrate the inventiveness, the initiative, and the fun of the computer business with book covers based on the rich diversity of regional life two centuries ago brought back to life by the pictures from this travel guide.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.162.51