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

  • PyCkBk-1-1 Section 1.1. Introduction

  • PyCkBk-1-2 Python 食谱 1.2. 不使用临时变量来交换值

  • PyCkBk-1-3 Python 食谱 1.3. 不用过多的引用来构造一个字典

  • PyCkBk-1-4 Python 食谱 1.4. 从字典中获取一个值

  • PyCkBk-1-5 Python 食谱 1.5. 增加一个入口到字典里

  • PyCkBk-1-6 Python 食谱 1.6. 在字典里把每一个键同多个值关联起来

  • PyCkBk-1-7 Python 食谱 1.7. 使用字典来分发

  • PyCkBk-1-8 Python 食谱 1.8. 集中一组命名的条目

  • PyCkBk-1-9 Python 食谱 1.9. 发现两个字典的交集

  • PyCkBk-1-10 Python 食谱 1.10. 用一个语句来赋值和测试

  • PyCkBk-1-11 Python 食谱 1.11. 使用list包容代替map和filter

  • PyCkBk-1-12 Python 食谱 1.12. 解包简单的类list对象

  • PyCkBk-1-13 Python 食谱 1.13. 拉平一个嵌套的序列

  • PyCkBk-1-14 Python 食谱 1.14. 在索引和序列元素上并行循环

  • PyCkBk-1-15 Python 食谱 1.15. 通过多个list循环

  • PyCkBk-1-16 Python 食谱 1.16. 生成一个浮点数定义的range

  • PyCkBk-1-17 Python 食谱 1.17. 变换二维数组

  • PyCkBk-1-18 Python 食谱 1.18. 不用共享引用建立列表的列表

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

  • PyCkBk-4-1 Section 4.1. Introduction

  • PyCkBk-4-2 Python 食谱 4.2. 读文件

  • PyCkBk-4-3 Python 食谱 4.3. 写文件

  • PyCkBk-4-4 Python 食谱 4.4. 查找替换文件中字符串

  • PyCkBk-4-5 Python 食谱 4.5. 读取文件特定行

  • PyCkBk-4-6 Python 食谱 4.6. 读取文件随机行

  • PyCkBk-4-7 Python 食谱 4.7. 统计文件行数

  • PyCkBk-4-8 Python 食谱 4.8. 处理文件每个单词

  • PyCkBk-4-9 Python 食谱 4.9. 读取文本文件段落

  • PyCkBk-4-10 Python 食谱 4.10. 读取具有连接符号的逻辑行

  • PyCkBk-4-11 Python 食谱 4.11. 读取ZIP压缩文件

  • PyCkBk-4-12 Python 食谱 4.12. 读取INI配置文件

  • PyCkBk-4-13 Python 食谱 4.13. 输出2进制数据到Windows标准输出

  • PyCkBk-4-14 Python 食谱 4.14. 读取随机访问文件中记录

  • PyCkBk-4-15 Python 食谱 4.15. 更新随机访问文件中记录

  • PyCkBk-4-16 Python 食谱 4.16. 分解目录或文件路径

  • PyCkBk-4-17 Python 食谱 4.17. 将路径作为对象处理

  • PyCkBk-4-18 Python 食谱 4.18. 创建(多层)目录

  • PyCkBk-4-19 Python 食谱 4.19. 遍历目录树

  • PyCkBk-4-20 Python 食谱 4.20. 改变目录树下文件类型

  • PyCkBk-4-21 Python 食谱 4.21. 目录集合下查找文件

  • PyCkBk-4-22 Python 食谱 4.22. 在Python搜索路径下查找文件

  • PyCkBk-4-23 Python 食谱 4.23. 动态改变Python查找路径

  • PyCkBk-4-24 Python 食谱 4.24. 平台无关的计算目录大小

  • PyCkBk-4-25 Python 食谱 4.25. 使用平台无关API对文件加锁

  • PyCkBk-4-26 Python 食谱 4.26. 控制文件版本

  • PyCkBk-4-27 Python 食谱 4.27. 模块: 使用版本进行备份

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 整理