Đăng ký Đăng nhập

Tài liệu Python projects

.PDF
384
85
53

Mô tả:

www.it-ebooks.info www.it-ebooks.info PYTHON ® PROJECTS INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv CHAPTER 1 Reviewing Core Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 CHAPTER 2 Scripting with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 CHAPTER 3 Managing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 CHAPTER 4 Building Desktop Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 CHAPTER 5 Python on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 CHAPTER 6 Python in Bigger Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 CHAPTER 7 Exploring Python’s Frontiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 APPENDIX A Answers to Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 APPENDIX B Python Standard Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 APPENDIX C Useful Python Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 REFERENCES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 www.it-ebooks.info www.it-ebooks.info Python® Projects www.it-ebooks.info www.it-ebooks.info Python® Projects Laura Cassell Alan Gauld www.it-ebooks.info Python ® Projects Published by John Wiley & Sons, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2015 by John Wiley & Sons, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-1-118-90866-2 ISBN: 978-1-118-90889-1 (ebk) ISBN: 978-1-118-90919-5 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2014946683 Trademarks: Wiley, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other countries, and may not be used without written permission. Python is a registered trademark of Python Software Foundation Corporation. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book. www.it-ebooks.info For my truly great boys—Nathan, Ben, and Matt: We will do so many things now that I’m not writing. Thank you for all the quiet time while I was writing; you can come out of your rooms now. —Laura Cassell To my wife, Heather, for her continued support and patience with my eccentric working hours. —Alan Gauld www.it-ebooks.info www.it-ebooks.info ABOUT THE AUTHORS LAURA CASSELL has been poking at code on the web since 1997. She taught herself Perl in the early 2000s where she discovered that programming materials were in dire need of an overhaul and the barrier to entry to teach programming was incredibly high. Thus, her journey to learn programming so she can bring it to other people began. Originally from Atlanta, GA, Laura founded PyLadies Atlanta, and got her start teaching Python and JavaScript for Big Nerd Ranch. She has since moved on to engineering management and currently resides in Portland, OR where she manages a team of Pythonistas doing software analytics for New Relic, Inc. She still volunteers for teaching and speaking gigs when time permits. ALAN GAULD is an enterprise architect with more than 40 years of experience in IT, working mainly in the telecommunications and customer service areas. He has programmed in more than 20 languages, producing everything from mainframe billing systems through embedded microcontrollers. For the last 15 years, his preference has been for Python. He is the author of a beginner’s book on Python and is co-moderator of the python-tutor mailing list. When not programming, he likes climbing, backpacking, and skiing. He is also a keen photographer, artist, and acoustic music enthusiast. He lives in Scotland with his wife, Heather. www.it-ebooks.info www.it-ebooks.info ABOUT THE TECHNICAL EDITORS ALEX BRADBURY is a compiler hacker, Linux geek, and Free Software enthusiast. He has been a long time contributor to the Raspberry Pi project and also co-authored Learning Python with Raspberry Pi. He is currently a researcher at the University of Cambridge Computer Laboratory and is a co-founder of lowRISC, a non-profit project to produce a complete open-source Systemon-Chip (SoC). TODD SHANDELMAN fondly remembers coding assembly language programs on punch cards for IBM System/370 mainframes. After occupying various other ecological niches in software technology over the years (C, C++, and Perl, to name but a few), Todd’s best days are now spent coding pure Python 2.x and 3.x from the Linux command line. In his spare time he is a professional translator of Russian and Hebrew, specializes in foreign-language typography, and can fi nd his way in the dark around Unicode and UTF-8. Todd earned a Bachelor of Science degree in Business Administration from the University of the State of New York. He lives in Houston, Texas, with his wife and son. www.it-ebooks.info www.it-ebooks.info CREDITS Acquisitions Editor Professional Technology & Strategy f Director Jim Minatel Barry Pruett Project Editor Business Manager Jennifer Lynn Amy Knies Technical Editors Alex Bradbury Todd Shandelman Associate Publisher Production Editor Project Coordinator, Cover Christine Mugnolo Patrick Redmond Copy Editor Proofreader Kimberly A. Cofer Sarah Kaikini, Word One New York Production Manager Indexer Kathleen Wisor Johnna Dinse Manager of Content Development and Assembly Cover Designer Jim Minatel Wiley Mary Beth Wakefield Cover Image Marketing Director © iStock.com/skodonnell David Mayhew Marketing Manager Carrie Sherrill www.it-ebooks.info www.it-ebooks.info ACKNOWLEDGMENTS I’D LIKE TO GIVE a big thank you to Alan Gauld, who helped put so much of this book together, and to Mary James and Jennifer Lynn for making sure this idea came to fruition. It’s a better book because of all of you. Thank you also to the Python community, you are all the most welcoming community that I’ve ever found. You have created a place where people feel welcome and able to approach everyone, experts to novices alike. Keep up the good work and thank you for allowing me to be a part of this wonderful community. —Laura Cassell I’D LIKE TO GIVE a shout out to Laura Cassell for kick-starting this project, Jennifer Lynn for steering us through it, and the Python community for their support over the last 15 years. —Alan Gauld www.it-ebooks.info www.it-ebooks.info CONTENTS INTRODUCTION xxv CHAPTER 1: REVIEWING CORE PYTHON Exploring the Python Language and the Interpreter Reviewing the Python Data Types Numeric Types: Integer and Float The Boolean Type The None Type Collection Types Strings Bytes and ByteArrays Tuples Lists Dictionaries Sets 1 2 3 4 5 6 6 7 8 10 10 12 13 Using Python Control Structures Structuring Your Program Using Sequences, Blocks and Comments Selecting an Execution Path Iteration Handling Exceptions Managing Context Getting Data In and Out of Python Interacting with Users Using Text Files 15 15 16 17 18 20 21 21 21 23 Extending Python 24 Defining and Using Functions Generator Functions Lambda Functions 24 26 27 Defining and Using Classes and Objects Creating and Using Modules and Packages Using and Creating Modules Using and Creating Packages Creating an Example Package Using Third‐Party Packages Summary www.it-ebooks.info 28 33 33 34 36 42 43 CONTENTS CHAPTER 2: SCRIPTING WITH PYTHON Accessing the Operating System Obtaining Information About Users and Their Computer Obtaining Information About the Current Process Managing Other Programs Managing Subprocesses More Effectively Obtaining Information About Files (and Devices) Navigating and Manipulating the File system Plumbing the Directory Tree Depths Working with Dates and Times Using the time Module Introducing the datetime Module Introducing the calendar Module Handling Common File Formats Using Comma‐Separated Values Working with Config Files Working with XML and HTML Files Parsing XML Files Parsing HTML Files 47 48 49 53 55 58 60 62 69 72 72 75 76 76 76 83 86 86 89 Accessing Native APIs with ctypes and pywin32 Accessing the Operating System Libraries Using ctypes with Windows Using ctypes on Linux Accessing a Windows Application Using COM 93 94 95 96 96 Automating Tasks Involving Multiple Applications 98 Using Python First Using Operating System Utilities Using Data Files Using a Third‐Party Module Interacting with Subprocesses via a CLI Using Web Services for Server‐Based Applications Using a Native Code API Using GUI Robotics 98 98 98 99 99 99 100 100 Summary 100 CHAPTER 3: MANAGING DATA 103 Storing Data Using Python 104 Using DBM as a Persistent Dictionary Using Pickle to Store and Retrieve Objects Accessing Objects with shelve xviii www.it-ebooks.info 104 109 111
- Xem thêm -

Tài liệu liên quan