8 Advanced Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
8.1 Miscellaneous Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
8.1.1 Parsing Command-Line Arguments. . . . . . . . . . . . . . . . 319
8.1.2 Platform-Dependent Operations . . . . . . . . . . . . . . . . . . 322
8.1.3 Run-Time Generation of Code . . . . . . . . . . . . . . . . . . . . 323
8.1.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
8.2 Regular Expressions and Text Processing . . . . . . . . . . . . . . . . . 326
8.2.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
8.2.2 Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
8.2.3 Regular Expressions for Real Numbers . . . . . . . . . . . . . 331
8.2.4 Using Groups to Extract Parts of a Text . . . . . . . . . . . 334
8.2.5 Extracting Interval Limits. . . . . . . . . . . . . . . . . . . . . . . . 335
8.2.6 Extracting Multiple Matches . . . . . . . . . . . . . . . . . . . . . 339
8.2.7 Splitting Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
8.2.8 Pattern-Matching Modifiers . . . . . . . . . . . . . . . . . . . . . . 345
8.2.9 Substitution and Backreferences . . . . . . . . . . . . . . . . . . 347
8.2.10 Example: Swapping Arguments in Function Calls . . . 348
8.2.11 A General Substitution Script . . . . . . . . . . . . . . . . . . . . 351
8.2.12 Debugging Regular Expressions . . . . . . . . . . . . . . . . . . . 353
8.2.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
8.3 Tools for Handling Data in Files . . . . . . . . . . . . . . . . . . . . . . . . . 362
8.3.1 Writing and Reading Python Data Structures . . . . . . 362
8.3.2 Pickling Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
8.3.3 Shelving Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
8.3.4 Writing and Reading Zip and Tar Archive Files . . . . . 366
8.3.5 Downloading Internet Files . . . . . . . . . . . . . . . . . . . . . . . 367
8.3.6 Binary Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
8.3.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
8.4 A Database for NumPy Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 371
8.4.1 The Structure of the Database . . . . . . . . . . . . . . . . . . . . 371
8.4.2 Pickling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
8.4.3 Formatted ASCII Storage . . . . . . . . . . . . . . . . . . . . . . . . 375
8.4.4 Shelving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
8.4.5 Comparing the Various Techniques . . . . . . . . . . . . . . . . 377
8.5 Scripts Involving Local and Remote Hosts. . . . . . . . . . . . . . . . . 378
8.5.1 Secure Shell Commands . . . . . . . . . . . . . . . . . . . . . . . . . 378
8.5.2 Distributed Simulation and Visualization . . . . . . . . . . 380
8.5.3 Client/Server Programming . . . . . . . . . . . . . . . . . . . . . . 382
8.5.4 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
8.6 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
8.6.1 Class Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
8.6.2 Checking the Class Type . . . . . . . . . . . . . . . . . . . . . . . . . 388
8.6.3 Private Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
8.6.4 Static Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
8.6.5 Special Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
8.6.6 Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
8.6.7 Multiple Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
8.6.8 Using a Class as a C-like Structure . . . . . . . . . . . . . . . . 393
8.6.9 Attribute Access via String Names . . . . . . . . . . . . . . . . 394
8.6.10 New-Style Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
8.6.11 Implementing Get/Set Functions via Properties . . . . . 395
8.6.12 Subclassing Built-in Types . . . . . . . . . . . . . . . . . . . . . . . 396
8.6.13 Building Class Interfaces at Run Time . . . . . . . . . . . . . 399
8.6.14 Building Flexible Class Interfaces . . . . . . . . . . . . . . . . . 403
8.6.15 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
8.7 Scope of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
8.7.1 Global, Local, and Class Variables . . . . . . . . . . . . . . . . 413
8.7.2 Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
8.7.3 Dictionaries of Variables in Namespaces . . . . . . . . . . . . 416
8.8 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
8.8.1 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
8.8.2 Raising Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
8.9 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
8.9.1 Constructing an Iterator . . . . . . . . . . . . . . . . . . . . . . . . . 421
8.9.2 A Pointwise Grid Iterator . . . . . . . . . . . . . . . . . . . . . . . . 423
8.9.3 A Vectorized Grid Iterator . . . . . . . . . . . . . . . . . . . . . . . 427
8.9.4 Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
8.9.5 Some Aspects of Generic Programming . . . . . . . . . . . . 432
8.9.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
8.10 Investigating Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
8.10.1 CPU-Time Measurements . . . . . . . . . . . . . . . . . . . . . . . . 437
8.10.2 Profiling Python Scripts . . . . . . . . . . . . . . . . . . . . . . . . . 441
8.10.3 Optimization of Python Code . . . . . . . . . . . . . . . . . . . . 442
8.10.4 Case Study on Numerical Efficiency . . . . . . . . . . . . . . . 445
9 Fortran Programming with NumPy Arrays . . . . . . . 451
9.1 Problem Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
9.2 Filling an Array in Fortran . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
9.2.1 The Fortran Subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . 454
9.2.2 Building and Inspecting the Extension Module . . . . . . 455
9.3 Array Storage Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
9.3.1 Generating an Erroneous Interface . . . . . . . . . . . . . . . . 457
9.3.2 Array Storage in C and Fortran. . . . . . . . . . . . . . . . . . . 459
9.3.3 Input and Output Arrays as Function Arguments . . . 459
9.3.4 F2PY Interface Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
9.3.5 Hiding Work Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
9.4 Increasing Callback Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
9.4.1 Callbacks to Vectorized Python Functions . . . . . . . . . . 471
9.4.2 Avoiding Callbacks to Python . . . . . . . . . . . . . . . . . . . . 473
9.4.3 Compiled Inline Callback Functions . . . . . . . . . . . . . . . 474
9.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
9.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
10 C and C++ Programming with NumPy Arrays . . 483
10.1 Automatic Interfacing of C/C++ Code . . . . . . . . . . . . . . . . . . . 484
10.1.1 Using F2PY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
10.1.2 Using Instant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
10.1.3 Using Weave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
10.2 C Programming with NumPy Arrays . . . . . . . . . . . . . . . . . . . . . 488
10.2.1 The Basics of the NumPy C API. . . . . . . . . . . . . . . . . . 489
10.2.2 The Handwritten Extension Code . . . . . . . . . . . . . . . . . 491
10.2.3 Sending Arguments from Python to C . . . . . . . . . . . . . 492
10.2.4 Consistency Checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
10.2.5 Computing Array Values . . . . . . . . . . . . . . . . . . . . . . . . . 494
10.2.6 Returning an Output Array . . . . . . . . . . . . . . . . . . . . . . 496
10.2.7 Convenient Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
10.2.8 Module Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
10.2.9 Extension Module Template . . . . . . . . . . . . . . . . . . . . . . 500
10.2.10 Compiling, Linking, and Debugging the Module . . . . . 502
10.2.11 Writing a Wrapper for a C Function . . . . . . . . . . . . . . . 503
10.3 C++ Programming with NumPy Arrays . . . . . . . . . . . . . . . . . . 506
10.3.1 Wrapping a NumPy Array in a C++ Object . . . . . . . 506
10.3.2 Using SCXX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
10.3.3 NumPy–C++ Class Conversion . . . . . . . . . . . . . . . . . . . 511
10.4 Comparison of the Implementations . . . . . . . . . . . . . . . . . . . . . . 519
10.4.1 Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
10.4.2 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
10.4.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
10.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525