1. Trang chủ
  2. » Công Nghệ Thông Tin

Python cookbook recipes for mastering python 3 3rd edition

706 271 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 706
Dung lượng 5,37 MB

Nội dung

www.allitebooks.com www.allitebooks.com THIRD EDITION Python Cookbook David Beazley and Brian K Jones www.allitebooks.com Python Cookbook, Third Edition by David Beazley and Brian K Jones Copyright © 2013 David Beazley and Brian Jones All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://my.safaribooksonline.com) For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com Editors: Meghan Blanchette and Rachel Roumeliotis Production Editor: Kristen Borg Copyeditor: Jasmine Kwityn Proofreader: BIM Proofreading Services May 2013: Indexer: WordCo Indexing Services Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Third Edition Revision History for the Third Edition: 2013-05-08: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449340377 for release details Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc Python Cookbook, the image of a springhaas, and related trade dress are trademarks of O’Reilly Media, Inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark 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 authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN: 978-1-449-34037-7 [LSI] www.allitebooks.com Table of Contents Preface xi Data Structures and Algorithms 1.1 Unpacking a Sequence into Separate Variables 1.2 Unpacking Elements from Iterables of Arbitrary Length 1.3 Keeping the Last N Items 1.4 Finding the Largest or Smallest N Items 1.5 Implementing a Priority Queue 1.6 Mapping Keys to Multiple Values in a Dictionary 1.7 Keeping Dictionaries in Order 1.8 Calculating with Dictionaries 1.9 Finding Commonalities in Two Dictionaries 1.10 Removing Duplicates from a Sequence while Maintaining Order 1.11 Naming a Slice 1.12 Determining the Most Frequently Occurring Items in a Sequence 1.13 Sorting a List of Dictionaries by a Common Key 1.14 Sorting Objects Without Native Comparison Support 1.15 Grouping Records Together Based on a Field 1.16 Filtering Sequence Elements 1.17 Extracting a Subset of a Dictionary 1.18 Mapping Names to Sequence Elements 1.19 Transforming and Reducing Data at the Same Time 1.20 Combining Multiple Mappings into a Single Mapping 11 12 13 15 17 18 20 21 23 24 26 28 29 32 33 Strings and Text 37 2.1 Splitting Strings on Any of Multiple Delimiters 2.2 Matching Text at the Start or End of a String 2.3 Matching Strings Using Shell Wildcard Patterns 2.4 Matching and Searching for Text Patterns 37 38 40 42 iii www.allitebooks.com 2.5 Searching and Replacing Text 2.6 Searching and Replacing Case-Insensitive Text 2.7 Specifying a Regular Expression for the Shortest Match 2.8 Writing a Regular Expression for Multiline Patterns 2.9 Normalizing Unicode Text to a Standard Representation 2.10 Working with Unicode Characters in Regular Expressions 2.11 Stripping Unwanted Characters from Strings 2.12 Sanitizing and Cleaning Up Text 2.13 Aligning Text Strings 2.14 Combining and Concatenating Strings 2.15 Interpolating Variables in Strings 2.16 Reformatting Text to a Fixed Number of Columns 2.17 Handling HTML and XML Entities in Text 2.18 Tokenizing Text 2.19 Writing a Simple Recursive Descent Parser 2.20 Performing Text Operations on Byte Strings 45 46 47 48 50 52 53 54 57 58 61 64 65 66 69 78 Numbers, Dates, and Times 83 3.1 Rounding Numerical Values 3.2 Performing Accurate Decimal Calculations 3.3 Formatting Numbers for Output 3.4 Working with Binary, Octal, and Hexadecimal Integers 3.5 Packing and Unpacking Large Integers from Bytes 3.6 Performing Complex-Valued Math 3.7 Working with Infinity and NaNs 3.8 Calculating with Fractions 3.9 Calculating with Large Numerical Arrays 3.10 Performing Matrix and Linear Algebra Calculations 3.11 Picking Things at Random 3.12 Converting Days to Seconds, and Other Basic Time Conversions 3.13 Determining Last Friday’s Date 3.14 Finding the Date Range for the Current Month 3.15 Converting Strings into Datetimes 3.16 Manipulating Dates Involving Time Zones 83 84 87 89 90 92 94 96 97 100 102 104 106 107 109 110 Iterators and Generators 113 4.1 Manually Consuming an Iterator 4.2 Delegating Iteration 4.3 Creating New Iteration Patterns with Generators 4.4 Implementing the Iterator Protocol 4.5 Iterating in Reverse 4.6 Defining Generator Functions with Extra State iv | Table of Contents www.allitebooks.com 113 114 115 117 119 120 4.7 Taking a Slice of an Iterator 4.8 Skipping the First Part of an Iterable 4.9 Iterating Over All Possible Combinations or Permutations 4.10 Iterating Over the Index-Value Pairs of a Sequence 4.11 Iterating Over Multiple Sequences Simultaneously 4.12 Iterating on Items in Separate Containers 4.13 Creating Data Processing Pipelines 4.14 Flattening a Nested Sequence 4.15 Iterating in Sorted Order Over Merged Sorted Iterables 4.16 Replacing Infinite while Loops with an Iterator 122 123 125 127 129 131 132 135 136 138 Files and I/O 141 5.1 Reading and Writing Text Data 5.2 Printing to a File 5.3 Printing with a Different Separator or Line Ending 5.4 Reading and Writing Binary Data 5.5 Writing to a File That Doesn’t Already Exist 5.6 Performing I/O Operations on a String 5.7 Reading and Writing Compressed Datafiles 5.8 Iterating Over Fixed-Sized Records 5.9 Reading Binary Data into a Mutable Buffer 5.10 Memory Mapping Binary Files 5.11 Manipulating Pathnames 5.12 Testing for the Existence of a File 5.13 Getting a Directory Listing 5.14 Bypassing Filename Encoding 5.15 Printing Bad Filenames 5.16 Adding or Changing the Encoding of an Already Open File 5.17 Writing Bytes to a Text File 5.18 Wrapping an Existing File Descriptor As a File Object 5.19 Making Temporary Files and Directories 5.20 Communicating with Serial Ports 5.21 Serializing Python Objects 141 144 144 145 147 148 149 151 152 153 156 157 158 160 161 163 165 166 167 170 171 Data Encoding and Processing 175 6.1 Reading and Writing CSV Data 6.2 Reading and Writing JSON Data 6.3 Parsing Simple XML Data 6.4 Parsing Huge XML Files Incrementally 6.5 Turning a Dictionary into XML 6.6 Parsing, Modifying, and Rewriting XML 6.7 Parsing XML Documents with Namespaces 175 179 183 186 189 191 193 Table of Contents www.allitebooks.com | v 6.8 Interacting with a Relational Database 6.9 Decoding and Encoding Hexadecimal Digits 6.10 Decoding and Encoding Base64 6.11 Reading and Writing Binary Arrays of Structures 6.12 Reading Nested and Variable-Sized Binary Structures 6.13 Summarizing Data and Performing Statistics 195 197 199 199 203 214 Functions 217 7.1 Writing Functions That Accept Any Number of Arguments 7.2 Writing Functions That Only Accept Keyword Arguments 7.3 Attaching Informational Metadata to Function Arguments 7.4 Returning Multiple Values from a Function 7.5 Defining Functions with Default Arguments 7.6 Defining Anonymous or Inline Functions 7.7 Capturing Variables in Anonymous Functions 7.8 Making an N-Argument Callable Work As a Callable with Fewer Arguments 7.9 Replacing Single Method Classes with Functions 7.10 Carrying Extra State with Callback Functions 7.11 Inlining Callback Functions 7.12 Accessing Variables Defined Inside a Closure 217 219 220 221 222 224 225 227 231 232 235 238 Classes and Objects 243 8.1 Changing the String Representation of Instances 8.2 Customizing String Formatting 8.3 Making Objects Support the Context-Management Protocol 8.4 Saving Memory When Creating a Large Number of Instances 8.5 Encapsulating Names in a Class 8.6 Creating Managed Attributes 8.7 Calling a Method on a Parent Class 8.8 Extending a Property in a Subclass 8.9 Creating a New Kind of Class or Instance Attribute 8.10 Using Lazily Computed Properties 8.11 Simplifying the Initialization of Data Structures 8.12 Defining an Interface or Abstract Base Class 8.13 Implementing a Data Model or Type System 8.14 Implementing Custom Containers 8.15 Delegating Attribute Access 8.16 Defining More Than One Constructor in a Class 8.17 Creating an Instance Without Invoking init 8.18 Extending Classes with Mixins 8.19 Implementing Stateful Objects or State Machines vi | Table of Contents www.allitebooks.com 243 245 246 248 250 251 256 260 264 267 270 274 277 283 287 291 293 294 299 8.20 Calling a Method on an Object Given the Name As a String 8.21 Implementing the Visitor Pattern 8.22 Implementing the Visitor Pattern Without Recursion 8.23 Managing Memory in Cyclic Data Structures 8.24 Making Classes Support Comparison Operations 8.25 Creating Cached Instances 305 306 311 317 321 323 Metaprogramming 329 9.1 Putting a Wrapper Around a Function 9.2 Preserving Function Metadata When Writing Decorators 9.3 Unwrapping a Decorator 9.4 Defining a Decorator That Takes Arguments 9.5 Defining a Decorator with User Adjustable Attributes 9.6 Defining a Decorator That Takes an Optional Argument 9.7 Enforcing Type Checking on a Function Using a Decorator 9.8 Defining Decorators As Part of a Class 9.9 Defining Decorators As Classes 9.10 Applying Decorators to Class and Static Methods 9.11 Writing Decorators That Add Arguments to Wrapped Functions 9.12 Using Decorators to Patch Class Definitions 9.13 Using a Metaclass to Control Instance Creation 9.14 Capturing Class Attribute Definition Order 9.15 Defining a Metaclass That Takes Optional Arguments 9.16 Enforcing an Argument Signature on *args and **kwargs 9.17 Enforcing Coding Conventions in Classes 9.18 Defining Classes Programmatically 9.19 Initializing Class Members at Definition Time 9.20 Implementing Multiple Dispatch with Function Annotations 9.21 Avoiding Repetitive Property Methods 9.22 Defining Context Managers the Easy Way 9.23 Executing Code with Local Side Effects 9.24 Parsing and Analyzing Python Source 9.25 Disassembling Python Byte Code 329 331 333 334 336 339 341 345 347 350 352 355 356 359 362 364 367 370 374 376 382 384 386 388 392 10 Modules and Packages 397 10.1 Making a Hierarchical Package of Modules 10.2 Controlling the Import of Everything 10.3 Importing Package Submodules Using Relative Names 10.4 Splitting a Module into Multiple Files 10.5 Making Separate Directories of Code Import Under a Common Namespace 10.6 Reloading Modules Table of Contents www.allitebooks.com 397 398 399 401 404 406 | vii 10.7 Making a Directory or Zip File Runnable As a Main Script 10.8 Reading Datafiles Within a Package 10.9 Adding Directories to sys.path 10.10 Importing Modules Using a Name Given in a String 10.11 Loading Modules from a Remote Machine Using Import Hooks 10.12 Patching Modules on Import 10.13 Installing Packages Just for Yourself 10.14 Creating a New Python Environment 10.15 Distributing Packages 407 408 409 411 412 428 431 432 433 11 Network and Web Programming 437 11.1 Interacting with HTTP Services As a Client 11.2 Creating a TCP Server 11.3 Creating a UDP Server 11.4 Generating a Range of IP Addresses from a CIDR Address 11.5 Creating a Simple REST-Based Interface 11.6 Implementing a Simple Remote Procedure Call with XML-RPC 11.7 Communicating Simply Between Interpreters 11.8 Implementing Remote Procedure Calls 11.9 Authenticating Clients Simply 11.10 Adding SSL to Network Services 11.11 Passing a Socket File Descriptor Between Processes 11.12 Understanding Event-Driven I/O 11.13 Sending and Receiving Large Arrays 437 441 445 447 449 454 456 458 461 464 470 475 481 12 Concurrency 485 12.1 Starting and Stopping Threads 12.2 Determining If a Thread Has Started 12.3 Communicating Between Threads 12.4 Locking Critical Sections 12.5 Locking with Deadlock Avoidance 12.6 Storing Thread-Specific State 12.7 Creating a Thread Pool 12.8 Performing Simple Parallel Programming 12.9 Dealing with the GIL (and How to Stop Worrying About It) 12.10 Defining an Actor Task 12.11 Implementing Publish/Subscribe Messaging 12.12 Using Generators As an Alternative to Threads 12.13 Polling Multiple Thread Queues 12.14 Launching a Daemon Process on Unix 485 488 491 497 500 504 505 509 513 516 520 524 531 534 13 Utility Scripting and System Administration 539 viii | Table of Contents www.allitebooks.com fsum() function (math module), 86 full() method (queue module), 496 functions, 217–241 accepting arbitrary arguments for, 217–218 accessor functions as attributes of, 238–241 anonymous/inline, defining, 224–225 argument annotations, 220 calling with fewer arguments, 227–230 closures, accessing variables inside, 238–241 default arguments for, 222–224 disassembling, 392–395 inlining callbacks, 235–238 keyword arguments only, 219–220 multiple return values for, 221 partial() function and, 227–230 pointers, converting to callables, 643–644 replacing classes with, 231–232 state, carrying, 232–235 wrapper layers for, 329–331 wrapping with Cython, 632–638 futures module (concurrent module), 479, 505– 509 ProcessPoolExecutor class, 509–513 FutureWarning argument (warn() function), 583 G garbage collection caching instances and, 325 cyclic data structures and, 318 gauss() function (random module), 103 gdb debugger, 663–664 General Decimal Arithmetic Specification (IBM), 86 generator expressions concatenating/combining strings and, 60 filtering sequence elements with, 26 flattening nested sequences with, 135–136 recursive algorithms vs., 311–317 transforming/reducing data with, 32–33 Generator Tricks for Systems Programmers (Beazley), 135 generator-expression argument, 32 GeneratorExit exception, 577 generators, 113–139 concurrency with, 524–531 creating iterator patterns with, 115–117 defining with extra states, 120 inlining callback functions with, 235 674 | Index islice() function and, 122–123 iterator protocol, implementing, 117–119 pipelines, creating with, 132–135 search functions and, slicing, 122–123 unpacking, GET requests (HTTP), 437 get() function (webbrowser class), 563 get() method (ElementTree module), 185 get() method (queue module), 491, 495 get () method, 347–349 descriptors, 265–266 getattr() function, 305 visitor patterns and, 310 getattr () method (delegation), 287, 290 getboolean() method (ConfigParser module), 554 getdefaultencoding() function (sys module), 142 getfilesystemencoding() function (sys module), 160–161 getframe() (sys module), 63 frame hacking with, 373 getitem () method, 23 getLogger() function (logging module), 558 getpass module, 544 getpass() method (getpass module), 545 getrandbits() function (random module), 103 getrecursionlimit() function (sys module), 310 gettempdir() function (tempfile module), 169 getuser() (getpass module), 544 get_archive_formats() function (shutil module), 550 get_data() function (pkgutil module), 409 get_terminal_size() function (os module), 65, 545 gevent module, 531 glob module, 42 Global Interpreter Lock (GIL), 487, 513–516 C, calling Python from, 624 C/Python threads, mixing, 625–626 releasing in C extensions, 625 releasing in Cython, 636 thread pools and, 508 grammar rules, for parsers, 69–78 greenlets, 531 group() method (re module), 46 groupby() function (itertools module), 24–26 grouping records, in dictionaries, 12 gzip compression format, 550 gzip module, 149–151 compresslevel keyword, 150 H hard limit on resources, 562 hashes, 17 heappop() method (heapq module), priority queues and, heapq module, 7–8 merge() function, 136–137 heaps, 7–11 nlargest()/nsmallest() functions and, reversing order of, 10 help function keyword arguments and, 219 metadata for arguments and, 220 hex() function, 89–90 hexadecimal digits, encoding, 197–198 hexadecimal integers, 89–90 hmac module, 461–463 HTML entities handling in text, 65–66 replacing, 66 html module, 65 HTTP services clients, 437–441 headers, custom, 438 testing client code, 441 httpbin.org service, 441 I I/O, 141–174 decoding/encoding functions for, 56 event-driven, 475–481 iter() function and, 139 objects, serializing, 171–174 operations on strings, 148–149 passing open files and, 659 serial ports, communicating with, 170–171 thread termination and, 487 IDEs for Python development, 587 init () method, data structures and, 273 if-elif-else blocks, 304 IGNORECASE flag (re module), 47 ignore_patterns() function (shutil module), 548 ignore_types argument (isinstance() function), 135 import hooks, 418–420 patching modules on import, 428–431 sys.path_hooks variable and, 420 import statement, 412–428 ImportError exceptions, 425 importlib module, 421 import_module() function (importlib module), 411, 430 index-value pairs, iterating over, 127–128 IndexError exception, 19 indexing in CSV files, 175–179 indices() method (slice), 19 infinite values, 94–95 info() function (logging module), 556 inheritance class decorators and, 356 class defined decorators and, 347 delegation vs., 289 implementation of, 258 ini file features, 555 init () method (classes), 579 bypassing, 293–294 coding conventions and, 367–370 data structure initialization and, 270–274 mixins and, 297 multiple constructors and, 291–292 optional arguments and, 363 super() function and, 256–260 inline functions callbacks, 235–238 defining, 224–225 input files, 539 input() function (fileinput), 540 insert() method (ElementTree module), 193 inspect() module, 364–367 instances cached, metaclasses and, 358 creation, controlling with metaclasses, 356– 359 descriptors and, 264–267 saving memory when creating, 248–249 serializing with JSON, 182 WSGI applications as, 452 int type, 90–92 int() function, 90 interface files (Swig), 627 interpreters, communication between, 456–458 invalidate_caches() function (importlib mod‐ ule), 427 Index | 675 io module, 148–149, 163–165 ioctl(), 545 IPv4Address objects (ipaddress module), 448 is operator, 223 isinf() function (math module), 94 isinstance() function, 135 islice() function (itertools module), 122–123, 124 isnan() function (math module), 94 issuing warning messages, 583 is_alive() method (Thread class), 486 itemgetter function (operator module), 22 items() method (dictionaries), 16 iter() function fixed size records and, 151 while loops vs., 138–139 iter () method (iterators), 114–115 generators with extra states and, 121 iterables custom containers for, 283–286 discarding parts of, 123–125 from C, consuming, 662–663 sorted/merged, iterating over, 136–137 unpacking, 1–2 iterating all possible combinations/permutations, 125–127 chain() method (itertools module), 131–132 iter() function vs while loops, 138–139 merge() function (heapq module), 136–137 over fixed-sized records, 151 over index-value pairs, 127–128 over separate containers, 131–132 over sorted/merged iterables, 136–137 reverse, 119 zip() function and, 129–130 iterators, 113–139 consuming manually, 113–114 creating patterns with generators, 115–117 delegating, 114–115 islice() function (itertools module), 122–123 protocol, implementing, 117–119 slicing, 122–123 iterparse() function (ElementTree module), 188 iterparse() method (ElementTree module), 194 itertools module, 123–125, 125–127 compress() function, 28 groupby() function, 24–26 iter_as() function, 211 676 | Index J join() method (str type), 58–61 changing single characters with, 145 join() method (Thread class), 486 JSON (JavaScript Object Notation) reading/writing, 179–183 types supported by, 179 json module, 179–183 just-in-time (JIT) compiling, 590, 594 K key argument (sorted() function), 23 KeyboardInterrupt exception, 577, 578 keys() method (dictionaries), 16 keyword arguments, 218 annotations, 220 for metaclasses, 362–364 functions that only accept, 219–220 help function and, 219 L lambda expressions, 24, 225 variables, capturing, 225–227 launching Python debugger, 585 web browsers, 562 lazy imports, 403 lazy unpacking, 212 leap years, datetime module and, 104 libraries, adding logging to, 558 limited history, 5–7 linalg subpackage (NumPy module), 101 linear algebra, 100–101 list comprehension, 26 listdir() function (os module), 158–159 bad filenames, printing, 161–163 lists NumPy vs., 99 processing and star unpacking, unpacking, ljust() method (str type), 57 llvmpy extension module, 643 load() function (pickle module), 171 untrusted data and, 172 LoadLibrary() function (ctypes.cdll module), 601 loads() function (json module), 179 loads() function (pickle module), 171 loadTestsFromModule() method (TestLoader module), 573 loadTestsFromTestCase() method (TestCase module), 573 local() method (threading module), 504–505 locale module, 88 locals() function and exec() function, 386–388 Lock objects (threading module), 497–500 logging adding to libraries, 558 adding to simple scripts, 555 output of, 557 test output to file, 572 logging module, 557, 559 critical(), 556 debug(), 556 error(), 556 info(), 556 warning(), 556 lower() method (str type), 54 lstrip() method (str type), 53 lxml module, 195 M MagicMock instances, 568 makefile() method (socket module), 167 make_archive() function (shutil module), 549 MANIFEST.in file, 434 manipulating pathnames, 156–157 map() operation (ProcessPoolExecutor class), 511 Mapping class (collections module), 283 mappings class definition order and, 362 consolidating multiple, 33–35 match() method (re module), 43 search/replace text and, 46 math() module, 94 matrix calculations, 100–101 matrix object (NumPy module), 100 max() function, 8, 15 attrgetter() function and, 24 generator expressions and, 33 itemgetter() function, 23 memory management in cyclic data structures, 317– 320 mapping, 153–156 OrderedDict objects and, 13 parsing XML and, 186–189 restricting use of, 561 wchar_t strings and, 651 slots attribute and, 248–249 memory mapping, 153–156 MemoryError exceptions, 562 memoryview() object (struct module), 153, 206, 213 Cython-typed, 640 large arrays and, 481–483 memory_map() function (mmap module), 154 merge() function (heapq module), 136–137 Mersenne Twister algorithm, 103 meta path importer, 414–418 sys.metapath object, 418 metaclass keyword (class statement), 362–364 metaclasses, 279 capturing attribute definition order, 359–362 controlling instance creation, 356–359 enforcing coding conventions, 367–370 initializing members at definition time, 374– 375 non-standardized binary files and, 207 optional arguments for, 362–364 prepare () method and, 361 metaprogramming, 329–395 adjustable attributes, 336–339 decorators, 329–356 decorators with arguments, 334–336 dis module, 392–395 exec() function vs., 386–388 function metadata in decorators, 331–333 function wrappers, 329–331 repetitive property methods and, 382–384 unwrapping decorators, 333–334 method overloading, 376–382 methodcaller() function (operator module), 305 Microsoft Excel, CSV encoding rules of, 177 Microsoft Visual Studio, 608 min() function, 8, 15 attrgetter() function and, 24 generator expressions and, 33 itemgetter() function, 23 missing () method (dict module), 62 mixin classes, 260 extending unrelated classes with, 294–299 in descriptors of data models, 280 mkdtemp() function (tempfile module), 169 Index | 677 mkstemp() function (tempfile module), 169 mmap module, 153–156 mock module (unittest module), 565, 570 module import * statement, 398–399 modules, 397–435 controlling import of, 398–399 from module import * statement, 398 hierarchical packages of, 397–398 import hooks, using, 412–428 importing with relative names, 399–401 importing, using string as name, 411 importlib module, 421 import_module() function (importlib mod‐ ule), 411 invalidate_caches() function (importlib module), 427 meta path importer, 414–418 new_module() function (imp module), 421 objects, creating, 421 patching on import, 428–431 relative imports vs absolute names, 400 reloading, 406–407 remote machines, loading from, 412–428 splitting into multiple files, 401–404 sys.path, adding directories to, 409–411 sys.path_importer_cache object, 424 virtual environments and, 432–433 all variable in, 398–399 init .py file, 397 main .py files, 407–408 monthrange() function (calendar module), 108 months, finding date ranges of, 107–109 moving directories/files, 547 mro attribute, 576 multidicts, 11–12 multiple-dispatch, 376–382 multiprocessing module, 488 GIL and, 514 passing file descriptors with, 470–475 reduction module, 470–475 RPCs and, 459 mutable buffers, reading into, 152–153 MutableMapping class (collections module), 283 MutableSequence class (collections module), 283 MutableSet class (collections module), 283 678 | Index N named pipes (Windows), 457, 470, 472 NamedTemporaryFile() function (tempfile module), 169 namedtuple() function (collections module), 30 namedtuple() method (collections module) new_class() function and, 372 unpacking binary data and, 203 NameError exception, 581 namespace package, checking for, 426 namespaces (XML), parsing, 193–195 namespaces, multiple directories in, 404–406 naming conventions for private data, 250–251 (double underscore) and, 250 NaN (not a number) values, 94–95 in JSON, 183 isnan() function and, 95 nested sequences, flattening, 135–136 network programming, 437–483 connection (multiprocessing module), 456– 458 event-driven I/O, 475–481 hmac module, 461–463 interpreters, communication between, 456– 458 large arrays, sending/receiving, 481–483 passing file descriptors, 470–475 remote procedure calls, 454–456, 458–461 simple authentication, 461–463 socketserver module, 441–444 SSL, implementing, 464–470 TCP servers, 441–444 UDP server, implementing, 445–446 UDPServer class, 446 XML-RPC, 454–456 new () method (classes) coding conventions and, 367–370 optional arguments and, 363 newlines, 142 new_class() function (types module), 370–373 new_module() function (imp module), 421 next() function (iterators), 113 nlargest() function (heapq module), 7–8 nonblocking, supporting with queues, 495 noncapture groups (regular expressions), 49 nonlocal declarations, 239 adjusting decorators with, 336–339 normalize() function (unicodedata module), 51, 55 normalize() method (pytz module), 111 normalizing Unicode, 50–51 supported forms in Python, 51 normpath() (os.path module), 551 nsmallest() function (heapq module), 7–8 null-terminated strings, passing to C, 644–648 NullHandler() class (logging module), 559 numerical operations, 83–112 complex-valued math, 92–94 decimal calculations, 84–86 formating for output, 87–88 infinity and, 94–95 linear algebra calculations, 100–101 matrix calculations, 100–101 NaNs and, 94–95 NumPy and, 97–100 on fractions, 96–96 packing/unpacking integers from byte string, 90–92 random number generators, 102–103 rounding, 83–84 time, 104–112 time conversions, 104–105 with binary integers, 89–90 with hexadecimal integers, 89–90 with octal integers, 89–90 NumPy module, 97–100, 640 complex math and, 93 CPU-bound programs and, 514 linear algebra and, 100–101 matrix calculations and, 100–101 unpacking binary data with, 203 O objects, 243–327 and context-management protocols, 246– 248 calling methods on, when named in strings, 305–306 creating large numbers of, 248–249 defining default arguments and, 224 format() function and, 245–246 formatting output of, 243–244 implementing states for, 299–305 iterator protocol, implementing, 117–119 JSON dictionary, decoding to, 181 memory management, 317–320 representing in C, 608 serializing, 171–174 visitor pattern, implementing, 306–311 visitor patterns without recursion, 311–317 with statement and, 246–248 object_hook (json.loads() function), 181 object_pairs_hook (json.loads() function), 181 oct() function, 89–90 octal integers, 89–90 Olson time zone database, 110 opaque pointers, 612–614 open() function, 141–144 binary data, reading/writing, 145–147 closefd argument, 166 file descriptors and, 166 on non-Unix systems, 167 rb/wb modes of, 145–147 rt/wt mode, 141–144 x mode of, 147–148 optimizating your programs, 590 optparse vs argparse, 544 OrderedDict object (collections module), 12–13 decoding JSON data into, 181 os module listdir() function, 158–159 path module in, 156–157 OSError exception, 575–576 ouput sent to stdout, 565 P pack() function (structs), 201 packages, 397–435 datafiles, reading, 408–409 directories/zip files, running, 407–408 distributing, 433–435 hierarchical, of modules, 397–398 MANIFEST.in file, 434 namespaces, multiple directories in, 404–406 per-user directory, installing, 431–432 submodules, importing with relative names, 399–401 sys.path, adding directories to, 409–411 third-party options for, 435 virtual environments and, 432–433 init .py file, 397 path variable and, 405 pack_into() function (struct module), 153 Pandas package, 178, 214–216 parallel programming, 509–513 Index | 679 parse tree, 73 parse() function (ElementTree module), 185 parser (html), 66 parser, recursive descent, 69–78 parse_and_remove() function (ElementTree module), 189 parsing command-line options, 539, 541 partial() function (functools module), 227–230 defining property methods and, 383 fixed size records and, 151 optional arguments for decorators and, 341 state, carrying with, 234, 235 Paste, 453 patch() function (unittest.mock class) testing output with, 565 unit tests with, 567 patching objects, 567 path module (os), 156–157 testing for existing files with, 157–158 pathnames, manipulating, 156–157 pattern matching, 42–45 pattern object (regular expressions), 43 pdb debugger, 663–664 per-user directory, 431–432 performance ast manipulation and, 392 attrgetter() function and, 24 Cython and, 638–642 dictionary comprehension vs dict() type, 29 join() method vs + operator, 60 keeping limited history and, lazy attributes and, 267–270 nlargest()/nsmallest() functions and, NumPy module and, 97–100 of byte vs text strings, 81 pattern objects and, 45 profiling/timing, 587 sanitizing strings and, 56 strptime() method and, 110 perf_counter() function (time class), 561, 589 PermissionError exception, 575 permutations() function (itertools module), 125 pexpect, 547 pickle module, 171–174 limits on, 172 multiprocessing.connection functions and, 457 RPCs and, 458–461 Pipe object (multiprocessing module), 471 680 | Index pipelines, creating, 132–135 pipes accepting script via, 539 mimicking, 132–135 PLY, 69, 76 polling thread queues, 531–534 Popen class (subprocess module), 547 POST method (HTTP), 438 pprint() function (pprint module), 180 predefined abstract base classes, 276 prefix variable, 554, 555 prepare () method (classes), 361 new_class() function and, 372 optional arguments and, 363 prepare_class() function (types module), 373 print() function, 243, 542, 586 end argument, 144 line ending, changing, 144–145 redirecting to files, 144 separator character, changing, 144–145 private data/methods caching instances within, 326 naming conventions for, 250–251 probability distributions (random module), 103 process pools, GIL and, 514, 516 ProcessPoolExecutor class (futures module), 509–513 process_time() function (time class), 561, 589 program crashes, debugging, 584 program profiling, 587 property attributes (classes) delegating, 287–291 extending in subclasses, 260–264 repetitive definitions for, 382–384 @property decorator, 330, 346 proxies, delegating attributes with, 288, 290 public-facing services, 457 put() method (queue module), 491, 495 pwd module, 544 PyArg_ParseTuple() function (C extensions), 609 converting string encoding with, 650 PyBuffer_GetBuffer() method (Py_buffer ob‐ ject), 611 PyBuffer_Release() method (Py_buffer object), 612 PyCallable_Check() function (C extensions), 622 PyCapsule_GetPointer() function (C exten‐ sions), 614 PyCapsule_Import() function (C extensions), 618 PyCapsule_New() function (C extensions), 613 PyCapsule_SetDestructor() function (C exten‐ sions), 614 PyErr_Occurred() function (C extensions), 623 PyFile_FromFd() function (C extensions), 658– 659 PyFloat_AsDouble() function (C extensions), 623 PyFloat_Check() function (C extensions), 623 PyGILState_Ensure() function (C extensions), 624, 626 PyGILState_Release() function (C extensions), 624, 626 PyIter_Next() function (C extensions), 662–663 PyObject data type (C extension) filenames, passing with, 657–658 PyObject_BuildValue function (C extensions), 623 PyObject_Call() function (C extensions), 622 file-like objects and, 661 PyObject_GetIter() function (C extensions), 662–663 PyParsing, 69, 76 PyPy, 514 PySequence_Length() function (C extensions), 661 pySerial package, 170–171 Python for Data Analysis (McKinney), 216 Python Package Index, 434 Python syntax vs JSON, 180 PYTHONPATH environment variable, 409 pytz module, 110–112 PyUnicode_FromWideChar() function (C ex‐ tensions) C strings, converting to Python objects, 653– 654 pyvenv command, 432–433 Py_BEGIN_ALLOW_THREADS (Py objects), 625 Py_BuildValue() function (C extensions), 609 C strings, converting to Python objects, 653– 654 Py_DECREF() function (C extensions), 623 Py_END_ALLOW_THREADS (Py objects), 625 Py_XDECREF() function (C extensions), 623 Q qsize() method (queue module), 496 queue module, 491–496 queues bounding, 495 structures, deque() and, quote() function (shlex class), 546 R race conditions, avoiding, 497–500 raise from statement, 580, 582 None modifier, 581 raise statement, 580, 582 randint() function (random module), 102 random module, 102–103 random() function (random module), 103 re module compile() function, 49 DOTALL flag, 49 IGNORECASE flag, 47 pattern matching and, 42–45 patterns, naming, 67 scanner() method, 67 search/replace text and, 45–46 split() method, 37–38 sub() function, 54 Unicode text and, 52–53 read() function, 139 file-like C objects and, 659–662 readability, naming slices for, 18–19 readinto() method (files), 147 reading into mutable buffers with, 152–153 recursion, getrecursionlimit() function (sys module), 310 recursive descent parsers, 69–78 limitations on, 76 recvfrom() method (socket module), 445 recv_handle() function (reduction module), 470–475 recv_into() function (socket module), 153, 483 redirection, 539 regex module, 53 register() function (atexit module), 538 register_function() method (XML-RPC), 455 regular expressions * (star) operator and, 48 ? (question mark) modifier and, 48 Index | 681 greedy vs nongreedy, 47–48 matching multiple lines, 48–49 newline support in, 48–49 on byte strings, 79 order of tokens in, 68 pattern matching and, 42–45 re.split() method and, 38 regex module for, 53 shortest match with, 47–48 stripping characters with, 54 Unicode and, 52–53 relational databases, 195–197 connecting to, 196 cursors, creating, 196 relative imports vs absolute names (modules), 400 relativedelta() function (dateutil module), 105 reload() function (imp module), 406–407, 431 remote machines loading modules from, 412–428 XML-RPC, 454–456 remote procedure calls (RPC), 458–461 exception handling with, 461 remove() method (ElementTree module), 193 replace() method (date module), 108 replace() method (datetime module), 108 _replace() method (namedtuple object), 31 replace() method (str type), 54, 54 performance and, 56 search/replace text and, 45 repr() function (built-in), 243 request module (urllib module), 438–441 client package vs., 440 requests package (urllib module) return values of, 439 reraising exceptions, 582 reserved words, clashing with, 251 resource forks, 548 resource management, 248 resource module, 561 ResourceWarning argument (warn() function), 583 REST-base interface, 449–453 testing, 450 restricting CPU time, 561 result() method (ProcessPoolExecutor class), 512 reverse iteration, 119–120 reversed() function, 119–120 682 | Index rjust() method (str type), 57 RLock objects (threading module), 498 round() function, 83–84 rounding numbers, 83–84 RSS feeds, parsing, 183 rstrip() method (str type), 53 RTS-CTS handshaking, 170 RuntimeWarning argument (warn() function), 583 S sample() function (random module), 102 sanitizing text, 54–57 scanner() method (re module), 67 search for shortest match with regular expressions, 47–48 matching multiple lines, 48–49 nlargest()/nsmallest() functions (heapq module), 7–8 noncapture groups, 49 normalization of Unicode text and, 51 visitor pattern and, 311 search/replace text, 45–46 case insensitive, 46–47 security import statement and, 412 pickle and, 172 RPCs and, 460 SSL certificates and, 466 seed() function (random module), 103 segmentation faults, 663–664 select() function (event driven I/O), 476 self-signed certificates (SSL), 468 Semaphore objects (threading module), 490, 498 send() function (socket module), 483 send() method (generators), 316 sendmsg() method (socket module), 473 sendto() method (socket module), 445 send_handle() function (reduction module), 470–475 Sequence class (collections module), 283 sequences filtering elements of, 26–28 flattening nested, 135–136 iterating over multiple, simultaneously, 129– 130 mapping names to elements of, 29–32 most frequently occurring item in, 20–21 removing duplicates from, 17–18 unpacking, 1–2 serial ports, communicating with, 170–171 serve_forever() method (XML-RPC), 455 Set class (collections module), 283 set () method (descriptors), 265 in data models, 280 setattr() method, 294 setattr () method (delegation), 290 setrlimit() function, 562 setsid() function (os module), 537 setup.py file, 633, 639 distributing packages and, 434 set_trace() function (pdb module), 586–587 shell, 546 shell scripts, 539 shelling out, 550 shuffle() function (random module), 102 shutil module archives and, 549 copying files/directories with, 547 sig module, 343–345 signature objects, 364–367 signature() function (inspect), 343 SIGXCPU signal, 562 simple scripts, 555 simplefilter() function (warnings class), 584 SimpleXMLRPCServer (XML-RPC), 456 site-packages directories, 410 virtual environments vs., 432–433 skip() decorator, 574 skipIf() function (unittest module), 574 skipping test failures, 573 skipUnless() function (unittest module), 574 slice() object, 19 slices, naming, 18–19 slots attribute classes with, 32 memory management and, 249 socket module, 444 ipaddress module and, 448 sending datagrams with, 445 socketpair() function (Unix), 532 sockets large arrays, sending/receiving, 483 setting options on, 443 threads and, 532 socketserver module implementing TCP servers with, 441–444 UDP server, implementing, 445–446 sorted() function and objects without comparison support, 23–24 itemgetter() function, 22 sorting dictionaries, 13–15 dictionaries by common key, 21–23 finding largest/smallest N items, 7–8 groupby() function and, 25 itemgetter function (operator module), 22 objects without comparison support, 23–24 sort_keys argument (json.dumps() function), 182 source file vs config file, 553 special characters, escaping, 66 split() method (re object), 37–38 split() method (str type), 37–38 SQLAlchemy, 197 sqlite3 module, 195 sqrt() function (math module), 592 ssh session, 547 SSL, 464–470 certificate authorities, 468 self-signed certificates, 468 ssl module, 464–470 random module vs., 103 Stackless Python, 531 stack_size() function (threading module), 509 star expressions discarding values from, unpacking iterables and, start attribute (slice), 19 start() method (Thread class), 486 startswith() method (str type), 38 pattern matching with, 42 start_response argument (WSGI), 452 state capturing with closures, 233–234 carrying with callback functions, 232–235 implementing for objects/machines, 299– 305 of mixins, 297 thread-specific, storing, 504–505 states, generators with extra, 120–121 static methods, applying decorators to, 350–352 Index | 683 @staticmethod decorator, 330 decorating class methods with, 350–352 func attribute and, 334 stdout object (sys), changing encoding on, 163 step attribute (slice), 19 stop attribute (slice), 19 StopIteration exception, 113 str type converting to datetime objects, 109–110 decode() method, 56 encode() method, 56 endswith() method, 38 format() function, 57 join() method, 58–61 lower() method, 54 pattern matching with, 42 replace() method, 54 sanitizing text with, 54–57 split() method, 37–38 startswith() method, 38 stripping unwanted characters from, 53–54 translate() method, 55 upper() method, 54 str() function, 243 StreamRequestHandler (socketserver module), 442–444 string module, 246 string templates and code readability, 453 StringIO object (io module), 566 StringIO() object (io module), 148–149 strings, 37–81 aligning, 57–58 bad encoding in C/Python extensions, 654– 657 C, converting to Python objects, 653–654 calling object methods when named in, 305– 306 combining, 58–61 concatenating, 58–61 converting to dates/times, 109–110 database API and, 197 I/O operations, performing on, 148–149 interpolating variables in, 61–64 matching start/end text of, 38–40 null-terminated, passing to C, 644–648 splitting on delimiters, 37–38 stripping unwanted characters from, 53–54 Unicode, passing to C modules, 648–653 unpacking, 684 | Index strip() method (str type), 53–54 strptime() method (datetime module), 109 struct module, 199–203 nested binary records, reading, 203–213 packing/unpacking integers from byte strings and, 91 structure codes for, 201 variable-sized binary records, reading, 203– 213 structures (data type), 612–614 sub() function (re module), 54, 63 search/replace text and, 45 submit() operation (ProcessPoolExecutor class), 512 subn() method (re module), 46 subprocess module, 547 subsitution callback functions, 46 super() function, 256–260 class decorators and, 356 coding conventions and, 370 extending properties in subclasses and, 262 mixin classes and, 298 surrogate encoding, 654–657 Swig, 604, 627–631 headers and, 630 symbolic links, 548 broken, 549 SyntaxWarning argument (warn() function), 583 sys.arg value, 543 sys.argv (commaand line arguments), 544 sys.metapath object, 418, 430 extending import operations and, 422 sys.modules dictionary, 421 sys.path adding directories to, 409–411 site-packages directories, 410 sys.path_hooks variable (importers), 420 sys.path_importer_cache object, 424 sys.stderr, 540 sys.stdout, 565–566 system-exiting exceptions, 578 SystemExit exception, 540, 577–580 T tag attribute (ElementTree module), 185 tarfile compression format, 550 TCP servers, 441–444 event-driven I/O implementation, 477 tempfile module, 167–170 temporary files, 167–170 TemporaryDirectory() method (tempfile mod‐ ule), 169 TemporaryFile (tempfile module), 168 terminal, finding size of, 65, 545 test failures, 573 TestCase classes (TestLoader module), 573 testing ouput sent to stdout, 565 output, logging to file, 572 unit tests for exceptions, 570 TestLoader class (unitest module), 573 text attribute (ElementTree module), 185 text data encoding, 141–144 reading/writing, 141–144 text manipulation, 37–81 aligning strings, 57–58 case insensitive search/replace, 46–47 combining/concatenating, 58–61 HTML entities, handling in text, 65–66 interpolating variables, 61–64 matching start/end of strings, 38–40 of Unicode with regular expressions, 52–53 on byte strings, 78–81 parsers, implementing, 69–78 pattern matching, 42–45 reformatting into columns, 64–65 sanitizing, 54–57 search/replace, 45–46 stripping unwanted characters, 53–54 tokenizing, 66–69 wildcard matcing, 40–42 XML entities, handling in text, 65–66 TextIOWrapper object (io module), 163–165 textwrap module, 64–65 Thread class (threading module), 485–488 thread pools GIL and, 508 queues and, 506 threading module, 488 Condition object, 489 Event object, 488–491 local() method, 504–505 Lock objects, 497–500 Semaphore objects, 490 stack_size() function, 509 ThreadingMixIn class (socketserver module), 297 ThreadingTCPServer objects (socketserver module), 442 ThreadingUDPServer objects (socketserver module), 446 ThreadPoolExecutor class (futures module), 505–509 threads actor model and, 516–520 C/Python, mixing, 625–626 communication between, 491–496 creating/destroying, 485–488 daemonic, 486 deadlocks between, 500–503 generators as alternative to, 524–531 locking critical sections, 497–500 nonblocking, supporting with queues, 495 polling multiple queues, 531–534 pools of, 505–509 priority queues and, 11 queue module and, 491–496 race conditions, avoiding, 497–500 status of, finding, 488–491 storing state of, 504–505 timeouts, supporting with queues, 495 throw() method (generators), 531 time command, 587 time module, 590 time zones, 110–112 country_timezones dictionary (pytz mod‐ ule), 112 UTC time, 111 time() function (time class), 561 time, operations on, 104–112 converting strings for, 109–110 date calculations, 106–107 date ranges, finding, 107–109 pytz module, 110–112 time conversions, 104–105 time zones, manipulating, 110–112 UTC time, 111 timedelta object (datetime module), 104, 107– 109 timeit module, 589, 590 timeouts, supporting with queues, 495 Timer class, 561 tokenizing and DOTALL flag (re module), 49 tokens streams, filtering, 68 Index | 685 tostring() function (ElementTree module), 190 total_ordering decorator (functools module), 321–323 to_bytes() method (int module), 91 traceback, segmentation faults and, 663–664 traceback.print_stack() function, 586 translate() method (str type), 54, 55 numerical output and, 88 performance and, 56 transmitting data, 155 tree structures, memory management of, 317– 320 tree traversal, 311, 314 TTYs, 545, 547 tuples and endswith()/startswith() methods, 39 as return values, 221 relational databases and, 195–197 unpacking, Twisted package, 238 type checking (data) abstract base classes and, 274–276 forcing with decorator functions, 341–345 type systems, 277–283 %typemap directive (Swig), 631 types module, 370–373 U UDP server, 445–446 event-driven I/O implementation, 476 UDPServer class, 446 umask() function (os module), 537 unescape() function (xml.sax.saxutils module), 191 Unicode, 50–53 bad encoding in C/Python extensions, 654– 657 IGNORECASE flag and matching, 47 regular expressions and, 52–53 strings, passing to C modules, 648–653 unicodedata module, 55 uniform() function (random module), 103 unittest module, 565, 572–573 Unix, 548 commands, 548 find utility, 551 unpack() function (struct module), 201 binary data and, 205 686 | Index unpacking archives, 549 discarding values while, enumerate() function and, 128 integers from byte string, 90–92 iterables into separate variables, 1–2 iterables of arbitrary length, 3–5 sequences into separate variables, 1–2 star expressions and, unpack_archive() function (shutil module), 549 unpack_from() method (struct module), 202 uploading using requests module, 440 upper() method (str type), 54 urllib module, 413, 437–441 urlopen() function (urllib module), 569–570 import statements vs., 413 sending query parameters with, 438 UserWarning argument (warn() function), 583 UTC time, 111 V validation of data, abstract base classes and, 274–276 ValueError, 570 unpacking and, values() method (dictionaries), 14, 16 variables interpolating, in strings, 61–64 understand locality of, 592 vars() method, 62 virtual environments, 432–433 visitor pattern implementing with recursion, 306–311 implementing without recursion, 311–317 W walk() function (os module), 550 warn() function (warning class), 583 warning messages, issuing, 583 warning module, 584 warning() function (logging module), 556 watchdog timers, 503 wchar_t * declarations (C), 648–653 converting to Python objects, 653 weak references and caching instances, 325 weakref module, 317, 320 web browsers, launching, 562 web programming, 437–483 HTTP service clients, 437–441 REST-base interface, 449–453 urllib module and, 437–441 webbrowser module, 563 WebOb, 453 while loops vs iter() function, 138–139 wildcard matcing, strings, 40–42 Windows, 548 C extension modules and, 608 with nogil: statement (Cython), 636 with statement, 246–248, 561, 566 contextmanager decorator and, 385 Lock objects and, 497–500 Semaphore objects and, 499 wraparound() decorator (Cython), 641 wrapped attribute, 332 @wraps decorator (functools) function metadata and, 331–333 unwrapping, 333–334 WSGI standard, 449–453 return value for apps based on, 452 X XML entities handling in text, 65–66 replacing, 66 XML files dictionaries, converting to, 189–191 extracting data from, 183–186 modifying, 191–193 parsing, 191–193 incrementally, 186–189 with namespaces, 193–195 rewriting, 191–193 tags, specifying, 185 XML-RPC, 454–456 adding SSL to, 466 data types handled by, 455 xml.etree.ElementTree module (see Element‐ Tree module) xml.sax.saxutils module, 191 XMLNamespaces class, 194 Y yield from statement, 135–136 yield statement, 60, 134 behavior in generators, 315 concurrency implementations and, 524–531 generators and, 116 search functions and, Z ZeroMQ, 457 zip files as runnable scripts, 407–408 zip() method (dictionaries), 13–15, 129–130 zipfile compression format, 550 Index | 687 About the Authors David Beazley is an independent software developer and book author living in the city of Chicago He primarily works on programming tools, providing custom software development, and teaching practical programming courses for software developers, scientists, and engineers He is best known for his work with the Python programming language, for which he has created several open source packages (e.g., Swig and PLY) and authored the acclaimed Python Essential Reference He also has significant experi‐ ence with systems programming in C, C++, and assembly language Brian K Jones is a system administrator in the department of computer science at Princeton University Colophon The animal on the cover of Python Cookbook, Third Edition is a springhaas (Pedetes capensis), also known as a spring hare Springhaas are not hares at all, but rather the only member of the family Pedetidae in the order Rodentia They are not marsupials, but they are vaguely kangaroo-like, with small front legs, powerful hind legs designed for hopping, jumping, and leaping, and long, strong, bushy (but not prehensile) tails used for balance and as a brace when sitting They grow to be about 14 to 18 inches long, with tails as long as their bodies, and can weigh approximately eight pounds Springhaas have rich, glossy, tawny, or golden-reddish coats with long, soft fur and white underbellies Their heads are disproportionately large, and they have long ears (with a flap of skin at the base they can close to prevent sand from getting inside while they are digging) and large, dark brown eyes Springhaas mate throughout the year and have a gestation period of about 78 to 82 days Females generally give birth to only one baby (which stays with its mother until it is approximately seven weeks old) per litter but have three or four litters each year Babies are born with teeth and are fully furred, with their eyes closed and ears open Springhaas are terrestrial and well-adapted for digging, and they tend to spend their days in the small networks of their burrows and tunnels They are nocturnal and pri‐ marily herbivorous, feeding on bulbs, roots, grains, and occasionally insects While they are foraging, they move about on all fours, but they are able to move 10 to 25 feet in a single horizontal leap and are capable of quick getaways when frightened Although they are often seen foraging in groups in the wild, they not form an organized social unit and usually nest alone or in breeding pairs Springhaas can live up to 15 years in captivity They are found in Zaire, Kenya, and South Africa, in dry, desert, or semiarid areas, and they are a favorite and important food source in South Africa The cover image is from Animal Creation: Mammalia The cover font is Adobe ITC Garamond The text font is Adobe Minion Pro; the heading font is Adobe Myriad Con‐ densed; and the code font is Dalton Maag’s Ubuntu Mono ... 9. 23 Executing Code with Local Side Effects 9.24 Parsing and Analyzing Python Source 9.25 Disassembling Python Byte Code 32 9 33 1 33 3 33 4 33 6 33 9 34 1 34 5 34 7 35 0 35 2 35 5 35 6 35 9 36 2 36 4 36 7 37 0 37 4... Values 3. 2 Performing Accurate Decimal Calculations 3. 3 Formatting Numbers for Output 3. 4 Working with Binary, Octal, and Hexadecimal Integers 3. 5 Packing and Unpacking Large Integers from Bytes 3. 6... author, publisher, and ISBN For example: Python Cookbook, 3rd edition, by David Beazley and Brian K Jones (O’Reilly) Copyright 20 13 David Beazley and Brian Jones, 978-1-449 -34 037 -7 If you feel your

Ngày đăng: 04/03/2019, 13:41

TỪ KHÓA LIÊN QUAN