Python Cookbook (2nd) Chinese Version
Basic Information
Author: Alex Martelli Anna Ravenscroft David Ascher
Translator: Gao tiejun [same translator's work]
Press: People's post and telecommunications Press
ISBN: 9787115222664
Mounting time:
Published on: February 1, May 2010
Start: 16
For more details, see: http://www.china-pub.com/196697
The first five chapter sample read address: http://www.china-pub.com/computers/common/mianfeisd.asp? Id = 196697
Edit recommendations
O'reilly, one of the famous cookbook series, is a well-known technical book in the python field. The first version is well received and Amazon five-star recommendation.
Introduction
This book introduces some tips and methods for using python in various fields, from the basic characters, file sequences, dictionaries, and sorting, to advanced object-oriented programming, database and data persistence, XML processing and web programming, to more advanced and abstract descriptors, decorator, Meta class, iterator and generator, are involved. The book also introduces the use of some third-party packages and libraries, including twisted, Gil, and pywin32. This book covers many common problems in Python applications and provides general solutions. The code and methods in the book are highly practical and can be easily applied to actual projects and produce immediate results. It is particularly rare that all the authors of this book have rich practical experience in the industry. Therefore, this book not only provides solutions to various problems, at the same time, it also reflects the way of thinking and good programming habits of many experts. Compared with the detailed knowledge, this part of content is undoubtedly the essence of this book.
This book is suitable for readers who have certain Python basics.
Directory
Chapter 1 text 1
Introduction 1
1.1 Each time processing a character 6
Conversion between 1.2 characters and character values 7
1.3 Test whether an object is a class string 8
1.4 string alignment 10
1.5 remove spaces at both ends of the string 11
1.6 merge string 11
1.7 reverse string by character or word by word 14
1.8 check whether the string contains 15 characters in a character set
1.9 simplify the use of the string's translate method 18
1.10 filter string 20 characters that do not belong to the specified set
1.11 check whether a string is text or binary 23
1.12 case sensitivity 25
1.13 access the substring 26
1.14 change the indentation of multi-line text strings 29
1.15 expand and compress tabs 31
1.16 Replace the substring 33 in the string
1.17 Replace the substring in the string-Python 2.4 34
1.18 complete multiple replications at a time 36
. 1.19 check the end tag 39 in the string
1.20 use Unicode to process international text 40
1.21 conversion between Unicode and common strings 43
1.22 print Unicode Character 45 in standard output
1.23 Unicode data encoding for XML and HTML 46
1.24 make some strings case-insensitive 49
1.25 convert HTML documents into text and display them on UNIX terminals 52
Chapter 5 documents 55
Introduction 55
2.1 read files 59
2.2 write files 62
2.3 search for and replace text in a file 64
2.4 read the specified row from the file 65
2.5 calculate the number of lines of files 66
2.6 process each word in a file 68
2.7 random input/output 70
2.8 update Random Access File 71
2.9 read data from the ZIP file 73
2.10 process the ZIP file 74 in the string
2.11 archive the file tree to a compressed tar file 76
2.12 send binary data to Windows standard output 77
2.13 use the iostream-like Syntax of C ++ 78
2.14 roll back the input file to start 80
2.15 use Class Object to adapt to real object 83
2.16 traverse directory tree 84
2.17 change the file extension 85 in the directory tree
2.18 search for files from the specified search path 86
2.19 searching for files based on the specified search path and mode 87
2.20 search for file 88 in the python search path
2.21 dynamically change the python search path 89
2.22 calculate the relative path between directories 91
2.23 read non-cached characters 93 across platforms
2.24 count the number of pages in the PDF document on the Mac OS X platform 94
2.25 modifying file attributes on Windows platform 95
2.26 extract text 96 from OpenOffice.org
2.27 extract text from Microsoft Word documents 97
2.28 cross-platform file lock 98
2.29 file name with version 100
2.30 calculating CRC-64 cyclic Verification Code 102
Chapter 2 time and financial computing 3rd
Introduction 105
3.1 calculate the date of yesterday and tomorrow 111
3.2 search for the last Friday 112
3.3 calculate the time period between dates 114
3.4 calculate the total playback time of a song by 115
3.5 workday between calculation date 116
3.6 automatically query festival 118
3.7 date fuzzy query 121
3.8 check whether 123 is being implemented during renewal
3.9 time zone conversion 124
3.10 execute a command repeatedly 125
3.11 scheduled Command Execution 127
3.12 decimal calculation 129
3.13 use decimal number for currency processing 130
3.14 simple addition tool implemented in Python 133
3.15 check credit card checksum 136
3.16 view exchange rate 137
Chapter 2 Python skills 4th
Introduction 139
4.1 object copy 140
4.2 list derivation build list 144
4.3 If an element exists in the list, 146 is returned.
4.4 elements and indexes in the circular access sequence 147
4.5 create a list 148 without sharing references
4.6 expand a nested sequence 149
4.7 Delete and sort columns in the row list 152
4.8 2D array transformation 154
4.9 value from the dictionary: 155
4.10 Add an entry to the dictionary 157
4.11 create a dictionary without too many quotes 158
4.12 create a dictionary by using the list elements alternately as keys and values 159
4.13 obtain a subset of the dictionary 161
4.14 reverse dictionary 163
4.15 dictionary one-click multi-value 164
4.16 dictionary assignment methods and functions 166
4.17 union and intersection of dictionaries 167
4.18 collect named subitem 169
4.19 use a statement to complete the assignment and test 171
4.20 use printf 174 in Python
4.21 get element 174 with the specified probability
4.22 handling exceptions in expressions 176
4.23 make sure that the name has been defined in the given module 178
Chapter 2 search and sorting 5th
Introduction 180
5.1 sort the dictionary by 185
5.2 case-insensitive sorting of string lists 185
5.3 sort the Object List by Object Attributes 187
5.4 sort keys or indexes by corresponding values 189
5.5 sort strings by embedded numbers 192
5.6 process the list elements in random order 193
5.7 keep sequence 195 when adding elements
5.8 obtain the smallest element in the sequence 197
5.9 search for elements in the sorted sequence 199
5.10 select the minimum N element in the sequence 200
5.11 fast sorting of three lines of code 203
5.12 check sequence members 206
5.13 searching for subsequences 208
5.14 adds the ranking function to the dictionary type by 210
5.15 sort and group names by the first letter of the last name 214
Chapter 2 Object-Oriented Programming 6th
Introduction 217
6.1 temperature Metric Conversion 223
6.2 define a constant 225
6.3 limit attribute setting 227
6.4 chain dictionary query 229
6.5 inherited alternative-Automatic hosting 231
6.6 hosting special methods in proxy 234
6.7 tuples with named subitems 237
6.8 avoid redundant code for Attribute read/write 239
6.9 fast copying objects 240
6.10 Retain references to the bound method and support garbage collection 243
6.11 cache ring implementation 245
6.12 check for status changes of an instance 249
6.13 check whether an object contains certain necessary attributes 252
6.14 implementation status design mode 255
6.15 single instance mode 257
6.16 avoid "Singleton" mode with Borg usage 259
6.17 Implementation of Null Object Design Pattern 263
6.18 use the _ init _ parameter to automatically initialize the instance variable 266
6.19 call the superclass' _ init _ method 267
6.20 precise and secure use of collaborative superclass 270
Chapter 2 Persistence and database 7th
Introduction 273
7.1 use the marshal module to serialize data 275
7.2 use the pickle and cpickle modules to serialize data 277
7.3 compression 280 during pickling
7.4 use the cpickle module for classes and instances 281
7.5 pickling binding method 284
7.6 pickling code object 286
7.7 modifying objects through shelve 288
7.8 use the Berkeley DB database 291
7.9 access MySQL database 294
7.10 store blob 295 in a MySQL database
7.11 store blob 296 in PostgreSQL
7.12 store blob 298 in SQLite
7.13 generate a dictionary to map field names to column number 300
7.14 use dtuple for flexible access to query results 302
7.15 print the content of the database cursor 304
7.16 single parameter transfer style applicable to various dB API modules 306
7.17 use Microsoft Jet 308 through ado
7.18 access JDBC database from Jython servlet 310
7.19 Excel data obtained through Jython and ODBC 313
Chapter 2 debugging and testing 8th
Introduction 315
8.1 block execution of certain conditions and loops 316
8.2 memory usage measured in Linux 317
8.3 debug the garbage collection process 318
8.4 capture and record exceptions 320
8.5 tracking expressions and comments in debug mode 322
8.6 get more information from traceback 324
8.7 enable debugger 327 when no caught exception occurs
8.8 simple unit test 328
8.9 automatic unit test 330
8.10 use doctest and unittest 2.4 in Python 331
8.11 Check Interval 334 in unit test
Chapter 2 processes, threads, and synchronization 9th
Introduction 336
9.1 synchronize all methods in the object 339
9.2 terminate thread 342
9.3 use queue. queue as the priority queue 344
9.4 Use thread pool 346
9.5 execute functions in parallel with multiple sets of parameters 349
9.6 use a simple message transfer coordination thread 351
9.7 store thread information 353
9.8 wireless multi-task collaboration 357
9.9 detect the running of another script instance in Windows 359
9.10 use msgwaitformultipleobjects to process Windows messages 360
9.11 use popen to drive external processes 363
9.12 obtain the output stream and error stream of Unix shell commands 364
9.13 fork a daemon in UNIX 367
Chapter 2 system management 10th
Introduction 370
10.1 generate random password 371
10.2 generate an easy-to-remember pseudo-random password 372
10.3 verify the user in POP server mode 375
10.4 calculate the click-through rate of each IP address in Apache by 376
10.5 calculate the customer cache hit rate of Apache by 378
10.6 call editor 379 in the script
10.7 backup file 381
10.8 selectively copy email files 383
10.9 create a whitelist of email addresses by email address 384
10.10 blocking duplicate emails 386
10.11 check your windows sound system 388
10.12 register and unregister DLL 388 in Windows
10.13 check and modify windows automatic running task 390
10.14 create sharing in Windows 391
10.15 connect to a running Internet Explorer instance 392
10.16 read Microsoft Outlook contacts 393
10.17 collect detailed system information in Mac OS X 396
Chapter 2 User Interface 11th
Introduction 400
11.1 display progress bar 402 on the text Console
11.2 avoid using Lambda 404 when writing callback Functions
11.3 use the default value and range 405 in the tksimpledialog Function
11.4 Add the dragging sorting capability to the tkinter list box by 406
11.5 enter an accent character 408 in the tkinter part
11.6 embed inline GIF 410 in tkinter
11.7 convert image format 412
11.8 implement a stopwatch 415 in tkinter
11.9 integration of GUI and asynchronous I/O with threads 417
11.10 use idle's tree part in tkinter 421
11.11 tkinter ListBox supports single row multi-value 423
11.12 copy the geometry method and option 427 between tkinter parts
11.13 implement a notebook with a tag in tkinter 429
11.14 use wxpython to implement notepad 431 with Panel
11.15 implement an imagej plug-in 433 in Jython
11.16 use swing and Jython to view images through URLs 434
11.17 obtain user input 434 in Mac OS
11.18 programmatic creation of Python cocoa GUI 437
11.19 use ironpython to implement fade-in window 439
Chapter 2 XML processing 12th
Introduction 441
12.1 check XML format integrity 443
12.2 calculate the number of tags in the document by 444
12.3 obtain the text in the XML document 445
12.4 automatically detect XML encoding 447
12.5 convert an XML document into a python Object Tree 449
12.6 delete text nodes with only blank characters from the subtree of the xml dom node 451
12.7 parsing Microsoft Excel XML 452
12.8 verify XML document 454
12.9 filter elements and attributes belonging to the specified namespace 455
12.10 merge continuous text events with Sax 458
12.11 use mshtml to parse XML or HTML 461
Chapter 2 network programming 13th
Introduction 462
13.1 transmit messages through socket datagram 464
13.2 capture documents from the Web 466
13.3 filter FTP site list 467
13.4 get time from server through SNTP 468
13.5 send HTML mail 469
13.6 bind a file to the mime message 471
13.7 split a multipart mime message 474
13.8 Delete attachments in the email message 475
13.9 fixed the message 2.4 parsed by email. feedparser of Python 477.
13.10 interactively check POP3 mailbox 479
13.11 detect inactive computers 482
13.12 monitoring network with HTTP 487
13.13 network port forwarding and redirection 489
13.14 build an SSL tunnel through proxy 492
13.15 implement dynamic IP protocol 495
13.16 log on to IRC and record messages to disk 498
13.17 access the LDAP service 500
Chapter 2 web programming 14th
Introduction 502
14.1 test whether CGI is working 503
14.2 use CGI scripts to process URL 506
14.3 use CGI to upload files 507
14.4 check the existence of web pages 509
14.5 HTTP check content type 510
14.6 resume HTTP download 512
14.7 process cookie 513 when crawling web pages
14.8 perform HTTPS navigation by using an authentication agent 516
14.9 using Jython to implement servlet 517
14.10 search for Internet Explorer cookies 519
14.11 generate an opml file 521
14.12 aggregate RSS feed 524
14.13 add data to the web page through a template 527
14.14 rendering any object in nevow 530
Chapter 2 distributed programming 15th
Introduction 534
15.1 implement a XML-RPC method call 536
15.2 service XML-RPC request 537
15.3 use XML-RPC 539 in Medusa
15.4 allow remote termination of XML-RPC services 541
15.5 simplexmlrpcserver details 542
15.6 provides a wxpython GUI 544 for a XML-RPC Service
15.7 use twisted's perspective broker 546
15.8 implement a CORBA service and customer 549
15.9 use telnetlib for remote logon 551
15.10 use SSH to perform remote logon 554
15.11 verify an SSL client over HTTPS 557
Chapter 4 Procedure 16th
Introduction 559
16.1 verify that the string represents a valid number of 564
16.2 import a dynamically generated module 565
16.3 import a module named at runtime 567
16.4 associate parameters with functions 568
16.5 composite functions 571
16.6 use the built-in tokenizer to color Python source code 572
16.7 merge and disassemble token 575
16.8 check whether the string has balanced parentheses 577
16.9 simulate enumeration in Python 580
16.10 reference itself when creating a list derivation 583
16.11 automated py2exe compilation of scripts into windows executable files 585
16.12 bind the master script and module to an executable file in UNIX 587
Chapter 2 extension and embedding 17th
Introduction 590
17.1 implement a simple extension type 592
17.2 implement a simple extension type with Pyrex 597
17.3 use the C ++ library in Python 598
17.4 call windows DLL Functions 601
17.5 use swig to generate a module in a multi-threaded environment 603
17.6 use pysequence_fast to convert Python sequence to C array 604
17.7 use an iterator to access elements of the python sequence one by one 608
17.8 return none 611 from the python callable c Function
17.9 use GDB to debug C extensions for dynamic loading 613
17.10 debugging memory problems 614
Chapter 1 algorithm 18th
Introduction 616
18.1 eliminate duplicates in the sequence 619
18.2 eliminate duplicates in the sequence while retaining the sequence 621
18.3 generate reset sampling 625
18.4 generate sampling with no return: 626
18.5 return value of the cache function: 627
18.6 implement a FIFO container 629
18.7 use the FIFO policy to cache objects 631
18.8 implement a bag (Multiset) collection type 634
18.9 simulate a ternary operator in Python 637
18.10 calculate the prime number 640
18.11 format the integer as a binary string 642
18.12 format the integer as a string 644 based on any number
18.13 convert the number into a rational number by using the Farai score 646
18.14 mathematical calculation with error transfer 648
18.15 sum with the maximum accuracy of 651
18.16 simulate floating point number 653
18.17 calculate the convex hull and diameter of a two-dimensional point set by 656
Chapter 2 iterator and generator 19th
Introduction 660
19.1 compile a function 663 that increases the number of floating points similar to range.
19.2 create a list from any iteratable object 665
19.3 generate a fiber-ACCI sequence 667
19.4 remove part of items in multi-value assignment 669
19.5 automatically disassemble the required number of items 670
19.6 split an Iterated object into N slices by step n 672
19.7 loop sequence through overlapping windows 674
19.8 loop multiple iteratable objects in parallel 678
19.9 target product of multiple iteratable objects in a loop 680
19.10 segment-by-segment reading of text files 683
19.11 read row 685 with a continuation character
19.12 process a data block stream in a row 687
19.13 use a generator to capture a large record set from the database 688
19.14 merge ordered sequence 690
19.15 generate an arrangement, combination, and select 694
19.16 generate integer division 696
19.17 replication iterator 697
19.18 future of the iterator 701
19.19 simplified queue consumer thread 703
19.20 run iterator 705 in another thread
19.21 use itertools. groupby to calculate the summary report 706
Chapter 2 descriptor, decorator, and metadata 20th
Introduction 710
20.1 obtain the new default value 712 in function calls
20.2 use nested functions to compile property 715
20.3 alias 717 for the property value
20.4 cache property value 719
20.5 access multiple attributes using the same method 722
20.6 encapsulate a method to add function 723 to the class
20.7 enhance all methods to add functions to the class 726
20.8 add method 728 to a class instance at runtime
20.9 Check interface implementation 730
20.10 correctly use _ new _ and _ init _ 732 in the Custom metadata class
20.11 allow chained calls to Variable list methods 734
20.12 use collaborative superclass with more compact syntaxes 736
20.13 do not use _ init _ to initialize the instance property 738
20.14 automatic instance attribute initialization 740
20.15 automatically update class instance 743 upon reload
20.16 bind the constant 747 during compilation
20.17 resolve metadata conflicts 752