Read Programming Python Online

Authors: Mark Lutz

Tags: #COMPUTERS / Programming Languages / Python

Programming Python (204 page)

So What’s “Python: The Sequel”?

As we’ve seen in this book, Python is a multifaceted tool, useful in
a wide variety of domains. What can we say about Python to sum up here? In
terms of some of its best attributes, the Python language is:

  • General purpose

  • Object-oriented

  • Interpreted

  • Very high level

  • Openly designed

  • Widely portable

  • Freely available

  • Refreshingly coherent

Python is useful for both standalone development and extension work,
and it is optimized to boost
developer productivity
on many fronts. But the real meaning of Python is really up to you, the
reader. Since Python is a general-purpose tool, what it “is” depends on
how you choose to use it.

In the Final Analysis…

I hope this book has taught you something about Python, both the
language and its roles. Beyond this text, there is really no substitute
for doing some original Python programming. Be sure to grab a reference
source or two to help you along the way.

The task of programming computers will probably always be
challenging. Perhaps happily, there will continue to be a need for
intelligent software engineers, skilled at translating real-world tasks
into computer-executable form, at least for the foreseeable future. After
all, if it were too easy, none of us would get paid. No language or tool
can obviate the hard work of full-scale programming entirely.

But current development practice and tools make our tasks
unnecessarily difficult: many of the obstacles faced by software
developers are purely artificial. We have come far in our quest to improve
the speed of computers; the time has come to focus our attention on
improving the speed of development. In an era of constantly shrinking
schedules, productivity must be paramount.

Python, as a mixed-paradigm tool, has the potential to foster
development modes that simultaneously leverage the benefits of rapid
development and of traditional languages. While Python won’t solve all the
problems of the software industry, it offers hope for making programming
simpler, faster, and at least a little more enjoyable.

It may not get us off that island altogether, but it sure beats
bananas and coconuts.

A Morality Tale of Perl Versus Python

(The following was
posted to the
rec.humor.funny
Usenet
newsgroup by Larry
Hastings
, and
it is reprinted here with the original author’s permission.)

This has been percolating in the back of my mind for a while. It’s
a scene from
The Empire Strikes Back
, reinterpreted
to serve a valuable moral lesson for aspiring
programmers
.

EXTERIOR: DAGOBAH—DAY

With Yoda strapped to his back, Luke climbs up one of the many
thick vines that grow in the swamp until he reaches the Dagobah
statistics lab. Panting heavily, he continues his exercises—grepping,
installing new packages, logging in as root, and writing replacements
for two-year-old shell scripts in Python.

YODA: Code! Yes. A programmer’s strength flows from code
maintainability. But beware of Perl. Terse syntax…more than one way to
do it…default variables. The dark side of code maintainability are they.
Easily they flow, quick to join you when code you write. If once you
start down the dark path, forever will it dominate your destiny, consume
you it will.

LUKE: Is Perl better than Python?

YODA: No…no…no. Quicker, easier, more seductive.

LUKE: But how will I know why Python is better than Perl?

YODA: You will know. When your code you try to read six months
from now.

Index
A note on the digital index

A link in an index entry is displayed as the section title in which that entry appears. Because some sections have multiple index markers, it is not unusual for an entry to have several links to the same section. Clicking on any link will take you directly to the place in the text in which the marker appears.

Symbols
\ (backslash),
The Module Search Path
,
The Hello World Selector
/ (forward slash),
The Module Search Path
| (pipe character),
Chaining programs with pipes
_ (underscore),
Standalone Container Classes
* wildcard
character,
The glob module
A
after method,
Time Tools, Threads, and Animation
,
Using the after Method
,
Hiding and redrawing widgets and windows
,
Simple Animation Techniques
,
Using widget.after events
drawbacks,
Using widget.after events
functionality,
Using the after Method
,
Hiding and redrawing widgets and windows
scheduling functions,
Time Tools, Threads, and Animation
,
Simple Animation Techniques
algebra, relational,
Adding Relational Algebra to Sets (External)
anchoring widgets,
Using Anchor to Position Instead of Stretch
,
Using Anchor to Position Instead of Stretch
animation,
Coordinates
,
Simple Animation Techniques
,
Using multiple time.sleep loop threads
,
Using time.sleep loops
,
Using time.sleep loops
,
Using multiple time.sleep loop threads
,
Other animation effects
,
Threads and animation
,
Graphics and gaming toolkits
,
Graphics and gaming toolkits
coordinate system and,
Coordinates
graphics and,
Graphics and gaming toolkits
other effects,
Other animation effects
simple techniques,
Simple Animation Techniques
,
Using multiple time.sleep loop threads
third-party toolkits,
Graphics and gaming toolkits
threads and,
Threads and animation
time.sleep loops,
Using time.sleep loops
,
Using time.sleep loops
,
Using multiple time.sleep loop threads
anonymous pipes,
Interprocess Communication
,
Anonymous Pipes
,
Anonymous pipe basics
,
Anonymous pipe basics
,
Wrapping pipe descriptors in file objects
,
Anonymous pipes and threads
,
Bidirectional IPC with anonymous pipes
,
Bidirectional IPC with anonymous pipes
,
Output stream buffering revisited: Deadlocks and
flushes
,
Output stream buffering revisited: Deadlocks and
flushes
basic functionality,
Anonymous pipe basics
,
Anonymous pipe basics
bidirectional IPC and,
Bidirectional IPC with anonymous pipes
,
Bidirectional IPC with anonymous pipes
defined,
Interprocess Communication
,
Anonymous Pipes
output stream buffering,
Output stream buffering revisited: Deadlocks and
flushes
,
Output stream buffering revisited: Deadlocks and
flushes
threads and,
Anonymous pipes and threads
wrapping descriptors in file objects,
Wrapping pipe descriptors in file objects
Apache web servers,
Python Internet Development Options
,
Web Server Options
append list operator,
A database list
,
Nested structures
arguments,
Coding alternatives: busy loops, arguments, and context
managers
,
Coding alternatives: busy loops, arguments, and context
managers
,
Arguments versus globals
,
One More for Old Times’ Sake
,
Arguments versus globals
global variables versus,
Arguments versus globals
,
Arguments versus globals
pass-by-name,
One More for Old Times’ Sake
threads and,
Coding alternatives: busy loops, arguments, and context
managers
,
Coding alternatives: busy loops, arguments, and context
managers
Array object (multiprocessing),
IPC Tools: Pipes, Shared Memory, and Queues
ASCII encoding,
Unicode encodings for text files
askyesno function,
Standard (Common) Dialogs
associated variables,
tkinter “Variables” and Form Layout Alternatives
,
tkinter “Variables” and Form Layout Alternatives
,
Checkbuttons
asyncore.py module,
Running the select server
attachments,
Sending Email and Attachments
,
Sending Email and Attachments
,
Viewing Email and Attachments
,
Ideas for Improvement
,
New in This Fourth Edition (Version 3.0)
propagating,
Ideas for Improvement
sending via PyMailCGI,
New in This Fourth Edition (Version 3.0)
sending via PyMailGUI,
Sending Email and Attachments
,
Sending Email and Attachments
viewing via PyMailGUI,
Viewing Email and Attachments
augmenting methods,
Augmenting methods
B
backslash (\),
The Module Search Path
,
The Hello World Selector
backups, verifying,
Verifying Backups
base64 module,
Python’s Internet Library Modules
BASIC language,
Enter Python
BDFL acronym,
Other Database Options
BeautifulSoup third-party extension,
mailconfig: User Configurations
,
Ideas for Improvement
Beazley, Dave,
The SWIG Integration Code Generator
bell method,
Using the after Method
BigGui client demo program,
BigGui: A Client Demo Program
,
BigGui: A Client Demo Program
binary files,
The File Object Model in Python 3.X
,
Binary and Text Files
,
Parsing packed binary data with the struct module
,
Parsing packed binary data with the struct module
,
Random access files
,
Random access files
,
Other binary mode considerations
defined,
The File Object Model in Python 3.X
,
Binary and Text Files
parsing with struct module,
Parsing packed binary data with the struct module
,
Parsing packed binary data with the struct module
random access processing,
Random access files
,
Random access files
Unicode encoding and,
Other binary mode considerations
binary mode transfers,
Uploading Site Directories
binary trees,
Binary Search Trees
,
Built-in Options
,
Implementing Binary Trees
,
Trees with Both Keys and Values
built-in options,
Built-in Options
defined,
Binary Search Trees
implementing,
Implementing Binary Trees
,
Trees with Both Keys and Values
binascii module,
Python’s Internet Library Modules
bind method mechanism,
Other tkinter Callback Protocols
,
Binding Events
,
Binding Events
,
More on events and the quit and destroy
methods
,
Advanced Text and Tag Operations
,
Binding events on specific items
,
Binding events on specific items
binding events,
Binding Events
,
Binding Events
,
More on events and the quit and destroy
methods
,
Binding events on specific items
,
Binding events on specific items
binding tags,
Advanced Text and Tag Operations
functionality,
Other tkinter Callback Protocols
binding,
Binding Events
,
Binding Events
,
More on events and the quit and destroy
methods
,
Advanced Text and Tag Operations
,
Binding events on specific items
,
Binding events on specific items
,
Binding reserved port servers
events,
Binding Events
,
Binding Events
,
More on events and the quit and destroy
methods
,
Binding events on specific items
,
Binding events on specific items
reserved port servers,
Binding reserved port servers
tags,
Advanced Text and Tag Operations
binhex module,
Python’s Internet Library Modules
BitmapImage widget class,
The End of the Tutorial
,
Images
,
Fun with Buttons and Pictures
BooleanVar class,
tkinter “Variables” and Form Layout Alternatives
Boost.Python system,
Other Extending Tools
bound methods,
Using OOP for GUIs
,
Bound Method Callback Handlers
,
Recoding with classes and bound methods
,
Recoding with classes and bound methods
,
Threading model implementation
callback handlers and,
Bound Method Callback Handlers
defined,
Using OOP for GUIs
PyMailGUI program support,
Threading model implementation
recoding with,
Recoding with classes and bound methods
,
Recoding with classes and bound methods
browsers,
Running Examples in This Part of the Book
,
Testing outside browsers with the module urllib.request
,
Testing outside browsers with the module urllib.request
,
Debugging CGI scripts
,
Creating a cookie
,
Using the Send Mail Script Outside a Browser
,
Using the Send Mail Script Outside a Browser
cookie support,
Creating a cookie
displaying CGI context in,
Debugging CGI scripts
Grail,
Running Examples in This Part of the Book
PyMailCGI send mail script,
Using the Send Mail Script Outside a Browser
,
Using the Send Mail Script Outside a Browser
testing with urllib.request,
Testing outside browsers with the module urllib.request
,
Testing outside browsers with the module urllib.request
buffering,
Text-mode files and buffered output streams
,
Line buffering
,
Line buffering
,
Buffering in other contexts: Command pipes revisited
command pipes and,
Buffering in other contexts: Command pipes revisited
line,
Line buffering
,
Line buffering
output streams,
Text-mode files and buffered output streams
Button widget class,
Adding Buttons and Callbacks
,
Adding Buttons and Callbacks
,
The End of the Tutorial
,
Checkbuttons
command option,
Checkbuttons
functionality,
Adding Buttons and Callbacks
,
Adding Buttons and Callbacks
,
The End of the Tutorial
buttons,
Adding Buttons and Callbacks
,
Adding Buttons and Callbacks
,
Deferring Calls with Lambdas and Object References
,
A “smart” and reusable Quit button
,
Fun with Buttons and Pictures
,
Fun with Buttons and Pictures
,
Layout options: Fixed-size buttons
,
Adding new buttons in new components
,
Adding new buttons in new components
adding,
Adding Buttons and Callbacks
,
Adding Buttons and Callbacks
adding in PyCalc,
Adding new buttons in new components
,
Adding new buttons in new components
deferring calls,
Deferring Calls with Lambdas and Object References
fixed-size,
Layout options: Fixed-size buttons
Quit button,
A “smart” and reusable Quit button
randomly changing images,
Fun with Buttons and Pictures
,
Fun with Buttons and Pictures
bytearray string type,
Other String Concepts in Python 3.X: Unicode and bytes
bytecode files,
Cleaning Up Bytecode Files
,
Cleaning Up Bytecode Files
,
Precompiling Strings to Bytecode
,
Precompiling Strings to Bytecode
cleaning up,
Cleaning Up Bytecode Files
,
Cleaning Up Bytecode Files
precompiling strings to,
Precompiling Strings to Bytecode
,
Precompiling Strings to Bytecode
bytes string type,
Other String Concepts in Python 3.X: Unicode and bytes
,
String types in the Text widget
,
String types in the Text widget
,
The problem with treating text as bytes
,
The problem with treating text as bytes
C
C language,
C API thread considerations
,
More on the holmes expert system shell
,
Scanning C Header Files for Patterns
,
Scanning C Header Files for Patterns
,
“I Am Lost at C”
,
Extending and Embedding
,
A Simple C Extension Module
,
Extending and Embedding
,
Extending and Embedding
,
Extending Python in C: Overview
,
A Simple C Extension Module
,
The SWIG Integration Code Generator
,
A Simple SWIG Example
,
Wrapping C Environment Calls
,
Wrapping C Environment Calls
,
Wrapping C Environment Calls
,
Wrapping C Environment Calls with SWIG
,
Wrapping C Environment Calls with SWIG
,
Wrapping C Environment Calls with SWIG
,
The C Embedding API
,
Precompiling Strings to Bytecode
,
The C Embedding API
,
Using Python Classes in C
,
Using Python Classes in C
,
Enter Python
API standard,
C API thread considerations
,
The C Embedding API
classes and,
Using Python Classes in C
,
Using Python Classes in C
embedding interface,
Extending and Embedding
,
Extending and Embedding
,
The C Embedding API
,
Precompiling Strings to Bytecode
extending interface,
Extending and Embedding
,
A Simple C Extension Module
,
Extending Python in C: Overview
,
A Simple C Extension Module
getenv function,
Wrapping C Environment Calls
putenv function,
Wrapping C Environment Calls
,
Wrapping C Environment Calls with SWIG
Python comparison,
Enter Python
Python support,
“I Am Lost at C”
scanning files for patterns,
Scanning C Header Files for Patterns
,
Scanning C Header Files for Patterns
strop extension module,
More on the holmes expert system shell
SWIG tool and,
The SWIG Integration Code Generator
,
A Simple SWIG Example
,
Wrapping C Environment Calls with SWIG
wrapping environment calls,
Wrapping C Environment Calls
,
Wrapping C Environment Calls with SWIG
C++ language,
Wrapping C++ Classes with SWIG
,
Exploring the wrappers interactively
,
Other Extending Tools
,
Enter Python
CalcGui class,
Running PyCalc
,
Evaluating expressions with stacks
calculators,
Building the GUI
(see PyCalc program)
callable objects,
What Is Embedded Code?
,
Calling Python Objects
,
Calling Python Objects
,
Registering Callback Handler Objects
,
Registration Implementation
defined,
What Is Embedded Code?
overview,
Calling Python Objects
,
Calling Python Objects
registering,
Registering Callback Handler Objects
,
Registration Implementation
callback handlers,
Programming structure
,
Adding User-Defined Callback Handlers
,
Lambda Callback Handlers
,
But you must still sometimes use defaults instead of enclosing
scopes
,
Deferring Calls with Lambdas and Object References
,
Deferring Calls with Lambdas and Object References
,
Bound Method Callback Handlers
,
Callable Class Object Callback Handlers
,
Other tkinter Callback Protocols
,
Binding Events
,
Binding Events
,
Reloading Callback Handlers Dynamically
,
Reloading Callback Handlers Dynamically
,
Registering Callback Handler Objects
,
Registration Implementation
adding user-defined,
Adding User-Defined Callback Handlers
additional protocols,
Other tkinter Callback Protocols
binding events,
Binding Events
,
Binding Events
bound method,
Bound Method Callback Handlers
callable class object,
Callable Class Object Callback Handlers
deferring calls,
Deferring Calls with Lambdas and Object References
,
Deferring Calls with Lambdas and Object References
defined,
Programming structure
lambda,
Lambda Callback Handlers
,
But you must still sometimes use defaults instead of enclosing
scopes
registering objects,
Registering Callback Handler Objects
,
Registration Implementation
reloading dynamically,
Reloading Callback Handlers Dynamically
,
Reloading Callback Handlers Dynamically
callbacks,
Adding Buttons and Callbacks
,
Adding Buttons and Callbacks
,
Arguments versus globals
,
Printing dialog results and passing callback data with
lambdas
,
Printing dialog results and passing callback data with
lambdas
,
Placing Callbacks on Queues
,
Passing bound method callbacks on queues
adding,
Adding Buttons and Callbacks
,
Adding Buttons and Callbacks
arguments versus globals,
Arguments versus globals
passing data with lambdas,
Printing dialog results and passing callback data with
lambdas
,
Printing dialog results and passing callback data with
lambdas
placing on queues,
Placing Callbacks on Queues
,
Passing bound method callbacks on queues
Canvas widget class,
Future directions
,
The End of the Tutorial
,
Images
,
Scrolling and canvases (ahead)
,
Programming Scroll Bars
,
Programming Scroll Bars
,
Programming Scroll Bars
,
Programming Scroll Bars
,
Text
,
Canvas
,
Basic Canvas Operations
,
Programming the Canvas Widget
,
Canvas object tags
,
Coordinates
,
Object construction
,
Object construction
,
Object identifiers and operations
,
Object identifiers and operations
,
Object identifiers and operations
,
Object identifiers and operations
,
Canvas object tags
,
Canvas object tags
,
Canvas object tags
,
Canvas object tags
,
Canvas object tags
,
Scrolling Canvases
,
Scrolling Canvases
,
Scrollable Canvases and Image Thumbnails
,
Scrolling images too: PyPhoto (ahead)
,
Using Canvas Events
,
Binding events on specific items
,
Using Canvas Events
,
Binding events on specific items
,
Binding events on specific items
,
Simple Animation Techniques
addtag_withtag method,
Canvas object tags
basic operations,
Basic Canvas Operations
controlling image displays,
Scrolling and canvases (ahead)
coordinate system,
Coordinates
create_ method,
Object identifiers and operations
create_polygon method,
Object construction
delete method,
Using Canvas Events
event support,
Using Canvas Events
,
Binding events on specific items
find_closest method,
Canvas object tags
,
Binding events on specific items
freeform graphic support,
Future directions
functionality,
The End of the Tutorial
,
Images
,
Text
,
Canvas
image thumbnails,
Scrollable Canvases and Image Thumbnails
,
Scrolling images too: PyPhoto (ahead)
itemconfig method,
Object identifiers and operations
move method,
Canvas object tags
object construction,
Object construction
object identifiers,
Object identifiers and operations
object tags,
Canvas object tags
postscript method,
Canvas object tags
programming,
Programming the Canvas Widget
,
Canvas object tags
scrolling canvases,
Scrolling Canvases
,
Scrolling Canvases
tag_bind method,
Binding events on specific items
tkraise method,
Object identifiers and operations
update method,
Simple Animation Techniques
xscrollcommand option,
Programming Scroll Bars
xview method,
Programming Scroll Bars
yscrollcommand option,
Programming Scroll Bars
yview method,
Programming Scroll Bars
canvases,
Canvas
,
Basic Canvas Operations
,
Basic Canvas Operations
,
Coordinates
,
Object construction
,
Object identifiers and operations
,
Canvas object tags
,
Scrolling Canvases
,
Scrolling Canvases
,
Scrollable Canvases and Image Thumbnails
,
Scrolling images too: PyPhoto (ahead)
,
Using Canvas Events
,
Binding events on specific items
,
Using Canvas Events
,
Using Canvas Events
,
Using Canvas Events
basic operations,
Basic Canvas Operations
,
Basic Canvas Operations
clearing,
Using Canvas Events
coordinate system,
Coordinates
defined,
Canvas
dragging out object shapes,
Using Canvas Events
event support,
Using Canvas Events
,
Binding events on specific items
image thumbnails,
Scrollable Canvases and Image Thumbnails
,
Scrolling images too: PyPhoto (ahead)
moving objects,
Using Canvas Events
object construction,
Object construction
object identifiers,
Object identifiers and operations
object tags,
Canvas object tags
scrolling,
Scrolling Canvases
,
Scrolling Canvases
cat command,
Shell command limitations
cd command,
CWD and Command Lines
cenviron module,
Wrapping C Environment Calls
cgi module,
Formatting Reply Text
,
Python’s Internet Library Modules
,
Writing CGI Scripts in Python
,
Response script
,
Adding Common Input Devices
,
The Hello World Selector
,
The Hello World Selector
,
More on HTML and URL Escapes
,
Escaping URLs Embedded in HTML Code
,
Implementation Overview
,
Passing State Information in URL Link Parameters
,
Passing State Information in URL Link Parameters
,
Escaping Mail Text and Passwords in HTML
escape function,
Formatting Reply Text
,
The Hello World Selector
,
More on HTML and URL Escapes
,
Escaping URLs Embedded in HTML Code
,
Passing State Information in URL Link Parameters
,
Escaping Mail Text and Passwords in HTML
FieldStorage class,
Response script
,
Adding Common Input Devices
,
The Hello World Selector
,
Passing State Information in URL Link Parameters
functionality,
Python’s Internet Library Modules
,
Writing CGI Scripts in Python
PyMailCGI program and,
Implementation Overview
CGI scripts,
CGI Basics
,
GUIs versus the Web
,
Running a Web Server
,
Running a Web Server
,
Using Query Strings and urllib
,
Using Query Strings and urllib
,
Using Query Strings and urllib
,
Using Query Strings and urllib
,
Formatting Reply Text
,
Future directions
,
Python Internet Development Options
,
Parser decoding requirement
,
“Oh, What a Tangled Web We Weave”
,
What’s a Server-Side CGI Script?
,
The Script Behind the Curtain
,
The Script Behind the Curtain
,
Writing CGI Scripts in Python
,
Writing CGI Scripts in Python
,
Running Server-Side Examples
,
Viewing Server-Side Examples and Output
,
A First Web Page
,
HTML file permission constraints
,
A First CGI Script
,
Finding Python on remote servers
,
Installing CGI scripts
,
Installing CGI scripts
,
Installing CGI scripts
,
Adding Pictures and Generating Tables
,
Table tags
,
Adding Pictures and Generating Tables
,
Table tags
,
Adding User Interaction
,
Testing outside browsers with the module urllib.request
,
Passing parameters in URLs
,
Using Tables to Lay Out Forms
,
Debugging CGI scripts
,
Converting strings in CGI scripts
,
Debugging CGI scripts
,
Debugging CGI scripts
,
Adding Common Input Devices
,
Adding Common Input Devices
,
Changing Input Layouts
,
Keeping display and logic separate
,
Passing Parameters in Hardcoded URLs
,
Passing Parameters in Hardcoded URLs
,
Passing Parameters in Hidden Form Fields
,
Saving State Information in CGI Scripts
,
Combining Techniques
,
Using cookies in CGI scripts
,
Extensions to the CGI Model
,
The Hello World Selector
,
Checking for Missing and Invalid Inputs
,
Checking for Missing and Invalid Inputs
,
Checking for Missing and Invalid Inputs
,
Refactoring Code for Maintainability
,
Step 3: Putting It All Together—A New Reply Script
,
More on HTML and URL Escapes
,
Avoiding conflicts
,
Handling private files and errors

Other books

Amaranth by Rachael Wade
Space Plague by Zac Harrison
AmericasDarlings by Gail Bridges
Conspiracy by Lady Grace Cavendish
Fortunate Wager by Jan Jones
Master of the Desert by Susan Stephens
The Key by Whitley Strieber
Murder Spins the Wheel by Brett Halliday


readsbookonline.com Copyright 2016 - 2024