CYAN MAGENTA YELLOW BLACK PANTONE 123 C Books for professionals by professionals ® The EXPERT’s VOIce ® in NET Companion eBook Available Dear Reader, The NET framework, introduced in early 2002, is undergoing a quiet revolution It’s easy to miss if you’re not paying attention The book you hold in your hands reveals just a part of that revolution IronPython is a radical new direction for NET developers who are used to statically typed languages such as C#, and one that opens many doors in terms of programming possibilities Mixing the time-tested and popular Python language with the stable and powerful NET framework, IronPython bridges the gap between programming styles and provides the best of both worlds Whether you’re interested in building command-line applications or the most scalable web application ever, IronPython puts the power in your hands to take your vision from design to implementation in less time and more elegantly than you ever thought possible, and the fun is just beginning Microsoft is pumping ever more resources into alternative languages and ensuring that all of them have an equal capability to make full use of the underlying NET framework, leaving you as a developer to enjoy learning and using these tools while creating the applications you want as you see fit IronPython not only stands on its own; it also integrates tightly with other NET languages It can be used as a scripting language in an existing application or to create an enterprise-level application from scratch The sky’s the limit, and this book will help equip you with the tools to get things done quickly You might even have a little fun along the way Pro IronPython Pro IronPython Pro IronPython Best regards, Explore the combination of the popular Python language with the powerful NET framework Alan Harris—Web Developer, Council of Better Business Bureaus Related Titles Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-1962-0 90000 www.apress.com Harris SOURCE CODE ONLINE Alan Harris Shelve in NET User level: Intermediate–Advanced 781430 219620 this print for content only—size & color not accurate spine = 0.723" 312 page count Pro IronPython Alan Harris Pro IronPython Copyright © 2009 by Alan Harris All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher ISBN-13 (pbk): 978-1-4302-1962-0 ISBN-13 (electronic): 978-1-4302-1963-7 Printed and bound in the United States of America Trademarked names may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark Lead Editors: Mark Beckner, Jonathan Hassel Technical Reviewer: Shawna Garver Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Beth Christmas Copy Editor: Elliot Simon Associate Production Director: Kari Brooks-Copony Production Editor: April Eddy Compositor: Linda Weidemann, Wolf Creek Publishing Services Proofreaders: Linda Seifert and Kim Burton Indexer: Julie Grady Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705 Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www apress.com Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use eBook versions and licenses are also available for most titles For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales The information in this book is distributed on an “as is” basis, without warranty Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work The source code for this book is available to readers at http://www.apress.com To Kelly, without whom my days would be both far less interesting and far less fun Contents at a Glance About the Author xiii About the Technical Reviewer xv Acknowledgments xvii Introduction xix Chapter Introduction to IronPython Chapter IronPython Syntax 15 Chapter Advanced IronPython 39 Chapter IronPython Studio 63 Chapter Mixing and Mingling with the CLR 79 Chapter Advanced Development 119 Chapter Data Manipulation 163 Chapter Caught in a Web 203 Chapter IronPython Recipes 239 index 277 v Contents About the Author xiii About the Technical Reviewer xv Acknowledgments xvii Introduction xix Chapter Introduction to IronPython A Humble Beginning Jython: A Taste for Java IronPython: “Import NET” Why Is NET Important? What Exactly Is IronPython? What Can IronPython Do for Me Today? Yes, But Will It Blend? What Is a Dynamic Language? What This Will Book Cover Who This Book Is For For Consenting Adults Only! Prerequisites 10 IPY and You 12 Summary 14 Chapter IronPython Syntax 15 Data Types and Control Structures Strings Integers Conditional Statements Input() or Raw_Input() Error Handling and Exceptions Try-Catch-Finally 15 15 17 19 20 21 23 vii viii ■CO NTENT S Built-In Functions 26 abs 26 chr 26 dict 27 dir 27 Files via open 28 for (iterations) 29 help 30 hex 30 int 31 len 32 list 32 max and 32 ord 33 pow 33 random 34 randrange 35 round 36 uniform 37 But Wait, There’s More! 37 Summary 38 Chapter Advanced IronPython 39 String Operations Revisited 39 A Quick Software Development Detour 43 Back on Track 44 Floating-Point Numbers 46 Booleans 48 Classes and OOP 48 NET Data Types 59 Value and Reference Types 60 Mixing and Matching 61 Summary 62 ■C O N T E N T S Chapter Chapter IronPython Studio 63 Hopping Onto the Steamroller So Much Typing Is There a Better Way? Forms, from the Ground Up It’s All This Substandard Wiring! Clean Code Is Happy Code Summary 63 Mixing and Mingling with the CLR 66 70 72 74 78 79 “CLR-ance, Clarence.” 79 The Plan 80 The Design 81 The Implementation 82 Bad Medicine 91 I’d Like to See a Menu 97 Reading, Writing, Arithmetic 99 Open Sesame 101 I Can’t Even Save Myself 106 Print, Please 110 A Touch of OOP 112 Exit Strategy 116 Beautification 116 Project Postmortem 117 Summary 118 Chapter Advanced Development Base Classes for Fun and Profit (aka “The LEGOs on the Bottom Don’t Really Exist”) Plug and Play Architecting Flexibility Calling IronPython Code Creating a Plug-in Base Choices, Choices Supporting Healthy Arguments 119 119 130 131 134 140 147 148 ix