收藏 分享(赏)

Python编程-第4版-Programming.Python.4th.Edition.Mark.Lutz.文字版.pdf

上传人:刘岱文 文档编号:3722 上传时间:2018-05-08 格式:PDF 页数:1628 大小:29.11MB
下载 相关 举报
Python编程-第4版-Programming.Python.4th.Edition.Mark.Lutz.文字版.pdf_第1页
第1页 / 共1628页
Python编程-第4版-Programming.Python.4th.Edition.Mark.Lutz.文字版.pdf_第2页
第2页 / 共1628页
Python编程-第4版-Programming.Python.4th.Edition.Mark.Lutz.文字版.pdf_第3页
第3页 / 共1628页
Python编程-第4版-Programming.Python.4th.Edition.Mark.Lutz.文字版.pdf_第4页
第4页 / 共1628页
Python编程-第4版-Programming.Python.4th.Edition.Mark.Lutz.文字版.pdf_第5页
第5页 / 共1628页
点击查看更多>>
资源描述

1、Programming PythonFOURTH EDITIONProgramming PythonMark LutzBeijing Cambridge Farnham Kln Sebastopol TokyoProgramming Python, Fourth Editionby Mark LutzCopyright 2011 Mark Lutz. All rights reserved.Printed in the United States of America.Published by OReilly Media, Inc., 1005 Gravenstein Highway Nort

2、h, Sebastopol, CA 95472.OReilly books may be purchased for educational, business, or sales promotional use. Online editionsare also available for most titles (http:/ ). For more information, contact ourcorporate/institutional sales department: (800) 998-9938 or .Editor: Julie SteeleProduction Edito

3、r: Teresa ElseyProofreader: Teresa ElseyIndexer: Lucie HaskinsCover Designer: Karen MontgomeryInterior Designer: David FutatoIllustrator: Robert RomanoPrinting History:October 1996: First Edition. March 2001: Second Edition. August 2006: Third Edition. December 2010: Fourth Edition. Nutshell Handboo

4、k, the Nutshell Handbook logo, and the OReilly logo are registered trademarks ofOReilly Media, Inc. Programming Python, the image of an African rock python, and related trade dressare trademarks of OReilly Media, Inc.Many of the designations used by manufacturers and sellers to distinguish their pro

5、ducts are claimed astrademarks. Where those designations appear in this book, and OReilly Media, Inc., was aware of atrademark claim, the designations have been printed in caps or initial caps.While every precaution has been taken in the preparation of this book, the publisher and author assumeno re

6、sponsibility for errors or omissions, or for damages resulting from the use of the information con-tained herein.ISBN: 978-0-596-15810-1QG1292258056Table of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7、 . . . xxiiiPart I. The Beginning 1. A Sneak Preview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3“Programming Python: The Short Story” 3The Task 4Step 1: Representing Records 4Using Lists 4Using Dictionaries 9Step 2: Storing Record

8、s Persistently 14Using Formatted Files 14Using Pickle Files 19Using Per-Record Pickle Files 22Using Shelves 23Step 3: Stepping Up to OOP 26Using Classes 27Adding Behavior 29Adding Inheritance 29Refactoring Code 31Adding Persistence 34Other Database Options 36Step 4: Adding Console Interaction 37A Co

9、nsole Shelve Interface 37Step 5: Adding a GUI 40GUI Basics 40Using OOP for GUIs 42Getting Input from a User 44A GUI Shelve Interface 46Step 6: Adding a Web Interface 52CGI Basics 52vDownload from Wow! eBook Running a Web Server 55Using Query Strings and urllib 57Formatting Reply Text 59A Web-Based S

10、helve Interface 60The End of the Demo 69Part II. System Programming 2. System Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73“The os.path to Knowledge” 73Why Python Here? 73The Next Five Chapters 74System Scripting Overview

11、 75Python System Modules 76Module Documentation Sources 77Paging Documentation Strings 78A Custom Paging Script 79String Method Basics 80Other String Concepts in Python 3.X: Unicode and bytes 82File Operation Basics 83Using Programs in Two Ways 84Python Library Manuals 85Commercially Published Refer

12、ences 86Introducing the sys Module 86Platforms and Versions 86The Module Search Path 87The Loaded Modules Table 88Exception Details 89Other sys Module Exports 90Introducing the os Module 90Tools in the os Module 90Administrative Tools 91Portability Constants 92Common os.path Tools 92Running Shell Co

13、mmands from Scripts 94Other os Module Exports 1003. Script Execution Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103“Id Like to Have an Argument, Please” 103Current Working Directory 104CWD, Files, and Import Paths 104CWD and Command Lines 10

14、6vi | Table of ContentsCommand-Line Arguments 106Parsing Command-Line Arguments 107Shell Environment Variables 109Fetching Shell Variables 110Changing Shell Variables 111Shell Variable Fine Points: Parents, putenv, and getenv 112Standard Streams 113Redirecting Streams to Files and Programs 114Redire

15、cted Streams and User Interaction 119Redirecting Streams to Python Objects 123The io.StringIO and io.BytesIO Utility Classes 126Capturing the stderr Stream 127Redirection Syntax in Print Calls 127Other Redirection Options: os.popen and subprocess Revisited 1284. File and Directory Tools . . . . . .

16、. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135“Erase Your Hard Drive in Five Easy Steps!” 135File Tools 135The File Object Model in Python 3.X 136Using Built-in File Objects 137Binary and Text Files 146Lower-Level File Tools in the os Module 155File Scanners

17、160Directory Tools 163Walking One Directory 164Walking Directory Trees 168Handling Unicode Filenames in 3.X: listdir, walk, glob 1725. Parallel System Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177“Telling the Monkeys What to Do” 177Fork

18、ing Processes 179The fork/exec Combination 182Threads 186The _thread Module 189The threading Module 199The queue Module 204Preview: GUIs and Threads 208More on the Global Interpreter Lock 211Program Exits 213sys Module Exits 214os Module Exits 215Shell Command Exit Status Codes 216Process Exit Statu

19、s and Shared State 219Table of Contents | viiThread Exits and Shared State 220Interprocess Communication 222Anonymous Pipes 224Named Pipes (Fifos) 234Sockets: A First Look 236Signals 240The multiprocessing Module 243Why multiprocessing? 243The Basics: Processes and Locks 245IPC Tools: Pipes, Shared

20、Memory, and Queues 248Starting Independent Programs 254And Much More 256Why multiprocessing? The Conclusion 257Other Ways to Start Programs 258The os.spawn Calls 258The os.startfile call on Windows 261A Portable Program-Launch Framework 263Other System Tools Coverage 2686. Complete System Programs .

21、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271“The Greps of Wrath” 271A Quick Game of “Find the Biggest Python File” 272Scanning the Standard Library Directory 272Scanning the Standard Library Tree 273Scanning the Module Search Path 274Scanning the Entire

22、Machine 276Printing Unicode Filenames 279Splitting and Joining Files 282Splitting Files Portably 283Joining Files Portably 286Usage Variations 289Generating Redirection Web Pages 292Page Template File 293Page Generator Script 294A Regression Test Script 297Running the Test Driver 299Copying Directory Trees 304Comparing Directory Trees 308Finding Directory Differences 309Finding Tree Differences 311Running the Script 314Verifying Backups 316Reporting Differences and Other Ideas 317viii | Table of Contents

展开阅读全文
相关资源
相关搜索
资源标签

当前位置:首页 > 网络技术 > 后端技术

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:文库网官方知乎号:文库网

经营许可证编号: 粤ICP备2021046453号世界地图

文库网官网©版权所有2025营业执照举报