Python Cookbook
Home > Computing and Information Technology > Computer programming / software engineering > Web programming > Python Cookbook
Python Cookbook

Python Cookbook

|
     5  |  1 Reviews 
5
4
3
2
1




Out of Stock


Notify me when this book is in stock
About the Book

This is a collection of problems, solutions, and practical examples for Python programmers, written by Python programmers. It contains over two hundred recipes for text manipulation, object oriented programming, XML processing, system administration, and more. It contains useful code for both novices and advanced practitioners, with contributions from Guido Van Rossum, Tim Peters, Paul Prescod, and Mark Hammond. Recipes include: searching and sorting; manipulating text; working with files and the filesystem; object-oriented programming; dealing with threads and processes; system administration; interacting with databases; creating user interfaces; network and Web programming; processing XML; distributed programming; debugging and testing; and extending Python.

Table of Contents:
Foreword Preface 1. Python Shortcuts 1.1 Swapping Values Without Using a Temporary Variable 1.2 Constructing a Dictionary Without Excessive Quoting 1.3 Getting a Value from a Dictionary 1.4 Adding an Entry to a Dictionary 1.5 Associating Multiple Values with Each Key in a Dictionary 1.6 Dispatching Using a Dictionary 1.7 Collecting a Bunch of Named Items 1.8 Finding the Intersection of Two Dictionaries 1.9 Assigning and Testing with One Statement 1.10 Using List Comprehensions Instead of map and filter 1.11 Unzipping Simple List-Like Objects 1.12 Flattening a Nested Sequence 1.13 Looping in Parallel over Index and Sequence Items 1.14 Looping Through Multiple Lists 1.15 Spanning a Range Defined by Floats 1.16 Transposing Two-Dimensional Arrays 1.17 Creating Lists of Lists Without Sharing References 2. Searching and Sorting 2.1 Sorting a Dictionary 2.2 Processing Selected Pairs of Structured Data Efficiently 2.3 Sorting While Guaranteeing Sort Stability 2.4 Sorting by One Field, Then by Another 2.5 Looking for Items in a Sorted Sequence Using Binary Search 2.6 Sorting a List of Objects by an Attribute of the Objects 2.7 Sorting by Item or by Attribute 2.8 Selecting Random Elements from a List Without Repetition 2.9 Performing Frequent Membership Tests on a Sequence 2.10 Finding the Deep Index of an Item in an Embedded Sequence 2.11 Showing Off Quicksort in Three Lines 2.12 Sorting Objects Using SQL's ORDER BY Syntax 3. Text 3.1 Processing a String One Character at a Time 3.2 Testing if an Object Is String-Like 3.3 Aligning Strings 3.4 Trimming Space from the Ends of a String 3.5 Combining Strings 3.6 Checking Whether a String Contains a Set of Characters 3.7 Filtering a String for a Set of Characters 3.8 Controlling Case 3.9 Reversing a String by Words or Characters 3.10 Accessing Substrings 3.11 Changing the Indentation of a Multiline String 3.12 Testing Whether a String Represents an Integer 3.13 Expanding and Compressing Tabs 3.14 Replacing Multiple Patterns in a Single Pass 3.15 Converting Between Different Naming Conventions 3.16 Converting Between Characters and Values 3.17 Converting Between Unicode and Plain Strings 3.18 Printing Unicode Characters to Standard Output 3.19 Dispatching Based on Pattern Matches 3.20 Evaluating Code Inside Strings 3.21 Replacing Python Code with the Results of Executing That Code 3.22 Module: Yet Another Python Templating Utility (YAPTU) 3.23 Module: Roman Numerals 4. Files 4.1 Reading from a File 4.2 Writing to a File 4.3 Searching and Replacing Text in a File 4.4 Reading a Particular Line from a File 4.5 Retrieving a Line at Random from a File of Unknown Size 4.6 Counting Lines in a File 4.7 Processing Every Word in a File 4.8 Reading a Text File by Paragraphs 4.9 Reading Lines with Continuation Characters 4.10 Reading Data from ZIP Files 4.11 Reading INI Configuration Files 4.12 Sending Binary Data to Standard Output Under Windows 4.13 Using Random-Access Input/Output 4.14 Updating a Random-Access File 4.15 Splitting a Path into All of Its Parts 4.16 Treating Pathnames as Objects 4.17 Creating Directories Including Necessary Parent Directories 4.18 Walking Directory Trees 4.19 Swapping One File Extension for Another Throughout a Directory Tree 4.20 Finding a File Given an Arbitrary Search Path 4.21 Finding a File on the Python Search Path 4.22 Dynamically Changing the Python Search Path 4.23 Computing Directory Sizes in a Cross-Platform Way 4.24 File Locking Using a Cross-Platform API 4.25 Versioning Filenames 4.26 Module: Versioned Backups 5. Object-Oriented Programming 5.1 Overriding a Built-In Method .2 Getting All Members of a Class Hierarchy 5.3 Calling a Superclass _ _init_ _ Method if It Exists 5.4 Calling a Superclass Implementation of a Method 5.5 Implementing Properties 5.6 Implementing Static Methods 5.7 Implementing Class Methods 5.8 Delegating Automatically as an Alternative to Inheritance 5.9 Decorating an Object with Print-Like Methods 5.10 Checking if an Object Has Necessary Attributes 5.11 Making a Fast Copy of an Object 5.12 Adding Methods to a Class at Runtime 5.13 Modifying the Class Hierarchy of an Instance 5.14 Keeping References to Bound Methods Without Inhibiting Garbage Collection 5.15 Defining Constants 5.16 Managing Options 5.17 Implementing a Set Class 5.18 Implementing a Ring Buffer 5.19 Implementing a Collection 5.20 Delegating Messages to Multiple Objects 5.21 Implementing the Singleton Design Pattern 5.22 Avoiding the Singleton Design Pattern with the Borg Idiom 5.23 Implementing the Null Object Design Pattern 6. Threads, Processes, and Synchronization 6.1 Storing Per-Thread Information 6.2 Terminating a Thread 6.3 Allowing Multithreaded Read Access While Maintaining a Write Lock 6.4 Running Functions in the Future 6.5 Synchronizing All Methods in an Object 6.6 Capturing the Output and Error Streams from a Unix Shell Command 6.7 Forking a Daemon Process on Unix 6.8 Determining if Another Instance of a Script Is Already Running in Windows 6.9 Processing Windows Messages Using MsgWaitForMultipleObjects 7. System Administration 7.1 Running a Command Repeatedly 7.2 Generating Random Passwords 7.3 Generating Non-Totally Random Passwords 7.4 Checking the Status of a Unix Network Interface 7.5 Calculating Apache Hits per IP Address 7.6 Calculating the Rate of Client Cache Hits on Apache 7.7 Manipulating the Environment on Windows NT/2000/XP 7.8 Checking and Modifying the Set of Tasks Windows Automatically Runs at Logon 7.9 Examining the Microsoft Windows Registry for a List of Name Server Addresses 7.10 Getting Information About the Current User on Windows NT/2000 7.11 Getting the Windows Service Name from Its Long Name 7.12 Manipulating Windows Services 7.13 Impersonating Principals on Windows 7.14 Changing a Windows NT Password Using ADSI 7.15 Working with Windows Scripting Host (WSH) from Python 7.16 Displaying Decoded Hotkeys for Shortcuts in Windows 8. Databases and Persistence 8.1 Serializing Data Using the marshal Module 8.2 Serializing Data Using the pickle and cPickle Modules 8.3 Using the cPickle Module on Classes and Instances 8.4 Mutating Objects with shelve 8.5 Accesssing a MySQL Database 8.6 Storing a BLOB in a MySQL Database 8.7 Storing a BLOB in a PostgreSQL Database 8.8 Generating a Dictionary Mapping from Field Names to Column Numbers 8.9 Using dtuple for Flexible Access to Query Results 8.10 Pretty-Printing the Contents of Database Cursors 8.11 Establishing Database Connections Lazily 8.12 Accessing a JDBC Database from a Jython Servlet 8.13 Module: jet2sql-Creating a SQL DDL from an Access Database 9. User Interfaces 9.1 Avoiding lambda in Writing Callback Functions 9.2 Creating Menus with Tkinter 9.3 Creating Dialog Boxes with Tkinter 9.4 Supporting Multiple Values per Row in a Tkinter Listbox 9.5 Embedding Inline GIFs Using Tkinter 9.6 Combining Tkinter and Asynchronous I/O with Threads 9.7 Using a wxPython Notebook with Panels 9.8 Giving the User Unobtrusive Feedback During Data Entry with Qt 9.9 Building GUI Solutions Independent of the Specific GUI Toolkit 9.10 Creating Color Scales 9.11 Using Publish/Subscribe Broadcasting to Loosen the Coupling Between GUI and Business Logic Systems 9.12 Module: Building GTK GUIs Interactively 10. Network Programming 10.1 Writing a TCP Client 10.2 Writing a TCP Server 10.3 Passing Messages with Socket Datagrams 10.4 Finding Your Own Name and Address 10.5 Converting IP Addresses 10.6 Grabbing a Document from the Web 10.7 Being an FTP Client 10.8 Sending HTML Mail 10.9 Sending Multipart MIME Email 10.10 Bundling Files in a MIME Message 10.11 Unpacking a Multipart MIME Message 10.12 Module: PyHeartBeat-Detecting Inactive Computers 10.13 Module: Interactive POP3 Mailbox Inspector 10.14 Module: Watching for New IMAP Mail Using a GUI 11. Web Programming 11.1 Testing Whether CGI Is Working 11.2 Writing a CGI Script 11.3 Using a Simple Dictionary for CGI Parameters 11.4 Handling URLs Within a CGI Script 11.5 Resuming the HTTP Download of a File 11.6 Stripping Dangerous Tags and Javascript from HTML 11.7 Running a Servlet with Jython 11.8 Accessing Netscape Cookie Information 11.9 Finding an Internet Explorer Cookie 11.10 Module: Fetching Latitude/Longitude Data from the Web 12. Processing XML 12.1 Checking XML Well-Formedness 12.2 Counting Tags in a Document 12.3 Extracting Text from an XML Document 12.4 Transforming an XML Document Using XSLT 12.5 Transforming an XML Document Using Python 12.6 Parsing an XML File with xml.parsers.expat 12.7 Converting Ad-Hoc Text into XML Markup 12.8 Normalizing an XML Document 12.9 Controlling XSLT Stylesheet Loading 12.10 Autodetecting XML Encoding 12.11 Module: XML Lexing (Shallow Parsing) 12.12 Module: Converting a List of Equal-Length Lists into XML 13. Distributed Programming 13.1 Making an XML-RPC Method Call 13.2 Serving XML-RPC Requests 13.3 Using XML-RPC with Medusa 13.4 Writing a Web Service That Supports Both XML-RPC and SOAP 13.5 Implementing a CORBA Client and Server 13.6 Performing Remote Logins Using telnetlib 13.7 Using Publish/Subscribe in a Distributed Middleware Architecture 13.8 Using Request/Reply in a Distributed Middleware Architecture 14. Debugging and Testing 14.1 Reloading All Loaded Modules 14.2 Tracing Expressions and Comments in Debug Mode 14.3 Wrapping Tracebacks in HTML 14.4 Getting More Information from Tracebacks 14.5 Starting the Debugger Automatically After an Uncaught Exception 14.6 Logging and Tracing Across Platforms 14.7 Determining the Name of the Current Function 14.8 Introspecting the Call Stack with Older Versions of Python 14.9 Debugging the Garbage-Collection Process 14.10 Tracking Instances of Particular Classes 15. Programs About Programs 15.1 Colorizing Python Source Using the Built-in Tokenizer 15.2 Importing a Dynamically Generated Module 15.3 Importing from a Module Whose Name Is Determined at Runtime 15.4 Importing Modules with Automatic End-of-Line Conversions 15.5 Simulating Enumerations in Python 15.6 Modifying Methods in Place 15.7 Associating Parameters with a Function (Currying) 15.8 Composing Functions 15.9 Adding Functionality to a Class 15.10 Adding a Method to a Class Instance at Runtime 15.11 Defining a Custom Metaclass to Control Class Behavior 15.12 Module: Allowing the Python Profiler to Profile C Modules 16. Extending and Embedding 16.1 Implementing a Simple Extension Type 16.2 Translating a Python Sequence into a C Array with the PySequence_Fast Protocol 16.3 Accessing a Python Sequence Item-by-Item with the Iterator Protocol 16.4 Returning None from a Python-Callable C Function 16.5 Coding the Methods of a Python Class in C 16.6 Implementing C Function Callbacks to a Python Function 16.7 Debugging Dynamically Loaded C Extensions with gdb 16.8 Debugging Memory Problems 16.9 Using SWIG-Generated Modules in a Multithreaded Environment 17. Algorithms 17.1 Testing if a Variable Is Defined 17.2 Evaluating Predicate Tests Across Sequences 17.3 Removing Duplicates from a Sequence 17.4 Removing Duplicates from a Sequence While Maintaining Sequence Order 17.5 Simulating the Ternary Operator in Python 17.6 Counting Items and Sorting by Incidence (Histograms) 17.7 Memoizing (Caching) the Return Values of Functions 17.8 Looking Up Words by Sound Similarity 17.9 Computing Factorials with lambda 17.10 Generating the Fibonacci Sequence 17.11 Wrapping an Unbounded Iterator to Restrict Its Output 17.12 Operating on Iterators 17.13 Rolling Dice 17.14 Implementing a First-In First-Out Container 7.15 Modeling a Priority Queue 17.16 Converting Numbers to Rationals via Farey Fractions 17.17 Evaluating a Polynomial 17.18 Module: Finding the Convex Hull of a Set of 2D Points 17.19 Module: Parsing a String into a Date/Time Object Portably List of Contributors Index


Best Sellers


Product Details
  • ISBN-13: 9780596001674
  • Publisher: O'Reilly Media
  • Publisher Imprint: O'Reilly Media
  • Height: 233 mm
  • Returnable: N
  • Width: 179 mm
  • ISBN-10: 0596001673
  • Publisher Date: 27 Aug 2002
  • Binding: Paperback
  • Language: English
  • Weight: 860 gr


Similar Products

Add Photo
Add Photo

Customer Reviews

     5  |  1 Reviews 
out of (%) reviewers recommend this product
Top Reviews
Rating Snapshot
Select a row below to filter reviews.
5
4
3
2
1
Average Customer Ratings
     5  |  1 Reviews 
00 of 0 Reviews
Sort by :
Active Filters

00 of 0 Reviews
SEARCH RESULTS
1–2 of 2 Reviews
    BoxerLover2 - 5 Days ago
    A Thrilling But Totally Believable Murder Mystery

    Read this in one evening. I had planned to do other things with my day, but it was impossible to put down. Every time I tried, I was drawn back to it in less than 5 minutes. I sobbed my eyes out the entire last 100 pages. Highly recommend!

    BoxerLover2 - 5 Days ago
    A Thrilling But Totally Believable Murder Mystery

    Read this in one evening. I had planned to do other things with my day, but it was impossible to put down. Every time I tried, I was drawn back to it in less than 5 minutes. I sobbed my eyes out the entire last 100 pages. Highly recommend!


Sample text
Photo of
    Media Viewer

    Sample text
    Reviews
    Reader Type:
    BoxerLover2
    00 of 0 review

    Your review was submitted!
    Python Cookbook
    O'Reilly Media -
    Python Cookbook
    Writing guidlines
    We want to publish your review, so please:
    • keep your review on the product. Review's that defame author's character will be rejected.
    • Keep your review focused on the product.
    • Avoid writing about customer service. contact us instead if you have issue requiring immediate attention.
    • Refrain from mentioning competitors or the specific price you paid for the product.
    • Do not include any personally identifiable information, such as full names.

    Python Cookbook

    Required fields are marked with *

    Review Title*
    Review
      Add Photo Add up to 6 photos
      Would you recommend this product to a friend?
      Tag this Book Read more
      Does your review contain spoilers?
      What type of reader best describes you?
      I agree to the terms & conditions
      You may receive emails regarding this submission. Any emails will include the ability to opt-out of future communications.

      CUSTOMER RATINGS AND REVIEWS AND QUESTIONS AND ANSWERS TERMS OF USE

      These Terms of Use govern your conduct associated with the Customer Ratings and Reviews and/or Questions and Answers service offered by Bookswagon (the "CRR Service").


      By submitting any content to Bookswagon, you guarantee that:
      • You are the sole author and owner of the intellectual property rights in the content;
      • All "moral rights" that you may have in such content have been voluntarily waived by you;
      • All content that you post is accurate;
      • You are at least 13 years old;
      • Use of the content you supply does not violate these Terms of Use and will not cause injury to any person or entity.
      You further agree that you may not submit any content:
      • That is known by you to be false, inaccurate or misleading;
      • That infringes any third party's copyright, patent, trademark, trade secret or other proprietary rights or rights of publicity or privacy;
      • That violates any law, statute, ordinance or regulation (including, but not limited to, those governing, consumer protection, unfair competition, anti-discrimination or false advertising);
      • That is, or may reasonably be considered to be, defamatory, libelous, hateful, racially or religiously biased or offensive, unlawfully threatening or unlawfully harassing to any individual, partnership or corporation;
      • For which you were compensated or granted any consideration by any unapproved third party;
      • That includes any information that references other websites, addresses, email addresses, contact information or phone numbers;
      • That contains any computer viruses, worms or other potentially damaging computer programs or files.
      You agree to indemnify and hold Bookswagon (and its officers, directors, agents, subsidiaries, joint ventures, employees and third-party service providers, including but not limited to Bazaarvoice, Inc.), harmless from all claims, demands, and damages (actual and consequential) of every kind and nature, known and unknown including reasonable attorneys' fees, arising out of a breach of your representations and warranties set forth above, or your violation of any law or the rights of a third party.


      For any content that you submit, you grant Bookswagon a perpetual, irrevocable, royalty-free, transferable right and license to use, copy, modify, delete in its entirety, adapt, publish, translate, create derivative works from and/or sell, transfer, and/or distribute such content and/or incorporate such content into any form, medium or technology throughout the world without compensation to you. Additionally,  Bookswagon may transfer or share any personal information that you submit with its third-party service providers, including but not limited to Bazaarvoice, Inc. in accordance with  Privacy Policy


      All content that you submit may be used at Bookswagon's sole discretion. Bookswagon reserves the right to change, condense, withhold publication, remove or delete any content on Bookswagon's website that Bookswagon deems, in its sole discretion, to violate the content guidelines or any other provision of these Terms of Use.  Bookswagon does not guarantee that you will have any recourse through Bookswagon to edit or delete any content you have submitted. Ratings and written comments are generally posted within two to four business days. However, Bookswagon reserves the right to remove or to refuse to post any submission to the extent authorized by law. You acknowledge that you, not Bookswagon, are responsible for the contents of your submission. None of the content that you submit shall be subject to any obligation of confidence on the part of Bookswagon, its agents, subsidiaries, affiliates, partners or third party service providers (including but not limited to Bazaarvoice, Inc.)and their respective directors, officers and employees.

      Accept

      New Arrivals

      Inspired by your browsing history


      Your review has been submitted!

      You've already reviewed this product!