Python programming for computational science, Lecture notes of Computer Science

programming in python is made easy by going through this book

Typology: Lecture notes

2017/2018

Uploaded on 08/11/2018

sanjay-kumar-singh
sanjay-kumar-singh 🇮🇳

1 document

1 / 745

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python Scripting
for Computational Science
Hans Petter Langtangen
Simula Research Laboratory
and
Department of Informatics
University of Oslo
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Python programming for computational science and more Lecture notes Computer Science in PDF only on Docsity!

Python Scripting

for Computational Science

Hans Petter Langtangen

Simula Research Laboratory

and

Department of Informatics

University of Oslo

IV

VI Preface

on Unix and Macintosh, also when graphical user interfaces and operating system interactions are involved. The interest in scripting with Python has exploded among Internet service developers and computer system administrators. However, Python scripting has a significant potential in computational science and engineering (CSE) as well. Software systems such as Maple, Mathematica, Matlab, and R/S- Plus are primary examples of very popular, widespread tools because of their simple and effective user interface. Python resembles the nature of these interfaces, but is a full-fledged, advanced, and very powerful programming language. With Python and the techniques explained in this book, you can actually create your own easy-to-use computational environment, which mir- rors the working style of Matlab-like tools, but tailored to your own number crunching codes and favorite visualization systems. Scripting enables you to develop scientific software that combines ”the best of all worlds”, i.e., highly different tools and programming styles for accomplishing a task. As a simple example, one can think of using a C++ library for creating a computational grid, a Fortran 77 library for solving partial differential equations on the grid, a C code for visualizing the solution, and Python for gluing the tools together in a high-level program, perhaps with an easy-to-use graphical interface.

Special Features of This Book. The current book addresses applications of scripting in CSE and is tailored to professionals and students in this field. The book differs from other scripting books on the market in that it has a different pedagogical strategy, a different composition of topics, and a different target audience. Practitioners in computational science and engineering seldom have the interest and time to sit down with a pure computer language book and figure out how to apply the new tools to their problem areas. Instead, they want to get quickly started with examples from their own world of applications and learn the tools while using them. The present book is written in this spirit – we dive into simple yet useful examples and learn about syntax and programming techniques during dissection of the examples. The idea is to get the reader started such that further development of the examples towards real-life applications can be done with the aid of online manuals or Python reference books.

Contents. The contents of the book can be briefly sketched as follows. Chap- ter 1 gives an introduction to what scripting is and what it can be good for in a computational science context. A quick introduction to scripting with Py- thin, using examples of relevance to computational scientists and engineers, is provided in Chapter 2. Chapter 3 presents an overview of basic Python functionality, including file handling, data structures, functions, and oper- ating system interaction. Numerical computing in Python, with particular focus on efficient array processing, is the subject of Chapter 4. Python can easily call up Fortran, C, and C++ code, which is demonstrated in Chapter 5.

Preface VII

A quick tutorial on building graphical user interfaces appears in Chapter 6 , while Chapter 7 builds the same user interfaces as interactive Web pages. Chapters 8 – 12 concern more advanced features of Python. In Chapter 8 we discuss regular expressions, persistent data, class programming, and ef- ficiency issues. Migrating slow loops over large array structures to Fortran, C, and C++ is the topic of Chapters 9 and 10. More advanced GUI pro- gramming, involving plot widgets, event bindings, animated graphics, and automatic generation of GUIs are treated in Chapter 11. More advanced tools and examples of relevance for problem solving environments in science and engineering, tying together many techniques from previous chapters, are presented in Chapter 12. Readers of this book need to have a considerable amount of software mod- ules installed in order to be able to run all examples successfully. Appendix A explains how to install Python and many of its modules as well as other soft- ware packages. All the software needed for this book is available for free over the Internet. Good software engineering practice is outlined in a scripting context in Appendix B. This includes building modules and packages, documentation techniques and tools, coding styles, verification of programs through auto- mated regression tests, and application of version control systems.

Required Background. This book is aimed at readers with programming ex- perience. Many of the comments throughout the text address Fortran or C programmers and try to show how much faster and more convenient Python code development turns out to be. Other comments, especially in the parts of the book that deal with class programming, are meant for C++ and Java programmers. No previous experience with scripting languages like Perl or Tcl is assumed, but there are scattered remarks on technical differences be- tween Python and other scripting languages (Perl in particular). I hope to convince computational scientists having experience with Perl that Python is a preferable alternative, especially for large long-term projects. Matlab programmers constitute an important target audience. These will pick up simple Python programming quite easily, but to take advantage of class programming at the level of Chapter 12 they probably need another source for introducing object-oriented programming and get experience with the dominating languages in that field, C++ or Java. Most of the examples are relevant for computational science. This means that the examples have a root in mathematical subjects, but the amount of mathematical details is kept as low as possible to enlarge the audience and allow focusing on software and not mathematics. To appreciate and see the relevance of the examples, it is advantageous to be familiar with basic mathematical modeling and numerical computations. The usefulness of the book is meant to scale with the reader’s amount of experience with numerical simulations.

Table of Contents

List of Exercises

  • 1 Introduction
    • 1.1 Scripting versus Traditional Programming
      • 1.1.1 Why Scripting is Useful in Computational Science
      • 1.1.2 Classification of Programming Languages
      • 1.1.3 Productive Pairs of Programming Languages
      • 1.1.4 Gluing Existing Applications
      • 1.1.5 Scripting Yields Shorter Code
      • 1.1.6 Efficiency
      • 1.1.7 Type-Specification (Declaration) of Variables
      • 1.1.8 Flexible Function Interfaces
      • 1.1.9 Interactive Computing
      • 1.1.10 Creating Code at Run Time
      • 1.1.11 Nested Heterogeneous Data Structures
      • 1.1.12 GUI Programming
      • 1.1.13 Mixed Language Programming
      • 1.1.14 When to Choose a Dynamically Typed Language
      • 1.1.15 Why Python?
      • 1.1.16 Script or Program?
    • 1.2 Preparations for Working with This Book
  • 2 Getting Started with Python Scripting
    • 2.1 A Scientific Hello World Script
      • 2.1.1 Executing Python Scripts
      • 2.1.2 Dissection of the Scientific Hello World Script
    • 2.2 Reading and Writing Data Files
      • 2.2.1 Problem Specification
      • 2.2.2 The Complete Code
      • 2.2.3 Dissection
      • 2.2.4 Working with Files in Memory
      • 2.2.5 Efficiency Measurements
      • 2.2.6 Exercises
    • 2.3 Automating Simulation and Visualization
      • 2.3.1 The Simulation Code
      • 2.3.2 Using Gnuplot to Visualize Curves
      • 2.3.3 Functionality of the Script
      • 2.3.4 The Complete Code
      • 2.3.5 Dissection
      • 2.3.6 Exercises
    • 2.4 Conducting Numerical Experiments
      • 2.4.1 Wrapping a Loop Around Another Script
      • 2.4.2 Generating an HTML Report X Table of Contents
      • 2.4.3 Making Animations
      • 2.4.4 Varying Any Parameter
      • 2.4.5 Exercises
    • 2.5 File Format Conversion
      • 2.5.1 The First Version of the Script
      • 2.5.2 The Second Version of the Script
  • 3 Basic Python
    • 3.1 Introductory Topics
      • 3.1.1 Recommended Python Documentation
      • 3.1.2 Testing Statements in the Interactive Shell
      • 3.1.3 Control Statements
      • 3.1.4 Running an Application
      • 3.1.5 File Reading and Writing
      • 3.1.6 Output Formatting
      • 3.1.7 Exercises
    • 3.2 Variables of Different Types
      • 3.2.1 Boolean Types
      • 3.2.2 The None Variable
      • 3.2.3 Numbers and Numerical Expressions
      • 3.2.4 Lists and Tuples
      • 3.2.5 Dictionaries
      • 3.2.6 Splitting and Joining Text
      • 3.2.7 String Operations
      • 3.2.8 Text Processing
      • 3.2.9 The Basics of a Python Class
      • 3.2.10 Determining a Variable’s Type
      • 3.2.11 Exercises
    • 3.3 Functions
      • 3.3.1 Keyword Arguments
      • 3.3.2 Doc Strings
      • 3.3.3 Variable Number of Arguments
      • 3.3.4 Call by Reference
      • 3.3.5 Treatment of Input and Output Arguments
      • 3.3.6 Function Objects
    • 3.4 Working with Files and Directories
      • 3.4.1 Listing Files in a Directory
      • 3.4.2 Testing File Types
      • 3.4.3 Copying and Renaming Files
      • 3.4.4 Removing Files and Directories
      • 3.4.5 Splitting Pathnames
      • 3.4.6 Creating and Moving to Directories
      • 3.4.7 Traversing Directory Trees
      • 3.4.8 Exercises
  • 4 Numerical Computing in Python Table of Contents XI
    • 4.1 A Quick NumPy Primer
      • 4.1.1 Creating Arrays
      • 4.1.2 Array Indexing
      • 4.1.3 Array Computations
      • 4.1.4 Type Testing
      • 4.1.5 Hidden Temporary Arrays
      • 4.1.6 Exercises
    • 4.2 Vectorized Algorithms
      • 4.2.1 Arrays as Function Arguments
      • 4.2.2 Slicing
      • 4.2.3 Remark on Efficiency
      • 4.2.4 Exercises
    • 4.3 More Advanced Array Computing
      • 4.3.1 Random Numbers
      • 4.3.2 Linear Algebra
      • 4.3.3 The Gnuplot Module
      • 4.3.4 Example: Curve Fitting
      • 4.3.5 Arrays on Structured Grids
      • 4.3.6 File I/O with NumPy Arrays
      • 4.3.7 Reading and Writing Tables with NumPy Arrays
      • 4.3.8 Functionality in the Numpytools Module
      • 4.3.9 Exercises
    • 4.4 Other Tools for Numerical Computations
      • 4.4.1 The ScientificPython Package
      • 4.4.2 The SciPy Package
      • 4.4.3 The Python–Matlab Interface
      • 4.4.4 Some Useful Python Modules
    • 4.5 A Database for NumPy Arrays
      • 4.5.1 The Structure of the Database
      • 4.5.2 Pickling
      • 4.5.3 Formatted ASCII Storage
      • 4.5.4 Shelving
      • 4.5.5 Comparing the Various Techniques
  • 5 Combining Python with Fortran, C, and C++
    • 5.1 About Mixed Language Programming
      • 5.1.1 Applications of Mixed Language Programming
      • 5.1.2 Calling C from Python
      • 5.1.3 Automatic Generation of Wrapper Code
    • 5.2 Scientific Hello World Examples
      • 5.2.1 Combining Python and Fortran
      • 5.2.2 Combining Python and C
      • 5.2.3 Combining Python and C++ Functions
      • 5.2.4 Combining Python and C++ Classes
      • 5.2.5 Exercises XII Table of Contents
    • 5.3 A Simple Computational Steering Example
      • 5.3.1 Modified Time Loop for Repeated Simulations
      • 5.3.2 Creating a Python Interface
      • 5.3.3 The Steering Python Script
      • 5.3.4 Equipping the Steering Script with a GUI
    • 5.4 Scripting Interfaces to Large Libraries
  • 6 Introduction to GUI Programming
    • 6.1 Scientific Hello World GUI
      • 6.1.1 Introductory Topics
      • 6.1.2 The First Python/Tkinter Encounter
      • 6.1.3 Binding Events
      • 6.1.4 Changing the Layout
      • 6.1.5 The Final Scientific Hello World GUI
      • 6.1.6 An Alternative to Tkinter Variables
      • 6.1.7 About the Pack Command
      • 6.1.8 An Introduction to the Grid Geometry Manager
      • 6.1.9 Implementing a GUI as a Class
      • 6.1.10 A Simple Graphical Function Evaluator
      • 6.1.11 Exercises
    • 6.2 Adding GUIs to Scripts
      • 6.2.1 A Simulation and Visualization Script with a GUI
      • 6.2.2 Improving the Layout
      • 6.2.3 Exercises
    • 6.3 A List of Common Widget Operations
      • 6.3.1 Frame
      • 6.3.2 Label
      • 6.3.3 Button
      • 6.3.4 Text Entry
      • 6.3.5 Balloon Help
      • 6.3.6 Option Menu
      • 6.3.7 Slider
      • 6.3.8 Check Button
      • 6.3.9 Making a Simple Megawidget
      • 6.3.10 Menu Bar
      • 6.3.11 List Data
      • 6.3.12 Listbox
      • 6.3.13 Radio Button
      • 6.3.14 Combo Box
      • 6.3.15 Message Box
      • 6.3.16 User-Defined Dialogs
      • 6.3.17 Color-Picker Dialogs
      • 6.3.18 File Selection Dialogs
      • 6.3.19 Toplevel
      • 6.3.20 Some Other Types of Widgets Table of Contents XIII
      • 6.3.21 Adapting Widgets to the User’s Resize Actions
      • 6.3.22 Customizing Fonts and Colors
      • 6.3.23 Widget Overview
      • 6.3.24 Exercises
  • 7 Web Interfaces and CGI Programming
    • 7.1 Introductory CGI Scripts
      • 7.1.1 Web Forms and CGI Scripts
      • 7.1.2 Generating Forms in CGI Scripts
      • 7.1.3 Debugging CGI Scripts
      • 7.1.4 Security Issues
      • 7.1.5 A General Shell Script Wrapper for CGI Scripts
    • 7.2 Making a Web Interface to a Script
      • 7.2.1 A Class for Form Parameters
      • 7.2.2 Calling Other Programs
      • 7.2.3 Running Simulations
      • 7.2.4 Getting a CGI Script to Work
      • 7.2.5 Using Web Services from Scripts
      • 7.2.6 Exercises
  • 8 Advanced Python
    • 8.1 Miscellaneous Topics
      • 8.1.1 Parsing Command-Line Arguments
      • 8.1.2 Platform-Dependent Operations
      • 8.1.3 Run-Time Generation of Code
      • 8.1.4 Exercises
    • 8.2 Regular Expressions and Text Processing
      • 8.2.1 Motivation
      • 8.2.2 Special Characters
      • 8.2.3 Regular Expressions for Real Numbers
      • 8.2.4 Using Groups to Extract Parts of a Text
      • 8.2.5 Extracting Interval Limits
      • 8.2.6 Extracting Multiple Matches
      • 8.2.7 Splitting Text
      • 8.2.8 Pattern-Matching Modifiers
      • 8.2.9 Substitution and Backreferences
      • 8.2.10 Example: Swapping Arguments in Function Calls
      • 8.2.11 A General Substitution Script
      • 8.2.12 Debugging Regular Expressions
      • 8.2.13 Exercises
    • 8.3 Tools for Handling Data in Files
      • 8.3.1 Writing and Reading Python Data Structures
      • 8.3.2 Pickling Objects
      • 8.3.3 Shelving Objects
    • 8.3.4 Writing and Reading Zip Archive Files XIV Table of Contents
    • 8.3.5 Downloading Internet Files
    • 8.3.6 Binary Input/Output
    • 8.3.7 Exercises
  • 8.4 Scripts Involving Local and Remote Hosts
    • 8.4.1 Secure Shell Commands
    • 8.4.2 Distributed Simulation and Visualization
    • 8.4.3 Client/Server Programming
    • 8.4.4 Threads
  • 8.5 Classes
    • 8.5.1 Class Programming
    • 8.5.2 Checking the Class Type
    • 8.5.3 Private Data
    • 8.5.4 Static Data
    • 8.5.5 Special Attributes
    • 8.5.6 Special Methods
    • 8.5.7 Multiple Inheritance
    • 8.5.8 Using a Class as a C-like Structure
    • 8.5.9 Attribute Access via String Names
    • 8.5.10 Example: Turning String Formulas into Functions
    • 8.5.11 Example: Class for Structured Grids
    • 8.5.12 New-Style Classes
    • 8.5.13 Implementing Get/Set Functions via Properties
    • 8.5.14 Subclassing Built-in Types
    • 8.5.15 Copy and Assignment
    • 8.5.16 Building Class Interfaces at Run Time
    • 8.5.17 Building Flexible Class Interfaces
    • 8.5.18 Exercises
  • 8.6 Scope of Variables
    • 8.6.1 Global, Local, and Class Variables
    • 8.6.2 Nested Functions
    • 8.6.3 Dictionaries of Variables in Namespaces
  • 8.7 Exceptions
    • 8.7.1 Handling Exceptions
    • 8.7.2 Raising Exceptions
  • 8.8 Iterators
    • 8.8.1 Constructing an Iterator
    • 8.8.2 A Pointwise Grid Iterator
    • 8.8.3 A Vectorized Grid Iterator
    • 8.8.4 Generators
    • 8.8.5 Some Aspects of Generic Programming
  • 8.9 Investigating Efficiency
    • 8.9.1 CPU-Time Measurements
    • 8.9.2 Profiling Python Scripts
    • 8.9.3 Optimization of Python Code
  • 9 Fortran Programming with NumPy Arrays Table of Contents XV
    • 9.1 Problem Definition
    • 9.2 Filling an Array in Fortran
      • 9.2.1 The Fortran Subroutine
      • 9.2.2 Building and Inspecting the Extension Module
    • 9.3 Array Storage Issues
      • 9.3.1 Generating an Erroneous Interface
      • 9.3.2 Array Storage in C and Fortran
      • 9.3.3 Input and Output Arrays as Function Arguments
      • 9.3.4 F2PY Interface Files
      • 9.3.5 Hiding Work Arrays
    • 9.4 Increasing Callback Efficiency
      • 9.4.1 Callbacks to Vectorized Python Functions
      • 9.4.2 Avoiding Callbacks to Python
      • 9.4.3 Compiled Inline Callback Functions
    • 9.5 Summary
    • 9.6 Exercises
      • 9.6.1 Exercises
  • 10 C and C++ Programming with NumPy Arrays
    • 10.1 C Programming with NumPy Arrays
      • 10.1.1 Basics of the NumPy C API
      • 10.1.2 The Handwritten Extension Code
      • 10.1.3 Sending Arguments from Python to C
      • 10.1.4 Consistency Checks
      • 10.1.5 Computing Array Values
      • 10.1.6 Returning an Output Array
      • 10.1.7 Convenient Macros
      • 10.1.8 Module Initialization
      • 10.1.9 Extension Module Template
      • 10.1.10 Compiling, Linking, and Debugging the Module
      • 10.1.11 Writing a Wrapper for a C Function
    • 10.2 C++ Programming with NumPy Arrays
      • 10.2.1 Wrapping a NumPy Array in a C++ Object
      • 10.2.2 Using SCXX
      • 10.2.3 NumPy–C++ Class Conversion.
    • 10.3 Comparison of the Implementations
      • 10.3.1 Efficiency
      • 10.3.2 Error Handling
      • 10.3.3 Summary
      • 10.3.4 Exercises
  • 11 More Advanced GUI Programming XVI Table of Contents
    • 11.1 Adding Plot Areas in GUIs
      • 11.1.1 The BLT Graph Widget
      • 11.1.2 Animation of Functions in BLT Graph Widgets
      • 11.1.3 Other Tools for Making GUIs with Plots
      • 11.1.4 Exercises
    • 11.2 Event Bindings
      • 11.2.1 Binding Events to Functions with Arguments
      • 11.2.2 A Text Widget with Tailored Keyboard Bindings
      • 11.2.3 A Fancy List Widget
    • 11.3 Animated Graphics with Canvas Widgets
      • 11.3.1 The First Canvas Encounter
      • 11.3.2 Coordinate Systems
      • 11.3.3 The Mathematical Model Class
      • 11.3.4 The Planet Class
      • 11.3.5 Drawing and Moving Planets
      • 11.3.6 Dragging Planets to New Positions
      • 11.3.7 Using Pmw’s Scrolled Canvas Widget
    • 11.4 Tools for Simulation & Visualization Scripts
      • 11.4.1 Restructuring the Script
      • 11.4.2 Representing a Parameter by a Class
      • 11.4.3 Improved Command-Line Script
      • 11.4.4 Improved GUI Script
      • 11.4.5 Improved CGI Script
      • 11.4.6 Parameters with Physical Dimensions
      • 11.4.7 Adding a Curve Plot Area
      • 11.4.8 Automatic Generation of Scripts
      • 11.4.9 Applications of the Tools
      • 11.4.10 Allowing Physical Units in Input Files
      • 11.4.11 Converting Input Files to GUIs
  • 12 Tools and Examples
    • 12.1 Running Series of Computer Experiments
      • 12.1.1 Multiple Values of Input Parameters
      • 12.1.2 Implementation Details
      • 12.1.3 Further Applications
    • 12.2 Tools for Representing Functions
      • 12.2.1 Functions Defined by String Formulas
      • 12.2.2 A Unified Interface to Functions
      • 12.2.3 Interactive Drawing of Functions
      • 12.2.4 A Notebook for Selecting Functions
    • 12.3 Solving Partial Differential Equations
      • 12.3.1 Numerical Methods for 1D Wave Equations
      • 12.3.2 Implementations of 1D Wave Equations
      • 12.3.3 Classes for Solving 1D Wave Equations
      • 12.3.4 A Problem Solving Environment Table of Contents XVII
      • 12.3.5 Numerical Methods for 2D Wave Equations
      • 12.3.6 Implementations of 2D Wave Equations
  • A Setting up the Required Software Environment
    • A.1 Installation on Unix Systems
      • A.1.1 A Suggested Directory Structure
      • A.1.2 Setting Some Environment Variables
      • A.1.3 Installing Tcl/Tk and Additional Modules
      • A.1.4 Installing Python
      • A.1.5 Installing Python Modules
      • A.1.6 Installing Gnuplot
      • A.1.7 Installing SWIG
      • A.1.8 Summary of Environment Variables
      • A.1.9 Testing the Installation of Scripting Utilities
    • A.2 Installation on Windows Systems
  • B Elements of Software Engineering
    • B.1 Building and Using Modules
      • B.1.1 Single-File Modules
      • B.1.2 Multi-File Modules
      • B.1.3 Debugging and Troubleshooting
    • B.2 Tools for Documenting Python Software
      • B.2.1 Doc Strings
      • B.2.2 Tools for Automatic Documentation
    • B.3 Coding Standards
      • B.3.1 Style Guide
      • B.3.2 Pythonic Programming
    • B.4 Verification of Scripts
      • B.4.1 Automating Regression Tests
      • B.4.2 Implementing a Tool for Regression Tests
      • B.4.3 Writing a Test Script
      • B.4.4 Verifying Output from Numerical Computations
      • B.4.5 Automatic Doc String Testing
      • B.4.6 Unit Testing
    • B.5 Version Control Management
      • B.5.1 Getting Started with CVS
      • B.5.2 Building Scripts to Simplify the Use of CVS
    • B.6 Exercises
  • Exercise 2.1 Become familiar with the electronic documentation
  • Exercise 2.2 Extend Exercise 2.1 with a loop
  • Exercise 2.3 Find five errors in a script
  • Exercise 2.4 Basic use of control structures
  • Exercise 2.5 Replace exception handling by an if-test
  • Exercise 2.6 Use standard input/output instead of files
  • Exercise 2.7 Read streams of (x, y) pairs from the command line
  • Exercise 2.8 Estimate the chance of an event in a dice game
  • Exercise 2.9 Determine if you win or loose a hazard game
  • Exercise 2.10 Generate an HTML report from the simviz1.py script
  • Exercise 2.11 Generate a LATEX report from the simviz1.py script
  • Exercise 2.12 Compute time step values in the simviz1.py script
  • Exercise 2.13 Use Matlab for curve plotting in the simviz1.py script
  • Exercise 2.14 Combine curves from two simulations in one plot
  • Exercise 2.15 Make an animated oscillating system figure
  • Exercise 2.16 Improve an automatically generated HTML report
  • Exercise 2.17 Combine two-column data files to a multi-column file
  • Exercise 3.1 Write format specifications in printf-style
  • Exercise 3.2 Write your own function for joining strings
  • Exercise 3.3 Write an improved function for joining strings
  • Exercise 3.4 Never modify a list you are iterating on
  • Exercise 3.5 Pack a collection of files
  • Exercise 3.6 Make a specialized sort function
  • Exercise 3.7 Check if your system has a specific program
  • Exercise 3.8 Find the paths to a collection of programs
  • Exercise 3.9 Use Exercise 3.8 to improve the simviz1.py script
  • Exercise 3.10 Use Exercise 3.8 to improve the loop4simviz2.py script
  • Exercise 3.11 Find the version number of a utility
  • Exercise 3.12 Automate execution of a family of similar commands
  • Exercise 3.13 Remove temporary files in a directory tree
  • Exercise 3.14 Find old and large files in a directory tree
  • Exercise 3.15 Remove redundant files in a directory tree
  • Exercise 3.16 Annotate a filename with the current date
  • Exercise 3.17 Automatic backup of recently modified files
  • Exercise 3.18 Search for a text in files with certain extensions
  • Exercise 3.19 Search directories for plots and make HTML report
  • Exercise 3.20 Fix Unix/Windows Line Ends
  • Exercise 3.21 Improve the scripts in Exercise 3.20
  • Exercise 4.1 Matrix-vector multiply with NumPy arrays
  • Exercise 4.2 Replace lists by NumPy arrays
  • Exercise 4.3 Efficiency of NumPy array initialization XX List of Exercises
  • Exercise 4.4 Assignment and in-place NumPy array modifications
  • Exercise 4.5 Process comma-separated numbers in a file
  • Exercise 4.6 Vectorized constant function
  • Exercise 4.7 Vectorize a numerical integration rule
  • Exercise 4.8 Vectorize a formula containing an if condition
  • Exercise 4.9 Vectorized Box-M¨uller method for normal variates
  • Exercise 4.10 Implement Exercise 2.8 using NumPy arrays
  • Exercise 4.11 Implement Exercise 2.9 using NumPy arrays
  • Exercise 4.12 Use the Gnuplot module in the simviz1.py script
  • Exercise 4.13 NumPy arrays and binary files
  • Exercise 4.14 One-dimensional Monte Carlo integration
  • Exercise 4.15 Higher-dimensional Monte Carlo integration
  • Exercise 4.16 Load data file into NumPy array and visualize
  • Exercise 4.17 Analyze trends in the data from Exercise 4.16
  • Exercise 4.18 Computing a function over a 3D grid
  • Exercise 5.1 Implement a numerical integration rule in F77
  • Exercise 5.2 Implement a numerical integration rule in C
  • Exercise 5.3 Implement a numerical integration rule in C++
  • Exercise 6.1 Modify the Scientific Hello World GUI
  • Exercise 6.2 Change the layout of the GUI in Exercise 6.1
  • Exercise 6.3 Control a layout with the grid geometry manager
  • Exercise 6.4 Make a demo of Newton’s method
  • Exercise 6.5 Program with Pmw.EntryField in hwGUI10.py
  • Exercise 6.6 Program with Pmw.EntryField in simvizGUI2.py
  • Exercise 6.7 Replace Tkinter variables by set/get-like functions
  • Exercise 6.8 Use simviz1.py as a module in simvizGUI2.py
  • Exercise 6.9 Apply Matlab for visualization in simvizGUI2.py
  • Exercise 6.10 Program with Pmw.OptionMenu in simvizGUI2.py
  • Exercise 6.11 Study the nonlinear motion of a pendulum
  • Exercise 6.12 Add error handling with an associated message box
  • Exercise 6.13 Add a message bar to a balloon help
  • Exercise 6.14 Select a file from a list and perform an action
  • Exercise 6.15 Make a GUI for finding and selecting font names
  • Exercise 6.16 Launch a GUI when command-line options are missing
  • Exercise 6.17 Write a GUI for Exercise 3.15
  • Exercise 6.18 Write a GUI for selecting files to be plotted
  • Exercise 6.19 Write an easy-to-use GUI generator
  • Exercise 7.1 Write a CGI debugging tool
  • Exercise 7.2 Make a Web calculator
  • Exercise 7.3 Make a Web service for registering participants
  • Exercise 7.4 Make a Web service for numerical experimentation
  • Exercise 7.5 Become a “nobody” user on a Web server
  • Exercise 8.1 Use the getopt/optparse module in simviz1.py
  • Exercise 8.2 Store command-line options in a dictionary
  • Exercise 8.3 Turn files with commands into Python variables List of Exercises XXI
  • Exercise 8.4 A grep script
  • Exercise 8.5 Experiment with a regex for real numbers
  • Exercise 8.6 Find errors in regular expressions
  • Exercise 8.7 Generate data from a user-supplied formula
  • Exercise 8.8 Explain the behavior of regular expressions
  • Exercise 8.9 Edit extensions in filenames
  • Exercise 8.10 Extract info from a program code
  • Exercise 8.11 Regex for splitting a pathname
  • Exercise 8.12 Rename a collection of files according to a pattern
  • Exercise 8.13 Reimplement the re.findall function
  • Exercise 8.14 Interpret a regex code and find programming errors
  • Exercise 8.15 Automatic fine tuning of PostScript figures
  • Exercise 8.16 Prefix name of digital image files with date and time
  • Exercise 8.17 Transform a list of lines to a list of paragraphs
  • Exercise 8.18 Copy computer codes into HTML documents directly from source files
  • Exercise 8.19 A very useful script for all writers
  • Exercise 8.20 Read Fortran 90 files with namelists
  • Exercise 8.21 Interpret Fortran 90 array subscripting syntax
  • Exercise 8.22 Regex for matching LATEX commands
  • Exercise 8.23 Automatic update of function calls in C++ files
  • Exercise 8.24 Read/write (x, y) pairs from/to binary files
  • Exercise 8.25 Use the XDR format in the script from Exercise 8.24
  • Exercise 8.26 Using a Web site for distributed simulation
  • Exercise 8.27 Convert data structures to/from strings
  • Exercise 8.28 Implement a class for vectors in 3D
  • Exercise 8.29 Extend the class from Exericse 8.28
  • Exercise 8.30 Make a dictionary type with ordered keys
  • Exercise 8.31 Make a smarter integration function
  • Exercise 8.32 Extend the Grid2D class
  • Exercise 8.33 Extend the functionality of class Grid2D at run time
  • Exercise 8.34 Make a boundary iterator in a 2D grid
  • Exercise 8.35 Make a generator for odd numbers
  • Exercise 8.36 Make a class for sparse vectors
  • Exercise 9.1 Extend Exercise 5.1 with a callback to Python
  • Exercise 9.2 Compile callback functions in Exercise 9.1
  • Exercise 9.3 Smoothing of time series
  • Exercise 9.4 Smoothing of 3D data
  • Exercise 9.5 Type incompatibility between Python and Fortran
  • Exercise 9.6 Problematic callbacks to Python from Fortran
  • Exercise 10.1 Extend Exercise 5.2 or 5.3 with a callback to Python
  • Exercise 10.2 Apply C/C++ function pointers in Exercise 5.3
  • Exercise 10.3 Investigate the efficiency of vector operations
  • Exercise 10.4 Make callbacks to vectorized Python functions
  • Exercise 10.5 Avoid Python callbacks in extension modules
  • Exercise 10.6 Extend Exercise 9.4 with C and C++ code XXII List of Exercises
  • Exercise 10.7 Apply SWIG to an array class in C++
  • Exercise 10.8 Build a dictionary in C
  • Exercise 10.9 Make a C module for computing random numbers
  • Exercise 10.10 Almost automatic generation of C extension modules
  • Exercise 10.11 Introduce C++ array objects in Exercise 10.10
  • Exercise 10.12 Introduce SCXX in Exercise 10.11
  • Exercise 11.1 Incorporate a BLT graph widget in simviz1.py
  • Exercise 11.2 Plot a two-column datafile in a Pmw.Blt widget
  • Exercise 11.3 Use a BLT graph widget in simvizGUI2.py
  • Exercise 11.4 Extend Exercise 11.3 to handle multiple curves
  • Exercise 11.5 Use a BLT graph widget in Exercise 6.4
  • Exercise 11.6 Interactive dump of snapshot plots in an animation
  • Exercise 11.7 Extend the animate.py GUI
  • Exercise 11.8 Animate a curve in a BLT graph widget
  • Exercise 11.9 Add animations to the GUI in Exercise 11.5
  • Exercise 11.10 Extend the GUI in Exercise 6.17 with a fancy list
  • Exercise 11.11 Remove canvas items
  • Exercise 11.12 Introducing properties in class Parameters
  • Exercise 12.1 Allow multiple values of parameters in input files
  • Exercise 12.2 Turn mathematical formulas into Fortran functions
  • Exercise 12.3 Move a wave source during simulation
  • Exercise 12.4 Include damping in a 1D wave simulator
  • Exercise 12.5 Use iterators in finite difference schemes
  • Exercise B.1 Pack modules and packages using Distutils
  • Exercise B.2 Distribute mixed-language code using Distutils
  • Exercise B.3 Make a Python module of simviz1.py
  • Exercise B.4 Use tools to document the script in Exercise 3.15
  • Exercise B.5 Make a regression test for a trivial script
  • Exercise B.6 Make a regression test for a script with I/O
  • Exercise B.7 Repeat Exercise B.5 using the test script tools
  • Exercise B.8 Make a regression test for a file traversal script
  • Exercise B.9 Make a regression test for the script in Exercise 3.15
  • Exercise B.10 Approximate floats in Exercise B.5
  • Exercise B.11 Make a tar/zip archive of files associated with a script
  • Exercise B.12 Semi-automatic evaluation of a student project