IronPytho IN ACTION Michael J Foord Christian Muirhead FOREWORD BY JIM HUGUNIN MANNING IronPython in Action Licensed to Deborah Christiansen Licensed to Deborah Christiansen IronPython in Action MICHAEL J FOORD CHRISTIAN MUIRHEAD MANNING Greenwich (74° w long.) Licensed to Deborah Christiansen For online information and ordering of this and other Manning books, please visit www.manning.com The publisher offers discounts on this book when ordered in quantity For more information, please contact Special Sales Department Manning Publications Co Sound View Court 3B fax: (609) 877-8256 Greenwich, CT 06830 email: orders@manning.com ©2009 by Manning Publications Co All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15% recycled and processed without the use of elemental chlorine Manning Publications Co Sound View Court 3B Greenwich, CT 06830 Development Editor: Copyeditors: Typesetter: Cover designer: Jeff Bleil Andrea Kaucher, Linda Recktenwald Gordan Salinovic Leslie Haimes ISBN 978-1-933988-33-7 Printed in the United States of America 10 – MAL – 14 13 12 11 10 09 Licensed to Deborah Christiansen This book is dedicated to the littlest gangster and the mushroom, who endured much throughout its creation Licensed to Deborah Christiansen Licensed to Deborah Christiansen brief contents PART PART PART GETTING STARTED WITH IRONPYTHON 1 ■ A new language for NET ■ Introduction to Python ■ NET objects and IronPython 62 29 CORE DEVELOPMENT TECHNIQUES 79 ■ Writing an application and design patterns with IronPython 81 ■ First-class functions in action with XML 110 ■ Properties, dialogs, and Visual Studio 133 ■ Agile testing: where dynamic typing shines ■ 157 Metaprogramming, protocols, and more 183 IRONPYTHON AND ADVANCED NET 215 ■ WPF and IronPython 217 10 ■ Windows system administration with IronPython 244 11 ■ IronPython and ASP.NET 273 vii Licensed to Deborah Christiansen viii PART BRIEF CONTENTS 12 ■ Databases and web services 299 13 ■ Silverlight: IronPython in the browser 329 REACHING OUT WITH IRONPYTHON 357 14 ■ Extending IronPython with C#/VB.NET 359 15 ■ Embedding the IronPython engine 386 Licensed to Deborah Christiansen contents foreword xvii preface xx acknowledgments xxii about this book xxiii PART GETTING STARTED WITH IRONPYTHON 1 A new language for NET 1.1 An introduction to IronPython What is IronPython? A brief history of IronPython IronPython for Python programmers 11 IronPython for NET programmers 13 ■ ■ 1.2 Python on the CLR 15 Dynamic languages on NET and the DLR 15 Silverlight: a new CLR 18 The Python programming language 20 Multiple programming paradigms 22 ■ ■ 1.3 ■ Live objects on the console: the interactive interpreter 23 Using the interactive interpreter 23 The NET framework: assemblies, namespaces, and references 25 Live objects and the interactive interpreter 25 Object introspection with dir and help 27 ■ ■ ■ 1.4 Summary 28 ix Licensed to Deborah Christiansen