SlideShare a Scribd company logo
Download the full version of the ebook at
https://ebookfinal.com
Programming Python with CD 2nd Edition Mark
Lutz
https://ebookfinal.com/download/programming-
python-with-cd-2nd-edition-mark-lutz/
Explore and download more ebook at https://ebookfinal.com
Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.
Beginning Programming With Python For Dummies 1st Edition
John Paul Mueller
https://ebookfinal.com/download/beginning-programming-with-python-for-
dummies-1st-edition-john-paul-mueller/
ebookfinal.com
Distributed Programming with Ruby 1st Edition Mark Bates
https://ebookfinal.com/download/distributed-programming-with-ruby-1st-
edition-mark-bates/
ebookfinal.com
Introduction to Computation and Programming Using Python
with Application to Understanding Data 2nd edition Edition
Guttag
https://ebookfinal.com/download/introduction-to-computation-and-
programming-using-python-with-application-to-understanding-data-2nd-
edition-edition-guttag/
ebookfinal.com
Hacking Secret Ciphers with Python A beginner s guide to
cryptography and computer programming with Python 1st
Edition Al Sweigart
https://ebookfinal.com/download/hacking-secret-ciphers-with-python-a-
beginner-s-guide-to-cryptography-and-computer-programming-with-
python-1st-edition-al-sweigart/
ebookfinal.com
Active Grammar Level 3 with Answers and CD ROM 1st Edition
Mark Lloyd
https://ebookfinal.com/download/active-grammar-level-3-with-answers-
and-cd-rom-1st-edition-mark-lloyd/
ebookfinal.com
Black Hat Python Python Programming For Hackers And
Pentesters 2nd Edition Justin Seitz And Tim Arnold
https://ebookfinal.com/download/black-hat-python-python-programming-
for-hackers-and-pentesters-2nd-edition-justin-seitz-and-tim-arnold/
ebookfinal.com
Programming Google App Engine with Python Build and Run
Scalable Python Apps on Google s Infrastructure 1st
Edition Dan Sanderson
https://ebookfinal.com/download/programming-google-app-engine-with-
python-build-and-run-scalable-python-apps-on-google-s-
infrastructure-1st-edition-dan-sanderson/
ebookfinal.com
Expert Python Programming 1st Edition Tarek Ziade
https://ebookfinal.com/download/expert-python-programming-1st-edition-
tarek-ziade/
ebookfinal.com
Deep Learning with Python 2nd Edition François Chollet
https://ebookfinal.com/download/deep-learning-with-python-2nd-edition-
francois-chollet/
ebookfinal.com
[FREE PDF sample] Programming Python with CD 2nd Edition Mark Lutz ebooks
Programming Python with CD 2nd Edition Mark Lutz
Digital Instant Download
Author(s): Mark Lutz
ISBN(s): 9780596000851, 0596000855
Edition: 2
File Details: PDF, 6.98 MB
Year: 2001
Language: english
Programming Python, 2nd Edition, O’Reilly
IT-SC book 1
Programming Python, 2nd Edition
Mark Lutz
Publisher: O'Reilly
2nd Edition March 2001
ISBN: 0-596-00085-5, 1256 pages
Programming Python focuses on advanced applications of
Python. Endorsed by Python creator Guido van Rossum, it
demonstrates advanced Python techniques, and addresses
software design issues such as reusability and object-
oriented programming. The enclosed platform-neutral CD-
ROM (view CD-ROM content online at
http://examples.oreilly.com/python2) has book examples
and various Python-related packages, including the full
Python Version 2.0 source code distribution.
Programming Python, 2nd Edition, O’Reilly
IT-SC book 2
Enjoy the life together.
Programming Python, 2nd Edition, O’Reilly
IT-SC book 3
Foreword
Preface
"And Now for Something Completely Different . . . Again"
Signs of the Python Times
Why This Edition?
Major Changes in This Edition
Using the Examples and Demos
Conventions Used in This Book
Where to Look for Updates
Contacting O'Reilly
Acknowledgments
1. Introducing Python
1.1 "And Now for Something Completely Different"
1.2 The Life of Python
1.3 The Compulsory Features List
1.4 What's Python Good For?
1.5 What's Python Not Good For?
I: System Interfaces
2. System Tools
2.1 "The os.path to Knowledge"
2.2 Why Python Here?
2.3 System Scripting Overview
2.4 The sys Module
2.5 The os Module
2.6 Script Execution Context
2.7 Current Working Directory
2.8 Command-Line Arguments
2.9 Shell Environment Variables
2.10 Standard Streams
2.11 File Tools
2.12 Directory Tools
3. Parallel System Tools
3.1 "Telling the Monkeys What to Do"
3.2 Forking Processes
3.3 Threads
3.4 Program Exits
3.5 Interprocess Communication
3.6 Pipes
3.7 Signals
3.8 Launching Programs on Windows
3.9 Other System Tools
4. Larger System Examples I
4.1 "Splits and Joins and Alien Invasions"
4.2 Splitting and Joining Files
4.3 Generating Forward-Link Web Pages
4.4 A Regression Test Script
4.5 Packing and Unpacking Files
Programming Python, 2nd Edition, O’Reilly
IT-SC book 4
4.6 User-Friendly Program Launchers
5. Larger System Examples II
5.1 "The Greps of Wrath"
5.2 Fixing DOS Line Ends
5.3 Fixing DOS Filenames
5.4 Searching Directory Trees
5.5 Visitor: Walking Trees Generically
5.6 Copying Directory Trees
5.7 Deleting Directory Trees
5.8 Comparing Directory Trees
II: GUI Programming
6. Graphical User Interfaces
6.1 "Here's Looking at You, Kid"
6.2 Python GUI Development Options
6.3 Tkinter Overview
6.4 Climbing the GUI Learning Curve
6.5 The End of the Tutorial
6.6 Python/Tkinter for Tcl/Tk Converts
7. A Tkinter Tour, Part 1
7.1 "Widgets and Gadgets and GUIs, Oh My!"
7.2 Configuring Widget Appearance
7.3 Toplevel Windows
7.4 Dialogs
7.5 Binding Events
7.6 Message and Entry
7.7 Checkbutton, Radiobutton, and Scale
7.8 Running GUI Code Three Ways
7.9 Images
8. A Tkinter Tour, Part 2
8.1 "On Today's Menu: Spam, Spam, and Spam"
8.2 Menus
8.3 Listboxes and Scrollbars
8.4 Text
8.5 Canvas
8.6 Grids
8.7 Time Tools, Threads, and Animation
8.8 The End of the Tour
8.9 The PyDemos and PyGadgets Launchers
9. Larger GUI Examples
9.1 "Building a Better Mouse Trap"
9.2 Advanced GUI Coding Techniques
9.3 Complete Program Examples
9.4 PyEdit: A Text Editor Program/Object
9.5 PyView: An Image and Notes Slideshow
9.6 PyDraw: Painting and Moving Graphics
9.7 PyClock: An Analog/Digital Clock Widget
9.8 PyToe: A Tic-Tac-Toe Game Widget
Programming Python, 2nd Edition, O’Reilly
IT-SC book 5
9.9 Where to Go from Here
III: Internet Scripting
10. Network Scripting
10.1 "Tune in, Log on, and Drop out"
10.2 Plumbing the Internet
10.3 Socket Programming
10.4 Handling Multiple Clients
10.5 A Simple Python File Server
11. Client-Side Scripting
11.1 "Socket to Me!"
11.2 Transferring Files over the Net
11.3 Processing Internet Email
11.4 The PyMailGui Email Client
11.5 Other Client-Side Tools
12. Server-Side Scripting
12.1 "Oh What a Tangled Web We Weave"
12.2 What's a Server-Side CGI Script?
12.3 Climbing the CGI Learning Curve
12.4 The Hello World Selector
12.5 Coding for Maintainability
12.6 More on HTML and URL Escapes
12.7 Sending Files to Clients and Servers
13. Larger Web Site Examples I
13.1 "Things to Do When Visiting Chicago"
13.2 The PyMailCgi Web Site
13.3 The Root Page
13.4 Sending Mail by SMTP
13.5 Reading POP Email
13.6 Utility Modules
13.7 CGI Script Trade-offs
14. Larger Web Site Examples II
14.1 "Typos Happen"
14.2 The PyErrata Web Site
14.3 The Root Page
14.4 Browsing PyErrata Reports
14.5 Submitting PyErrata Reports
14.6 PyErrata Database Interfaces
14.7 Administrative Tools
14.8 Designing for Reuse and Growth
15. Advanced Internet Topics
15.1 "Surfing on the Shoulders of Giants"
15.2 Zope: A Web Publishing Framework
15.3 HTMLgen: Web Pages from Objects
15.4 JPython ( Jython): Python for Java
15.5 Grail: A Python-Based Web Browser
15.6 Python Restricted Execution Mode
Programming Python, 2nd Edition, O’Reilly
IT-SC book 6
15.7 XML Processing Tools
15.8 Windows Web Scripting Extensions
15.9 Python Server Pages
15.10 Rolling Your Own Servers in Python
IV: Assorted Topics
16. Databases and Persistence
16.1 "Give Me an Order of Persistence, but Hold the Pickles"
16.2 Persistence Options in Python
16.3 DBM Files
16.4 Pickled Objects
16.5 Shelve Files
16.6 SQL Database Interfaces
16.7 PyForm: A Persistent Object Viewer
17. Data Structures
17.1 "Roses Are Red, Violets Are Blue; Lists Are Mutable, and So Is Class Foo"
17.2 Implementing Stacks
17.3 Implementing Sets
17.4 Binary Search Trees
17.5 Graph Searching
17.6 Reversing Sequences
17.7 Permuting Sequences
17.8 Sorting Sequences
17.9 Data Structures Versus Python Built-ins
17.10 PyTree: A Generic Tree Object Viewer
18. Text and Language
18.1 "See Jack Hack. Hack, Jack, Hack"
18.2 Strategies for Parsing Text in Python
18.3 String Module Utilities
18.4 Regular Expression Matching
18.5 Parser Generators
18.6 Hand-Coded Parsers
18.7 PyCalc: A Calculator Program/Object
V: Integration
19. Extending Python
19.1 "I Am Lost at C"
19.2 C Extensions Overview
19.3 A Simple C Extension Module
19.4 The SWIG Integration Code Generator
19.5 Wrapping C Environment Calls
19.6 A C Extension Module String Stack
19.7 A C Extension Type String Stack
19.8 Wrapping C++ Classes with SWIG
20. Embedding Python
20.1 "Add Python. Mix Well. Repeat."
20.2 C Embedding API Overview
20.3 Basic Embedding Techniques
Programming Python, 2nd Edition, O’Reilly
IT-SC book 7
20.4 Registering Callback Handler Objects
20.5 Using Python Classes in C
20.6 ppembed: A High-Level Embedding API
20.7 Other Integration Topics
VI: The End
21. Conclusion: Python and the Development Cycle
21.1 "That's the End of the Book, Now Here's the Meaning of Life"
21.2 "Something's Wrong with the Way We Program Computers"
21.3 The "Gilligan Factor"
21.4 Doing the Right Thing
21.5 Enter Python
21.6 But What About That Bottleneck?
21.7 On Sinking the Titanic
21.8 So What's Python: The Sequel
21.9 In the Final Analysis...
21.10 Postscript to the Second Edition
A. Recent Python Changes
A.1 Major Changes in 2.0
A.2 Major Changes in 1.6
A.3 Major Changes Between 1.3 and 1.5.2
B. Pragmatics
B.1 Installing Python
B.2 Book Examples Distribution
B.3 Environment Configuration
B.4 Running Python Programs
B.5 Python Internet Resources
C. Python Versus C++
Colophon
Programming Python, 2nd Edition, O’Reilly
IT-SC book 8
Foreword
Less than five years ago, I wrote the Foreword for the first edition of Programming
Python. Since then, the book has changed about as much as the language and the
Python community! I no longer feel the need to defend Python: the statistics and
developments listed in Mark's Preface speak for themselves.
In the past year, Python has made great strides. We released Python 2.0, a big step
forward, with new standard library features such as Unicode and XML support, and
several new syntactic constructs, including augmented assignment: you can now
write x += 1 instead of x = x+1. A few people wondered what the big deal was
(answer: instead of x, imagine dict[key] or list[index]), but overall this was a big hit
with those users who were already used to augmented assignment in other
languages.
Less warm was the welcome for the extended print statement, print>>file, a
shortcut for printing to a different file object than standard output. Personally, it's
the Python 2.0 feature I use most frequently, but most people who opened their
mouths about it found it an abomination. The discussion thread on the newsgroup
berating this simple language extension was one of the longest ever-apart from the
never-ending Python versus Perl thread.
Which brings me to the next topic. (No, not Python versus Perl. There are better
places to pick a fight than a Foreword.) I mean the speed of Python's evolution, a
topic dear to the heart of the author of this book. Every time I add a feature to
Python, another patch of Mark's hair turns gray-there goes another chapter out of
date! Especially the slew of new features added to Python 2.0, which appeared just
as he was working on this second edition, made him worry: what if Python 2.1 added
as many new things? The book would be out of date as soon as it was published!
Relax, Mark. Python will continue to evolve, but I promise that I won't remove things
that are in active use! For example, there was a lot of worry about the string
module. Now that string objects have methods, the string module is mostly
redundant. I wish I could declare it obsolete (or deprecated) to encourage Python
programmers to start using string methods instead. But given that a large majority
of existing Python code-even many standard library modules-imports the string
module, this change is obviously not going to happen overnight. The first likely
opportunity to remove the string module will be when we introduce Python 3000;
and even at that point, there will probably be a string module in the backwards
compatibility library for use with old code.
Python 3000?! Yes, that's the nickname for the next generation of the Python
interpreter. The name may be considered a pun on Windows 2000, or a reference to
Mystery Science Theater 3000, a suitably Pythonesque TV show with a cult following.
When will Python 3000 be released? Not for a loooooong time-although you won't
quite have to wait until the year 3000.
Originally, Python 3000 was intended to be a complete rewrite and redesign of the
language. It would allow me to make incompatible changes in order to fix problems
with the language design that weren't solvable in a backwards compatible way. The
current plan, however, is that the necessary changes will be introduced gradually
Programming Python, 2nd Edition, O’Reilly
IT-SC book 9
into the current Python 2.x line of development, with a clear transition path that
includes a period of backwards compatibility support.
Take, for example, integer division. In line with C, Python currently defines x/y with
two integer arguments to have an integer result. In other words, 1/2 yields 0! While
most dyed-in-the-wool programmers expect this, it's a continuing source of
confusion for newbies, who make up an ever-larger fraction of the (exponentially
growing) Python user population. From a numerical perspective, it really makes more
sense for the / operator to yield the same value regardless of the type of the
operands: after all, that's what all other numeric operators do. But we can't simply
change Python so that 1/2 yields 0.5, because (like removing the string module) it
would break too much existing code. What to do?
The solution, too complex to describe here in detail, will have to span several Python
releases, and involves gradually increasing pressure on Python programmers (first
through documentation, then through deprecation warnings, and eventually through
errors) to change their code. By the way, a framework for issuing warnings will be
introduced as part of Python 2.1. Sorry, Mark!
So don't expect the announcement of the release of Python 3000 any time soon.
Instead, one day you may find that you are already using Python 3000-only it won't
be called that, but rather something like Python 2.8.7. And most of what you've
learned in this book will still apply! Still, in the meantime, references to Python 3000
will abound; just know that this is intentionally vaporware in the purest sense of the
word. Rather than worry about Python 3000, continue to use and learn more about
the Python version that you do have.
I'd like to say a few words about Python's current development model. Until early
2000, there were hundreds of contributors to Python, but essentially all contributions
had to go through my inbox. To propose a change to Python, you would mail me a
context diff, which I would apply to my work version of Python, and if I liked it, I
would check it into my CVS source tree. (CVS is a source code version management
system, and the subject of several books.) Bug reports followed the same path,
except I also ended up having to come up with the patch. Clearly, with the
increasing number of contributions, my inbox became a bottleneck. What to do?
Fortunately, Python wasn't the only open source project with this problem, and a few
smart people at VA Linux came up with a solution: SourceForge! This is a dynamic
web site with a complete set of distributed project management tools available: a
public CVS repository, mailing lists (using Mailman, a very popular Python
application!), discussion forums, bug and patch managers, and a download area, all
made available to any open source project for the asking.
We currently have a development group of 30 volunteers with SourceForge checkin
privileges, and a development mailing list comprising twice as many folks. The
privileged volunteers have all sworn their allegiance to the BDFL (Benevolent Dictator
For Life-that's me :-). Introduction of major new features is regulated via a
lightweight system of proposals and feedback called Python Enhancement Proposals
(PEPs). Our PEP system proved so successful that it was copied almost verbatim by
the Tcl community when they made a similar transition from Cathedral to Bazaar.
Programming Python, 2nd Edition, O’Reilly
IT-SC book 10
So, it is with confidence in Python's future that I give the floor to Mark Lutz.
Excellent job, Mark. And to finish with my favorite Monty Python quote: Take it
away, Eric, the orchestra leader!
Guido van Rossum
Reston, Virginia, January 2001
"And Now for Something Completely Different . . .
Again"
The first edition of this book was one of the first to present the Python language.
This second edition is an almost completely new advanced Python topics book,
designed to be a follow-up to the core language material in Learning Python and
supplemented by the reference material in Python Pocket Reference.
That is, this edition is focused on ways to use Python, rather than on the language
itself. Python development concepts are explored along the way -- in fact, they really
become meaningful only in the context of larger examples like those in this edition.
But in general, this text now assumes that you already have at least a passing
acquaintance with Python language fundamentals, and moves on to present the rest
of the Python story.
In this preface, I'll explain some of the rationales for this major rewrite, describe the
structure of this edition in more detail, and give a brief overview of how to use the
Python programs shipped on the enclosed CD-ROM (view CD-ROM content online at
http://examples.oreilly.com/python2). First of all, though, a history lesson is in
order.
Signs of the Python Times
It's been an exciting five years in the Python world. Since I wrote the first edition of
this book between 1995 and 1996, Python has grown from a new kid on the scripting
languages block to an established and widely used tool in companies around the
world. Although measuring the popularity of an open source (http://opensource.org)
and freely distributed tool such as Python is not always easy, most statistics
available reveal exponential growth in Python's popularity over the last five years.
Among the most recent signs of Python's explosive growth:
Books
As I write this in 2001, there are now over a dozen Python books on the market,
with almost that many more on the way (in 1995 there were none). Some of these
books are focused on a particular domain (e.g., Windows), and some are available in
German, French, and Japanese language editions.
Users
In 1999, one leading industry observer suggested that there were as many as
300,000 Python users worldwide, based on various statistics. Other estimates are
more optimistic still. In early 2000, for instance, the Python web site was already on
track to service 500,000 new Python interpreter downloads by year end (in addition
Programming Python, 2nd Edition, O’Reilly
IT-SC book 11
to other Python distribution mediums); this figure is likely closer to the true user-
base size as I write this book.
Press
Python is now regularly featured in industry publications. In fact, since 1995, Python
creator Guido van Rossum has appeared on the cover of prominent tech magazines
such as Linux Journal and Dr. Dobb's Journal; the latter publication gave him a
programming excellence award for Python.[1]
[1]
As I was writing this book, Linux Journal also published a special Python
supplement with their May 2000 edition -- the cover of which, of course, featured a
naked man seated outdoors in front of a computer desk instead of a piano. If you
don't know why that is funny, you need to watch a few reruns from Python's
namesake, the Monty Python television series (consider it a first suggested exercise).
I'll say more about the implications of Python's name in the first chapter.
Applications
Real companies have adopted Python for real products. It has shown up animating
the latest Star Wars movie (Industrial Light & Magic), serving up maps and
directories on the Internet (Yahoo), guiding users through Linux operating system
installation (Red Hat), testing chips and boards (Intel), managing Internet discussion
forums (Egroups), scripting online games (Origin), talking to CORBA frameworks
(TCSI), implementing web site tools (Digital Creations' Zope), scripting wireless
products (Agilent), and much more.[2]
[2]
See http://www.python.org for more details. Some companies don't disclose their
Python use for competitive reasons, though many eventually become known when
one of their web pages crashes and displays a Python error message in a browser.
Hewlett Packard is generally counted among companies thus "outed."
Newsgroup
User traffic on the main Python Internet newsgroup, comp.lang.python, has risen
dramatically too. For instance, according to eGroups (see
http://www.egroups.com/group/python-list), there were 76 articles posted on that
list in January 1994, and 2678 in January 2000 -- a 35-fold increase. Recent months
have been busier still (e.g., 4226 articles during June, 2000 alone -- roughly 140 per
day), and growth has been constant since the list's inception. This, and all other
user-base figures cited in this preface, are likely to have increased by the time you
read this text. But even at current traffic rates, Python forums are easily busy
enough to consume the full-time attention of anyone with full-time attention to burn.
Conferences
There are now two annual Python conferences, one of which is hosted by O'Reilly &
Associates. Attendance at Python conferences has roughly doubled in size every
year. An annual Python Day is now also held in Europe.
Group therapy
Regional Python user groups have begun springing up in numerous sites in the U.S.
and abroad, including Oregon, San Francisco, Washington D.C., Colorado, Italy,
Programming Python, 2nd Edition, O’Reilly
IT-SC book 12
Korea, and England. Such groups work on Python-related enhancements, organize
Python events, and more.
Domains
Python has grown to embrace both Microsoft Windows developers, with new support
for COM and Active Scripting, as well as Java developers, with the new JPython
(renamed "Jython") Java-based implementation of the language. As we'll see in this
edition, the new COM support allows Python scripts to be both component server and
client; Active Scripting allows Python code to be embedded in HTML web page code
and run on either client or server; and JPython compiles Python scripts to Java
Virtual Machine code so that they can be run in Java-aware systems and can
seamlessly integrate Java class libraries for use by Python code. As an open source
tool for simplifying web site construction, the Python-based Zope web application
framework discussed in this edition has also begun capturing the attention of
webmasters and CGI coders.
Services
On the pragmatics front, commercial support, consulting, prepackaged distributions,
and professional training for Python are now readily available from a variety of
sources. For instance, the Python interpreter can be obtained on CDs and packages
sold by various companies (including Walnut Creek, Dr. Dobb's Journal, and
ActiveState), and Python usually comes prebuilt and free with most Linux operating
system distributions.
Jobs
It's now possible to make money as a Python programmer (without having to resort
to writing large, seminal books). As I write this book, the Python job board at
http://www.python.org/Jobs.html lists some 60 companies seeking Python
programmers in the U.S. and abroad. Searches for Python at popular employment
sites yield even more hits -- for instance, 285 Python-related jobs on Monster.com,
and 369 on dice.com. Not that anyone should switch jobs, of course, but it's nice to
know that you can now make a living by applying a language that also happens to be
a pleasure to use.
Tools
Python has also played host to numerous tools development efforts. Among the most
prominent as I write these words: the Software Carpentry project, which is
developing new core software tools in Python; ActiveState, which ison the verge of
releasing a set of Windows and Linux-focused Python development products; and
PythonWare, which is about to release an integrated Python development
environment and GUI builder.
Compilers
As I write this preface, ActiveState has also announced a new Python compiler for
the Microsoft .NET framework and C# language environment -- a true Python
compiler and independent implementation of the Python language that generates
DLL and EXE files, allows Python code to be developed under Visual Studio, and
provides seamless .NET integration for Python scripts. It promises to be a third
Programming Python, 2nd Edition, O’Reilly
IT-SC book 13
implementation of Python, along with the standard C-based Python, and the JPython
Java-based system.
Education
Python has also begun attracting the attention of educators, many of whom see
Python as a "Pascal of the 2000s" -- an ideal language for teaching programming,
due to its simplicity and structure. Part of this appeal was spawned by Guido van
Rossum's proposed Computer Programming for Everybody (CP4E) project, aimed at
making Python the language of choice for first-time programmers worldwide. At this
writing the future of CP4E itself is uncertain, but a Python special interest group
(SIG) has been formed to address education-related topics. Regardless of any
particular initiative's outcome, Python promises to make programming more
accessible to the masses of people who will surely soon grow tired of clicking
preprogrammed links, as they evolve from computer users to computer scripters.
In other words, it's not 1995 anymore. Much of the preceding list was unimaginable
when the first edition of this book was conceived. Naturally, this list is doomed to be
out of date even before this book hits the shelves, but it is nonetheless
representative of the sorts of milestones that have occurred over the last five years,
and will continue to occur for years to come. As a language optimized to address the
productivity demands of today's software world, Python's best is undoubtedly yet to
come.
So What's Python?
If you are looking for a concise definition of this book's topic, try this:
Python is a general-purpose open source computer programming language,
optimized for quality, productivity, portability, and integration. It is used by
hundreds of thousands of developers around the world, in areas such as
Internet scripting, systems programming, user interfaces, product
customization, and more.
Among other things, Python sports object-oriented programming (OOP); a
remarkably simple, readable, and maintainable syntax; integration with C
components; and a vast collection of precoded interfaces and utilities.
Although general-purpose, Python is often called a scripting language
because it makes it easy to utilize and direct other software components.
Perhaps Python's best asset is simply that it makes software development
more rapid and enjoyable. To truly understand how, read on.
Programming Python, 2nd Edition, O’Reilly
IT-SC book 14
Why This Edition?
One consequence of the growing popularity of Python has been an influx of new
users, programming styles, and applications, all of which have conspired to make
parts of the first edition of this book prime for updates. Python itself has changed in
smaller ways, but important extensions have simplified various aspects of Python
development and merit new coverage.
Perhaps most relevant for this edition is that the Python "audience" has changed.
Over the last five years, Python has transitioned from an emerging language of
interest primarily to pioneers to a widely accepted tool used by programmers for
day-to-day development tasks. This edition has been refocused for this new Python
audience. You will find that it is now more of a nuts-and-bolts text, geared less
toward introducing and popularizing the language than to showing how to apply it for
realistically scaled programming tasks.
Because of the breadth of change, this edition is something of an entirely new book.
To readers who enjoyed the first edition, I would like to express my gratitude, and I
hope you will find the same spirit in this second edition. Although this is a major
rewrite, I have tried to retain as much of the original book's material and flavor as
possible (especially the jokes :-).
Since writing the first edition five years ago, I have also had the opportunity to teach
Python classes in the U.S. and abroad, and some of the new examples reflect
feedback garnered from these training sessions. The new application domain
examples reflect common interests and queries of both myself and my students.
Teaching Python to workers in the trenches, many of whom are now compelled to
use Python on the job, also inspired a new level of practicality that you will notice in
this edition's examples and topics.
Other new examples are simply the result of my having fun programming Python.
Yes, fun; I firmly believe that one of Python's greatest intangible assets is its ability
both to kindle the excitement of programming among newcomers, and to rekindle
the excitement among those who have toiled for years with more demanding tools.
As we will see in this edition, Python makes it incredibly easy to play with advanced
but practical tools such as threads, sockets, GUIs, web sites, and OOP -- areas that
can be both tedious and daunting in traditional compiled languages like C and C++.
Frankly, even after eight years as a bona fide Pythonista, I still find programming
most enjoyable when it is done in Python. Python is a wildly productive language,
and witnessing its application first-hand is an aesthetic delight. I hope this edition, as
much as the first, will demonstrate how to reap Python's productivity benefits and
communicate some of the satisfaction and excitement found in a rapid-development
tool such as Python.
Major Changes in This Edition
The best way to get a feel for any book is to read it, of course. But especially for
people who are familiar with the first edition, the next few sections go into more
detail about what is new in this edition.
Programming Python, 2nd Edition, O’Reilly
IT-SC book 15
It's Been Updated for Python 2.0
This edition has been updated for Python 2.0, and the Graphical User Interface (GUI)
material has been updated for Tk versions 8.0 or later. Technically, this update was
begun under Python 1.5.2, but all examples were revisited for 2.0 before publication.
For the trivia buffs among you: release 2.0 was the first Python release following
Guido's move to BeOpen, while 1.6 was the last release from Guido's prior employer,
CNRI. Just before I finished this book's final draft and after the 2.0 release, Guido
and the core Python development team moved from BeOpen to Digital Creations,
home of the Zope web application construction kit, but this move is independent of
Python releases (see Chapter 1, for more details).
Release 2.0 introduces a few language extensions, but 2.0 and 1.6 are similar in
content, and the updates just add a handful of features. The examples in this book
should generally work with later Python releases. Remarkably, almost all examples in
the first edition still work five years later, with the latest Python releases; those that
didn't work required only small fixes (e.g., GUI call formats and C API interfaces).
On the other hand, although the core language hasn't changed much since the first
edition, a number of new constructs have been added, and we'll apply them all here.
Among these new Python features: module packages, class exceptions, pseudo-
private class attributes, unicode strings, the new regular expression module, new
Tkinter features such as the grid manager, standard dialogs, and top-level menus,
and so on. A new appendix summarizes all of the major changes in Python between
the first and second editions of this book.
In addition to the language changes, this book presents new Python tools and
applications that have emerged in recent years. Among them: the IDLE
programming interface, the JPython (a.k.a. "Jython") compiler, Active Scripting and
COM extensions, the Zope web framework, Python Server Pages (PSP), restricted
execution mode, the HTMLgen and SWIG code generators, thread support, CGI and
Internet protocol modules, and more (it's been a busy five years). Such applications
are the heart and soul of this second edition.
It's Been Refocused for a More Advanced Audience
This edition presents Python programming by advanced examples. Becoming
proficient in Python involves two distinct tasks: learning the core language itself, and
then learning how to apply it in applications. This edition addresses the latter (and
larger) of these tasks by presenting Python libraries, tools, and programming
techniques. Since this is a very different focus, I should say a few words about its
rationale here.
Because there were no other Python books on the horizon at the time, the first
edition was written to appeal to many audiences at once -- beginners and gurus
alike. Since then, another O'Reilly book, Learning Python, has been developed to
address the needs of beginners, and Python Pocket Reference was published for
readers seeking a short Python reference. As a result, the core language
introductory-level material and the original reference appendixes have been removed
from this book.
Programming Python, 2nd Edition, O’Reilly
IT-SC book 16
Learning Python introduces the core language -- its syntax, datatypes, and so on --
using intentionally simplistic examples. Many have found it to be ideal for learning
the language itself, but Python can become even more interesting once you master
the basic syntax and can write simple examples at the interactive prompt. Very soon
after you've learned how to slice a list, you find yourself wanting to do real things,
like writing scripts to compare file directories, responding to user requests on the
Internet, displaying images in a window, reading email, and so on. Most of the day-
to-day action is in applying the language, not the language itself.
Programming Python focuses on the "everything else" of Python development. It
covers libraries and tools beyond the core language, which become paramount when
you begin writing real applications. It also addresses larger software design issues
such as reusability and OOP, which can only be illustrated in the context of
realistically scaled programs. In other words, Programming Python, especially in this
new edition, is designed to pick up where Learning Python leaves off.
Therefore, if you find this book too advanced, I encourage you to read Learning
Python as a prelude to this text, and return here for the rest of the story once you've
mastered the basics. Unless you already have substantial programming experience,
this edition might serve you best as a second Python text.
It Covers New Topics
Most of the changes in this edition were made to accommodate new topics. There
are new chapters and sections on Internet scripting, CGI scripts, operating system
interfaces, the SWIG integration code generator, advanced Tkinter topics, the
HTMLgen web page generator, JPython, threads, restricted execution mode, and
more. You should consult the Table of Contents for the full scoop, but here are some
of the new topics and structural changes you'll find in this edition:
Topics
The Internet, systems programming, Tkinter GUIs, and C integration domains get
much more attention, and are arguably now the main focus of this text. For instance,
you'll find six new chapters on Internet scripting, covering client-side tools, server-
side scripts and web sites, and advanced Internet topics and systems. Four new
chapters address systems topics: threads, directory processing, program launching,
and so on. And the GUI material has also grown from one chapter to a much more
complete four-chapter presentation, and now covers all widgets (including text and
canvas), as well as new grid, menu, and dialog support.
C integration
The C extending and embedding chapters have been expanded to cover new topics
such as SWIG (the way to mix Python with C/C++ libraries today) and present new
mixed-mode examples such as callback dispatch (extending plus embedding). C
integration is at the heart of many Python systems, but the examples in this domain
are inevitably complex, and involve large C programs that are only useful to C users.
In deference to readers who don't need to code C integrations, this material is now
isolated at the end of the text. Some of the C code listings are gone as well -- to
reduce page count, I have opted instead to point readers to C source files on the
enclosed CD-ROM where possible (see http://examples.oreilly.com/python2).
Programming Python, 2nd Edition, O’Reilly
IT-SC book 17
Although later chapters build upon material in earlier chapters, topics in this edition
are covered fairly independently, and are associated by book parts. Because of that,
it's not too much of a stretch to consider this edition to be akin to four or five books
in one. Its top-level structure underscores its application topics focus:
Preface (you are here)
Chapter 1
Part I
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Part II
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Part III
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Part IV
Chapter 16
Chapter 17
Chapter 18
Part V
Chapter 19
Chapter 20
Part VI
Chapter 21
Appendix A
Appendix B
Appendix C
Two notes here: First of all, don't let these titles fool you -- although most have to
do with application topics, Python language features and general design concepts are
still explored along the way, in the context of real-world goals. Second, readers who
use Python as a standalone tool can safely skip the integration chapters, though I
still recommend a quick glance. C programming isn't nearly as fun or easy as Python
programming. Yet because integration is central to Python's role as a scripting tool, a
cursory understanding can be useful, regardless of whether you do integrating,
scripting, or both.
First edition readers will notice that most of this material is new, and even chapters
with old titles contain largely new material. Noticeably absent in this edition are the
original Sneak Preview, Mini Reference, Tutorial Appendix, and all of the old Part II --
a reflection of the new focus and intended readership.
It's More Example-Oriented
Programming Python, 2nd Edition, O’Reilly
IT-SC book 18
This book is largely about its examples. In this edition, old examples have been
extended to become more realistic (e.g., PyForm and PyCalc), and new examples
have been added throughout. Among the major examples, you'll find:
PyEdit
A Python/Tk text file editor object and program
PyView
A photo image and note-file slideshow
PyDraw
A paint program for drawing and moving image objects
PyTree
A tree data structure drawing program
PyClock
A Python/Tk analog and digital clock widget
PyToe
An AI-powered graphical tic-tac-toe program
PyForm
A persistent object table browser
PyCalc
A calculator widget in Python/Tk
PyMail
A Python/Tk POP and SMTP email client
PyFtp
A simple Python/Tk file-transfer GUI
PyErrata
A web-based error report system
PyMailCgi
A web-based email interface
Programming Python, 2nd Edition, O’Reilly
IT-SC book 19
There are also new mixed-mode C integration examples (e.g., callback registration
and class object processing), SWIG examples (with and without "shadow" classes for
C++), more Internet examples (FTP upload and download scripts, NNTP and HTTP
examples, email tools, and new socket and select module examples), many new
examples of Python threads, and new coverage of JPython, HTMLgen, Zope, Active
Scripting, COM, and Python database interfaces. Many of the new examples are
somewhat advanced, but of course this is now a somewhat advanced text.
In addition, the old Python/C embedding API (now called ppembed ) has been
extended to support precompiling strings to bytecode, and the original calculator
example (now called PyCalc) has been beefed up to support keyboard entry, history
lists, colors, and more.
In fact, the new book examples tree distributed on this edition's CD-ROM (see
http://examples.oreilly.com/python2) is itself a fairly sophisticated Python software
system, and the examples within it have been upgraded structurally in a number of
important ways:
Examples tree
The entire examples distribution has been organized as one big Python module
package to facilitate cross-directory imports and avoid name-clashes with other
Python code installed on your computer. Using directory paths in import statements
(instead of a complex PYTHONPATH) also tends to make it easier to tell where
modules come from. Moreover, you now need to add only one directory to your
PYTHONPATH search-path setting for the entire book examples tree: the directory
containing the PP2E examples root directory. To reuse code in this book within your
own applications, simply import through the PP2E package root (e.g., from
PP2E.Launcher import which).
Example filenames
Module names are generally much less cryptic now. I punted on 8.3 DOS
compatibility long ago, and use more descriptive filenames. I've also fixed some old
all-uppercase filenames, which were a last vestige of MS-DOS.
Example titles
Labels of example listings now give the full directory pathname of the example's
source file to help you locate it in the examples distribution. For instance, an
example source-code file whose name is given as Example N-M:
PP2EInternetFtpsousa.py refers to the file sousa.py in the PP2EInternetFtp
subdirectory of the examples distribution directory.[4]
[4]
The "examples distribution directory" is the directory containing the top-level PP2E
directory of the book examples tree. On the CD (see
http://examples.oreilly.com/python2), it's the topmost Examples directory; if you've
copied the examples to your machine, it's wherever you copied (or unpacked) the
PP2E root directory. You can run most of the examples from the CD directly, but
you'll want to copy them to your hard drive to make changes, and to allow Python to
save .pyc compiled bytecode files for quicker startups.
Example command lines
Programming Python, 2nd Edition, O’Reilly
IT-SC book 20
Similarly, when a command line is shown typed after a prompt such as
C:...PP2ESystemStreams>, for example, it is really to be typed in the
PP2ESystemStreams subdirectory in your examples tree. Unix and Linux users:
please think / when you see  in filename paths (my official excuse for which is
outlined in the next section).
Example launchers
Because it's just plain fun to click on things right away, there are new self-
configuring demo launcher programs (described later in this preface in Section
P.5.1), to give you a quick look at Python scripts in action with minimal configuration
requirements. You can generally run them straight off the book's CD without setting
any shell variables first.
It's More Platform-Neutral
Except for some C integration examples, the majority of the programs in this edition
were developed on my Windows 98 laptop, with an eye toward portability to Linux
and other platforms. In fact, some of the examples were born of my desire to
provide portable Python equivalents of tools missing on Windows (e.g., file splitters).
When programs are shown in action, it's usually on Windows; they are demonstrated
on the Red Hat Linux 6.x platform only if they exercise Unix-specific interfaces.
This is not a political statement at all -- I like Linux too. It's mostly a function of the
fact that I wrote this book with MS Word; when time is tight, it's more convenient to
run scripts on the same platform as your publishing tools than to frequently reboot
into Linux. Luckily, because Python has now become so portable to both Windows
and Linux, the underlying operating system is less of a concern to Python developers
than it once was. Python, its libraries, and its Tkinter GUI framework all work
extremely well on both platforms today.
Because I'm not a politician, though, I've tried to make the examples as platform-
neutral as possible, and point out platform-specific issues along the way. Generally
speaking, most of the scripts should work on common Python platforms unchanged.
For instance, all the GUI examples were tested on both Windows (98, 95) and Linux
(KDE, Gnome), and most of the command-line and thread examples were developed
on Windows but work on Linux too. Because Python's system interfaces are generally
built to be portable, this is easier than it may sound.
On the other hand, this book does delve into platform-specific topics where
appropriate. There is new coverage of many Windows-specific topics -- Active
Scripting, COM, program launch options, and so on. Linux and Unix readers will also
find material geared towards their platforms -- forks, pipes, and the like. There is
also new discussion of ways to edit and run Python programs on most major
platforms.
The one place where readers may still catch a glimpse of platform biases is in the
Python/C integration examples. For simplicity, the C compilation details covered in
this text are still somewhat Unix/Linux-biased. One can at least make a reasonable
case for such a focus -- not only does Linux come with C compilers for free, but its
development environment grew up around that language. On Windows, the C
extension code shown in this book will work, but you may need to use different build
Programming Python, 2nd Edition, O’Reilly
IT-SC book 21
procedures that vary per Windows compiler. O'Reilly has published an outstanding
text, Python Programming on Win32, that covers Windows-specific Python topics like
this, and should help address some of the disparity here. If you do Windows-specific
programming, please see that book for all Windows details skipped here.
But It's Still Not a Reference Manual
Please note that this edition, like the first, is still more of a tutorial than a
reference manual (despite sharing a title pattern with a popular Perl
reference text). This book aims to teach, not document. You can use its
table of contents and index to track down specifics, and the new structure
helps make this easy to do. But this edition is still designed to be used in
conjunction with, rather than to replace, Python reference manuals. Because
Python's manuals are free, well-written, available online, and change
frequently, it would be folly to devote space to parroting their contents. For
an exhaustive list of all tools available in the Python system, consult other
books (e.g., O'Reilly's Python Pocket Reference) or the standard manuals at
Python's web site and on this book's CD-ROM (see
http://examples.oreilly.com/python2).
Using the Examples and Demos
I want to briefly describe how to use the book's examples here. In general, though,
please see the following text files in the examples distribution directory for more
details:
README-root.txt: package structure notes
PP2EREADME-PP2E.txt: general usage notes
PP2EConfigsetup-pp.bat: Windows configuration
PP2EConfigsetup-pp.csh: Unix and Linux configuration
Of these, the README-PP2E.txt file is the most informative, and the PP2EConfig
directory contains all configuration file examples. I give an overview here, but the
files listed give a complete description.
The Short Story
If you want to see some Python examples right away, do this:
Install Python from the book's CD-ROM (see http://examples.oreilly.com/python2),
unless it is already installed on your computer. On Windows, click on the name of the
self-installer program on the CD and do a default install (say "yes" or "next" to every
prompt). On other systems, see the README file (the gzipped source distribution on
the CD can be used to build Python locally).
Programming Python, 2nd Edition, O’Reilly
IT-SC book 22
Start one of the following self-configuring scripts located in the top-level
ExamplesPP2E directory on the CD (see http://examples.oreilly.com/python2).
Either click on their icons in your file explorer, or run them from your system prompt
(e.g., DOS console box, Linux Xterm) using command lines of the form python
script-name (you may need to use the full path to python if it's not on your
system):
Launch_PyDemos.pyw: the main Python/Tk demo launcher toolbar
Launch_PyGadgets_bar.pyw: a Python/Tk utilities launcher bar
Launch_PyGadgets.py: starts standard Python/Tk utilities
LaunchBrowser.py: opens web examples index in web browser
The Launch_* scripts start Python programs portably[5]
and require only that Python
be installed -- you don't need to set environment variables first or tweak the
included PP2EConfig setup files to run them. LaunchBrowser will work if it can find a
web browser on your machine, even if you don't have an Internet link (though some
Internet examples won't work completely without a live link).
[5]
All the demo and launcher scripts are written portably but are known to work only on
Windows 95/98 and Linux at the time of this writing; they may require minor changes on
other platforms. Apologies if you're using a platform that I could not test: Tk runs on
Windows, X11, and Macs; Python itself runs on everything from handheld PDAs to
mainframes; and my advance for writing this book wasn't as big as you may think.
If installing Python isn't an option, you can still run a few Python web demos by
visiting http://starship.python.net/~lutz/PyInternetDemos.html with your browser.
Because these examples execute scripts on a server, they tend to work best when
run live from this site, rather than from the book's CD.
The Details
To help organize the new examples, I've provided a demo launcher program,
PyDemos.pyw, in the top-level PP2E directory of the examples distribution. Figure P-
1 shows PyDemos in action on Windows after pressing a few buttons. The launcher
bar appears on the left of the screen; with it, you can run most of the major
graphical examples in the book with a mouse click. The demo launcher bar can also
be used to start major Internet book examples if a browser can be located on your
machine (see the following launcher description).
Besides launching demos, the PyDemos source code provides pointers to major
examples in the distribution; see its source code for details. You'll also find Linux
automated build scripts for the Python/C integration examples in the top-level
examples directory, which serve as indexes to major C examples.
Figure P-1. The PyDemos launcher with pop-ups and demos (Guido's photo
reprinted with permission from Dr. Dobb's Journal)
Programming Python, 2nd Edition, O’Reilly
IT-SC book 23
I've also included a top-level program called PyGadgets.py, and its relative
PyGadgets_bar.pyw, to launch some of the more useful GUI book examples for real
use instead of demonstration (mostly, the programs I use often; configure as
desired). Figure P-2 shows what PyGadgets_bar looks like on Windows, along with a
few of the utilities that its buttons can launch. All of the programs are presented in
this book and included in the examples distribution. Most gadgets require a Python
with Tkinter support, but that is the default configuration for the standard Windows
port on the book's CD (see http://examples.oreilly.com/python2).
Figure P-2. The PyGadgets utilities launcher bar, with gadgets
Programming Python, 2nd Edition, O’Reilly
IT-SC book 24
To run the files listed in the prior paragraph directly, you'll need to set up your
Python module search path (see the top-level PP2E/Config/setup* files for hints).
But if you want to run a collection of Python demos from the book and don't want to
bother with setting up your environment first, simply run the self-launching utility
scripts in the PP2E directory instead: Launch_PyDemos.pyw, Launch_PyGadgets.py,
and Launch_PyGadgets_bar.pyw.
These Python-coded launcher scripts assume Python has already been installed, but
will automatically find your Python executable and the book examples distribution,
and set up your Python module and system search paths as needed to run the
demos. You can probably run these launch scripts by simply clicking on their names
in a file explorer, and you should also be able to run them directly from the book's
CD-ROM (see http://examples.oreilly.com/python2). See the comments at the top of
Launcher.py for more details (or read about these scripts in Chapter 4 ).
Many of the browser-based Internet examples from the book can also be found
online at http://starship.python.net/~lutz/PyInternetDemos.html, where you can
test-drive a few Python Internet scripting examples. Because these examples run in
your web browser, they can be tested even if you haven't installed Python (or
Python's Tk support) on your machine.
The PyDemos program also attempts to launch a web browser on the major example
web pages by starting the LaunchBrowser.py script in the examples root directory.
That script tries to find a usable browser on your machine, with generally good
results; see the script for more details if it fails. Provided LaunchBrowser can find a
browser on your machine, some demo buttons will pop up web pages automatically,
whether you have a live Internet connection or not (if not, you'll see local files in
your browser). Figure P-3 shows what the PyInternetDemos page looks like under
Internet Explorer on Windows.
Programming Python, 2nd Edition, O’Reilly
IT-SC book 25
Figure P-3. The PyInternetDemos web page
Of special interest, the getfile.html link on this page allows you to view the source
code of any other file on the book's site -- HTML code, Python CGI scripts, and so on;
see Chapter 12 for details. To summarize, here is what you'll find in the top-level
PP2E directory of the book's examples distribution:
PyDemos.pyw
Button bar for starting major GUI and Internet examples
PyGadgets.py
Starts programs in non-demo mode for regular use
PyGadgets_bar.pyw
Button bar for starting PyGadgets on demand
Launch_*.py*
Starts PyDemos and PyGadgets programs using Launcher.py to autoconfigure search
paths (run these for a quick look)
Launcher.py
Programming Python, 2nd Edition, O’Reilly
IT-SC book 26
Used to start programs without environment settings -- finds Python, sets
PYTHONPATH, spawns Python programs
LaunchBrowser.py
Opens example web pages with an automatically located web browser, either live off
the Net or by opening local web page files; if started directly, opens the
PyInternetDemos index page
There are also subdirectories for examples from each major topic area of the book.
In addition, the top-level PP2EPyTools directory contains Python-coded command-
line utilities for converting line-feeds in all example text files to DOS or Unix format
(useful if they look odd in your text editor), making all example files writable (useful
if you drag-and-drop off the book's CD (see http://examples.oreilly.com/python2),
deleting old .pyc bytecode files in the tree, and more. Again, see the distribution's
README-PP2E.txt file for more details on all example issues.
Where It's At
The book examples distribution can be found on the CD-ROM that accompanies this
book. See the CD's top-level README file for usage details, or browse the CD's
example root directory in your favorite file explorer for a quick tour.
In addition to the book examples, the CD also contains various Python-related
packages, including a full Windows self-installer program with Python and Tk support
(double-click and say "yes" at all prompts to install), the full Python source code
distribution (unpack and compile on your machine), and Python's standard
documentation set in HTML form (click to view in your web browser).
Extra open source packages such as the latest releases (at the time of publication) of
the SWIG code generator and JPython are also included, but you can always find up-
to-date releases of Python and other packages at Python's web site,
http://www.python.org.
Conventions Used in This Book
The following font conventions are used in this book:
Italic
Used for file and directory names, commands, to emphasize new terms when first
introduced, and for some comments within code sections
Constant width
Used for code listings and to designate modules, methods, options, classes,
functions, statements, programs, objects, and HTML tags
Constant width bold
Used in code sections to show user input
Programming Python, 2nd Edition, O’Reilly
IT-SC book 27
Constant width italic
Used to mark replaceables
This icon designates a note related to the nearby text.
This icon designates a warning related to the nearby text.
Where to Look for Updates
As before, updates, corrections, and supplements for this book will be maintained at
the author's web site, http://www.rmi.net/~lutz. Look for the second edition's link on
that page for all supplemental information related to this version of the book. As for
the first edition, I will also be maintaining a log on this web site of Python changes
over time, which you should consider a supplement to this text.
Beginning with this edition, I am making available a user-driven book errata
reporting system on the World Wide Web, at this site:
http://starship.python.net/~lutz/PyErrata/pyerrata.html
There, you'll find forms for submitting book problem reports and comments, as well
as viewing the report database by various sort keys. Reports are stored in a publicly
browsable database by default, but an option lets you email them privately instead.
The PyErrata system also happens to be written in Python, and is an example
presented and shipped with this book; see Chapter 14. Figure P-4 shows what the
root page of PyErrata looks like.
Figure P-4. The PyErrata book updates site
Programming Python, 2nd Edition, O’Reilly
IT-SC book 28
If any of these addresses stop working over time, these pages might also be
accessible from O'Reilly's web site, http://www.oreilly.com.[6]
I'm still happy to
receive direct emails from readers, of course, but the PyErrata site will hopefully
streamline the report-posting process.
[6]
O'Reilly has an errata report system at their site too, and you should consider the union of
these two lists to be the official word on book bugs and updates.
Contacting O'Reilly
You can also address comments and questions about this book to the publisher:
O'Reilly & Associates, Inc.
101 Morris Street
Sebastopol, CA 95472
(800) 998-9938 (in the United States or Canada)
(707) 829-0515 (international/local)
(707) 829-0104 (fax)
O'Reilly has a web page for this book, which lists errata, examples, and any
additional information. You can access this page at:
http://www.oreilly.com/catalog/python2/
To comment or ask technical questions about this book, send email to:
bookquestions@oreilly.com
Programming Python, 2nd Edition, O’Reilly
IT-SC book 29
For more information about books, conferences, software, Resource Centers, and the
O'Reilly Network, see the O'Reilly web site at:
http://www.oreilly.com
Programming Python, 2nd Edition, O’Reilly
IT-SC book 30
Acknowledgments
In addition to the people I mentioned in the first edition's preface, I'd like to extend
appreciation to a few of the people who helped in some way during this second
edition project:
To this book's first editor, Frank Willison, for overseeing this update, as well as
championing the Python cause at O'Reilly and beyond. To this book's later editor,
Laura Lewin, for picking up the ball and lighting necessary fires.
To Python creator Guido van Rossum for making this stuff fun again.
To the people who took part in a review of an early draft of this edition: Eric
Raymond, Mark Hammond, David Ascher, Tim Peters, and Dave Beazley.
To Tim O'Reilly and the staff of O'Reilly & Associates, both for producing this book,
and supporting open source software in general.
To the Python community at large, for diligence, hard work, and humor -- both in the
early years and today. We've come far, but to steal a line from the 1970s: You aint'
seen nothin' yet.
And to the students of the many Python classes I have taught, as well as the scores
of readers who took the time to send me comments about the first edition; your
opinions helped shape this update.
Finally, a few personal notes of thanks. To my children, Michael, Samantha, and
Roxanne, for purpose. If they are at all representative of their generation, the future
of our species seems in very good hands. You'll have to pardon me if that sounds
proud; with kids like mine, it's impossible to feel otherwise.
And most of all to Lisa, the mother of those amazing kids. I owe her my largest debt
of gratitude, for everything from enduring my flights from reality while writing books
like this, to keeping me out of jail in our youth. No matter what the future may hold,
I'll always be glad that something threw us together two decades ago.
Mark Lutz
November 2000
Somewhere in Colorado
"When Billy Goes Down, He's Going
Down Fast"
The last five years have also been host to the rise of the open source
movement. Open source refers to software that is distributed free of charge
with full source code, and is usually the product of many developers working
in a loosely knit collaborative fashion. Python, the Linux operating system,
and many other tools such as Perl and the Apache web server fall into this
category. Partly because of its challenge to the dominance of mega-
companies, the open source movement has quickly spread through society
Programming Python, 2nd Edition, O’Reilly
IT-SC book 31
in profound ways.
Let me tell you about an event that recently underscored the scope of this
movement's impact on me. To understand this story, you first need to know
that as I was writing this book, I lived in a small town in Colorado not
generally known for being on the cutting edge of technological innovation.
To put that more colorfully, it's the sort of place that is sometimes called a
"cowboy town."
I was at a small local bookstore hunting for the latest Linux Journal. After
browsing for a while, I found a copy and walked it to the checkout. Behind
the counter were two clerks who looked as if they might be more at home at
a rodeo than behind the counter of this establishment. The older of the two
sported gray hair, a moustache, and the well-worn skin of a person
accustomed to life on a ranch. Both wore obligatory baseball caps. Cowboys,
to be sure.
As I put the magazine down, the elder clerk looked up for a moment, and
said, in classic cowboy drawl, "Linux, huh? I tell you what, when Billy goes
down, he's goin' down fast!" Of course, this was in reference to the widely
publicized competition between Linux and Bill Gates' Microsoft Windows,
spurred by the open source movement.
Now, in another time and place, these two might have instead been
discussing livestock and firearms over strong cups of coffee. Yet somehow,
somewhere, they had become passionate advocates of the Linux open
source operating system. After collecting my chin from the floor, we wound
up having a lively discussion about Linux, Microsoft, Python, and all things
open. You might even say we had a good-old time.
I'm not trying to express a preference for one operating system over
another here; both have merits, and Python runs equally well on either
platform (indeed, this book's examples were developed on both systems).
But I am amazed that an idea that software developers often take for
granted has had such a deep, mainstream impact. That seems a very
hopeful thing to me; if technology is to truly improve the quality of life in
the next millennium, we need all the cowboys we can get.
Programming Python, 2nd Edition, O’Reilly
IT-SC book 32
Chapter 1. Introducing Python
1.1 "And Now for Something Completely
Different"
1.2 The Life of Python
1.3 The Compulsory Features List
1.4 What's Python Good For?
1.5 What's Python Not Good For?
1.1 "And Now for Something Completely Different"
This book is about using Python, a very high-level, object-oriented, open source[1]
programming language, designed to optimize development speed. Although it is
completely general-purpose, Python is often called an object-oriented scripting
language, partly because of its sheer ease of use, and partly because it is commonly
used to orchestrate or "glue" other software components in an application.
[1]
Open source systems are sometimes called freeware, in that their source code is freely
distributed and community-controlled. Don't let that concept fool you, though; with roughly
half a million users in that community today, Python is very well supported.
If you are new to Python, chances are you've heard about the language somewhere,
but are not quite sure what it is about. To help you get started, this chapter provides
a nontechnical introduction to Python's features and roles. Most of it will make more
sense once you have seen real Python programs, but let's first take a quick pass
over the forest before wandering among the trees.
In the preface, I mentioned that Python emphasizes concepts such as quality,
productivity, portability, and integration. Since these four terms summarize most of
the reasons for using Python, I'd like to define them in a bit more detail:
Quality
Python makes it easy to write software that can be reused and maintained. It was
deliberately designed to raise development quality expectations in the scripting
world. Python's clear syntax and coherent design almost forces programmers to
write readable code -- a critical feature for software that may be changed by others.
The Python language really does look like it was designed, not accumulated. Python
is also well tooled for modern software reuse methodologies. In fact, writing high-
quality Python components that may be applied in multiple contexts is almost
automatic.
Productivity
Programming Python, 2nd Edition, O’Reilly
IT-SC book 33
Python is optimized for speed of development. It's easy to write programs fast in
Python, because the interpreter handles details you must code explicitly in lower-
level languages. Things like type declarations, memory management, and build
procedures are nowhere to be found in Python scripts. But fast initial development is
only one component of productivity. In the real world, programmers must write code
both for a computer to execute and for other programmers to read and maintain.
Because Python's syntax resembles executable pseudocode, it yields programs that
are easy to understand long after they have been written. In addition, Python
supports (but does not impose) advanced paradigms such as object-oriented
programming, which further boost developer productivity and shrink development
schedules.
Portability
Most Python programs run without change on almost every computer system in use
today. In fact, Python programs run today on everything from IBM mainframes and
Cray Supercomputers to notebook PCs and handheld PDAs. Although some platforms
offer nonportable extensions, the core Python language and libraries are platform-
neutral. For instance, most Python scripts developed on Linux will generally run on
Windows immediately, and vice versa -- simply copy the script over. Moreover, a
graphical user interface (GUI) program written with Python's standard Tkinter library
will run on the X Windows system, Microsoft Windows, and the Macintosh, with
native look-and-feel on each, and without modifying the program's source code at
all.
Integration
Python is designed to be integrated with other tools. Programs written in Python can
be easily mixed with and script (i.e., direct) other components of a system. Today,
for example, Python scripts can call out to existing C and C++ libraries, talk to Java
classes, integrate with COM and CORBA components, and more. In addition,
programs written in other languages can just as easily run Python scripts by calling C
and Java API functions, accessing Python-coded COM servers, and so on. Python is
not a closed box.
In an era of increasingly short development schedules, faster machines, and
heterogeneous applications, these strengths have proven to be powerful allies in
both small and large development projects. Naturally, there are other aspects of
Python that attract developers, such as its simple learning curve for developers and
users alike, libraries of precoded tools to minimize up-front development, and
completely free nature that cuts product development and deployment costs.
But Python's productivity focus is perhaps its most attractive and defining quality. As
I write this, the main problem facing the software development world is not just
writing programs quickly, but finding developers with time to write programs at all.
Developers' time has become paramount -- much more critical than execution speed.
There are simply more projects than programmers to staff them.
As a language optimized for developer productivity, Python seems to be the right
answer to the questions being asked by the development world. Not only can Python
developers implement systems quickly, but the resulting systems will be
maintainable, portable, and easily integrated with other application components.
Programming Python, 2nd Edition, O’Reilly
IT-SC book 34
1.2 The Life of Python
Python was invented around 1990 by Guido van Rossum, when he was at CWI in
Amsterdam. Despite the reptiles, it is named after the BBC comedy series Monty
Python's Flying Circus, of which Guido is a fan (see the following silly sidebar). Guido
was also involved with the Amoeba distributed operating system and the ABC
language. In fact, the original motivation for Python was to create an advanced
scripting language for the Amoeba system.
But Python's design turned out to be general enough to address a wide variety of
domains. It's now used by hundreds of thousands of engineers around the world, in
increasingly diverse roles. Companies use Python today in commercial products, for
tasks such as testing chips and boards, developing GUIs, searching the Web,
animating movies, scripting games, serving up maps and email on the Internet,
customizing C++ class libraries, and much more.[2]
In fact, because Python is a
completely general-purpose language, its target domains are only limited by the
scope of computers in general.
[2]
See the preface for more examples of companies using Python in these ways, and see
http://www.python.org for a more comprehensive list of commercial applications.
Since it first appeared on the public domain scene in 1991, Python has continued to
attract a loyal following, and spawned a dedicated Internet newsgroup,
comp.lang.python, in 1994. And as the first edition of this book was being written in
1995, Python's home page debuted on the WWW at http://www.python.org -- still
the official place to find all things Python.
What's in a Name?
Python gets its name from the 1970s British TV comedy series, Monty
Python's Flying Circus. According to Python folklore, Guido van Rossum,
Python's creator, was watching reruns of the show at about the same time
he needed a name for a new language he was developing. And, as they say
in show business, "the rest is history."
Because of this heritage, references to the comedy group's work often show
up in examples and discussion. For instance, the name "Spam" has a special
connotation to Python users, and confrontations are sometimes referred to
as "The Spanish Inquisition." As a rule, if a Python user starts using phrases
that have no relation to reality, they're probably borrowed from the Monty
Python series or movies. Some of these phrases might even pop up in this
book. You don't have to run out and rent The Meaning of Life or The Holy
Grail to do useful work in Python, of course, but it can't hurt.
While "Python" turned out to be a distinctive name, it's also had some
interesting side effects. For instance, when the Python newsgroup,
comp.lang.python, came online in 1994, its first few weeks of activity were
almost entirely taken up by people wanting to discuss topics from the TV
show. More recently, a special Python supplement in the Linux Journal
magazine featured photos of Guido garbed in an obligatory "nice red
Programming Python, 2nd Edition, O’Reilly
IT-SC book 35
uniform."
There's still an occasional post from fans of the show on Python's news list.
For instance, one poster innocently offered to swap Monty Python scripts
with other fans. Had he known the nature of the forum, he might have at
least mentioned whether they ran under DOS or Unix.
To help manage Python's growth, organizations aimed at supporting Python
developers have taken shape over the years: among them, Python Software Activity
(PSA) was formed to help facilitate Python conferences and web sites, and the
Python Consortium was formed by organizations interested in helping to foster
Python's growth. Although the future of the PSA is unclear as I write these words, it
has helped to support Python through the early years.
Today, Guido and a handful of other key Python developers, are employed by a
company named Digital Creations to do Python development on a full-time basis.
Digital Creations, based in Virginia, is also home to the Python-based Zope web
application toolkit (see http://www.zope.org). However, the Python language is
owned and managed by an independent body, and remains a true open source,
community-driven system.
Other companies have Python efforts underway as well. For instance, ActiveState
and PythonWare develop Python tools, O'Reilly (the publisher of this book) and a
company named Foretech both organize annual Python conferences, and O'Reilly
manages a supplemental Python web site (see the O'Reilly Network's Python
DevCenter at http://www.oreillynet.com/python). The O'Reilly Python Conference is
held as part of the annual Open Source Software Convention. Although the world of
professional organizations and companies changes more frequently than do
published books, it seems certain that the Python language will continue to meet the
needs of its user community.
1.3 The Compulsory Features List
One way to describe a language is by listing its features. Of course, this will be more
meaningful after you've seen Python in action; the best I can do now is speak in the
abstract. And it's really how Python's features work together, that make it what it is.
But looking at some of Python's attributes may help define it; Table 1-1 lists some of
the common reasons cited for Python's appeal.
Table 1-1. Python Language Features
Features Benefits
No compile or link steps Rapid development cycle turnaround
No type declarations Simpler, shorter, and more flexible programs
Programming Python, 2nd Edition, O’Reilly
IT-SC book 36
Automatic memory management Garbage collection avoids bookkeeping code
High-level datatypes and operations Fast development using built-in object types
Object-oriented programming Code reuse, C++, Java, and COM integration
Embedding and extending in C Optimization, customization, system "glue"
Classes, modules, exceptions Modular "programming-in-the-large" support
A simple, clear syntax and design Readability, maintainability, ease of learning
Dynamic loading of C modules Simplified extensions, smaller binary files
Dynamic reloading of Python
modules
Programs can be modified without stopping
Universal "first-class" object model Fewer restrictions and special-case rules
Runtime program construction Handles unforeseen needs, end-user coding
Interactive, dynamic nature Incremental development and testing
Access to interpreter information Metaprogramming, introspective objects
Wide interpreter portability Cross-platform programming without ports
Compilation to portable bytecode Execution speed, protecting source code
Standard portable GUI framework Tkinter scripts run on X, Windows, and Macs
Standard Internet protocol support Easy access to email, FTP, HTTP, CGI, etc.
Programming Python, 2nd Edition, O’Reilly
IT-SC book 37
Standard portable system calls Platform-neutral system scripting
Built-in and third-party libraries
Vast collection of precoded software
components
True open source software May be freely embedded and shipped
To be fair, Python is really a conglomeration of features borrowed from other
languages. It includes elements taken from C, C++, Modula-3, ABC, Icon, and
others. For instance, Python's modules came from Modula, and its slicing operation
from Icon (as far as anyone can seem to remember, at least). And because of
Guido's background, Python borrows many of ABC's ideas, but adds practical
features of its own, such as support for C-coded extensions.
1.4 What's Python Good For?
Because Python is used in a wide variety of ways, it's almost impossible to give an
authoritative answer to this question. In general, any application that can benefit
from the inclusion of a language optimized for speed of development is a good target
Python application domain. Given the ever-shrinking schedules in software
development, this a very broad category.
A more specific answer is less easy to formulate. For instance, some use Python as
an embedded extension language, while others use it exclusively as a standalone
programming tool. And to some extent, this entire book will answer this very
question -- it explores some of Python's most common roles. For now, here's a
summary of some of the more common ways Python is being applied today:
System utilities
Portable command-line tools, testing systems
Internet scripting
CGI web sites, Java applets, XML, ASP, email tools
Graphical user interfaces
With APIs such as Tk, MFC, Gnome, KDE
Component integration
C/C++ library front-ends, product customization
Database access
Persistent object stores, SQL database system interfaces
Programming Python, 2nd Edition, O’Reilly
IT-SC book 38
Distributed programming
With client/server APIs like CORBA, COM
Rapid-prototyping /development
Throwaway or deliverable prototypes
Language-based modules
Replacing special-purpose parsers with Python
And more
Image processing, numeric programming, AI, etc.
"Buses Considered Harmful"
The PSA organization described earlier was originally formed in response to
an early thread on the Python newsgroup, which posed the semiserious
question: "What would happen if Guido was hit by a bus?"
These days, Guido van Rossum is still the ultimate arbiter of proposed
Python changes, but Python's user base helps support the language, work
on extensions, fix bugs, and so on. In fact, Python development is now a
completely open process -- anyone can inspect the latest source-code files
or submit patches by visiting a web site (see http://www.python.org for
details).
As an open source package, Python development is really in the hands of a
very large cast of developers working in concert around the world. Given
Python's popularity, bus attacks seem less threatening now than they once
did; of course, I can't speak for Guido.
On the other hand, Python is not really tied to any particular application area at all.
For example, Python's integration support makes it useful for almost any system that
can benefit from a frontend, programmable interface. In abstract terms, Python
provides services that span domains. It is:
A dynamic programming language, for situations in which a compile/link step is
either impossible (on-site customization), or inconvenient (prototyping, rapid
development, system utilities)
A powerful but simple programming language designed for development speed, for
situations in which the complexity of larger languages can be a liability (prototyping,
end-user coding)
A generalized language tool, for situations where we might otherwise need to invent
and implement yet another "little language" (programmable system interfaces,
configuration tools)
Programming Python, 2nd Edition, O’Reilly
IT-SC book 39
Given these general properties, Python can be applied to any area we're interested in
by extending it with domain libraries, embedding it in an application, or using it all
by itself. For instance, Python's role as a system tools language is due as much to its
built-in interfaces to operating system services as to the language itself. In fact,
because Python was built with integration in mind, it has naturally given rise to a
growing library of extensions and tools, available as off-the-shelf components to
Python developers. Table 1-2 names just a few; you can find more about most of
these components in this book or on Python's web site.
Table 1-2. A Few Popular Python Tools and Extensions
Domain Extensions
Systems programming Sockets, threads, signals, pipes, RPC calls, POSIX bindings
Graphical user interfaces Tk, PMW, MFC, X11, wxPython, KDE, Gnome
Database interfaces Oracle, Sybase, PostGres, mSQL, persistence, dbm
Microsoft Windows tools MFC, COM, ActiveX, ASP, ODBC, .NET
Internet tools JPython, CGI tools, HTML/XML parsers, email tools, Zope
Distributed objects DCOM, CORBA, ILU, Fnorb
Other popular tools SWIG, PIL, regular expressions, NumPy, cryptography
1.5 What's Python Not Good For?
To be fair again, some tasks are outside of Python's scope. Like all dynamic
languages, Python (as currently implemented) isn't as fast or efficient as static,
compiled languages like C. In many domains, the difference doesn't matter; for
programs that spend most of their time interacting with users or transferring data
over networks, Python is usually more than adequate to meet the performance
needs of the entire application. But efficiency is still a priority in some domains.
Because it is interpreted today,[3]
Python alone usually isn't the best tool for delivery
of performance-critical components. Instead, computationally intensive operations
can be implemented as compiled extensions to Python, and coded in a low-level
language like C. Python can't be used as the sole implementation language for such
components, but it works well as a frontend scripting interface to them.
Programming Python, 2nd Edition, O’Reilly
IT-SC book 40
[3]
Python is "interpreted" in the same way that Java is: Python source code is automatically
compiled (translated) to an intermediate form called "bytecode," which is then executed by
the Python virtual machine (that is, the Python runtime system). This makes Python scripts
more portable and faster than a pure interpreter that runs raw source code or trees. But it
also makes Python slower than true compilers that translate source code to binary machine
code for the local CPU. Keep in mind, though, that some of these details are specific to the
standard Python implementation; the JPython (a.k.a. "Jython") port compiles Python scripts to
Java bytecode, and the new C#/.NET port compiles Python scripts to binary .exe files. An
optimizing Python compiler might make most of the performance cautions in this chapter
invalid (we can hope).
For example, numerical programming and image processing support has been added
to Python by combining optimized extensions with a Python language interface. In
such a system, once the optimized extensions have been developed, most of the
programming occurs at the higher-level Python scripting level. The net result is a
numerical programming tool that's both efficient and easy to use.
Moreover, Python can still serve as a prototyping tool in such domains. Systems may
be implemented in Python first, and later moved in whole or piecemeal to a language
like C for delivery. C and Python have distinct strengths and roles; a hybrid
approach, using C for compute-intensive modules, and Python for prototyping and
frontend interfaces, can leverage the benefits of both.
In some sense, Python solves the efficiency/flexibility tradeoff by not solving it at all.
It provides a language optimized for ease of use, along with tools needed to
integrate with other languages. By combining components written in Python and
compiled languages like C and C++, developers may select an appropriate mix of
usability and performance for each particular application. While it's unlikely that it
will ever be as fast as C, Python's speed of development is at least as important as
C's speed of execution in most modern software projects.
On Truth in Advertising
In this book's conclusion we will return to some of the bigger ideas
introduced in this chapter, after we've had a chance to study Python in
action. I want to point out up front, though, that my background is in
Computer Science, not marketing. I plan to be brutally honest in this book,
both about Python's features and its downsides. Despite the fact that Python
is one of the most easy-to-use programming languages ever created, there
are indeed some pitfalls, which we will examine in this book.
Let's start now. Perhaps the biggest pitfall you should know about is this
one: Python makes it incredibly easy to throw together a bad design quickly.
It's a genuine problem. Because developing programs in Python is so simple
and fast compared to traditional languages, it's easy to get wrapped up in
the act of programming itself, and pay less attention to the problem you are
really trying to solve.
In fact, Python can be downright seductive -- so much so that you may need
to consciously resist the temptation to quickly implement a program in
Python that works, and is arguably "cool," but leaves you as far from a
maintainable implementation of your original conception as you were when
you started. The natural delays built in to compiled language development --
fixing compiler error messages, linking libraries, and the like -- aren't there
Random documents with unrelated
content Scribd suggests to you:
THE BATTLE OF APACHE CAÑON.
March 28, 1862.
The immediate consequence of the battle of Valvende was that the
insurgents marched directly past Fort Craig, which for want of men
and provisions they were powerless to invest or capture, direct on
Albuquerque and Santa Fé, which fell into their power without
resistance. Albuquerque was the depot of United States Government
stores, most of which was removed on the advance of the insurgents,
and the rest destroyed. The occupation of Santa Fé was followed by
the proclamation of a provisional government, which however never
entered into practical operation. Fort Craig still remained in the
rebel rear, and Fort Union in the possession of the national troops,
on the north-east, from which direction reinforcements might be
expected. The policy of the insurgents was therefore either to capture
Fort Union before relief could arrive, or maintain their position,
isolating Fort Craig until that post should be compelled to surrender
for want of supplies.
Meantime, news of the critical condition of affairs having reached
the Colorado territory and Kansas, troops were at once organized to
go to the relief of the threatened positions. By forced marches,
scarcely paralleled in history, a Colorado regiment 950 strong, under
Colonel Hough, reached Fort Union on the 13th of March. Here he
gathered around him all the troops available, or possible to obtain,
and marched for Santa Fé, to give battle to the invaders. The latter
moved their forces forward to meet him. The numbers on both sides
were nearly equal—between 1,200 and 1,500. They met at a point
called Apache Pass.
The main fight took place at Apache Cañon, eighty miles from Fort
Union, and twenty miles from Santa Fé. Three battalions, one under
Major Chivington, one under Captain Lewis, and one under Captain
Wynkoop, advanced to the cañon, on the 28th, when the pickets
reported no enemy in sight. The command then advanced, when
shots were fired at them by the Texans, who were in ambush and
succeeded in killing four privates. The Union men, under Hough,
rushed on them, killing 20 or 30 Texans, wounding many of them,
and taking seven prisoners, four officers and three privates. Major
Chivington’s command, which went ahead and surprised the Texan
pickets, taking 67 prisoners, and 64 provision wagons, now arrived,
and a plan of action was determined upon. It was to meet the enemy
in front and flank them at the same time.
About 12 o’clock they advanced, and the action became general,
the Coloradans doing wonders. The battery under Captain Ritter,
and also the howitzer battery under Lieutenant Claflin, swept the
Texans from the field. The fight lasted until four o’clock, when flags
of truce were interchanged to bury the dead and care for the
wounded. The enemy had about 2,000 men and one 6-pounder. The
Unionists had 1,300 men, one six and one 12-pounder, and four
howitzers. The enemy lost their entire train (64 wagons and
provisions), 230 mules, about 150 killed, 200 wounded and 93 taken
prisoners, among whom were 13 officers.
The Texans, when surprised, supposed it was Colonel Canby’s
force that was coming. The Texan officer in command, with two of
his companies, made several attempts to charge on the Union men
and seize their batteries, but they were each time repulsed, with
tremendous loss, while daring, noble deeds were performed by the
Federal soldiers. At one time, the Texan companies charged within a
few yards of the Union batteries.
The defeat at Apache Pass proved an effectual check on the
invaders, and so far weakened their forces as to compel their
abandonment of the territory, and its complete restoration under the
national authority.
The enemy fled into Arizona, where they found it useless to
remain, and applied to the authorities of Mexico for permission to
cross their territory on their return home, but were refused; they
however succeeded in reaching Texas. A reinforcement of Federal
troops soon after arrived in New Mexico.
FIGHT AT BLOOMING GAP, VA.
February 14, 1862.
To General F. W. Lander’s brigade had been assigned the perilous
duty of protecting the Baltimore and Ohio railroad at Cumberland,
Md., and the various towns and strategic points in Virginia within a
radius of forty or fifty miles from that centre, at several of which his
troops were quartered.
On the 13th of February, Lander received information that a
brigade of rebels under General Carson had occupied Blooming Gap,
a strong pass in the mountains seven miles beyond the Cacapon
river, whose turbid waters, swollen by the storms of winter, were
deemed an impassable barrier to the advance of the Federal forces.
No bridge spanned the torrent, and the blackened buttress and
crumbled pier gave evidence that the incendiary torch had been at
work.
Lander was then at Pawpaw Tunnels, on the Maryland shore of the
Potomac, a station on the Baltimore and Ohio railroad, midway
between Hancock and Cumberland, with a small force. He
immediately marched to New Creek, in Hampshire county, Va., to
join the detachment of troops at that point, where he also hastily
concentrated all his available command. Taking twenty wagons
loaded with lumber, he proceeded to a point on the Cacapon river,
seven miles south of the railroad, and between the hours of nine and
one o’clock at night he improvised a bridge one hundred and eighty
feet long, by placing the wagons in the river as a foundation, over
which he marched his force of four thousand men, and advanced
upon the enemy’s pickets before the dawn of day.
With five hundred of the First Virginia cavalry, under Colonel
Anastanzel, he had designed to charge through the rebel camp at the
Gap, and then form immediately in his rear, cut off the retreat, and
capture the whole force, after the Federal infantry, following up the
cavalry charge, should have completed the discomfiture of the
enemy. But the rebels had retired before Lander’s approach; and
when led by the General and his staff, the cavalry flew through the
Gap and beyond it, they met with no opposition. Colonel Anastanzel
was at once ordered to push forward on the Winchester road with the
cavalry, reconnoitre, and, if possible, overtake and capture the
baggage of the enemy.
General Lander meantime brought up Colonel Carroll with the
Eighth Ohio regiment, and the Seventh Virginia, Colonel Evans, for a
support. Colonel Anastanzel encountered the enemy at the head of
the pass, two miles from Blooming. He was met by a sharp fire, and
halted his command. On hearing the firing, General Lander came up
and led the charge, followed by Major Armstrong, Assistant
Adjutant-General; Lieutenants Fitz-James O’Brien, the well-known
poet of his staff, and Major Bannister, Paymaster U.S.A., who had
volunteered for the expedition. A group of rebel officers were distant
about three hundred yards, encouraging their men. General Lander
being the best mounted, outran the rest of the party, and cut off the
retreat of the rebel officers.
“Surrender, gentlemen,” he said, coolly dismounting, and
extending his hand to receive the sword of Colonel Baldwin, over
whom an instant before he had appeared to be riding.
Five of the rebel officers surrendered to General Lander, and four
others immediately afterward, to the officers of his staff, among them
the Assistant Adjutant-General of General Carson.
By this time the rebel infantry, perceiving the small number of
their adversaries, commenced a heavy fire from the woods, but the
cavalry had recovered from its panic, and now rushed up the hill.
General Lander ordered Anastanzel to charge up the road, and
capture the baggage of the enemy. The cavalry dashed forward, and
the advance guard soon overtook and turned fifteen wagons and
horses out of the road. Colonel Evans now came up with his regiment
of infantry, and captured many more of the rebels. Colonel Carroll
cleared the road as he went, both infantry regiments behaving
admirably, following and engaging the enemy to the last, until
ordered back. The pursuit was continued eight miles.
The result of this affair was the capture of eighteen commissioned
officers, and forty-five non-commissioned officers and privates.
Thirty-three of the rebels were killed and wounded, with a loss on the
Union side of seven killed and wounded.
During this engagement Lieutenant Fitz-James O’Brien was shot
mortally while in advance of his comrades, and like the author-
soldier Winthrop, immortalized his name with the sword, as he had
before proved himself great with the pen.
General Dunning, of Lander’s command, returned to New Creek
the same day from an expedition to Moorfield, forty miles south of
Romney, having captured 225 beef cattle and 4,000 bushels of corn.
In a skirmish two of his men were wounded, and several rebels
killed.
EAST TENNESSEE UNDER CONFEDERATE
RULE.
The history of the world has never exhibited more exalted devotion
to an idea, nor a more splendid patriotism than that of the people of
East Tennessee. We may almost challenge the records of religious
history to produce anything more like holy enthusiasm, than the lofty
inspiration which has characterized these people. In no country, and
among no class can be found more heroic persistence or unfaltering
adherence to principle than has exalted the patriotism of this region.
With many inhabitants of the eastern portion of the State, loyalty
and devotion to the Union became in truth a part of their religion.
The rebel leaders knew that they had very little sympathy in East
Tennessee, and took measures to crush out all Union sentiment with
the iron heel of military despotism. Any expression of sympathy with
the Union cause, any co-operation of its inhabitants with the
loyalists, either for their own protection or for the aid of the
Government, was punished as a crime. The presses of that part of the
State had all been silenced or converted to their own use by the
Secessionists, with one exception. The Knoxville Whig remained true
to the Union. Its vigorous defence of the Government, its exposures
and denunciations of the rebel leaders, its unsparing invective
against the rebellion, and its bold, defiant appeals to the people, rang
like a clarion through the hills and valleys of East Tennessee, and as
the echo gathered from thousands of loyal voices, it made itself heard
through all the valleys and mountain passes of that noble border
State.
The heroic editor of this paper was not to be silenced either in his
voice or his press without a vigorous struggle. The Rev. Wm. G.
Brownlow had learned how to denounce and how to endure, for that
is a lesson most Methodist clergymen are called upon to learn; and
being brave in deeds as well as words, he stood forth in defence of
the country he loved, when she greatly needed the power of his
eloquence and the strength of his arm. The popularity which this
man had won by his uprightness, his courage, and firm adherence to
the Constitution, gave his opinions a force that made him an object
of peculiar importance to the enemy—yet they hesitated to lay violent
hands upon a man whose words were more potent than their
bayonets.
He was frequently threatened by soldiers passing through
Knoxville from other States, yet none dared to execute their threats.
His family were inspired with the same lofty heroism, and on one
occasion when a company of rebels came to his house to haul down
the Stars and Stripes, which was kept floating over his domicil, one
of his daughters stepped out to meet them, and by her courage and
decision protected the flag.
The suppression of this undaunted advocate of the Union, and
faithful and fearless witness against secession, became an inevitable
necessity; and at last, in the hope that he would at least become
silent on political affairs, it was resolved to offer him the alternative
of the oath of allegiance or the cell of a prison. He chose the latter,
and in a valedictory to his readers, published October 26, which must
ever be memorable for its heroic defence of the Union, its bold
denunciation of the rebels and their course, he announced to his
readers the suspension of his paper. This remarkable address, which,
under the circumstances, rises to the sublime in its moral courage,
closed with these words:
“Exchanging, with proud satisfaction, the editorial chair and the sweet
endearments of home for a cell in the prison, or the lot of an exile, I have the honor
to be, &c.
William G. Brownlow.”
Mr. Brownlow was sent to prison, and for months occupied a room
with several other patriots who preferred imprisonment to denial of
the government they loved. Here he was in daily expectation of being
led forth to execution. Though suffering from ill-health he was no
way daunted by the dark fate that threatened him. Nor were these
anticipations groundless, for during his stay there, many a brave
man left that prison to meet a violent death, and he had no reason to
expect a happier destiny.
During the closing months of the summer and fall the hopes of the
people were excited by promises of aid from the government. Loud
and earnest appeals were made for help, and with the energy of
despair the people clung to their principles, through every species of
persecution, robbery, arson, and imprisonment. Hundreds were
hung or assassinated, and the records of Tennessee are among the
most heart-rending that this war for the Union will leave to posterity.
The position of the rebel armies in western Tennessee was at that
time very strong, but the importance of keeping their lines of
communication open with the Atlantic States was great, and
thoroughly understood by the loyalists. To cut these lines was to the
Federals a work of pressing necessity; and in view of the probable
redemption of East Tennessee, the loyalists organized, and on the
night of November 8 they destroyed several bridges, and broke the
lines. Two of these were on the Georgia State road, two on
Chickamanye Creek, Hamilton county, and one on the East
Tennessee and Georgia railroad, on Hiawassee river, Bradley county.
Besides these, two bridges on the East Tennessee and Georgia
railroad on Lick Creek, Green County, and another on Holstein river,
were also burned. The rebels were thrown into consternation by
these events, and their leaders took the most active measures to
arrest and punish the perpetrators. A correspondence between some
of the prominent men ensued, and a large portion of the letters was
discovered among other papers and effects captured after the battle
of Mill Spring, which took place on the 19th of January, 1862. This
correspondence, in which the names Colonel William B. Wood and
General F. K. Zollicoffer appear, prove that the majority of the people
were unalterably for the Union, and that they could only be
restrained by the most oppressive and cruel measures. Colonel Wood
wrote to J. P. Benjamin, the Secretary of War, asking what
disposition should be made of the bridge-burners, to which Mr.
Benjamin replied—“All such as can be identified as having been
engaged in bridge-burning are to be tried summarily by drumhead
court-martial, and if found guilty, executed on the spot by hanging. It
would be well to leave their bodies hanging in the vicinity of the
burnt bridges.”
The loyalists were encouraged in their cause by the devotion of
Hon. Andrew Johnson, U. S. Senator, and Hon. Horace Maynard, M.
C., for Tennessee, whose eloquent and powerful appeals, and
confident assurances of aid, cheered the hearts of the people.
Thousands of East Tennesseeans escaped by night, wandering
along unfrequented roads, until they reached Kentucky, where they
organized regiments, under the direction of the Federal
commanders. Their cherished desire was to return to their own State,
with a powerful army, and redeem their soil. The atrocity of the rebel
guerrillas drove them almost to a passion of revenge, and when
disappointed at the announcement that their time had not come, and
that they must await a more favorable condition of the army,
hundreds of them, when ordered to retreat from the border lines of
their State, strayed from the ranks, despairing and heart-sick, and
falling down by the way, wept bitterly. Several of them, exhausted by
hard labor and forced marches, never rose again, but were
afterwards found dead on the road to Mount Vernon.
On the 26th of November the house of a gentleman named Bell
was attacked by an armed party of the enemy and set on fire. The
inmates, a large family of nine persons, were consigned to the
flames. Two alone of the whole household escaped this horrible fate.
On the 29th a band of twenty-one Union prisoners at Nashville
were compelled to take the oath of allegiance, and enter a company
in the rebel army.
Leadbetter, the secession commander in East Tennessee, had his
headquarters at Greenville, and on the 30th of November issued a
proclamation promising protection and pardon to all who would lay
down their arms and submit to the Confederate government. From
this clemency he excepted bridge-burners and destroyers of railroad
tracks. He closed his proclamation with the assurance that “they will
be tried by drumhead court-martial, and be hung on the spot.” This
terrible order was put into execution a few days afterward. Jacob M.
Hemslier and Henry Fry, two Unionists, being tried and pronounced
guilty of these offences, were hung.
The days of hope for the Unionists were weary and prolonged, but
deliverance was drawing nigh. The loyal men of the western part of
the State organized to oppose the measures of the leaders, and early
in January a bold resistance was made in Carroll, Weakly, McNairy,
and other counties, against the conscription act. Rebel troops were
sent into these counties to compel submission, and enforce
obedience.
The defeat and death of Zollicoffer, the breaking up of his army,
and the destruction of his stronghold, at last gave a brilliant promise
to these persecuted people that their deliverance was drawing nigh.
This event, succeeded in a few weeks by the capture of Fort Henry,
Fort Donelson, the evacuation of Bowling Green and Columbus, and
the occupation of Nashville, filled every true heart with rejoicing, and
the good old flag once more swept its folds freely over the houses of
East Tennessee.
BOMBARDMENT OF FORT HENRY, FEB. 6, 1862.
The appointment of Hon. Andrew Johnson as military governor of
Tennessee was greeted with enthusiasm by the people. His
reputation and conservative principles were a guarantee for the
character of his administration, and he soon began to rally to his
support the wavering and timid of the people who were still
apprehensive that the Confederates would return and restore their
rule.
Parson Brownlow, after having borne a long and severe
confinement in prison, in which his health suffered terribly, was
released, and sent beyond the military lines of the Confederates. His
reception by the Federal guards was enthusiastic and joyous in the
extreme. As soon as his health permitted he visited several cities in
the West, where he was greeted with overwhelming demonstrations
of popular admiration and respect. On his arrival at New York, May
17th, he was honored with a public reception at the Academy of
Music, which was densely filled with a brilliant audience, eager to
welcome him.
CAPTURE OF FORT HENRY, TENN.
February 6, 1862.
The brilliant victory obtained by General Thomas’ army over the
Confederate forces at Mill Spring, on the 19th of January, laid open
the rebel lines to the successful advance of the Federal arms, and
served to stimulate the commanders of the land and naval forces to
avail themselves of the opportunity thus afforded.
Previous to the battle of Mill Spring, General Grant, with a large
force, had left Cairo and marched toward Columbus, for the purpose
of reconnoitering the country, and to prevent rebel reinforcements
moving from that point to the assistance of General Buckner, at
Bowling Green, Ky., who was then threatened by the approach of
General Buell’s army. Upon the return of General Grant’s division to
Cairo, a combined movement of the land and naval forces was
determined on for the purpose of capturing Fort Henry, on the
Tennessee river, in Henry county, Tenn., just beyond the Kentucky
State line.
Fort Henry and its approaches were reconnoitered on the 21st
January by the United States gunboat Lexington, with a view to
ascertain its strength and the position of the rebels. She went within
two miles of the fort, and flung a number of shells into it without
eliciting any reply. At first it was thought the rebels had evacuated
the work, but on approaching it still nearer pickets were discovered
at various points. The heavy guns on the work were seen distinctly;
also a number of field pieces. In addition to the fort proper,
numerous earthworks had been thrown up on a high bluff above the
fort, on the west bank of the river. This additional work, named Fort
Hieman, commanded Fort Henry.
On the 22d January, Brigadier-General C. F. Smith, commanding
the second division of General Grant’s army, was at Crown Point,
Ky., where he had arrived with 6,000 men after a fatiguing march of
over 100 miles from Paducah. He proceeded thence on a personal
reconnoissance, on the gunboat Lexington, in the direction of Fort
Henry. The gunboat advanced up the west channel of the river to a
point within one mile and a half from the fort. General Smith
obtained an excellent view of the rebel fort, camp and garrison, and
sent his report to headquarters. He then marched his division back to
Paducah.
The flotilla of gunboats, which had been so long in course of
preparation on the Ohio and Mississippi, was now ready to take part
in the impending battles of the nation, and to assume that
prominence in the momentous events which were to follow to which
they have proved themselves justly entitled.
Flag-officer Andrew H. Foote was appointed by the Government to
command the naval forces on the Upper Mississippi and the Western
waters, and now led forth his gallant fleet to attack the enemy, in
conjunction with the land forces under General Ulysses S. Grant. The
fleet consisted of
Fleet Officers.—Flag-Officer Andrew H. Foote; Fleet Captain,
Commodore A. M. Pennock; Ordnance Officer, Lieutenant J. F.
Sanford; Ordnance Lieutenant, Byron Wilson; Flag Lieutenant,
James M. Prickett. Essex, 9 guns, Commander William D. Porter. St.
Louis, 13 guns, Lieutenant-Commanding Leonard Paulding.
Cincinnati, 13 guns, Commander R. N. Stembel. Carondelet, 13
guns, Commander Henry Walke. Conestoga, 9 guns, Lieutenant-
Commanding —— Phelps. Tyler, 9 guns, Lieutenant-Commanding
W. Gwin.
For several days, at Paducah, the utmost vigilance was exercised at
the headquarters of the Provost Marshal, in issuing passes, and on
Sunday and Monday, the 3d February, no persons were allowed in or
out of the lines. Half a dozen gunboats steamed leisurely into port
and brought their black forms to anchor opposite the levee, in the
centre of the river.
Monday afternoon, steamers commenced coming up from Cairo,
laden with troops and stores, and by night the whole landing in front
of the town was crowded with the arrivals. The fleet which came up
brought General Grant and Staff, and the first division, under
command of Brigadier-General McClernand. The steamers were
under command of Commodore G. W. Graham, and consisted of the
following boats: City of Memphis, Iatan, D. A. January, Chancellor,
Alp, “W. H. B.,” New Uncle Sam, Rob Roy, Alex. Scott, Minnehaha,
Illinois, Emerald, and Fanny Bullett.
The first division, on these boats, was made up of two brigades,
composed as follows, and commanded by General John A.
McClernand:—First Brigade, Colonel Oglesby, Commanding.—
Seventh Illinois, Colonel Cook; Eighth Illinois, Lieutenant-Colonel
Rhoades; Eighteenth Illinois, Lieutenant-Colonel Lawler; Twenty-
ninth Illinois, Colonel Reardon; Thirtieth Illinois, Lieutenant-
Colonel Dennis; Thirty-first Illinois, Colonel John A. Logan; Swartz’s
and Dresser’s Batteries; Stewart’s, Dollins’, O. Harnett’s and
Carmichael’s Cavalry.
Second Brigade, W. H. L. Wallace, Commanding.—Eleventh
Illinois, Lieutenant-Colonel Hart; Twentieth Illinois, Colonel Marsh;
Forty-fifth Illinois, Colonel Smith; Forty-eighth Illinois, Colonel
Harney; Taylor’s and McAllister’s Batteries—in the latter four siege
guns; Fourth Illinois Cavalry, Colonel Kellogg; Seventh Illinois
Cavalry, Colonel Dickey.
Soon after arriving, General Grant and staff paid a visit to General
Smith, and had a conference, in which it was determined to forward
the division of General McClernand that night, and after landing
them at some point below Fort Henry, out of range of its guns, send
the boats back after General Smith’s division at Paducah. It was
nearly midnight before the boats took their departure.
The point at which the troops were landed is about four or five
miles below Fort Henry, opposite a small town in Kentucky, called
Buffalo. Immediately at the place is a clearing of about one hundred
acres, surrounded on three sides by high bluffs densely timbered,
and reaching down to the river. The troops, on landing, immediately
took possession of these eminences, and planted batteries which
commanded the country in every direction, and then awaited the
arrival of the remaining forces, under General Smith.
Tuesday afternoon, while the troops were disembarking, the
Osband Cavalry, with Carson’s and Carpenter’s scouts thoroughly
examined the country in every direction, even up to within two miles
of Fort Henry. Tuesday night was beautiful; a thousand camp-fires
flashed through the shadows that lay upon the amphitheatre of
wooded hills. The sky was warm and serenely purple, as if brooding
over the first sweet blossoms of May. The silver crescent of a new
moon glittered in the western sky, shedding a faint radiance over the
tree-tops and sloping hill sides. All at once the music of half a dozen
bands broke through the stillness of this lovely scene, and the “Star-
Spangled Banner,” “Red, White and Blue,” and “Columbia the Gem
of the Ocean,” filled the night with bursts of patriotic music. Then
some dreamy strain followed, hushing the soldier’s heart with
thoughts of “Home, Sweet Home.”
On Wednesday, parties were out reconnoitering near the enemy’s
works, and in one case a squad of cavalry went within a mile of the
fort and encountered two hundred rebel horsemen. Both sides fired,
when the rebels ran, leaving one of their number dead, and carrying
off three severely wounded. One man on the Union side was shot
through the brain, and killed instantly. He was the first man who
gave up his life in the vicinity of Fort Henry.
It had been noticed that a steamer belonging to the rebels was
busily engaged in running from the fortifications to some point up or
across the river, which was doubtless bringing in reinforcements.
Two of the gunboats—the Taylor and Conestoga, ran up to nearly the
centre of the island, and dropped a few shells in the direction of the
fort and the steamer, with what result was not known. They effected
a thorough reconnoissance on both sides, and discovered two ugly
torpedoes sunk in the west channel, which they carefully hauled out
and towed down to the shore below.
During the day and night the division of General Smith, from
Paducah, arrived, and was landed on the west shore of the river, with
a view of operating against batteries supposed to be on that side, and
also to counteract a large body of troops, which scouts reported to be
concentrating opposite the fort.
Wednesday night was cold and most disagreeable. About eight
o’clock a heavy storm set in, which speedily quenched the camp-fires,
and sent the troops wet and disconsolate under any shelter that
could be found. All over the southern horizon, in the direction of Fort
Henry, a tremendous thunder storm swept its way, filling the hills
with flashes of fiery blue lightning, and shaking the forests with loud
reverberations of thunder. Hailing this burst of heaven’s artillery,
rolling southward toward the enemy, as a good omen, the Union
soldiers pulled the wet blankets closer around them, turned drearily
in the yielding mud, and fell asleep.
Thursday dawned cloudily, but towards nine o’clock it cleared up
and the sun came out warm and gloriously. Nature nowhere seemed
to anticipate the bloody event which gives the day prominence. A few
more troops arrived, among whom were the Ohio Seventh, Colonel
Lauman, and the Ohio Twelfth, Colonel Wood, both from Smithland,
and which, together with the Seventh Illinois, Lieutenant-Colonel
Bancock; Thirteenth Missouri, Colonel Wright; the Fifteenth Illinois,
Colonel ——, and Company D, First Missouri Artillery, made the
Third Brigade, Colonel John Cook commanding, assigned the right
wing of the advance up the Tennessee shore.
About ten o’clock the gunboats started slowly up the river, four
iron clad steamers leading abreast—the Essex, Captain Porter, on the
right, and the Cincinnati, Commodore Foote, on the left. The three
wooden gunboats ranged themselves abreast and followed, half a
mile or so to the rear.
The iron-clad boats moved up abreast, keeping up the west or high
water channel. Almost immediately on passing the lower end of the
island, the boats and the forts were in each others’ range, but on both
sides an ominous silence was preserved—a silence that betokened
deadly intent on the part of the belligerents. On swept the boats,
coming in full view of the long line of breastworks that broke the east
shore—in full view of the black muzzles of the heavy guns which
seemed watching the approach of the gallant little fleet in ominous
silence—in full view of the flag waving defiantly from a high staff in
the centre of the works, until one could almost see down the huge
bore of the guns, the bright straps of the shells, which seemed like
leashes to prevent the deadly missiles from springing forth upon
their work of destruction—and yet not a trigger was pulled on either
side.
Less than a mile separated the fleet and the fort, and yet not a
word was said. The insurgents appeared to be confidently
anticipating the conflict; and grouped like statues around their guns,
with lanyards stretched, they waited for the onset.
When about six hundred yards from the fort, the bow-guns of the
flag-ship poured their contents into it, and so close after, that the
reports seemed almost one, the other three poured in their fire.
Scarcely had the smoke cleared from the muzzles of the pieces, ere
the whole ten guns of the rebels belched forth their contents, sending
a terrific iron shower in, above and around the gunboats. Taking
their cue from the others, the three wooden gunboats, which were
about a mile below, opened from their bow-guns, and then the
contest was fairly begun. For one hour the roar was so incessant that
the successive reports of the guns could not, in many cases, be
distinguished. Occasionally there would be a momentary lull—then a
single reverberating roar would give the key-note, and an instant
after all the voices would swell together in one tremendous chorus.
A thick cloud of smoke enveloped the boats, hiding them
completely from view. Over them hovered a dense white vapor, from
which quick flashes of flame leaped and quivered, incessantly
followed by delicate balloon-like forms of smoke, which burst like
ghostly shadows from the enemy’s shells.
From the very first, the fire of the rebel guns seemed directed at
the Essex. In their first volley two thirty-two pound shots struck the
Essex on the starboard bow, indenting deeply the iron sheathing, and
then glanced off, down the river, while a perfect storm of the iron
missiles whistled over her decks, and plowed into the water on either
side. She received in all eleven shots—one of which carried death
through the whole length of the vessel. It entered a larboard port,
carried off the head of the master’s mate, and passing on, entered the
boiler. The steam and water poured out, filling the whole space
between decks, and causing more destruction than all the enemy’s
missiles put together—four men were instantly suffocated, and some
twenty-five severely scalded, among whom was the gallant
Commander Porter. The two pilots, who were in the pilot-house
above, had no escape except through a passage from below, and up
this the steam rushed, as if coming from a safety-valve, and of course
with fatal effect. Both these poor men perished.
Of course the Essex was thenceforth unmanageable. She slowly
drifted down the main channel, and was soon after met by a steamer,
which towed her down to the place occupied by the boats before
starting. Soon after the Essex became disabled, the pelting of the iron
storm proved too hot for endurance, and the rebel flag came rapidly
down. The firing on the part of the gunboats immediately ceased,
and messengers were sent off from the flag-boat, which found, upon
landing, that the rebels were disposed to an unconditional surrender.
In scarcely more than an hour after the first attack, the flag of Fort
Henry was in the dust.
The fort was soon after taken possession of, and it was found that
the sum total of rebel prisoners was between seventy and one
hundred, the balance having left the night before on the steamer
Dunbar.
Among those who surrendered were Brigadier-General Tilghman,
Major Corrico, Colonel Carmichael, Captain Hayden, of the
Engineers, and Captain Miller, with several other commissioned
officers.
Ten of the rebels were found killed, and some twelve or fifteen
wounded. Three hundred and six tents were found on the west side
of the river, and about as many near the fort, all of which bore
evidences of the haste with which the rebels had evacuated their
quarters. Several hundred stands of arms were found, chiefly squirrel
rifles and double-barrelled shot-guns, also a large amount of
clothing, forage, provisions, wagons, mules and horses.
There was a large supply of ammunition, and when the Union
forces entered the fort there was beside each gun an abundance
unexpended. The tents were new and of excellent make, sufficient to
shelter five or six thousand men. The enemy had flour, corn, bacon
and sugar in large quantities, but no salt, and not a large supply of
beef.
There were nineteen guns in position, of the following calibre: two
128-pounders, one 80-pounder, two 42-pounders, rifled, ten 32-
pounders, two 24-pound howitzers, two 12-pound howitzers. Three
6-pound smooth bores, five 6-pound rifles, found outside the
intrenchments.
A twenty-four-pound rifled gun exploded on the fourth round, and
near the close of the fight a shell from one of the Union boats entered
the eighty-pounder and burst, disabling it. Several caissons were
captured in the redan upon the west side of the river, but no guns
were in position.
Evidences abounded on all sides of the deadly accuracy of the
Federal gunners. Every one of the eleven log buildings within the
ramparts was perforated with shot, the roof of one of the small
magazines was torn open, hurdle-work scattered in all direction, half
the guns knocked out of place, and great gulleys cut in the parapet
and the ground. A thirty-two pounder bearing upon the gunboats
had been struck by a Union shell, completely shattering the muzzle.
The ground beside the embrasure was stained with blood, which lay
in pools on the uneven surface. Beside one of the buildings, with gray
blankets thrown hastily over them, lay six dead soldiers, all fearfully
mutilated. Inside, ten wounded men were stretched upon cots, or on
the ground, some insensible, and others rending the air with groans,
while the surgeons of the garrison were attending upon them. Just
above, on the river, was the hospital ship of the rebels, the stern-
wheel steamer R. M. Patten, which had been captured with the fort.
The ensign of disease, the yellow flag, was flying from the staff,
waving off destruction from sixty invalids.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!
ebookfinal.com

More Related Content

PDF
Get Programming Python with CD 2nd Edition Mark Lutz free all chapters
PDF
Programming Python 3rd ed Edition Mark Lutz
PDF
Programming Python 3rd ed Edition Mark Lutz
PDF
Modern Tkinter For Busy Python Developers Mark Roseman
PDF
x86 Software Reverse-Engineering, Cracking, and Counter-Measures 1st Edition ...
PDF
Download full ebook of Learning Python 2nd ed Edition Mark Lutz instant downl...
PDF
Python Essentials For Dummies John C Shovic Alan Simpson
PDF
Computer Organisation and Architecture An Introduction 2nd Edition B.S. Chalk
Get Programming Python with CD 2nd Edition Mark Lutz free all chapters
Programming Python 3rd ed Edition Mark Lutz
Programming Python 3rd ed Edition Mark Lutz
Modern Tkinter For Busy Python Developers Mark Roseman
x86 Software Reverse-Engineering, Cracking, and Counter-Measures 1st Edition ...
Download full ebook of Learning Python 2nd ed Edition Mark Lutz instant downl...
Python Essentials For Dummies John C Shovic Alan Simpson
Computer Organisation and Architecture An Introduction 2nd Edition B.S. Chalk

Similar to [FREE PDF sample] Programming Python with CD 2nd Edition Mark Lutz ebooks (20)

PDF
The Complete Internet and World Wide Web Cyber Classroom 2nd ed Edition Deitel
PDF
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
PDF
Internet World Wide Web How to Program 2nd Edition Harvey M. Deitel
PDF
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
PDF
C 20 for Programmers 3rd Edition P. Deitel
PDF
Internet World Wide Web How to Program 2nd Edition Harvey M. Deitel
PDF
Writing Fast Code (JP) - PyCon JP 2015
PDF
Using MPI third edition Portable Parallel Programming with the Message Passin...
PDF
Using MPI third edition Portable Parallel Programming with the Message Passin...
PDF
Professional Git 1st Edition Brent Laster
PPTX
The Onward Journey: Porting Twisted to Python 3
PDF
Writing Fast Code - PyCon HK 2015
PDF
Using MPI third edition Portable Parallel Programming with the Message Passin...
PDF
Internet World Wide Web How to Program 2nd Edition Harvey M. Deitel
PDF
C 20 for Programmers 3rd Edition P. Deitel
PPTX
.NET Core Today and Tomorrow
PDF
[Ebooks PDF] download C How to Program 1ST Edition Harvey M. Deitel full chap...
PDF
Raspberry Pi IoT In C 2nd Edition Harry Fairhead
PDF
Using MPI third edition Portable Parallel Programming with the Message Passin...
PDF
(eBook PDF) Python Programming in Context 3rd Edition
The Complete Internet and World Wide Web Cyber Classroom 2nd ed Edition Deitel
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
Internet World Wide Web How to Program 2nd Edition Harvey M. Deitel
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
C 20 for Programmers 3rd Edition P. Deitel
Internet World Wide Web How to Program 2nd Edition Harvey M. Deitel
Writing Fast Code (JP) - PyCon JP 2015
Using MPI third edition Portable Parallel Programming with the Message Passin...
Using MPI third edition Portable Parallel Programming with the Message Passin...
Professional Git 1st Edition Brent Laster
The Onward Journey: Porting Twisted to Python 3
Writing Fast Code - PyCon HK 2015
Using MPI third edition Portable Parallel Programming with the Message Passin...
Internet World Wide Web How to Program 2nd Edition Harvey M. Deitel
C 20 for Programmers 3rd Edition P. Deitel
.NET Core Today and Tomorrow
[Ebooks PDF] download C How to Program 1ST Edition Harvey M. Deitel full chap...
Raspberry Pi IoT In C 2nd Edition Harry Fairhead
Using MPI third edition Portable Parallel Programming with the Message Passin...
(eBook PDF) Python Programming in Context 3rd Edition
Ad

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Complications of Minimal Access Surgery at WLH
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
VCE English Exam - Section C Student Revision Booklet
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
Yogi Goddess Pres Conference Studio Updates
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Pharma ospi slides which help in ospi learning
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Orientation - ARALprogram of Deped to the Parents.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial disease of the cardiovascular and lymphatic systems
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Complications of Minimal Access Surgery at WLH
A systematic review of self-coping strategies used by university students to ...
Weekly quiz Compilation Jan -July 25.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Ad

[FREE PDF sample] Programming Python with CD 2nd Edition Mark Lutz ebooks

  • 1. Download the full version of the ebook at https://ebookfinal.com Programming Python with CD 2nd Edition Mark Lutz https://ebookfinal.com/download/programming- python-with-cd-2nd-edition-mark-lutz/ Explore and download more ebook at https://ebookfinal.com
  • 2. Recommended digital products (PDF, EPUB, MOBI) that you can download immediately if you are interested. Beginning Programming With Python For Dummies 1st Edition John Paul Mueller https://ebookfinal.com/download/beginning-programming-with-python-for- dummies-1st-edition-john-paul-mueller/ ebookfinal.com Distributed Programming with Ruby 1st Edition Mark Bates https://ebookfinal.com/download/distributed-programming-with-ruby-1st- edition-mark-bates/ ebookfinal.com Introduction to Computation and Programming Using Python with Application to Understanding Data 2nd edition Edition Guttag https://ebookfinal.com/download/introduction-to-computation-and- programming-using-python-with-application-to-understanding-data-2nd- edition-edition-guttag/ ebookfinal.com Hacking Secret Ciphers with Python A beginner s guide to cryptography and computer programming with Python 1st Edition Al Sweigart https://ebookfinal.com/download/hacking-secret-ciphers-with-python-a- beginner-s-guide-to-cryptography-and-computer-programming-with- python-1st-edition-al-sweigart/ ebookfinal.com
  • 3. Active Grammar Level 3 with Answers and CD ROM 1st Edition Mark Lloyd https://ebookfinal.com/download/active-grammar-level-3-with-answers- and-cd-rom-1st-edition-mark-lloyd/ ebookfinal.com Black Hat Python Python Programming For Hackers And Pentesters 2nd Edition Justin Seitz And Tim Arnold https://ebookfinal.com/download/black-hat-python-python-programming- for-hackers-and-pentesters-2nd-edition-justin-seitz-and-tim-arnold/ ebookfinal.com Programming Google App Engine with Python Build and Run Scalable Python Apps on Google s Infrastructure 1st Edition Dan Sanderson https://ebookfinal.com/download/programming-google-app-engine-with- python-build-and-run-scalable-python-apps-on-google-s- infrastructure-1st-edition-dan-sanderson/ ebookfinal.com Expert Python Programming 1st Edition Tarek Ziade https://ebookfinal.com/download/expert-python-programming-1st-edition- tarek-ziade/ ebookfinal.com Deep Learning with Python 2nd Edition François Chollet https://ebookfinal.com/download/deep-learning-with-python-2nd-edition- francois-chollet/ ebookfinal.com
  • 5. Programming Python with CD 2nd Edition Mark Lutz Digital Instant Download Author(s): Mark Lutz ISBN(s): 9780596000851, 0596000855 Edition: 2 File Details: PDF, 6.98 MB Year: 2001 Language: english
  • 6. Programming Python, 2nd Edition, O’Reilly IT-SC book 1 Programming Python, 2nd Edition Mark Lutz Publisher: O'Reilly 2nd Edition March 2001 ISBN: 0-596-00085-5, 1256 pages Programming Python focuses on advanced applications of Python. Endorsed by Python creator Guido van Rossum, it demonstrates advanced Python techniques, and addresses software design issues such as reusability and object- oriented programming. The enclosed platform-neutral CD- ROM (view CD-ROM content online at http://examples.oreilly.com/python2) has book examples and various Python-related packages, including the full Python Version 2.0 source code distribution.
  • 7. Programming Python, 2nd Edition, O’Reilly IT-SC book 2 Enjoy the life together.
  • 8. Programming Python, 2nd Edition, O’Reilly IT-SC book 3 Foreword Preface "And Now for Something Completely Different . . . Again" Signs of the Python Times Why This Edition? Major Changes in This Edition Using the Examples and Demos Conventions Used in This Book Where to Look for Updates Contacting O'Reilly Acknowledgments 1. Introducing Python 1.1 "And Now for Something Completely Different" 1.2 The Life of Python 1.3 The Compulsory Features List 1.4 What's Python Good For? 1.5 What's Python Not Good For? I: System Interfaces 2. System Tools 2.1 "The os.path to Knowledge" 2.2 Why Python Here? 2.3 System Scripting Overview 2.4 The sys Module 2.5 The os Module 2.6 Script Execution Context 2.7 Current Working Directory 2.8 Command-Line Arguments 2.9 Shell Environment Variables 2.10 Standard Streams 2.11 File Tools 2.12 Directory Tools 3. Parallel System Tools 3.1 "Telling the Monkeys What to Do" 3.2 Forking Processes 3.3 Threads 3.4 Program Exits 3.5 Interprocess Communication 3.6 Pipes 3.7 Signals 3.8 Launching Programs on Windows 3.9 Other System Tools 4. Larger System Examples I 4.1 "Splits and Joins and Alien Invasions" 4.2 Splitting and Joining Files 4.3 Generating Forward-Link Web Pages 4.4 A Regression Test Script 4.5 Packing and Unpacking Files
  • 9. Programming Python, 2nd Edition, O’Reilly IT-SC book 4 4.6 User-Friendly Program Launchers 5. Larger System Examples II 5.1 "The Greps of Wrath" 5.2 Fixing DOS Line Ends 5.3 Fixing DOS Filenames 5.4 Searching Directory Trees 5.5 Visitor: Walking Trees Generically 5.6 Copying Directory Trees 5.7 Deleting Directory Trees 5.8 Comparing Directory Trees II: GUI Programming 6. Graphical User Interfaces 6.1 "Here's Looking at You, Kid" 6.2 Python GUI Development Options 6.3 Tkinter Overview 6.4 Climbing the GUI Learning Curve 6.5 The End of the Tutorial 6.6 Python/Tkinter for Tcl/Tk Converts 7. A Tkinter Tour, Part 1 7.1 "Widgets and Gadgets and GUIs, Oh My!" 7.2 Configuring Widget Appearance 7.3 Toplevel Windows 7.4 Dialogs 7.5 Binding Events 7.6 Message and Entry 7.7 Checkbutton, Radiobutton, and Scale 7.8 Running GUI Code Three Ways 7.9 Images 8. A Tkinter Tour, Part 2 8.1 "On Today's Menu: Spam, Spam, and Spam" 8.2 Menus 8.3 Listboxes and Scrollbars 8.4 Text 8.5 Canvas 8.6 Grids 8.7 Time Tools, Threads, and Animation 8.8 The End of the Tour 8.9 The PyDemos and PyGadgets Launchers 9. Larger GUI Examples 9.1 "Building a Better Mouse Trap" 9.2 Advanced GUI Coding Techniques 9.3 Complete Program Examples 9.4 PyEdit: A Text Editor Program/Object 9.5 PyView: An Image and Notes Slideshow 9.6 PyDraw: Painting and Moving Graphics 9.7 PyClock: An Analog/Digital Clock Widget 9.8 PyToe: A Tic-Tac-Toe Game Widget
  • 10. Programming Python, 2nd Edition, O’Reilly IT-SC book 5 9.9 Where to Go from Here III: Internet Scripting 10. Network Scripting 10.1 "Tune in, Log on, and Drop out" 10.2 Plumbing the Internet 10.3 Socket Programming 10.4 Handling Multiple Clients 10.5 A Simple Python File Server 11. Client-Side Scripting 11.1 "Socket to Me!" 11.2 Transferring Files over the Net 11.3 Processing Internet Email 11.4 The PyMailGui Email Client 11.5 Other Client-Side Tools 12. Server-Side Scripting 12.1 "Oh What a Tangled Web We Weave" 12.2 What's a Server-Side CGI Script? 12.3 Climbing the CGI Learning Curve 12.4 The Hello World Selector 12.5 Coding for Maintainability 12.6 More on HTML and URL Escapes 12.7 Sending Files to Clients and Servers 13. Larger Web Site Examples I 13.1 "Things to Do When Visiting Chicago" 13.2 The PyMailCgi Web Site 13.3 The Root Page 13.4 Sending Mail by SMTP 13.5 Reading POP Email 13.6 Utility Modules 13.7 CGI Script Trade-offs 14. Larger Web Site Examples II 14.1 "Typos Happen" 14.2 The PyErrata Web Site 14.3 The Root Page 14.4 Browsing PyErrata Reports 14.5 Submitting PyErrata Reports 14.6 PyErrata Database Interfaces 14.7 Administrative Tools 14.8 Designing for Reuse and Growth 15. Advanced Internet Topics 15.1 "Surfing on the Shoulders of Giants" 15.2 Zope: A Web Publishing Framework 15.3 HTMLgen: Web Pages from Objects 15.4 JPython ( Jython): Python for Java 15.5 Grail: A Python-Based Web Browser 15.6 Python Restricted Execution Mode
  • 11. Programming Python, 2nd Edition, O’Reilly IT-SC book 6 15.7 XML Processing Tools 15.8 Windows Web Scripting Extensions 15.9 Python Server Pages 15.10 Rolling Your Own Servers in Python IV: Assorted Topics 16. Databases and Persistence 16.1 "Give Me an Order of Persistence, but Hold the Pickles" 16.2 Persistence Options in Python 16.3 DBM Files 16.4 Pickled Objects 16.5 Shelve Files 16.6 SQL Database Interfaces 16.7 PyForm: A Persistent Object Viewer 17. Data Structures 17.1 "Roses Are Red, Violets Are Blue; Lists Are Mutable, and So Is Class Foo" 17.2 Implementing Stacks 17.3 Implementing Sets 17.4 Binary Search Trees 17.5 Graph Searching 17.6 Reversing Sequences 17.7 Permuting Sequences 17.8 Sorting Sequences 17.9 Data Structures Versus Python Built-ins 17.10 PyTree: A Generic Tree Object Viewer 18. Text and Language 18.1 "See Jack Hack. Hack, Jack, Hack" 18.2 Strategies for Parsing Text in Python 18.3 String Module Utilities 18.4 Regular Expression Matching 18.5 Parser Generators 18.6 Hand-Coded Parsers 18.7 PyCalc: A Calculator Program/Object V: Integration 19. Extending Python 19.1 "I Am Lost at C" 19.2 C Extensions Overview 19.3 A Simple C Extension Module 19.4 The SWIG Integration Code Generator 19.5 Wrapping C Environment Calls 19.6 A C Extension Module String Stack 19.7 A C Extension Type String Stack 19.8 Wrapping C++ Classes with SWIG 20. Embedding Python 20.1 "Add Python. Mix Well. Repeat." 20.2 C Embedding API Overview 20.3 Basic Embedding Techniques
  • 12. Programming Python, 2nd Edition, O’Reilly IT-SC book 7 20.4 Registering Callback Handler Objects 20.5 Using Python Classes in C 20.6 ppembed: A High-Level Embedding API 20.7 Other Integration Topics VI: The End 21. Conclusion: Python and the Development Cycle 21.1 "That's the End of the Book, Now Here's the Meaning of Life" 21.2 "Something's Wrong with the Way We Program Computers" 21.3 The "Gilligan Factor" 21.4 Doing the Right Thing 21.5 Enter Python 21.6 But What About That Bottleneck? 21.7 On Sinking the Titanic 21.8 So What's Python: The Sequel 21.9 In the Final Analysis... 21.10 Postscript to the Second Edition A. Recent Python Changes A.1 Major Changes in 2.0 A.2 Major Changes in 1.6 A.3 Major Changes Between 1.3 and 1.5.2 B. Pragmatics B.1 Installing Python B.2 Book Examples Distribution B.3 Environment Configuration B.4 Running Python Programs B.5 Python Internet Resources C. Python Versus C++ Colophon
  • 13. Programming Python, 2nd Edition, O’Reilly IT-SC book 8 Foreword Less than five years ago, I wrote the Foreword for the first edition of Programming Python. Since then, the book has changed about as much as the language and the Python community! I no longer feel the need to defend Python: the statistics and developments listed in Mark's Preface speak for themselves. In the past year, Python has made great strides. We released Python 2.0, a big step forward, with new standard library features such as Unicode and XML support, and several new syntactic constructs, including augmented assignment: you can now write x += 1 instead of x = x+1. A few people wondered what the big deal was (answer: instead of x, imagine dict[key] or list[index]), but overall this was a big hit with those users who were already used to augmented assignment in other languages. Less warm was the welcome for the extended print statement, print>>file, a shortcut for printing to a different file object than standard output. Personally, it's the Python 2.0 feature I use most frequently, but most people who opened their mouths about it found it an abomination. The discussion thread on the newsgroup berating this simple language extension was one of the longest ever-apart from the never-ending Python versus Perl thread. Which brings me to the next topic. (No, not Python versus Perl. There are better places to pick a fight than a Foreword.) I mean the speed of Python's evolution, a topic dear to the heart of the author of this book. Every time I add a feature to Python, another patch of Mark's hair turns gray-there goes another chapter out of date! Especially the slew of new features added to Python 2.0, which appeared just as he was working on this second edition, made him worry: what if Python 2.1 added as many new things? The book would be out of date as soon as it was published! Relax, Mark. Python will continue to evolve, but I promise that I won't remove things that are in active use! For example, there was a lot of worry about the string module. Now that string objects have methods, the string module is mostly redundant. I wish I could declare it obsolete (or deprecated) to encourage Python programmers to start using string methods instead. But given that a large majority of existing Python code-even many standard library modules-imports the string module, this change is obviously not going to happen overnight. The first likely opportunity to remove the string module will be when we introduce Python 3000; and even at that point, there will probably be a string module in the backwards compatibility library for use with old code. Python 3000?! Yes, that's the nickname for the next generation of the Python interpreter. The name may be considered a pun on Windows 2000, or a reference to Mystery Science Theater 3000, a suitably Pythonesque TV show with a cult following. When will Python 3000 be released? Not for a loooooong time-although you won't quite have to wait until the year 3000. Originally, Python 3000 was intended to be a complete rewrite and redesign of the language. It would allow me to make incompatible changes in order to fix problems with the language design that weren't solvable in a backwards compatible way. The current plan, however, is that the necessary changes will be introduced gradually
  • 14. Programming Python, 2nd Edition, O’Reilly IT-SC book 9 into the current Python 2.x line of development, with a clear transition path that includes a period of backwards compatibility support. Take, for example, integer division. In line with C, Python currently defines x/y with two integer arguments to have an integer result. In other words, 1/2 yields 0! While most dyed-in-the-wool programmers expect this, it's a continuing source of confusion for newbies, who make up an ever-larger fraction of the (exponentially growing) Python user population. From a numerical perspective, it really makes more sense for the / operator to yield the same value regardless of the type of the operands: after all, that's what all other numeric operators do. But we can't simply change Python so that 1/2 yields 0.5, because (like removing the string module) it would break too much existing code. What to do? The solution, too complex to describe here in detail, will have to span several Python releases, and involves gradually increasing pressure on Python programmers (first through documentation, then through deprecation warnings, and eventually through errors) to change their code. By the way, a framework for issuing warnings will be introduced as part of Python 2.1. Sorry, Mark! So don't expect the announcement of the release of Python 3000 any time soon. Instead, one day you may find that you are already using Python 3000-only it won't be called that, but rather something like Python 2.8.7. And most of what you've learned in this book will still apply! Still, in the meantime, references to Python 3000 will abound; just know that this is intentionally vaporware in the purest sense of the word. Rather than worry about Python 3000, continue to use and learn more about the Python version that you do have. I'd like to say a few words about Python's current development model. Until early 2000, there were hundreds of contributors to Python, but essentially all contributions had to go through my inbox. To propose a change to Python, you would mail me a context diff, which I would apply to my work version of Python, and if I liked it, I would check it into my CVS source tree. (CVS is a source code version management system, and the subject of several books.) Bug reports followed the same path, except I also ended up having to come up with the patch. Clearly, with the increasing number of contributions, my inbox became a bottleneck. What to do? Fortunately, Python wasn't the only open source project with this problem, and a few smart people at VA Linux came up with a solution: SourceForge! This is a dynamic web site with a complete set of distributed project management tools available: a public CVS repository, mailing lists (using Mailman, a very popular Python application!), discussion forums, bug and patch managers, and a download area, all made available to any open source project for the asking. We currently have a development group of 30 volunteers with SourceForge checkin privileges, and a development mailing list comprising twice as many folks. The privileged volunteers have all sworn their allegiance to the BDFL (Benevolent Dictator For Life-that's me :-). Introduction of major new features is regulated via a lightweight system of proposals and feedback called Python Enhancement Proposals (PEPs). Our PEP system proved so successful that it was copied almost verbatim by the Tcl community when they made a similar transition from Cathedral to Bazaar.
  • 15. Programming Python, 2nd Edition, O’Reilly IT-SC book 10 So, it is with confidence in Python's future that I give the floor to Mark Lutz. Excellent job, Mark. And to finish with my favorite Monty Python quote: Take it away, Eric, the orchestra leader! Guido van Rossum Reston, Virginia, January 2001 "And Now for Something Completely Different . . . Again" The first edition of this book was one of the first to present the Python language. This second edition is an almost completely new advanced Python topics book, designed to be a follow-up to the core language material in Learning Python and supplemented by the reference material in Python Pocket Reference. That is, this edition is focused on ways to use Python, rather than on the language itself. Python development concepts are explored along the way -- in fact, they really become meaningful only in the context of larger examples like those in this edition. But in general, this text now assumes that you already have at least a passing acquaintance with Python language fundamentals, and moves on to present the rest of the Python story. In this preface, I'll explain some of the rationales for this major rewrite, describe the structure of this edition in more detail, and give a brief overview of how to use the Python programs shipped on the enclosed CD-ROM (view CD-ROM content online at http://examples.oreilly.com/python2). First of all, though, a history lesson is in order. Signs of the Python Times It's been an exciting five years in the Python world. Since I wrote the first edition of this book between 1995 and 1996, Python has grown from a new kid on the scripting languages block to an established and widely used tool in companies around the world. Although measuring the popularity of an open source (http://opensource.org) and freely distributed tool such as Python is not always easy, most statistics available reveal exponential growth in Python's popularity over the last five years. Among the most recent signs of Python's explosive growth: Books As I write this in 2001, there are now over a dozen Python books on the market, with almost that many more on the way (in 1995 there were none). Some of these books are focused on a particular domain (e.g., Windows), and some are available in German, French, and Japanese language editions. Users In 1999, one leading industry observer suggested that there were as many as 300,000 Python users worldwide, based on various statistics. Other estimates are more optimistic still. In early 2000, for instance, the Python web site was already on track to service 500,000 new Python interpreter downloads by year end (in addition
  • 16. Programming Python, 2nd Edition, O’Reilly IT-SC book 11 to other Python distribution mediums); this figure is likely closer to the true user- base size as I write this book. Press Python is now regularly featured in industry publications. In fact, since 1995, Python creator Guido van Rossum has appeared on the cover of prominent tech magazines such as Linux Journal and Dr. Dobb's Journal; the latter publication gave him a programming excellence award for Python.[1] [1] As I was writing this book, Linux Journal also published a special Python supplement with their May 2000 edition -- the cover of which, of course, featured a naked man seated outdoors in front of a computer desk instead of a piano. If you don't know why that is funny, you need to watch a few reruns from Python's namesake, the Monty Python television series (consider it a first suggested exercise). I'll say more about the implications of Python's name in the first chapter. Applications Real companies have adopted Python for real products. It has shown up animating the latest Star Wars movie (Industrial Light & Magic), serving up maps and directories on the Internet (Yahoo), guiding users through Linux operating system installation (Red Hat), testing chips and boards (Intel), managing Internet discussion forums (Egroups), scripting online games (Origin), talking to CORBA frameworks (TCSI), implementing web site tools (Digital Creations' Zope), scripting wireless products (Agilent), and much more.[2] [2] See http://www.python.org for more details. Some companies don't disclose their Python use for competitive reasons, though many eventually become known when one of their web pages crashes and displays a Python error message in a browser. Hewlett Packard is generally counted among companies thus "outed." Newsgroup User traffic on the main Python Internet newsgroup, comp.lang.python, has risen dramatically too. For instance, according to eGroups (see http://www.egroups.com/group/python-list), there were 76 articles posted on that list in January 1994, and 2678 in January 2000 -- a 35-fold increase. Recent months have been busier still (e.g., 4226 articles during June, 2000 alone -- roughly 140 per day), and growth has been constant since the list's inception. This, and all other user-base figures cited in this preface, are likely to have increased by the time you read this text. But even at current traffic rates, Python forums are easily busy enough to consume the full-time attention of anyone with full-time attention to burn. Conferences There are now two annual Python conferences, one of which is hosted by O'Reilly & Associates. Attendance at Python conferences has roughly doubled in size every year. An annual Python Day is now also held in Europe. Group therapy Regional Python user groups have begun springing up in numerous sites in the U.S. and abroad, including Oregon, San Francisco, Washington D.C., Colorado, Italy,
  • 17. Programming Python, 2nd Edition, O’Reilly IT-SC book 12 Korea, and England. Such groups work on Python-related enhancements, organize Python events, and more. Domains Python has grown to embrace both Microsoft Windows developers, with new support for COM and Active Scripting, as well as Java developers, with the new JPython (renamed "Jython") Java-based implementation of the language. As we'll see in this edition, the new COM support allows Python scripts to be both component server and client; Active Scripting allows Python code to be embedded in HTML web page code and run on either client or server; and JPython compiles Python scripts to Java Virtual Machine code so that they can be run in Java-aware systems and can seamlessly integrate Java class libraries for use by Python code. As an open source tool for simplifying web site construction, the Python-based Zope web application framework discussed in this edition has also begun capturing the attention of webmasters and CGI coders. Services On the pragmatics front, commercial support, consulting, prepackaged distributions, and professional training for Python are now readily available from a variety of sources. For instance, the Python interpreter can be obtained on CDs and packages sold by various companies (including Walnut Creek, Dr. Dobb's Journal, and ActiveState), and Python usually comes prebuilt and free with most Linux operating system distributions. Jobs It's now possible to make money as a Python programmer (without having to resort to writing large, seminal books). As I write this book, the Python job board at http://www.python.org/Jobs.html lists some 60 companies seeking Python programmers in the U.S. and abroad. Searches for Python at popular employment sites yield even more hits -- for instance, 285 Python-related jobs on Monster.com, and 369 on dice.com. Not that anyone should switch jobs, of course, but it's nice to know that you can now make a living by applying a language that also happens to be a pleasure to use. Tools Python has also played host to numerous tools development efforts. Among the most prominent as I write these words: the Software Carpentry project, which is developing new core software tools in Python; ActiveState, which ison the verge of releasing a set of Windows and Linux-focused Python development products; and PythonWare, which is about to release an integrated Python development environment and GUI builder. Compilers As I write this preface, ActiveState has also announced a new Python compiler for the Microsoft .NET framework and C# language environment -- a true Python compiler and independent implementation of the Python language that generates DLL and EXE files, allows Python code to be developed under Visual Studio, and provides seamless .NET integration for Python scripts. It promises to be a third
  • 18. Programming Python, 2nd Edition, O’Reilly IT-SC book 13 implementation of Python, along with the standard C-based Python, and the JPython Java-based system. Education Python has also begun attracting the attention of educators, many of whom see Python as a "Pascal of the 2000s" -- an ideal language for teaching programming, due to its simplicity and structure. Part of this appeal was spawned by Guido van Rossum's proposed Computer Programming for Everybody (CP4E) project, aimed at making Python the language of choice for first-time programmers worldwide. At this writing the future of CP4E itself is uncertain, but a Python special interest group (SIG) has been formed to address education-related topics. Regardless of any particular initiative's outcome, Python promises to make programming more accessible to the masses of people who will surely soon grow tired of clicking preprogrammed links, as they evolve from computer users to computer scripters. In other words, it's not 1995 anymore. Much of the preceding list was unimaginable when the first edition of this book was conceived. Naturally, this list is doomed to be out of date even before this book hits the shelves, but it is nonetheless representative of the sorts of milestones that have occurred over the last five years, and will continue to occur for years to come. As a language optimized to address the productivity demands of today's software world, Python's best is undoubtedly yet to come. So What's Python? If you are looking for a concise definition of this book's topic, try this: Python is a general-purpose open source computer programming language, optimized for quality, productivity, portability, and integration. It is used by hundreds of thousands of developers around the world, in areas such as Internet scripting, systems programming, user interfaces, product customization, and more. Among other things, Python sports object-oriented programming (OOP); a remarkably simple, readable, and maintainable syntax; integration with C components; and a vast collection of precoded interfaces and utilities. Although general-purpose, Python is often called a scripting language because it makes it easy to utilize and direct other software components. Perhaps Python's best asset is simply that it makes software development more rapid and enjoyable. To truly understand how, read on.
  • 19. Programming Python, 2nd Edition, O’Reilly IT-SC book 14 Why This Edition? One consequence of the growing popularity of Python has been an influx of new users, programming styles, and applications, all of which have conspired to make parts of the first edition of this book prime for updates. Python itself has changed in smaller ways, but important extensions have simplified various aspects of Python development and merit new coverage. Perhaps most relevant for this edition is that the Python "audience" has changed. Over the last five years, Python has transitioned from an emerging language of interest primarily to pioneers to a widely accepted tool used by programmers for day-to-day development tasks. This edition has been refocused for this new Python audience. You will find that it is now more of a nuts-and-bolts text, geared less toward introducing and popularizing the language than to showing how to apply it for realistically scaled programming tasks. Because of the breadth of change, this edition is something of an entirely new book. To readers who enjoyed the first edition, I would like to express my gratitude, and I hope you will find the same spirit in this second edition. Although this is a major rewrite, I have tried to retain as much of the original book's material and flavor as possible (especially the jokes :-). Since writing the first edition five years ago, I have also had the opportunity to teach Python classes in the U.S. and abroad, and some of the new examples reflect feedback garnered from these training sessions. The new application domain examples reflect common interests and queries of both myself and my students. Teaching Python to workers in the trenches, many of whom are now compelled to use Python on the job, also inspired a new level of practicality that you will notice in this edition's examples and topics. Other new examples are simply the result of my having fun programming Python. Yes, fun; I firmly believe that one of Python's greatest intangible assets is its ability both to kindle the excitement of programming among newcomers, and to rekindle the excitement among those who have toiled for years with more demanding tools. As we will see in this edition, Python makes it incredibly easy to play with advanced but practical tools such as threads, sockets, GUIs, web sites, and OOP -- areas that can be both tedious and daunting in traditional compiled languages like C and C++. Frankly, even after eight years as a bona fide Pythonista, I still find programming most enjoyable when it is done in Python. Python is a wildly productive language, and witnessing its application first-hand is an aesthetic delight. I hope this edition, as much as the first, will demonstrate how to reap Python's productivity benefits and communicate some of the satisfaction and excitement found in a rapid-development tool such as Python. Major Changes in This Edition The best way to get a feel for any book is to read it, of course. But especially for people who are familiar with the first edition, the next few sections go into more detail about what is new in this edition.
  • 20. Programming Python, 2nd Edition, O’Reilly IT-SC book 15 It's Been Updated for Python 2.0 This edition has been updated for Python 2.0, and the Graphical User Interface (GUI) material has been updated for Tk versions 8.0 or later. Technically, this update was begun under Python 1.5.2, but all examples were revisited for 2.0 before publication. For the trivia buffs among you: release 2.0 was the first Python release following Guido's move to BeOpen, while 1.6 was the last release from Guido's prior employer, CNRI. Just before I finished this book's final draft and after the 2.0 release, Guido and the core Python development team moved from BeOpen to Digital Creations, home of the Zope web application construction kit, but this move is independent of Python releases (see Chapter 1, for more details). Release 2.0 introduces a few language extensions, but 2.0 and 1.6 are similar in content, and the updates just add a handful of features. The examples in this book should generally work with later Python releases. Remarkably, almost all examples in the first edition still work five years later, with the latest Python releases; those that didn't work required only small fixes (e.g., GUI call formats and C API interfaces). On the other hand, although the core language hasn't changed much since the first edition, a number of new constructs have been added, and we'll apply them all here. Among these new Python features: module packages, class exceptions, pseudo- private class attributes, unicode strings, the new regular expression module, new Tkinter features such as the grid manager, standard dialogs, and top-level menus, and so on. A new appendix summarizes all of the major changes in Python between the first and second editions of this book. In addition to the language changes, this book presents new Python tools and applications that have emerged in recent years. Among them: the IDLE programming interface, the JPython (a.k.a. "Jython") compiler, Active Scripting and COM extensions, the Zope web framework, Python Server Pages (PSP), restricted execution mode, the HTMLgen and SWIG code generators, thread support, CGI and Internet protocol modules, and more (it's been a busy five years). Such applications are the heart and soul of this second edition. It's Been Refocused for a More Advanced Audience This edition presents Python programming by advanced examples. Becoming proficient in Python involves two distinct tasks: learning the core language itself, and then learning how to apply it in applications. This edition addresses the latter (and larger) of these tasks by presenting Python libraries, tools, and programming techniques. Since this is a very different focus, I should say a few words about its rationale here. Because there were no other Python books on the horizon at the time, the first edition was written to appeal to many audiences at once -- beginners and gurus alike. Since then, another O'Reilly book, Learning Python, has been developed to address the needs of beginners, and Python Pocket Reference was published for readers seeking a short Python reference. As a result, the core language introductory-level material and the original reference appendixes have been removed from this book.
  • 21. Programming Python, 2nd Edition, O’Reilly IT-SC book 16 Learning Python introduces the core language -- its syntax, datatypes, and so on -- using intentionally simplistic examples. Many have found it to be ideal for learning the language itself, but Python can become even more interesting once you master the basic syntax and can write simple examples at the interactive prompt. Very soon after you've learned how to slice a list, you find yourself wanting to do real things, like writing scripts to compare file directories, responding to user requests on the Internet, displaying images in a window, reading email, and so on. Most of the day- to-day action is in applying the language, not the language itself. Programming Python focuses on the "everything else" of Python development. It covers libraries and tools beyond the core language, which become paramount when you begin writing real applications. It also addresses larger software design issues such as reusability and OOP, which can only be illustrated in the context of realistically scaled programs. In other words, Programming Python, especially in this new edition, is designed to pick up where Learning Python leaves off. Therefore, if you find this book too advanced, I encourage you to read Learning Python as a prelude to this text, and return here for the rest of the story once you've mastered the basics. Unless you already have substantial programming experience, this edition might serve you best as a second Python text. It Covers New Topics Most of the changes in this edition were made to accommodate new topics. There are new chapters and sections on Internet scripting, CGI scripts, operating system interfaces, the SWIG integration code generator, advanced Tkinter topics, the HTMLgen web page generator, JPython, threads, restricted execution mode, and more. You should consult the Table of Contents for the full scoop, but here are some of the new topics and structural changes you'll find in this edition: Topics The Internet, systems programming, Tkinter GUIs, and C integration domains get much more attention, and are arguably now the main focus of this text. For instance, you'll find six new chapters on Internet scripting, covering client-side tools, server- side scripts and web sites, and advanced Internet topics and systems. Four new chapters address systems topics: threads, directory processing, program launching, and so on. And the GUI material has also grown from one chapter to a much more complete four-chapter presentation, and now covers all widgets (including text and canvas), as well as new grid, menu, and dialog support. C integration The C extending and embedding chapters have been expanded to cover new topics such as SWIG (the way to mix Python with C/C++ libraries today) and present new mixed-mode examples such as callback dispatch (extending plus embedding). C integration is at the heart of many Python systems, but the examples in this domain are inevitably complex, and involve large C programs that are only useful to C users. In deference to readers who don't need to code C integrations, this material is now isolated at the end of the text. Some of the C code listings are gone as well -- to reduce page count, I have opted instead to point readers to C source files on the enclosed CD-ROM where possible (see http://examples.oreilly.com/python2).
  • 22. Programming Python, 2nd Edition, O’Reilly IT-SC book 17 Although later chapters build upon material in earlier chapters, topics in this edition are covered fairly independently, and are associated by book parts. Because of that, it's not too much of a stretch to consider this edition to be akin to four or five books in one. Its top-level structure underscores its application topics focus: Preface (you are here) Chapter 1 Part I Chapter 2 Chapter 3 Chapter 4 Chapter 5 Part II Chapter 6 Chapter 7 Chapter 8 Chapter 9 Part III Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Part IV Chapter 16 Chapter 17 Chapter 18 Part V Chapter 19 Chapter 20 Part VI Chapter 21 Appendix A Appendix B Appendix C Two notes here: First of all, don't let these titles fool you -- although most have to do with application topics, Python language features and general design concepts are still explored along the way, in the context of real-world goals. Second, readers who use Python as a standalone tool can safely skip the integration chapters, though I still recommend a quick glance. C programming isn't nearly as fun or easy as Python programming. Yet because integration is central to Python's role as a scripting tool, a cursory understanding can be useful, regardless of whether you do integrating, scripting, or both. First edition readers will notice that most of this material is new, and even chapters with old titles contain largely new material. Noticeably absent in this edition are the original Sneak Preview, Mini Reference, Tutorial Appendix, and all of the old Part II -- a reflection of the new focus and intended readership. It's More Example-Oriented
  • 23. Programming Python, 2nd Edition, O’Reilly IT-SC book 18 This book is largely about its examples. In this edition, old examples have been extended to become more realistic (e.g., PyForm and PyCalc), and new examples have been added throughout. Among the major examples, you'll find: PyEdit A Python/Tk text file editor object and program PyView A photo image and note-file slideshow PyDraw A paint program for drawing and moving image objects PyTree A tree data structure drawing program PyClock A Python/Tk analog and digital clock widget PyToe An AI-powered graphical tic-tac-toe program PyForm A persistent object table browser PyCalc A calculator widget in Python/Tk PyMail A Python/Tk POP and SMTP email client PyFtp A simple Python/Tk file-transfer GUI PyErrata A web-based error report system PyMailCgi A web-based email interface
  • 24. Programming Python, 2nd Edition, O’Reilly IT-SC book 19 There are also new mixed-mode C integration examples (e.g., callback registration and class object processing), SWIG examples (with and without "shadow" classes for C++), more Internet examples (FTP upload and download scripts, NNTP and HTTP examples, email tools, and new socket and select module examples), many new examples of Python threads, and new coverage of JPython, HTMLgen, Zope, Active Scripting, COM, and Python database interfaces. Many of the new examples are somewhat advanced, but of course this is now a somewhat advanced text. In addition, the old Python/C embedding API (now called ppembed ) has been extended to support precompiling strings to bytecode, and the original calculator example (now called PyCalc) has been beefed up to support keyboard entry, history lists, colors, and more. In fact, the new book examples tree distributed on this edition's CD-ROM (see http://examples.oreilly.com/python2) is itself a fairly sophisticated Python software system, and the examples within it have been upgraded structurally in a number of important ways: Examples tree The entire examples distribution has been organized as one big Python module package to facilitate cross-directory imports and avoid name-clashes with other Python code installed on your computer. Using directory paths in import statements (instead of a complex PYTHONPATH) also tends to make it easier to tell where modules come from. Moreover, you now need to add only one directory to your PYTHONPATH search-path setting for the entire book examples tree: the directory containing the PP2E examples root directory. To reuse code in this book within your own applications, simply import through the PP2E package root (e.g., from PP2E.Launcher import which). Example filenames Module names are generally much less cryptic now. I punted on 8.3 DOS compatibility long ago, and use more descriptive filenames. I've also fixed some old all-uppercase filenames, which were a last vestige of MS-DOS. Example titles Labels of example listings now give the full directory pathname of the example's source file to help you locate it in the examples distribution. For instance, an example source-code file whose name is given as Example N-M: PP2EInternetFtpsousa.py refers to the file sousa.py in the PP2EInternetFtp subdirectory of the examples distribution directory.[4] [4] The "examples distribution directory" is the directory containing the top-level PP2E directory of the book examples tree. On the CD (see http://examples.oreilly.com/python2), it's the topmost Examples directory; if you've copied the examples to your machine, it's wherever you copied (or unpacked) the PP2E root directory. You can run most of the examples from the CD directly, but you'll want to copy them to your hard drive to make changes, and to allow Python to save .pyc compiled bytecode files for quicker startups. Example command lines
  • 25. Programming Python, 2nd Edition, O’Reilly IT-SC book 20 Similarly, when a command line is shown typed after a prompt such as C:...PP2ESystemStreams>, for example, it is really to be typed in the PP2ESystemStreams subdirectory in your examples tree. Unix and Linux users: please think / when you see in filename paths (my official excuse for which is outlined in the next section). Example launchers Because it's just plain fun to click on things right away, there are new self- configuring demo launcher programs (described later in this preface in Section P.5.1), to give you a quick look at Python scripts in action with minimal configuration requirements. You can generally run them straight off the book's CD without setting any shell variables first. It's More Platform-Neutral Except for some C integration examples, the majority of the programs in this edition were developed on my Windows 98 laptop, with an eye toward portability to Linux and other platforms. In fact, some of the examples were born of my desire to provide portable Python equivalents of tools missing on Windows (e.g., file splitters). When programs are shown in action, it's usually on Windows; they are demonstrated on the Red Hat Linux 6.x platform only if they exercise Unix-specific interfaces. This is not a political statement at all -- I like Linux too. It's mostly a function of the fact that I wrote this book with MS Word; when time is tight, it's more convenient to run scripts on the same platform as your publishing tools than to frequently reboot into Linux. Luckily, because Python has now become so portable to both Windows and Linux, the underlying operating system is less of a concern to Python developers than it once was. Python, its libraries, and its Tkinter GUI framework all work extremely well on both platforms today. Because I'm not a politician, though, I've tried to make the examples as platform- neutral as possible, and point out platform-specific issues along the way. Generally speaking, most of the scripts should work on common Python platforms unchanged. For instance, all the GUI examples were tested on both Windows (98, 95) and Linux (KDE, Gnome), and most of the command-line and thread examples were developed on Windows but work on Linux too. Because Python's system interfaces are generally built to be portable, this is easier than it may sound. On the other hand, this book does delve into platform-specific topics where appropriate. There is new coverage of many Windows-specific topics -- Active Scripting, COM, program launch options, and so on. Linux and Unix readers will also find material geared towards their platforms -- forks, pipes, and the like. There is also new discussion of ways to edit and run Python programs on most major platforms. The one place where readers may still catch a glimpse of platform biases is in the Python/C integration examples. For simplicity, the C compilation details covered in this text are still somewhat Unix/Linux-biased. One can at least make a reasonable case for such a focus -- not only does Linux come with C compilers for free, but its development environment grew up around that language. On Windows, the C extension code shown in this book will work, but you may need to use different build
  • 26. Programming Python, 2nd Edition, O’Reilly IT-SC book 21 procedures that vary per Windows compiler. O'Reilly has published an outstanding text, Python Programming on Win32, that covers Windows-specific Python topics like this, and should help address some of the disparity here. If you do Windows-specific programming, please see that book for all Windows details skipped here. But It's Still Not a Reference Manual Please note that this edition, like the first, is still more of a tutorial than a reference manual (despite sharing a title pattern with a popular Perl reference text). This book aims to teach, not document. You can use its table of contents and index to track down specifics, and the new structure helps make this easy to do. But this edition is still designed to be used in conjunction with, rather than to replace, Python reference manuals. Because Python's manuals are free, well-written, available online, and change frequently, it would be folly to devote space to parroting their contents. For an exhaustive list of all tools available in the Python system, consult other books (e.g., O'Reilly's Python Pocket Reference) or the standard manuals at Python's web site and on this book's CD-ROM (see http://examples.oreilly.com/python2). Using the Examples and Demos I want to briefly describe how to use the book's examples here. In general, though, please see the following text files in the examples distribution directory for more details: README-root.txt: package structure notes PP2EREADME-PP2E.txt: general usage notes PP2EConfigsetup-pp.bat: Windows configuration PP2EConfigsetup-pp.csh: Unix and Linux configuration Of these, the README-PP2E.txt file is the most informative, and the PP2EConfig directory contains all configuration file examples. I give an overview here, but the files listed give a complete description. The Short Story If you want to see some Python examples right away, do this: Install Python from the book's CD-ROM (see http://examples.oreilly.com/python2), unless it is already installed on your computer. On Windows, click on the name of the self-installer program on the CD and do a default install (say "yes" or "next" to every prompt). On other systems, see the README file (the gzipped source distribution on the CD can be used to build Python locally).
  • 27. Programming Python, 2nd Edition, O’Reilly IT-SC book 22 Start one of the following self-configuring scripts located in the top-level ExamplesPP2E directory on the CD (see http://examples.oreilly.com/python2). Either click on their icons in your file explorer, or run them from your system prompt (e.g., DOS console box, Linux Xterm) using command lines of the form python script-name (you may need to use the full path to python if it's not on your system): Launch_PyDemos.pyw: the main Python/Tk demo launcher toolbar Launch_PyGadgets_bar.pyw: a Python/Tk utilities launcher bar Launch_PyGadgets.py: starts standard Python/Tk utilities LaunchBrowser.py: opens web examples index in web browser The Launch_* scripts start Python programs portably[5] and require only that Python be installed -- you don't need to set environment variables first or tweak the included PP2EConfig setup files to run them. LaunchBrowser will work if it can find a web browser on your machine, even if you don't have an Internet link (though some Internet examples won't work completely without a live link). [5] All the demo and launcher scripts are written portably but are known to work only on Windows 95/98 and Linux at the time of this writing; they may require minor changes on other platforms. Apologies if you're using a platform that I could not test: Tk runs on Windows, X11, and Macs; Python itself runs on everything from handheld PDAs to mainframes; and my advance for writing this book wasn't as big as you may think. If installing Python isn't an option, you can still run a few Python web demos by visiting http://starship.python.net/~lutz/PyInternetDemos.html with your browser. Because these examples execute scripts on a server, they tend to work best when run live from this site, rather than from the book's CD. The Details To help organize the new examples, I've provided a demo launcher program, PyDemos.pyw, in the top-level PP2E directory of the examples distribution. Figure P- 1 shows PyDemos in action on Windows after pressing a few buttons. The launcher bar appears on the left of the screen; with it, you can run most of the major graphical examples in the book with a mouse click. The demo launcher bar can also be used to start major Internet book examples if a browser can be located on your machine (see the following launcher description). Besides launching demos, the PyDemos source code provides pointers to major examples in the distribution; see its source code for details. You'll also find Linux automated build scripts for the Python/C integration examples in the top-level examples directory, which serve as indexes to major C examples. Figure P-1. The PyDemos launcher with pop-ups and demos (Guido's photo reprinted with permission from Dr. Dobb's Journal)
  • 28. Programming Python, 2nd Edition, O’Reilly IT-SC book 23 I've also included a top-level program called PyGadgets.py, and its relative PyGadgets_bar.pyw, to launch some of the more useful GUI book examples for real use instead of demonstration (mostly, the programs I use often; configure as desired). Figure P-2 shows what PyGadgets_bar looks like on Windows, along with a few of the utilities that its buttons can launch. All of the programs are presented in this book and included in the examples distribution. Most gadgets require a Python with Tkinter support, but that is the default configuration for the standard Windows port on the book's CD (see http://examples.oreilly.com/python2). Figure P-2. The PyGadgets utilities launcher bar, with gadgets
  • 29. Programming Python, 2nd Edition, O’Reilly IT-SC book 24 To run the files listed in the prior paragraph directly, you'll need to set up your Python module search path (see the top-level PP2E/Config/setup* files for hints). But if you want to run a collection of Python demos from the book and don't want to bother with setting up your environment first, simply run the self-launching utility scripts in the PP2E directory instead: Launch_PyDemos.pyw, Launch_PyGadgets.py, and Launch_PyGadgets_bar.pyw. These Python-coded launcher scripts assume Python has already been installed, but will automatically find your Python executable and the book examples distribution, and set up your Python module and system search paths as needed to run the demos. You can probably run these launch scripts by simply clicking on their names in a file explorer, and you should also be able to run them directly from the book's CD-ROM (see http://examples.oreilly.com/python2). See the comments at the top of Launcher.py for more details (or read about these scripts in Chapter 4 ). Many of the browser-based Internet examples from the book can also be found online at http://starship.python.net/~lutz/PyInternetDemos.html, where you can test-drive a few Python Internet scripting examples. Because these examples run in your web browser, they can be tested even if you haven't installed Python (or Python's Tk support) on your machine. The PyDemos program also attempts to launch a web browser on the major example web pages by starting the LaunchBrowser.py script in the examples root directory. That script tries to find a usable browser on your machine, with generally good results; see the script for more details if it fails. Provided LaunchBrowser can find a browser on your machine, some demo buttons will pop up web pages automatically, whether you have a live Internet connection or not (if not, you'll see local files in your browser). Figure P-3 shows what the PyInternetDemos page looks like under Internet Explorer on Windows.
  • 30. Programming Python, 2nd Edition, O’Reilly IT-SC book 25 Figure P-3. The PyInternetDemos web page Of special interest, the getfile.html link on this page allows you to view the source code of any other file on the book's site -- HTML code, Python CGI scripts, and so on; see Chapter 12 for details. To summarize, here is what you'll find in the top-level PP2E directory of the book's examples distribution: PyDemos.pyw Button bar for starting major GUI and Internet examples PyGadgets.py Starts programs in non-demo mode for regular use PyGadgets_bar.pyw Button bar for starting PyGadgets on demand Launch_*.py* Starts PyDemos and PyGadgets programs using Launcher.py to autoconfigure search paths (run these for a quick look) Launcher.py
  • 31. Programming Python, 2nd Edition, O’Reilly IT-SC book 26 Used to start programs without environment settings -- finds Python, sets PYTHONPATH, spawns Python programs LaunchBrowser.py Opens example web pages with an automatically located web browser, either live off the Net or by opening local web page files; if started directly, opens the PyInternetDemos index page There are also subdirectories for examples from each major topic area of the book. In addition, the top-level PP2EPyTools directory contains Python-coded command- line utilities for converting line-feeds in all example text files to DOS or Unix format (useful if they look odd in your text editor), making all example files writable (useful if you drag-and-drop off the book's CD (see http://examples.oreilly.com/python2), deleting old .pyc bytecode files in the tree, and more. Again, see the distribution's README-PP2E.txt file for more details on all example issues. Where It's At The book examples distribution can be found on the CD-ROM that accompanies this book. See the CD's top-level README file for usage details, or browse the CD's example root directory in your favorite file explorer for a quick tour. In addition to the book examples, the CD also contains various Python-related packages, including a full Windows self-installer program with Python and Tk support (double-click and say "yes" at all prompts to install), the full Python source code distribution (unpack and compile on your machine), and Python's standard documentation set in HTML form (click to view in your web browser). Extra open source packages such as the latest releases (at the time of publication) of the SWIG code generator and JPython are also included, but you can always find up- to-date releases of Python and other packages at Python's web site, http://www.python.org. Conventions Used in This Book The following font conventions are used in this book: Italic Used for file and directory names, commands, to emphasize new terms when first introduced, and for some comments within code sections Constant width Used for code listings and to designate modules, methods, options, classes, functions, statements, programs, objects, and HTML tags Constant width bold Used in code sections to show user input
  • 32. Programming Python, 2nd Edition, O’Reilly IT-SC book 27 Constant width italic Used to mark replaceables This icon designates a note related to the nearby text. This icon designates a warning related to the nearby text. Where to Look for Updates As before, updates, corrections, and supplements for this book will be maintained at the author's web site, http://www.rmi.net/~lutz. Look for the second edition's link on that page for all supplemental information related to this version of the book. As for the first edition, I will also be maintaining a log on this web site of Python changes over time, which you should consider a supplement to this text. Beginning with this edition, I am making available a user-driven book errata reporting system on the World Wide Web, at this site: http://starship.python.net/~lutz/PyErrata/pyerrata.html There, you'll find forms for submitting book problem reports and comments, as well as viewing the report database by various sort keys. Reports are stored in a publicly browsable database by default, but an option lets you email them privately instead. The PyErrata system also happens to be written in Python, and is an example presented and shipped with this book; see Chapter 14. Figure P-4 shows what the root page of PyErrata looks like. Figure P-4. The PyErrata book updates site
  • 33. Programming Python, 2nd Edition, O’Reilly IT-SC book 28 If any of these addresses stop working over time, these pages might also be accessible from O'Reilly's web site, http://www.oreilly.com.[6] I'm still happy to receive direct emails from readers, of course, but the PyErrata site will hopefully streamline the report-posting process. [6] O'Reilly has an errata report system at their site too, and you should consider the union of these two lists to be the official word on book bugs and updates. Contacting O'Reilly You can also address comments and questions about this book to the publisher: O'Reilly & Associates, Inc. 101 Morris Street Sebastopol, CA 95472 (800) 998-9938 (in the United States or Canada) (707) 829-0515 (international/local) (707) 829-0104 (fax) O'Reilly has a web page for this book, which lists errata, examples, and any additional information. You can access this page at: http://www.oreilly.com/catalog/python2/ To comment or ask technical questions about this book, send email to: [email protected]
  • 34. Programming Python, 2nd Edition, O’Reilly IT-SC book 29 For more information about books, conferences, software, Resource Centers, and the O'Reilly Network, see the O'Reilly web site at: http://www.oreilly.com
  • 35. Programming Python, 2nd Edition, O’Reilly IT-SC book 30 Acknowledgments In addition to the people I mentioned in the first edition's preface, I'd like to extend appreciation to a few of the people who helped in some way during this second edition project: To this book's first editor, Frank Willison, for overseeing this update, as well as championing the Python cause at O'Reilly and beyond. To this book's later editor, Laura Lewin, for picking up the ball and lighting necessary fires. To Python creator Guido van Rossum for making this stuff fun again. To the people who took part in a review of an early draft of this edition: Eric Raymond, Mark Hammond, David Ascher, Tim Peters, and Dave Beazley. To Tim O'Reilly and the staff of O'Reilly & Associates, both for producing this book, and supporting open source software in general. To the Python community at large, for diligence, hard work, and humor -- both in the early years and today. We've come far, but to steal a line from the 1970s: You aint' seen nothin' yet. And to the students of the many Python classes I have taught, as well as the scores of readers who took the time to send me comments about the first edition; your opinions helped shape this update. Finally, a few personal notes of thanks. To my children, Michael, Samantha, and Roxanne, for purpose. If they are at all representative of their generation, the future of our species seems in very good hands. You'll have to pardon me if that sounds proud; with kids like mine, it's impossible to feel otherwise. And most of all to Lisa, the mother of those amazing kids. I owe her my largest debt of gratitude, for everything from enduring my flights from reality while writing books like this, to keeping me out of jail in our youth. No matter what the future may hold, I'll always be glad that something threw us together two decades ago. Mark Lutz November 2000 Somewhere in Colorado "When Billy Goes Down, He's Going Down Fast" The last five years have also been host to the rise of the open source movement. Open source refers to software that is distributed free of charge with full source code, and is usually the product of many developers working in a loosely knit collaborative fashion. Python, the Linux operating system, and many other tools such as Perl and the Apache web server fall into this category. Partly because of its challenge to the dominance of mega- companies, the open source movement has quickly spread through society
  • 36. Programming Python, 2nd Edition, O’Reilly IT-SC book 31 in profound ways. Let me tell you about an event that recently underscored the scope of this movement's impact on me. To understand this story, you first need to know that as I was writing this book, I lived in a small town in Colorado not generally known for being on the cutting edge of technological innovation. To put that more colorfully, it's the sort of place that is sometimes called a "cowboy town." I was at a small local bookstore hunting for the latest Linux Journal. After browsing for a while, I found a copy and walked it to the checkout. Behind the counter were two clerks who looked as if they might be more at home at a rodeo than behind the counter of this establishment. The older of the two sported gray hair, a moustache, and the well-worn skin of a person accustomed to life on a ranch. Both wore obligatory baseball caps. Cowboys, to be sure. As I put the magazine down, the elder clerk looked up for a moment, and said, in classic cowboy drawl, "Linux, huh? I tell you what, when Billy goes down, he's goin' down fast!" Of course, this was in reference to the widely publicized competition between Linux and Bill Gates' Microsoft Windows, spurred by the open source movement. Now, in another time and place, these two might have instead been discussing livestock and firearms over strong cups of coffee. Yet somehow, somewhere, they had become passionate advocates of the Linux open source operating system. After collecting my chin from the floor, we wound up having a lively discussion about Linux, Microsoft, Python, and all things open. You might even say we had a good-old time. I'm not trying to express a preference for one operating system over another here; both have merits, and Python runs equally well on either platform (indeed, this book's examples were developed on both systems). But I am amazed that an idea that software developers often take for granted has had such a deep, mainstream impact. That seems a very hopeful thing to me; if technology is to truly improve the quality of life in the next millennium, we need all the cowboys we can get.
  • 37. Programming Python, 2nd Edition, O’Reilly IT-SC book 32 Chapter 1. Introducing Python 1.1 "And Now for Something Completely Different" 1.2 The Life of Python 1.3 The Compulsory Features List 1.4 What's Python Good For? 1.5 What's Python Not Good For? 1.1 "And Now for Something Completely Different" This book is about using Python, a very high-level, object-oriented, open source[1] programming language, designed to optimize development speed. Although it is completely general-purpose, Python is often called an object-oriented scripting language, partly because of its sheer ease of use, and partly because it is commonly used to orchestrate or "glue" other software components in an application. [1] Open source systems are sometimes called freeware, in that their source code is freely distributed and community-controlled. Don't let that concept fool you, though; with roughly half a million users in that community today, Python is very well supported. If you are new to Python, chances are you've heard about the language somewhere, but are not quite sure what it is about. To help you get started, this chapter provides a nontechnical introduction to Python's features and roles. Most of it will make more sense once you have seen real Python programs, but let's first take a quick pass over the forest before wandering among the trees. In the preface, I mentioned that Python emphasizes concepts such as quality, productivity, portability, and integration. Since these four terms summarize most of the reasons for using Python, I'd like to define them in a bit more detail: Quality Python makes it easy to write software that can be reused and maintained. It was deliberately designed to raise development quality expectations in the scripting world. Python's clear syntax and coherent design almost forces programmers to write readable code -- a critical feature for software that may be changed by others. The Python language really does look like it was designed, not accumulated. Python is also well tooled for modern software reuse methodologies. In fact, writing high- quality Python components that may be applied in multiple contexts is almost automatic. Productivity
  • 38. Programming Python, 2nd Edition, O’Reilly IT-SC book 33 Python is optimized for speed of development. It's easy to write programs fast in Python, because the interpreter handles details you must code explicitly in lower- level languages. Things like type declarations, memory management, and build procedures are nowhere to be found in Python scripts. But fast initial development is only one component of productivity. In the real world, programmers must write code both for a computer to execute and for other programmers to read and maintain. Because Python's syntax resembles executable pseudocode, it yields programs that are easy to understand long after they have been written. In addition, Python supports (but does not impose) advanced paradigms such as object-oriented programming, which further boost developer productivity and shrink development schedules. Portability Most Python programs run without change on almost every computer system in use today. In fact, Python programs run today on everything from IBM mainframes and Cray Supercomputers to notebook PCs and handheld PDAs. Although some platforms offer nonportable extensions, the core Python language and libraries are platform- neutral. For instance, most Python scripts developed on Linux will generally run on Windows immediately, and vice versa -- simply copy the script over. Moreover, a graphical user interface (GUI) program written with Python's standard Tkinter library will run on the X Windows system, Microsoft Windows, and the Macintosh, with native look-and-feel on each, and without modifying the program's source code at all. Integration Python is designed to be integrated with other tools. Programs written in Python can be easily mixed with and script (i.e., direct) other components of a system. Today, for example, Python scripts can call out to existing C and C++ libraries, talk to Java classes, integrate with COM and CORBA components, and more. In addition, programs written in other languages can just as easily run Python scripts by calling C and Java API functions, accessing Python-coded COM servers, and so on. Python is not a closed box. In an era of increasingly short development schedules, faster machines, and heterogeneous applications, these strengths have proven to be powerful allies in both small and large development projects. Naturally, there are other aspects of Python that attract developers, such as its simple learning curve for developers and users alike, libraries of precoded tools to minimize up-front development, and completely free nature that cuts product development and deployment costs. But Python's productivity focus is perhaps its most attractive and defining quality. As I write this, the main problem facing the software development world is not just writing programs quickly, but finding developers with time to write programs at all. Developers' time has become paramount -- much more critical than execution speed. There are simply more projects than programmers to staff them. As a language optimized for developer productivity, Python seems to be the right answer to the questions being asked by the development world. Not only can Python developers implement systems quickly, but the resulting systems will be maintainable, portable, and easily integrated with other application components.
  • 39. Programming Python, 2nd Edition, O’Reilly IT-SC book 34 1.2 The Life of Python Python was invented around 1990 by Guido van Rossum, when he was at CWI in Amsterdam. Despite the reptiles, it is named after the BBC comedy series Monty Python's Flying Circus, of which Guido is a fan (see the following silly sidebar). Guido was also involved with the Amoeba distributed operating system and the ABC language. In fact, the original motivation for Python was to create an advanced scripting language for the Amoeba system. But Python's design turned out to be general enough to address a wide variety of domains. It's now used by hundreds of thousands of engineers around the world, in increasingly diverse roles. Companies use Python today in commercial products, for tasks such as testing chips and boards, developing GUIs, searching the Web, animating movies, scripting games, serving up maps and email on the Internet, customizing C++ class libraries, and much more.[2] In fact, because Python is a completely general-purpose language, its target domains are only limited by the scope of computers in general. [2] See the preface for more examples of companies using Python in these ways, and see http://www.python.org for a more comprehensive list of commercial applications. Since it first appeared on the public domain scene in 1991, Python has continued to attract a loyal following, and spawned a dedicated Internet newsgroup, comp.lang.python, in 1994. And as the first edition of this book was being written in 1995, Python's home page debuted on the WWW at http://www.python.org -- still the official place to find all things Python. What's in a Name? Python gets its name from the 1970s British TV comedy series, Monty Python's Flying Circus. According to Python folklore, Guido van Rossum, Python's creator, was watching reruns of the show at about the same time he needed a name for a new language he was developing. And, as they say in show business, "the rest is history." Because of this heritage, references to the comedy group's work often show up in examples and discussion. For instance, the name "Spam" has a special connotation to Python users, and confrontations are sometimes referred to as "The Spanish Inquisition." As a rule, if a Python user starts using phrases that have no relation to reality, they're probably borrowed from the Monty Python series or movies. Some of these phrases might even pop up in this book. You don't have to run out and rent The Meaning of Life or The Holy Grail to do useful work in Python, of course, but it can't hurt. While "Python" turned out to be a distinctive name, it's also had some interesting side effects. For instance, when the Python newsgroup, comp.lang.python, came online in 1994, its first few weeks of activity were almost entirely taken up by people wanting to discuss topics from the TV show. More recently, a special Python supplement in the Linux Journal magazine featured photos of Guido garbed in an obligatory "nice red
  • 40. Programming Python, 2nd Edition, O’Reilly IT-SC book 35 uniform." There's still an occasional post from fans of the show on Python's news list. For instance, one poster innocently offered to swap Monty Python scripts with other fans. Had he known the nature of the forum, he might have at least mentioned whether they ran under DOS or Unix. To help manage Python's growth, organizations aimed at supporting Python developers have taken shape over the years: among them, Python Software Activity (PSA) was formed to help facilitate Python conferences and web sites, and the Python Consortium was formed by organizations interested in helping to foster Python's growth. Although the future of the PSA is unclear as I write these words, it has helped to support Python through the early years. Today, Guido and a handful of other key Python developers, are employed by a company named Digital Creations to do Python development on a full-time basis. Digital Creations, based in Virginia, is also home to the Python-based Zope web application toolkit (see http://www.zope.org). However, the Python language is owned and managed by an independent body, and remains a true open source, community-driven system. Other companies have Python efforts underway as well. For instance, ActiveState and PythonWare develop Python tools, O'Reilly (the publisher of this book) and a company named Foretech both organize annual Python conferences, and O'Reilly manages a supplemental Python web site (see the O'Reilly Network's Python DevCenter at http://www.oreillynet.com/python). The O'Reilly Python Conference is held as part of the annual Open Source Software Convention. Although the world of professional organizations and companies changes more frequently than do published books, it seems certain that the Python language will continue to meet the needs of its user community. 1.3 The Compulsory Features List One way to describe a language is by listing its features. Of course, this will be more meaningful after you've seen Python in action; the best I can do now is speak in the abstract. And it's really how Python's features work together, that make it what it is. But looking at some of Python's attributes may help define it; Table 1-1 lists some of the common reasons cited for Python's appeal. Table 1-1. Python Language Features Features Benefits No compile or link steps Rapid development cycle turnaround No type declarations Simpler, shorter, and more flexible programs
  • 41. Programming Python, 2nd Edition, O’Reilly IT-SC book 36 Automatic memory management Garbage collection avoids bookkeeping code High-level datatypes and operations Fast development using built-in object types Object-oriented programming Code reuse, C++, Java, and COM integration Embedding and extending in C Optimization, customization, system "glue" Classes, modules, exceptions Modular "programming-in-the-large" support A simple, clear syntax and design Readability, maintainability, ease of learning Dynamic loading of C modules Simplified extensions, smaller binary files Dynamic reloading of Python modules Programs can be modified without stopping Universal "first-class" object model Fewer restrictions and special-case rules Runtime program construction Handles unforeseen needs, end-user coding Interactive, dynamic nature Incremental development and testing Access to interpreter information Metaprogramming, introspective objects Wide interpreter portability Cross-platform programming without ports Compilation to portable bytecode Execution speed, protecting source code Standard portable GUI framework Tkinter scripts run on X, Windows, and Macs Standard Internet protocol support Easy access to email, FTP, HTTP, CGI, etc.
  • 42. Programming Python, 2nd Edition, O’Reilly IT-SC book 37 Standard portable system calls Platform-neutral system scripting Built-in and third-party libraries Vast collection of precoded software components True open source software May be freely embedded and shipped To be fair, Python is really a conglomeration of features borrowed from other languages. It includes elements taken from C, C++, Modula-3, ABC, Icon, and others. For instance, Python's modules came from Modula, and its slicing operation from Icon (as far as anyone can seem to remember, at least). And because of Guido's background, Python borrows many of ABC's ideas, but adds practical features of its own, such as support for C-coded extensions. 1.4 What's Python Good For? Because Python is used in a wide variety of ways, it's almost impossible to give an authoritative answer to this question. In general, any application that can benefit from the inclusion of a language optimized for speed of development is a good target Python application domain. Given the ever-shrinking schedules in software development, this a very broad category. A more specific answer is less easy to formulate. For instance, some use Python as an embedded extension language, while others use it exclusively as a standalone programming tool. And to some extent, this entire book will answer this very question -- it explores some of Python's most common roles. For now, here's a summary of some of the more common ways Python is being applied today: System utilities Portable command-line tools, testing systems Internet scripting CGI web sites, Java applets, XML, ASP, email tools Graphical user interfaces With APIs such as Tk, MFC, Gnome, KDE Component integration C/C++ library front-ends, product customization Database access Persistent object stores, SQL database system interfaces
  • 43. Programming Python, 2nd Edition, O’Reilly IT-SC book 38 Distributed programming With client/server APIs like CORBA, COM Rapid-prototyping /development Throwaway or deliverable prototypes Language-based modules Replacing special-purpose parsers with Python And more Image processing, numeric programming, AI, etc. "Buses Considered Harmful" The PSA organization described earlier was originally formed in response to an early thread on the Python newsgroup, which posed the semiserious question: "What would happen if Guido was hit by a bus?" These days, Guido van Rossum is still the ultimate arbiter of proposed Python changes, but Python's user base helps support the language, work on extensions, fix bugs, and so on. In fact, Python development is now a completely open process -- anyone can inspect the latest source-code files or submit patches by visiting a web site (see http://www.python.org for details). As an open source package, Python development is really in the hands of a very large cast of developers working in concert around the world. Given Python's popularity, bus attacks seem less threatening now than they once did; of course, I can't speak for Guido. On the other hand, Python is not really tied to any particular application area at all. For example, Python's integration support makes it useful for almost any system that can benefit from a frontend, programmable interface. In abstract terms, Python provides services that span domains. It is: A dynamic programming language, for situations in which a compile/link step is either impossible (on-site customization), or inconvenient (prototyping, rapid development, system utilities) A powerful but simple programming language designed for development speed, for situations in which the complexity of larger languages can be a liability (prototyping, end-user coding) A generalized language tool, for situations where we might otherwise need to invent and implement yet another "little language" (programmable system interfaces, configuration tools)
  • 44. Programming Python, 2nd Edition, O’Reilly IT-SC book 39 Given these general properties, Python can be applied to any area we're interested in by extending it with domain libraries, embedding it in an application, or using it all by itself. For instance, Python's role as a system tools language is due as much to its built-in interfaces to operating system services as to the language itself. In fact, because Python was built with integration in mind, it has naturally given rise to a growing library of extensions and tools, available as off-the-shelf components to Python developers. Table 1-2 names just a few; you can find more about most of these components in this book or on Python's web site. Table 1-2. A Few Popular Python Tools and Extensions Domain Extensions Systems programming Sockets, threads, signals, pipes, RPC calls, POSIX bindings Graphical user interfaces Tk, PMW, MFC, X11, wxPython, KDE, Gnome Database interfaces Oracle, Sybase, PostGres, mSQL, persistence, dbm Microsoft Windows tools MFC, COM, ActiveX, ASP, ODBC, .NET Internet tools JPython, CGI tools, HTML/XML parsers, email tools, Zope Distributed objects DCOM, CORBA, ILU, Fnorb Other popular tools SWIG, PIL, regular expressions, NumPy, cryptography 1.5 What's Python Not Good For? To be fair again, some tasks are outside of Python's scope. Like all dynamic languages, Python (as currently implemented) isn't as fast or efficient as static, compiled languages like C. In many domains, the difference doesn't matter; for programs that spend most of their time interacting with users or transferring data over networks, Python is usually more than adequate to meet the performance needs of the entire application. But efficiency is still a priority in some domains. Because it is interpreted today,[3] Python alone usually isn't the best tool for delivery of performance-critical components. Instead, computationally intensive operations can be implemented as compiled extensions to Python, and coded in a low-level language like C. Python can't be used as the sole implementation language for such components, but it works well as a frontend scripting interface to them.
  • 45. Programming Python, 2nd Edition, O’Reilly IT-SC book 40 [3] Python is "interpreted" in the same way that Java is: Python source code is automatically compiled (translated) to an intermediate form called "bytecode," which is then executed by the Python virtual machine (that is, the Python runtime system). This makes Python scripts more portable and faster than a pure interpreter that runs raw source code or trees. But it also makes Python slower than true compilers that translate source code to binary machine code for the local CPU. Keep in mind, though, that some of these details are specific to the standard Python implementation; the JPython (a.k.a. "Jython") port compiles Python scripts to Java bytecode, and the new C#/.NET port compiles Python scripts to binary .exe files. An optimizing Python compiler might make most of the performance cautions in this chapter invalid (we can hope). For example, numerical programming and image processing support has been added to Python by combining optimized extensions with a Python language interface. In such a system, once the optimized extensions have been developed, most of the programming occurs at the higher-level Python scripting level. The net result is a numerical programming tool that's both efficient and easy to use. Moreover, Python can still serve as a prototyping tool in such domains. Systems may be implemented in Python first, and later moved in whole or piecemeal to a language like C for delivery. C and Python have distinct strengths and roles; a hybrid approach, using C for compute-intensive modules, and Python for prototyping and frontend interfaces, can leverage the benefits of both. In some sense, Python solves the efficiency/flexibility tradeoff by not solving it at all. It provides a language optimized for ease of use, along with tools needed to integrate with other languages. By combining components written in Python and compiled languages like C and C++, developers may select an appropriate mix of usability and performance for each particular application. While it's unlikely that it will ever be as fast as C, Python's speed of development is at least as important as C's speed of execution in most modern software projects. On Truth in Advertising In this book's conclusion we will return to some of the bigger ideas introduced in this chapter, after we've had a chance to study Python in action. I want to point out up front, though, that my background is in Computer Science, not marketing. I plan to be brutally honest in this book, both about Python's features and its downsides. Despite the fact that Python is one of the most easy-to-use programming languages ever created, there are indeed some pitfalls, which we will examine in this book. Let's start now. Perhaps the biggest pitfall you should know about is this one: Python makes it incredibly easy to throw together a bad design quickly. It's a genuine problem. Because developing programs in Python is so simple and fast compared to traditional languages, it's easy to get wrapped up in the act of programming itself, and pay less attention to the problem you are really trying to solve. In fact, Python can be downright seductive -- so much so that you may need to consciously resist the temptation to quickly implement a program in Python that works, and is arguably "cool," but leaves you as far from a maintainable implementation of your original conception as you were when you started. The natural delays built in to compiled language development -- fixing compiler error messages, linking libraries, and the like -- aren't there
  • 46. Random documents with unrelated content Scribd suggests to you:
  • 47. THE BATTLE OF APACHE CAÑON. March 28, 1862. The immediate consequence of the battle of Valvende was that the insurgents marched directly past Fort Craig, which for want of men and provisions they were powerless to invest or capture, direct on Albuquerque and Santa Fé, which fell into their power without resistance. Albuquerque was the depot of United States Government stores, most of which was removed on the advance of the insurgents, and the rest destroyed. The occupation of Santa Fé was followed by the proclamation of a provisional government, which however never entered into practical operation. Fort Craig still remained in the rebel rear, and Fort Union in the possession of the national troops, on the north-east, from which direction reinforcements might be expected. The policy of the insurgents was therefore either to capture Fort Union before relief could arrive, or maintain their position, isolating Fort Craig until that post should be compelled to surrender for want of supplies. Meantime, news of the critical condition of affairs having reached the Colorado territory and Kansas, troops were at once organized to go to the relief of the threatened positions. By forced marches, scarcely paralleled in history, a Colorado regiment 950 strong, under Colonel Hough, reached Fort Union on the 13th of March. Here he gathered around him all the troops available, or possible to obtain, and marched for Santa Fé, to give battle to the invaders. The latter moved their forces forward to meet him. The numbers on both sides were nearly equal—between 1,200 and 1,500. They met at a point called Apache Pass.
  • 48. The main fight took place at Apache Cañon, eighty miles from Fort Union, and twenty miles from Santa Fé. Three battalions, one under Major Chivington, one under Captain Lewis, and one under Captain Wynkoop, advanced to the cañon, on the 28th, when the pickets reported no enemy in sight. The command then advanced, when shots were fired at them by the Texans, who were in ambush and succeeded in killing four privates. The Union men, under Hough, rushed on them, killing 20 or 30 Texans, wounding many of them, and taking seven prisoners, four officers and three privates. Major Chivington’s command, which went ahead and surprised the Texan pickets, taking 67 prisoners, and 64 provision wagons, now arrived, and a plan of action was determined upon. It was to meet the enemy in front and flank them at the same time. About 12 o’clock they advanced, and the action became general, the Coloradans doing wonders. The battery under Captain Ritter,
  • 49. and also the howitzer battery under Lieutenant Claflin, swept the Texans from the field. The fight lasted until four o’clock, when flags of truce were interchanged to bury the dead and care for the wounded. The enemy had about 2,000 men and one 6-pounder. The Unionists had 1,300 men, one six and one 12-pounder, and four howitzers. The enemy lost their entire train (64 wagons and provisions), 230 mules, about 150 killed, 200 wounded and 93 taken prisoners, among whom were 13 officers. The Texans, when surprised, supposed it was Colonel Canby’s force that was coming. The Texan officer in command, with two of his companies, made several attempts to charge on the Union men and seize their batteries, but they were each time repulsed, with tremendous loss, while daring, noble deeds were performed by the Federal soldiers. At one time, the Texan companies charged within a few yards of the Union batteries. The defeat at Apache Pass proved an effectual check on the invaders, and so far weakened their forces as to compel their abandonment of the territory, and its complete restoration under the national authority. The enemy fled into Arizona, where they found it useless to remain, and applied to the authorities of Mexico for permission to cross their territory on their return home, but were refused; they however succeeded in reaching Texas. A reinforcement of Federal troops soon after arrived in New Mexico.
  • 50. FIGHT AT BLOOMING GAP, VA. February 14, 1862. To General F. W. Lander’s brigade had been assigned the perilous duty of protecting the Baltimore and Ohio railroad at Cumberland, Md., and the various towns and strategic points in Virginia within a radius of forty or fifty miles from that centre, at several of which his troops were quartered. On the 13th of February, Lander received information that a brigade of rebels under General Carson had occupied Blooming Gap, a strong pass in the mountains seven miles beyond the Cacapon river, whose turbid waters, swollen by the storms of winter, were deemed an impassable barrier to the advance of the Federal forces. No bridge spanned the torrent, and the blackened buttress and crumbled pier gave evidence that the incendiary torch had been at work. Lander was then at Pawpaw Tunnels, on the Maryland shore of the Potomac, a station on the Baltimore and Ohio railroad, midway between Hancock and Cumberland, with a small force. He immediately marched to New Creek, in Hampshire county, Va., to join the detachment of troops at that point, where he also hastily concentrated all his available command. Taking twenty wagons loaded with lumber, he proceeded to a point on the Cacapon river, seven miles south of the railroad, and between the hours of nine and one o’clock at night he improvised a bridge one hundred and eighty feet long, by placing the wagons in the river as a foundation, over which he marched his force of four thousand men, and advanced upon the enemy’s pickets before the dawn of day.
  • 51. With five hundred of the First Virginia cavalry, under Colonel Anastanzel, he had designed to charge through the rebel camp at the Gap, and then form immediately in his rear, cut off the retreat, and capture the whole force, after the Federal infantry, following up the cavalry charge, should have completed the discomfiture of the enemy. But the rebels had retired before Lander’s approach; and when led by the General and his staff, the cavalry flew through the Gap and beyond it, they met with no opposition. Colonel Anastanzel was at once ordered to push forward on the Winchester road with the cavalry, reconnoitre, and, if possible, overtake and capture the baggage of the enemy. General Lander meantime brought up Colonel Carroll with the Eighth Ohio regiment, and the Seventh Virginia, Colonel Evans, for a support. Colonel Anastanzel encountered the enemy at the head of the pass, two miles from Blooming. He was met by a sharp fire, and halted his command. On hearing the firing, General Lander came up and led the charge, followed by Major Armstrong, Assistant Adjutant-General; Lieutenants Fitz-James O’Brien, the well-known poet of his staff, and Major Bannister, Paymaster U.S.A., who had volunteered for the expedition. A group of rebel officers were distant about three hundred yards, encouraging their men. General Lander being the best mounted, outran the rest of the party, and cut off the retreat of the rebel officers. “Surrender, gentlemen,” he said, coolly dismounting, and extending his hand to receive the sword of Colonel Baldwin, over whom an instant before he had appeared to be riding. Five of the rebel officers surrendered to General Lander, and four others immediately afterward, to the officers of his staff, among them the Assistant Adjutant-General of General Carson. By this time the rebel infantry, perceiving the small number of their adversaries, commenced a heavy fire from the woods, but the cavalry had recovered from its panic, and now rushed up the hill. General Lander ordered Anastanzel to charge up the road, and capture the baggage of the enemy. The cavalry dashed forward, and the advance guard soon overtook and turned fifteen wagons and horses out of the road. Colonel Evans now came up with his regiment of infantry, and captured many more of the rebels. Colonel Carroll cleared the road as he went, both infantry regiments behaving
  • 52. admirably, following and engaging the enemy to the last, until ordered back. The pursuit was continued eight miles. The result of this affair was the capture of eighteen commissioned officers, and forty-five non-commissioned officers and privates. Thirty-three of the rebels were killed and wounded, with a loss on the Union side of seven killed and wounded. During this engagement Lieutenant Fitz-James O’Brien was shot mortally while in advance of his comrades, and like the author- soldier Winthrop, immortalized his name with the sword, as he had before proved himself great with the pen. General Dunning, of Lander’s command, returned to New Creek the same day from an expedition to Moorfield, forty miles south of Romney, having captured 225 beef cattle and 4,000 bushels of corn. In a skirmish two of his men were wounded, and several rebels killed.
  • 53. EAST TENNESSEE UNDER CONFEDERATE RULE. The history of the world has never exhibited more exalted devotion to an idea, nor a more splendid patriotism than that of the people of East Tennessee. We may almost challenge the records of religious history to produce anything more like holy enthusiasm, than the lofty inspiration which has characterized these people. In no country, and among no class can be found more heroic persistence or unfaltering adherence to principle than has exalted the patriotism of this region. With many inhabitants of the eastern portion of the State, loyalty and devotion to the Union became in truth a part of their religion. The rebel leaders knew that they had very little sympathy in East Tennessee, and took measures to crush out all Union sentiment with the iron heel of military despotism. Any expression of sympathy with the Union cause, any co-operation of its inhabitants with the loyalists, either for their own protection or for the aid of the Government, was punished as a crime. The presses of that part of the State had all been silenced or converted to their own use by the Secessionists, with one exception. The Knoxville Whig remained true to the Union. Its vigorous defence of the Government, its exposures and denunciations of the rebel leaders, its unsparing invective against the rebellion, and its bold, defiant appeals to the people, rang like a clarion through the hills and valleys of East Tennessee, and as the echo gathered from thousands of loyal voices, it made itself heard through all the valleys and mountain passes of that noble border State. The heroic editor of this paper was not to be silenced either in his voice or his press without a vigorous struggle. The Rev. Wm. G. Brownlow had learned how to denounce and how to endure, for that is a lesson most Methodist clergymen are called upon to learn; and
  • 54. being brave in deeds as well as words, he stood forth in defence of the country he loved, when she greatly needed the power of his eloquence and the strength of his arm. The popularity which this man had won by his uprightness, his courage, and firm adherence to the Constitution, gave his opinions a force that made him an object of peculiar importance to the enemy—yet they hesitated to lay violent hands upon a man whose words were more potent than their bayonets. He was frequently threatened by soldiers passing through Knoxville from other States, yet none dared to execute their threats. His family were inspired with the same lofty heroism, and on one occasion when a company of rebels came to his house to haul down the Stars and Stripes, which was kept floating over his domicil, one of his daughters stepped out to meet them, and by her courage and decision protected the flag. The suppression of this undaunted advocate of the Union, and faithful and fearless witness against secession, became an inevitable necessity; and at last, in the hope that he would at least become silent on political affairs, it was resolved to offer him the alternative of the oath of allegiance or the cell of a prison. He chose the latter, and in a valedictory to his readers, published October 26, which must ever be memorable for its heroic defence of the Union, its bold denunciation of the rebels and their course, he announced to his readers the suspension of his paper. This remarkable address, which, under the circumstances, rises to the sublime in its moral courage, closed with these words: “Exchanging, with proud satisfaction, the editorial chair and the sweet endearments of home for a cell in the prison, or the lot of an exile, I have the honor to be, &c. William G. Brownlow.” Mr. Brownlow was sent to prison, and for months occupied a room with several other patriots who preferred imprisonment to denial of the government they loved. Here he was in daily expectation of being led forth to execution. Though suffering from ill-health he was no way daunted by the dark fate that threatened him. Nor were these
  • 55. anticipations groundless, for during his stay there, many a brave man left that prison to meet a violent death, and he had no reason to expect a happier destiny. During the closing months of the summer and fall the hopes of the people were excited by promises of aid from the government. Loud and earnest appeals were made for help, and with the energy of despair the people clung to their principles, through every species of persecution, robbery, arson, and imprisonment. Hundreds were hung or assassinated, and the records of Tennessee are among the most heart-rending that this war for the Union will leave to posterity. The position of the rebel armies in western Tennessee was at that time very strong, but the importance of keeping their lines of communication open with the Atlantic States was great, and thoroughly understood by the loyalists. To cut these lines was to the Federals a work of pressing necessity; and in view of the probable redemption of East Tennessee, the loyalists organized, and on the night of November 8 they destroyed several bridges, and broke the lines. Two of these were on the Georgia State road, two on Chickamanye Creek, Hamilton county, and one on the East Tennessee and Georgia railroad, on Hiawassee river, Bradley county. Besides these, two bridges on the East Tennessee and Georgia railroad on Lick Creek, Green County, and another on Holstein river, were also burned. The rebels were thrown into consternation by these events, and their leaders took the most active measures to arrest and punish the perpetrators. A correspondence between some of the prominent men ensued, and a large portion of the letters was discovered among other papers and effects captured after the battle of Mill Spring, which took place on the 19th of January, 1862. This correspondence, in which the names Colonel William B. Wood and General F. K. Zollicoffer appear, prove that the majority of the people were unalterably for the Union, and that they could only be restrained by the most oppressive and cruel measures. Colonel Wood wrote to J. P. Benjamin, the Secretary of War, asking what disposition should be made of the bridge-burners, to which Mr. Benjamin replied—“All such as can be identified as having been engaged in bridge-burning are to be tried summarily by drumhead court-martial, and if found guilty, executed on the spot by hanging. It
  • 56. would be well to leave their bodies hanging in the vicinity of the burnt bridges.” The loyalists were encouraged in their cause by the devotion of Hon. Andrew Johnson, U. S. Senator, and Hon. Horace Maynard, M. C., for Tennessee, whose eloquent and powerful appeals, and confident assurances of aid, cheered the hearts of the people. Thousands of East Tennesseeans escaped by night, wandering along unfrequented roads, until they reached Kentucky, where they organized regiments, under the direction of the Federal commanders. Their cherished desire was to return to their own State, with a powerful army, and redeem their soil. The atrocity of the rebel guerrillas drove them almost to a passion of revenge, and when disappointed at the announcement that their time had not come, and that they must await a more favorable condition of the army, hundreds of them, when ordered to retreat from the border lines of their State, strayed from the ranks, despairing and heart-sick, and falling down by the way, wept bitterly. Several of them, exhausted by hard labor and forced marches, never rose again, but were afterwards found dead on the road to Mount Vernon. On the 26th of November the house of a gentleman named Bell was attacked by an armed party of the enemy and set on fire. The inmates, a large family of nine persons, were consigned to the flames. Two alone of the whole household escaped this horrible fate. On the 29th a band of twenty-one Union prisoners at Nashville were compelled to take the oath of allegiance, and enter a company in the rebel army. Leadbetter, the secession commander in East Tennessee, had his headquarters at Greenville, and on the 30th of November issued a proclamation promising protection and pardon to all who would lay down their arms and submit to the Confederate government. From this clemency he excepted bridge-burners and destroyers of railroad tracks. He closed his proclamation with the assurance that “they will be tried by drumhead court-martial, and be hung on the spot.” This terrible order was put into execution a few days afterward. Jacob M. Hemslier and Henry Fry, two Unionists, being tried and pronounced guilty of these offences, were hung.
  • 57. The days of hope for the Unionists were weary and prolonged, but deliverance was drawing nigh. The loyal men of the western part of the State organized to oppose the measures of the leaders, and early in January a bold resistance was made in Carroll, Weakly, McNairy, and other counties, against the conscription act. Rebel troops were sent into these counties to compel submission, and enforce obedience. The defeat and death of Zollicoffer, the breaking up of his army, and the destruction of his stronghold, at last gave a brilliant promise to these persecuted people that their deliverance was drawing nigh. This event, succeeded in a few weeks by the capture of Fort Henry, Fort Donelson, the evacuation of Bowling Green and Columbus, and the occupation of Nashville, filled every true heart with rejoicing, and the good old flag once more swept its folds freely over the houses of East Tennessee. BOMBARDMENT OF FORT HENRY, FEB. 6, 1862. The appointment of Hon. Andrew Johnson as military governor of Tennessee was greeted with enthusiasm by the people. His reputation and conservative principles were a guarantee for the character of his administration, and he soon began to rally to his support the wavering and timid of the people who were still
  • 58. apprehensive that the Confederates would return and restore their rule. Parson Brownlow, after having borne a long and severe confinement in prison, in which his health suffered terribly, was released, and sent beyond the military lines of the Confederates. His reception by the Federal guards was enthusiastic and joyous in the extreme. As soon as his health permitted he visited several cities in the West, where he was greeted with overwhelming demonstrations of popular admiration and respect. On his arrival at New York, May 17th, he was honored with a public reception at the Academy of Music, which was densely filled with a brilliant audience, eager to welcome him.
  • 59. CAPTURE OF FORT HENRY, TENN. February 6, 1862. The brilliant victory obtained by General Thomas’ army over the Confederate forces at Mill Spring, on the 19th of January, laid open the rebel lines to the successful advance of the Federal arms, and served to stimulate the commanders of the land and naval forces to avail themselves of the opportunity thus afforded. Previous to the battle of Mill Spring, General Grant, with a large force, had left Cairo and marched toward Columbus, for the purpose of reconnoitering the country, and to prevent rebel reinforcements moving from that point to the assistance of General Buckner, at Bowling Green, Ky., who was then threatened by the approach of General Buell’s army. Upon the return of General Grant’s division to Cairo, a combined movement of the land and naval forces was determined on for the purpose of capturing Fort Henry, on the Tennessee river, in Henry county, Tenn., just beyond the Kentucky State line. Fort Henry and its approaches were reconnoitered on the 21st January by the United States gunboat Lexington, with a view to ascertain its strength and the position of the rebels. She went within two miles of the fort, and flung a number of shells into it without eliciting any reply. At first it was thought the rebels had evacuated the work, but on approaching it still nearer pickets were discovered at various points. The heavy guns on the work were seen distinctly; also a number of field pieces. In addition to the fort proper, numerous earthworks had been thrown up on a high bluff above the fort, on the west bank of the river. This additional work, named Fort Hieman, commanded Fort Henry.
  • 60. On the 22d January, Brigadier-General C. F. Smith, commanding the second division of General Grant’s army, was at Crown Point, Ky., where he had arrived with 6,000 men after a fatiguing march of over 100 miles from Paducah. He proceeded thence on a personal reconnoissance, on the gunboat Lexington, in the direction of Fort Henry. The gunboat advanced up the west channel of the river to a point within one mile and a half from the fort. General Smith obtained an excellent view of the rebel fort, camp and garrison, and sent his report to headquarters. He then marched his division back to Paducah. The flotilla of gunboats, which had been so long in course of preparation on the Ohio and Mississippi, was now ready to take part in the impending battles of the nation, and to assume that prominence in the momentous events which were to follow to which they have proved themselves justly entitled. Flag-officer Andrew H. Foote was appointed by the Government to command the naval forces on the Upper Mississippi and the Western waters, and now led forth his gallant fleet to attack the enemy, in conjunction with the land forces under General Ulysses S. Grant. The fleet consisted of Fleet Officers.—Flag-Officer Andrew H. Foote; Fleet Captain, Commodore A. M. Pennock; Ordnance Officer, Lieutenant J. F. Sanford; Ordnance Lieutenant, Byron Wilson; Flag Lieutenant, James M. Prickett. Essex, 9 guns, Commander William D. Porter. St. Louis, 13 guns, Lieutenant-Commanding Leonard Paulding. Cincinnati, 13 guns, Commander R. N. Stembel. Carondelet, 13 guns, Commander Henry Walke. Conestoga, 9 guns, Lieutenant- Commanding —— Phelps. Tyler, 9 guns, Lieutenant-Commanding W. Gwin. For several days, at Paducah, the utmost vigilance was exercised at the headquarters of the Provost Marshal, in issuing passes, and on Sunday and Monday, the 3d February, no persons were allowed in or out of the lines. Half a dozen gunboats steamed leisurely into port and brought their black forms to anchor opposite the levee, in the centre of the river. Monday afternoon, steamers commenced coming up from Cairo, laden with troops and stores, and by night the whole landing in front of the town was crowded with the arrivals. The fleet which came up
  • 61. brought General Grant and Staff, and the first division, under command of Brigadier-General McClernand. The steamers were under command of Commodore G. W. Graham, and consisted of the following boats: City of Memphis, Iatan, D. A. January, Chancellor, Alp, “W. H. B.,” New Uncle Sam, Rob Roy, Alex. Scott, Minnehaha, Illinois, Emerald, and Fanny Bullett. The first division, on these boats, was made up of two brigades, composed as follows, and commanded by General John A. McClernand:—First Brigade, Colonel Oglesby, Commanding.— Seventh Illinois, Colonel Cook; Eighth Illinois, Lieutenant-Colonel Rhoades; Eighteenth Illinois, Lieutenant-Colonel Lawler; Twenty- ninth Illinois, Colonel Reardon; Thirtieth Illinois, Lieutenant- Colonel Dennis; Thirty-first Illinois, Colonel John A. Logan; Swartz’s and Dresser’s Batteries; Stewart’s, Dollins’, O. Harnett’s and Carmichael’s Cavalry. Second Brigade, W. H. L. Wallace, Commanding.—Eleventh Illinois, Lieutenant-Colonel Hart; Twentieth Illinois, Colonel Marsh; Forty-fifth Illinois, Colonel Smith; Forty-eighth Illinois, Colonel Harney; Taylor’s and McAllister’s Batteries—in the latter four siege guns; Fourth Illinois Cavalry, Colonel Kellogg; Seventh Illinois Cavalry, Colonel Dickey. Soon after arriving, General Grant and staff paid a visit to General Smith, and had a conference, in which it was determined to forward the division of General McClernand that night, and after landing them at some point below Fort Henry, out of range of its guns, send the boats back after General Smith’s division at Paducah. It was nearly midnight before the boats took their departure. The point at which the troops were landed is about four or five miles below Fort Henry, opposite a small town in Kentucky, called Buffalo. Immediately at the place is a clearing of about one hundred acres, surrounded on three sides by high bluffs densely timbered, and reaching down to the river. The troops, on landing, immediately took possession of these eminences, and planted batteries which commanded the country in every direction, and then awaited the arrival of the remaining forces, under General Smith. Tuesday afternoon, while the troops were disembarking, the Osband Cavalry, with Carson’s and Carpenter’s scouts thoroughly examined the country in every direction, even up to within two miles
  • 62. of Fort Henry. Tuesday night was beautiful; a thousand camp-fires flashed through the shadows that lay upon the amphitheatre of wooded hills. The sky was warm and serenely purple, as if brooding over the first sweet blossoms of May. The silver crescent of a new moon glittered in the western sky, shedding a faint radiance over the tree-tops and sloping hill sides. All at once the music of half a dozen bands broke through the stillness of this lovely scene, and the “Star- Spangled Banner,” “Red, White and Blue,” and “Columbia the Gem of the Ocean,” filled the night with bursts of patriotic music. Then some dreamy strain followed, hushing the soldier’s heart with thoughts of “Home, Sweet Home.” On Wednesday, parties were out reconnoitering near the enemy’s works, and in one case a squad of cavalry went within a mile of the fort and encountered two hundred rebel horsemen. Both sides fired, when the rebels ran, leaving one of their number dead, and carrying off three severely wounded. One man on the Union side was shot through the brain, and killed instantly. He was the first man who gave up his life in the vicinity of Fort Henry. It had been noticed that a steamer belonging to the rebels was busily engaged in running from the fortifications to some point up or across the river, which was doubtless bringing in reinforcements. Two of the gunboats—the Taylor and Conestoga, ran up to nearly the centre of the island, and dropped a few shells in the direction of the fort and the steamer, with what result was not known. They effected a thorough reconnoissance on both sides, and discovered two ugly torpedoes sunk in the west channel, which they carefully hauled out and towed down to the shore below. During the day and night the division of General Smith, from Paducah, arrived, and was landed on the west shore of the river, with a view of operating against batteries supposed to be on that side, and also to counteract a large body of troops, which scouts reported to be concentrating opposite the fort. Wednesday night was cold and most disagreeable. About eight o’clock a heavy storm set in, which speedily quenched the camp-fires, and sent the troops wet and disconsolate under any shelter that could be found. All over the southern horizon, in the direction of Fort Henry, a tremendous thunder storm swept its way, filling the hills with flashes of fiery blue lightning, and shaking the forests with loud
  • 63. reverberations of thunder. Hailing this burst of heaven’s artillery, rolling southward toward the enemy, as a good omen, the Union soldiers pulled the wet blankets closer around them, turned drearily in the yielding mud, and fell asleep. Thursday dawned cloudily, but towards nine o’clock it cleared up and the sun came out warm and gloriously. Nature nowhere seemed to anticipate the bloody event which gives the day prominence. A few more troops arrived, among whom were the Ohio Seventh, Colonel Lauman, and the Ohio Twelfth, Colonel Wood, both from Smithland, and which, together with the Seventh Illinois, Lieutenant-Colonel Bancock; Thirteenth Missouri, Colonel Wright; the Fifteenth Illinois, Colonel ——, and Company D, First Missouri Artillery, made the Third Brigade, Colonel John Cook commanding, assigned the right wing of the advance up the Tennessee shore. About ten o’clock the gunboats started slowly up the river, four iron clad steamers leading abreast—the Essex, Captain Porter, on the right, and the Cincinnati, Commodore Foote, on the left. The three wooden gunboats ranged themselves abreast and followed, half a mile or so to the rear. The iron-clad boats moved up abreast, keeping up the west or high water channel. Almost immediately on passing the lower end of the island, the boats and the forts were in each others’ range, but on both sides an ominous silence was preserved—a silence that betokened deadly intent on the part of the belligerents. On swept the boats, coming in full view of the long line of breastworks that broke the east shore—in full view of the black muzzles of the heavy guns which seemed watching the approach of the gallant little fleet in ominous silence—in full view of the flag waving defiantly from a high staff in the centre of the works, until one could almost see down the huge bore of the guns, the bright straps of the shells, which seemed like leashes to prevent the deadly missiles from springing forth upon their work of destruction—and yet not a trigger was pulled on either side. Less than a mile separated the fleet and the fort, and yet not a word was said. The insurgents appeared to be confidently anticipating the conflict; and grouped like statues around their guns, with lanyards stretched, they waited for the onset.
  • 64. When about six hundred yards from the fort, the bow-guns of the flag-ship poured their contents into it, and so close after, that the reports seemed almost one, the other three poured in their fire. Scarcely had the smoke cleared from the muzzles of the pieces, ere the whole ten guns of the rebels belched forth their contents, sending a terrific iron shower in, above and around the gunboats. Taking their cue from the others, the three wooden gunboats, which were about a mile below, opened from their bow-guns, and then the contest was fairly begun. For one hour the roar was so incessant that the successive reports of the guns could not, in many cases, be distinguished. Occasionally there would be a momentary lull—then a single reverberating roar would give the key-note, and an instant after all the voices would swell together in one tremendous chorus. A thick cloud of smoke enveloped the boats, hiding them completely from view. Over them hovered a dense white vapor, from which quick flashes of flame leaped and quivered, incessantly followed by delicate balloon-like forms of smoke, which burst like ghostly shadows from the enemy’s shells. From the very first, the fire of the rebel guns seemed directed at the Essex. In their first volley two thirty-two pound shots struck the Essex on the starboard bow, indenting deeply the iron sheathing, and then glanced off, down the river, while a perfect storm of the iron missiles whistled over her decks, and plowed into the water on either side. She received in all eleven shots—one of which carried death through the whole length of the vessel. It entered a larboard port, carried off the head of the master’s mate, and passing on, entered the boiler. The steam and water poured out, filling the whole space between decks, and causing more destruction than all the enemy’s missiles put together—four men were instantly suffocated, and some twenty-five severely scalded, among whom was the gallant Commander Porter. The two pilots, who were in the pilot-house above, had no escape except through a passage from below, and up this the steam rushed, as if coming from a safety-valve, and of course with fatal effect. Both these poor men perished. Of course the Essex was thenceforth unmanageable. She slowly drifted down the main channel, and was soon after met by a steamer, which towed her down to the place occupied by the boats before starting. Soon after the Essex became disabled, the pelting of the iron
  • 65. storm proved too hot for endurance, and the rebel flag came rapidly down. The firing on the part of the gunboats immediately ceased, and messengers were sent off from the flag-boat, which found, upon landing, that the rebels were disposed to an unconditional surrender. In scarcely more than an hour after the first attack, the flag of Fort Henry was in the dust. The fort was soon after taken possession of, and it was found that the sum total of rebel prisoners was between seventy and one hundred, the balance having left the night before on the steamer Dunbar. Among those who surrendered were Brigadier-General Tilghman, Major Corrico, Colonel Carmichael, Captain Hayden, of the Engineers, and Captain Miller, with several other commissioned officers. Ten of the rebels were found killed, and some twelve or fifteen wounded. Three hundred and six tents were found on the west side of the river, and about as many near the fort, all of which bore evidences of the haste with which the rebels had evacuated their quarters. Several hundred stands of arms were found, chiefly squirrel rifles and double-barrelled shot-guns, also a large amount of clothing, forage, provisions, wagons, mules and horses. There was a large supply of ammunition, and when the Union forces entered the fort there was beside each gun an abundance unexpended. The tents were new and of excellent make, sufficient to shelter five or six thousand men. The enemy had flour, corn, bacon and sugar in large quantities, but no salt, and not a large supply of beef. There were nineteen guns in position, of the following calibre: two 128-pounders, one 80-pounder, two 42-pounders, rifled, ten 32- pounders, two 24-pound howitzers, two 12-pound howitzers. Three 6-pound smooth bores, five 6-pound rifles, found outside the intrenchments. A twenty-four-pound rifled gun exploded on the fourth round, and near the close of the fight a shell from one of the Union boats entered the eighty-pounder and burst, disabling it. Several caissons were captured in the redan upon the west side of the river, but no guns were in position.
  • 66. Evidences abounded on all sides of the deadly accuracy of the Federal gunners. Every one of the eleven log buildings within the ramparts was perforated with shot, the roof of one of the small magazines was torn open, hurdle-work scattered in all direction, half the guns knocked out of place, and great gulleys cut in the parapet and the ground. A thirty-two pounder bearing upon the gunboats had been struck by a Union shell, completely shattering the muzzle. The ground beside the embrasure was stained with blood, which lay in pools on the uneven surface. Beside one of the buildings, with gray blankets thrown hastily over them, lay six dead soldiers, all fearfully mutilated. Inside, ten wounded men were stretched upon cots, or on the ground, some insensible, and others rending the air with groans, while the surgeons of the garrison were attending upon them. Just above, on the river, was the hospital ship of the rebels, the stern- wheel steamer R. M. Patten, which had been captured with the fort. The ensign of disease, the yellow flag, was flying from the staff, waving off destruction from sixty invalids.
  • 67. Welcome to our website – the ideal destination for book lovers and knowledge seekers. With a mission to inspire endlessly, we offer a vast collection of books, ranging from classic literary works to specialized publications, self-development books, and children's literature. Each book is a new journey of discovery, expanding knowledge and enriching the soul of the reade Our website is not just a platform for buying books, but a bridge connecting readers to the timeless values of culture and wisdom. With an elegant, user-friendly interface and an intelligent search system, we are committed to providing a quick and convenient shopping experience. Additionally, our special promotions and home delivery services ensure that you save time and fully enjoy the joy of reading. Let us accompany you on the journey of exploring knowledge and personal growth! ebookfinal.com