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 9 8 7 6 5 4 3 2 1 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 Cor- nell, 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 [email protected], 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 [email protected], 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 precau- tion 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 indi- rectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. Contents at a Glance About the Author ..................................................................xiii About the Technical Reviewer ......................................................xv Acknowledgments ............................................................... xvii Introduction ......................................................................xix CHAPTER 1 Introduction to IronPython ...................................... 1 CHAPTER 2 IronPython Syntax ..............................................15 CHAPTER 3 Advanced IronPython ...........................................39 CHAPTER 4 IronPython Studio ............................................. 63 CHAPTER 5 Mixing and Mingling with the CLR ...............................79 CHAPTER 6 Advanced Development ...................................... 119 CHAPTER 7 Data Manipulation .............................................163 CHAPTER 8 Caught in a Web ............................................. 203 CHAPTER 9 IronPython Recipes ............................................239 INDEX ...................................................................... 277 v Contents About the Author ..................................................................xiii About the Technical Reviewer ......................................................xv Acknowledgments ............................................................... xvii Introduction ......................................................................xix CHAPTER 1 Introduction to IronPython ................................. 1 A Humble Beginning ............................................. 1 Jython: A Taste for Java ...........................................2 IronPython: “Import .NET” ........................................ 2 Why Is .NET Important? .......................................... 3 What Exactly Is IronPython? ........................................3 What Can IronPython Do for Me Today? ............................ 4 Yes, But Will It Blend? .............................................5 What Is a Dynamic Language? .................................... 6 What This Will Book Cover .........................................8 Who This Book Is For ............................................ 9 For Consenting Adults Only! ........................................9 Prerequisites ....................................................10 IPY and You .....................................................12 Summary .......................................................14 CHAPTER 2 IronPython Syntax ......................................... 15 Data Types and Control Structures .................................15 Strings .........................................................15 Integers ....................................................... 17 Conditional Statements ...........................................19 Input() or Raw_Input() ............................................20 Error Handling and Exceptions ................................... 21 Try-Catch-Finally ................................................23 vii viii ■CONTENTS 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 min ...............................................32 ord ...................................................... 33 pow .......................................................33 random ....................................................34 randrange ................................................ 35 round .................................................... 36 uniform .................................................. 37 But Wait, There’s More! ......................................... 37 Summary .......................................................38 CHAPTER 3 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 ■CONTENTS ix CHAPTER 4 IronPython Studio ...........................................63 Hopping Onto the Steamroller .....................................63 So Much Typing...Is There a Better Way? ..........................66 Forms, from the Ground Up .......................................70 It’s All This Substandard Wiring! ...................................72 Clean Code Is Happy Code ........................................74 Summary .......................................................78 CHAPTER 5 Mixing and Mingling with the CLR .........................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 6 Advanced Development ....................................119 Base Classes for Fun and Profit (aka “The LEGOs on the Bottom Don’t Really Exist”) ...........................................119 Plug and Play ................................................. 130 Architecting Flexibility ...........................................131
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages298 Page
-
File Size-