python CookBook

  • Post author:
  • Post category:python


  • 此处格式化的很多章节都是从 中国Linux论坛 Limodou 管理的“Python 编程”板块中得来!向先行者表示感謝!

有效而实用的 pythonCookBook 浅显明了,但是还是母语使用的更爽一些!




精华-Python CookBook

(Python食谱)



欢迎志愿人员参加翻译,审核!



  • 建议模板用“中文Python食谱模板”

1. Python Cookbook






04/22/04 22:19 开始!

By

Alex Martelli, David Ascher




  • Publisher : O’Reilly

  • Pub Date : July 2002

  • ISBN : 0-596-00167-3

  • Pages : 606

1.1. 序 Preface


  • The Design of the Book

  • The Implementation of the Book

  • A Note About Licenses

  • Audience

  • Organization

  • Further Reading

  • Conventions Used in This Book

  • How to Contact Us

  • Acknowledgments

1.2. Chapter 1. 短平快 Python Shortcuts


1.3. Chapter 2. 搜索和排序 Searching and Sorting




  • PyCkBk-2-1


    Section 2.1. Introduction



  • PyCkBk-2-2



    Python 食谱 2.2.

    给字典排序



  • PyCkBk-2-3



    Python 食谱 2.3.

    有效的处理选择的结构数据对



  • PyCkBk-2-4



    Python 食谱 2.4.

    在保持稳定性的同时来排序



  • PyCkBk-2-5



    Python 食谱 2.5.

    通过两个字段来排序



  • PyCkBk-2-6



    Python 食谱 2.6.

    在一个有序的序列里使用二分查找法来查找条目



  • PyCkBk-2-7



    Python 食谱 2.7.

    通过对象的属性来排序对象的列表



  • PyCkBk-2-8



    Python 食谱 2.8.

    通过元素或者属性来排序



  • PyCkBk-2-9



    Python 食谱 2.9.

    不用循环从一个list中选择随机的元素



  • PyCkBk-2-10



    Python 食谱 2.10.

    在一个序列上执行频繁的成员关系测试



  • PyCkBk-2-11



    Python 食谱 2.11.

    在一个嵌入的序列中发现一个条目的深度索引



  • PyCkBk-2-12



    Python 食谱 2.12.

    在三行代码中展示Quicksort



  • PyCkBk-2-13



    Python 食谱 2.13.

    使用sql的 ORDER BY 语法来为对象排序

1.4. Chapter 3. 文本 Text




  • PyCkBk-3-1


    Section 3.1. Introduction



  • PyCkBk-3-2



    Python 食谱 3.2.

    每次处理字符串的一个字符



  • PyCkBk-3-3



    Python 食谱 3.3.

    测试是否一个对象是类似的string



  • PyCkBk-3-4



    Python 食谱 3.4.

    排列字符串



  • PyCkBk-3-5



    Python 食谱 3.5.

    从一个string的尾部清除空格



  • PyCkBk-3-6



    Python 食谱 3.6.

    合并字符串



  • PyCkBk-3-7



    Python 食谱 3.7.

    检查是否一个字符串中包合某个集合中的字符



  • PyCkBk-3-8



    Python 食谱 3.8.

    用一个字符集过滤一个字符串



  • PyCkBk-3-9



    Python 食谱 3.9.

    操纵大小写



  • PyCkBk-3-10



    Python 食谱 3.10.

    在字符串中倒转字符或单词(的顺序)



  • PyCkBk-3-11



    Python 食谱 3.11.

    访问子串



  • PyCkBk-3-12



    Python 食谱 3.12.

    改变多行字符串的缩进



  • PyCkBk-3-13



    Python 食谱 3.13.

    检测一个字符串是否表达一个整数



  • PyCkBk-3-14



    Python 食谱 3.14.

    扩展以及压缩制表符(Tab)



  • PyCkBk-3-15



    Python 食谱 3.15.

    一次替换多个模式



  • PyCkBk-3-16



    Python 食谱 3.16.

    在不同的命名约定之间转换



  • PyCkBk-3-17



    Python 食谱 3.17.

    在字符与和编码值间转换



  • PyCkBk-3-18



    Python 食谱 3.18.

    在Unicode和普通字符串之间转换



  • PyCkBk-3-19



    Python 食谱 3.19.

    打印Unicode字符到标准输出



  • PyCkBk-3-20



    Python 食谱 3.20.

    基于模式匹配的调度



  • PyCkBk-3-21



    Python 食谱 3.21.

    字符串内代码的求值



  • PyCkBk-3-22



    Python 食谱 3.22.

    把Python代码替换成其运行结果



  • PyCkBk-3-23


    Section 3.23. Module: Yet Another Python Templating Utility (YAPTU)



  • PyCkBk-3-24


    Section 3.24. Module: Roman Numerals

1.5. Chapter 4. 文件 Files


1.6. Chapter 5. 面向对象 Object-Oriented Programming




  • PyCkBk-5-1

    “Section 5.1. Introduction



  • PyCkBk-5-2

    “Python 食谱–5.2. 覆盖内置方法



  • PyCkBk-5-3

    “Python 食谱–5.3. 获得类层次中所有成员属性



  • PyCkBk-5-4

    “Section 5.4. Calling a Superclass _ _init_ _ Method if It Exists



  • PyCkBk-5-5

    “Section 5.5. Calling a Superclass Implementation of a Method



  • PyCkBk-5-6

    “Section 5.6. Implementing Properties



  • PyCkBk-5-7

    “Python 食谱–5.7. 实现Static方法



  • PyCkBk-5-8

    “Section 5.8. Implementing Class Methods



  • PyCkBk-5-9

    “Section 5.9. Delegating Automatically as an Alternative to Inheritance



  • PyCkBk-5-10

    “Section 5.10. Decorating an Object with Print-Like Methods



  • PyCkBk-5-11

    “Section 5.11. Checking if an Object Has Necessary Attributes



  • PyCkBk-5-12

    “Section 5.12. Making a Fast Copy of an Object



  • PyCkBk-5-13

    “Python 食谱–5.13. 在运行时给类添加方法



  • PyCkBk-5-14

    “Python 食谱–5.14. 修改实体的类继承关系



  • PyCkBk-5-15

    “Section 5.15. Keeping References to Bound Methods Without Inhibiting Garbage Collection



  • PyCkBk-5-16

    “Section 5.16. Defining Constants



  • PyCkBk-5-17

    “Section 5.17. Managing Options



  • PyCkBk-5-18

    “Section 5.18. Implementing a Set Class



  • PyCkBk-5-19

    “Section 5.19. Implementing a Ring Buffer



  • PyCkBk-5-20

    “Section 5.20. Implementing a Collection



  • PyCkBk-5-21

    “Section 5.21. Delegating Messages to Multiple Objects



  • PyCkBk-5-22

    “Section 5.22. Implementing the Singleton Design Pattern



  • PyCkBk-5-23

    “Section 5.23. Avoiding the Singleton Design Pattern with the Borg Idiom



  • PyCkBk-5-24

    “Section 5.24. Implementing the Null Object Design Pattern

1.7. Chapter 6. 线程,进程,同步 Threads, Processes, and Synchronization




  • PyCkBk-6-1

    “Section 6.1. Introduction



  • PyCkBk-6-2

    “Section 6.2. Storing Per-Thread Information



  • PyCkBk-6-3

    “Section 6.3. Terminating a Thread



  • PyCkBk-6-4

    “Section 6.4. Allowing Multithreaded Read Access While Maintaining a Write Lock



  • PyCkBk-6-5

    “Section 6.5. Running Functions in the Future



  • PyCkBk-6-6

    “Section 6.6. Synchronizing All Methods in an Object



  • PyCkBk-6-7

    “Section 6.7. Capturing the Output and Error Streams from a Unix Shell Command



  • PyCkBk-6-8

    “Section 6.8. Forking a Daemon Process on Unix



  • PyCkBk-6-9

    “Section 6.9. Determining if Another Instance of a Script Is Already Running in Windows



  • PyCkBk-6-10

    “Section 6.10. Processing Windows Messages Using

    MsgWaitForMultipleObjects


1.8. Chapter 7. 系统管理 System Administration




  • PyCkBk-7-1

    “Section 7.1. Introduction



  • PyCkBk-7-2

    “Section 7.2. Running a Command Repeatedly



  • PyCkBk-7-3

    “Section 7.3. Generating Random Passwords



  • PyCkBk-7-4

    “Section 7.4. Generating Non-Totally Random Passwords



  • PyCkBk-7-5

    “Section 7.5. Checking the Status of a Unix Network Interface



  • PyCkBk-7-6

    “Section 7.6. Calculating Apache Hits per IP Address



  • PyCkBk-7-7

    “Section 7.7. Calculating the Rate of Client Cache Hits on Apache



  • PyCkBk-7-9

    “Section 7.8. Manipulating the Environment on Windows NT/2000/XP



  • PyCkBk-7-9

    “Section 7.9. Checking and Modifying the Set of Tasks Windows Automatically Runs at Logon



  • PyCkBk-7-10

    “Section 7.10. Examining the Microsoft Windows Registry for a List of Name Server Addresses



  • PyCkBk-7-11

    “Section 7.11. Getting Information About the Current User on Windows NT/2000



  • PyCkBk-7-12

    “Section 7.12. Getting the Windows Service Name from Its Long Name



  • PyCkBk-7-13

    “Section 7.13. Manipulating Windows Services



  • PyCkBk-7-14

    “Section 7.14. Impersonating Principals on Windows



  • PyCkBk-7-15

    “Section 7.15. Changing a Windows NT Password Using ADSI



  • PyCkBk-7-16

    “Section 7.16. Working with Windows Scripting Host (WSH) from Python



  • PyCkBk-7-17

    “Section 7.17. Displaying Decoded Hotkeys for Shortcuts in Windows

1.9. Chapter 8. 数据和持续性 Databases and Persistence




  • PyCkBk-8-1

    “Section 8.1. Introduction



  • PyCkBk-8-2

    “Section 8.2. Serializing Data Using the marshal Module



  • PyCkBk-8-3

    “Section 8.3. Serializing Data Using the pickle and cPickle Modules



  • PyCkBk-8-4

    “Section 8.4. Using the cPickle Module on Classes and Instances



  • PyCkBk-8-5

    “Section 8.5. Mutating Objects with shelve



  • PyCkBk-8-6

    “Section 8.6. Accesssing a MySQL Database



  • PyCkBk-8-7

    “Section 8.7. Storing a BLOB in a MySQL Database



  • PyCkBk-8-8

    “Section 8.8. Storing a BLOB in a PostgreSQL Database



  • PyCkBk-8-9

    “Section 8.9. Generating a Dictionary Mapping from Field Names to Column Numbers



  • PyCkBk-8-10

    “Section 8.10. Using dtuple for Flexible Access to Query Results



  • PyCkBk-8-11

    “Section 8.11. Pretty-Printing the Contents of Database Cursors



  • PyCkBk-8-12

    “Section 8.12. Establishing Database Connections Lazily



  • PyCkBk-8-13

    “Section 8.13. Accessing a JDBC Database from a Jython Servlet



  • PyCkBk-8-14

    “Section 8.14. Module: jet2sql桟reating a SQL DDL from an Access Database

1.10. Chapter 9. 用户界面 User Interfaces




  • PyCkBk-9-1

    “Section 9.1. Introduction



  • PyCkBk-9-2

    “Section 9.2. Avoiding lambda in Writing Callback Functions



  • PyCkBk-9-3

    “Section 9.3. Creating Menus with Tkinter



  • PyCkBk-9-4

    “Section 9.4. Creating Dialog Boxes with Tkinter



  • PyCkBk-9-5

    “Section 9.5. Supporting Multiple Values per Row in a Tkinter Listbox



  • PyCkBk-9-6

    “Section 9.6. Embedding Inline GIFs Using Tkinter



  • PyCkBk-9-7

    “Section 9.7. Combining Tkinter and Asynchronous I/O with Threads



  • PyCkBk-9-8

    “Section 9.8. Using a wxPython Notebook with Panels



  • PyCkBk-9-9

    “Section 9.9. Giving the User Unobtrusive Feedback During Data Entry with Qt



  • PyCkBk-9-10

    “Section 9.10. Building GUI Solutions Independent of the Specific GUI Toolkit



  • PyCkBk-9-11

    “Section 9.11. Creating Color Scales



  • PyCkBk-9-12

    “Section 9.12. Using Publish/Subscribe Broadcasting to Loosen the Coupling Between GUI and Business Logic Systems



  • PyCkBk-9-13

    “Section 9.13. Module: Building GTK GUIs Interactively

1.11. Chapter 10. 网络 Network Programming


1.12. Chapter 11. 互联网 Web Programming




  • PyCkBk-11-1

    “Section 11.1. Introduction



  • PyCkBk-11-2

    “Section 11.2. Testing Whether CGI Is Working



  • PyCkBk-11-3

    “Section 11.3. Writing a CGI Script



  • PyCkBk-11-4

    “Section 11.4. Using a Simple Dictionary for CGI Parameters



  • PyCkBk-11-5

    “Section 11.5. Handling URLs Within a CGI Script



  • PyCkBk-11-6

    “Section 11.6. Resuming the HTTP Download of a File



  • PyCkBk-11-7

    “Section 11.7. Stripping Dangerous Tags and Javascript from HTML



  • PyCkBk-11-8

    “Section 11.8. Running a Servlet with Jython



  • PyCkBk-11-9

    “Section 11.9. Accessing Netscape Cookie Information



  • PyCkBk-11-10

    “Section 11.10. Finding an Internet Explorer Cookie



  • PyCkBk-11-11

    “Section 11.11. Module: Fetching Latitude/Longitude Data from the Web

1.13. Chapter 12. 处理XML Processing XML




  • PyCkBk-12-1

    “Python 食谱–12.1. 介绍



  • PyCkBk-12-2

    “Python 食谱–12.2. 检查XML是否为良好格式



  • PyCkBk-12-3

    “Python 食谱–12.3. 统计文档中标记出现次数



  • PyCkBk-12-4

    “Python 食谱–12.4. 从XML文档提取文本



  • PyCkBk-12-5

    “Python 食谱–12.5. 使用XSLT来转换XML文档



  • PyCkBk-12-6

    “Python 食谱–12.6. 使用Python来转换XML文档



  • PyCkBk-12-7

    “Python 食谱–12.6. 使用xml.parsers.expat来处理XML文档



  • PyCkBk-12-8

    “Section 12.8. Converting Ad-Hoc Text into XML Markup



  • PyCkBk-12-9

    “Section 12.9. Normalizing an XML Document



  • PyCkBk-12-10

    “Section 12.10. Controlling XSLT Stylesheet Loading



  • PyCkBk-12-11

    “Section 12.11. Autodetecting XML Encoding



  • PyCkBk-12-12

    “Section 12.12. Module: XML Lexing (Shallow Parsing)



  • PyCkBk-12-13

    “Section 12.13. Module: Converting a List of Equal-Length Lists into XML

1.14. Chapter 13. 分布式编程 Distributed Programming




  • PyCkBk-13-1

    “Section 13.1. 简介 Introduction



  • PyCkBk-13-2

    “Section 13.2. 发起一个 XML-RPC 方法调用 Making an XML-RPC Method Call



  • PyCkBk-13-3

    “Section 13.3. Serving XML-RPC Requests



  • PyCkBk-13-4

    “Section 13.4. Using XML-RPC with Medusa



  • PyCkBk-13-5

    “Section 13.5. Writing a Web Service That Supports Both XML-RPC and SOAP



  • PyCkBk-13-6

    “Section 13.6. Implementing a CORBA Client and Server



  • PyCkBk-13-7

    “Section 13.7. Performing Remote Logins Using telnetlib



  • PyCkBk-13-8

    “Section 13.8. Using Publish/Subscribe in a Distributed Middleware Architecture



  • PyCkBk-13-9

    “Section 13.9. Using Request/Reply in a Distributed Middleware Architecture

1.15. Chapter 14. 调试与测试 Debugging and Testing




  • PyCkBk-14-1

    “Section 14.1. Introduction



  • PyCkBk-14-2

    “Section 14.2. Reloading All Loaded Modules



  • PyCkBk-14-3

    “Section 14.3. Tracing Expressions and Comments in Debug Mode



  • PyCkBk-14-4

    “Section 14.4. Wrapping Tracebacks in HTML



  • PyCkBk-14-5

    “Section 14.5. Getting More Information from Tracebacks



  • PyCkBk-14-6

    “Section 14.6. Starting the Debugger Automatically After an Uncaught Exception



  • PyCkBk-14-7

    “Section 14.7. Logging and Tracing Across Platforms



  • PyCkBk-14-8

    “Section 14.8. Determining the Name of the Current Function



  • PyCkBk-14-9

    “Section 14.9. Introspecting the Call Stack with Older Versions of Python



  • PyCkBk-14-10

    “Section 14.10. Debugging the Garbage-Collection Process



  • PyCkBk-14-11

    “Section 14.11. Tracking Instances of Particular Classes

1.16. Chapter 15. 编程 Programs About Programs




  • PyCkBk-15-1

    “Section 15.1. Introduction



  • PyCkBk-15-2

    “Section 15.2. Colorizing Python Source Using the Built-in Tokenizer



  • PyCkBk-15-3

    “Section 15.3. Importing a Dynamically Generated Module



  • PyCkBk-15-4

    “Section 15.4. Importing from a Module Whose Name Is Determined at Runtime



  • PyCkBk-15-5

    “Section 15.5. Importing Modules with Automatic End-of-Line Conversions



  • PyCkBk-15-6

    “Section 15.6. Simulating Enumerations in Python



  • PyCkBk-15-7

    “Section 15.7. Modifying Methods in Place



  • PyCkBk-15-8

    “Section 15.8. Associating Parameters with a Function (Currying)



  • PyCkBk-15-9

    “Section 15.9. Composing Functions



  • PyCkBk-15-10

    “Section 15.10. Adding Functionality to a Class



  • PyCkBk-15-11

    “Section 15.11. Adding a Method to a Class Instance at Runtime



  • PyCkBk-15-12

    “Section 15.12. Defining a Custom Metaclass to Control Class Behavior



  • PyCkBk-15-13

    “Section 15.13. Module: Allowing the Python Profiler to Profile C Modules

1.17. Chapter 16. 扩展和嵌入 Extending and Embedding




  • PyCkBk-16-1

    “Section 16.1. Introduction



  • PyCkBk-16-2

    “Section 16.2. Implementing a Simple Extension Type



  • PyCkBk-16-3

    “Section 16.3. Translating a Python Sequence into a C Array with the

    PySequence

    _Fast Protocol



  • PyCkBk-16-4

    “Section 16.4. Accessing a Python Sequence Item-by-Item with the Iterator Protocol



  • PyCkBk-16-5

    “Section 16.5. Returning None from a Python-Callable C Function



  • PyCkBk-16-6

    “Section 16.6. Coding the Methods of a Python Class in C



  • PyCkBk-16-7

    “Section 16.7. Implementing C Function Callbacks to a Python Function



  • PyCkBk-16-8

    “Section 16.8. Debugging Dynamically Loaded C Extensions with gdb



  • PyCkBk-16-9

    “Section 16.9. Debugging Memory Problems



  • PyCkBk-16-10

    “Section 16.10. Using SWIG-Generated Modules in a Multithreaded Environment

1.18. Chapter 17. 算法 Algorithms




  • PyCkBk-17-1

    “Section 17.1. Introduction



  • PyCkBk-17-2

    “Section 17.2. Testing if a Variable Is Defined



  • PyCkBk-17-3

    “Section 17.3. Evaluating Predicate Tests Across Sequences



  • PyCkBk-17-4

    “Section 17.4. Removing Duplicates from a Sequence



  • PyCkBk-17-5

    “Section 17.5. Removing Duplicates from a Sequence While Maintaining Sequence Order



  • PyCkBk-17-6

    “Section 17.6. Simulating the Ternary Operator in Python



  • PyCkBk-17-7

    “Section 17.7. Counting Items and Sorting by Incidence (Histograms)



  • PyCkBk-17-8

    “Section 17.8. Memoizing (Caching) the Return Values of Functions



  • PyCkBk-17-9

    “Section 17.9. Looking Up Words by Sound Similarity



  • PyCkBk-17-10

    “Section 17.10. Computing Factorials with lambda



  • PyCkBk-17-11

    “Section 17.11. Generating the Fibonacci Sequence



  • PyCkBk-17-12

    “Section 17.12. Wrapping an Unbounded Iterator to Restrict Its Output



  • PyCkBk-17-13

    “Section 17.13. Operating on Iterators



  • PyCkBk-17-14

    “Section 17.14. Rolling Dice



  • PyCkBk-17-15

    “Section 17.15. Implementing a First-In First-Out Container



  • PyCkBk-17-16

    “Section 17.16. Modeling a Priority Queue



  • PyCkBk-17-17

    “Section 17.17. Converting Numbers to Rationals via Farey Fractions



  • PyCkBk-17-18

    “Section 17.18. Evaluating a Polynomial



  • PyCkBk-17-19

    “Section 17.19. Module: Finding the Convex Hull of a Set of 2D Points



  • PyCkBk-17-20

    “Section 17.20. Module: Parsing a String into a Date/Time Object Portably

1.19. Chapter 18. 索引 List of Contributors



Zoomq::040430 09:40 Moin 整理


— Zoom.Quiet [2004-08-09 23:30:50]



2. 多嘴多舌


  • Chapter 13. 分发 Distributed Programming 应该是“分布式编程”吧? —

    LiJie




  • CookBook

    ” 通常是”食谱”的意思,不过用在书籍上的时候都是翻译为“大全”,是这么回事吧?

    • 其实名字倒也无所谓,重要的是内容,只是刚看到“食谱”的时候还以为咱么这要办食堂呢,呵呵。 —

      SpiritAuding


  • 直接升级到第二版吧。好象站内没有第二版下载。

    http://www.infoxa.com/asp/book_file/xxnr_book_2300.htm