Read Programming Python Online

Authors: Mark Lutz

Tags: #COMPUTERS / Programming Languages / Python

Programming Python (2 page)

BOOK: Programming Python
13.67Mb size Format: txt, pdf, ePub
ads

Although new topics were added, all told, four chapters were cut
outright (the nontechnical introduction, one of the system example
chapters, advanced Internet topics, and one integration chapter), some
additional examples and material were trimmed (including PyForm and
PyTree), and focus was deliberately restricted to Python 3.X and
application fundamentals to conserve space.

What’s Left, Then?

The combined effect of all the changes just outlined is that this
edition more concisely and sharply reflects its core focus—that of a
tutorial introduction to ways to apply Python in common programming
domains. Nevertheless, as you can tell from this book’s page count, it is
still a substantial and
in-depth
book, designed to be
a first step on your path to mastering realistic applications of
Python.

Contrary to recent trends (and at some risk of being branded a
heretic), I firmly believe that the job of books like this one is to
elevate their readers, not pander to them. Lowering the intellectual bar
does a disservice both to readers and to the fields in which they hope to
work. While that means you won’t find as many cartoons in this book as in
some, this book also won’t insult you by emphasizing entertainment at the
expense of technical depth. Instead, the goal of my books is to impart
sophisticated concepts in a satisfying and substantive way and to equip
you with the tools you’ll need in the real world of software
development.

There are many types of learners, of course, and no one book can
ever satisfy every possible audience. In fact, that’s why the original
version of this book later became two, with language basics delegated to
Learning Python
.
Moreover, one can make a case for a distinction between
programmers
, who must acquire deep software
development skills, and
scripters,
who do not. For
some, a rudimentary knowledge of programming may be enough to leverage a
system or library that solves the problem at hand. That is, until their
coding forays start encroaching on the realm of full-scale software
engineering—a threshold that can inspire disappointment at worst, but a
better appreciation of the challenging nature of this field at
best.

No matter which camp you’re from, it’s important to understand this
book’s intent up-front. If you’re looking for a shortcut to proficiency
that’s light on technical content, you probably won’t be happy with this
book (or the software field in general). If your goal is to master
programming Python well, though, and have some fun along the way, you’ll
probably find this book to be an important piece of your learning
experience.

At the end of the day, learning to program well is much more
demanding than implied by some contemporary media. If you’re willing to
invest the focus and effort required, though, you’ll find that it’s also
much more rewarding. This is especially true for those who equip
themselves for the journey with a programmer-friendly tool like Python.
While no book or class can turn you into a Python “Master of the Universe”
by itself, this book’s goal is to help you get there, by shortening your
start-up time and providing a solid foundation in Python’s most common
application domains.

Python 3.X Impacts on This Book

As mentioned, this edition now covers Python 3.X only. Python 3.X is
an incompatible version of the language. The 3.X core language itself is
very similar to Python 2.X, but there are substantial changes in both the
language and its many standard libraries. Although some readers with no
prior background in 2.X may be able to bypass the differences, the changes
had a big impact on the content of this edition. For the still very large
existing Python 2.X user base, this section documents the most noteworthy
changes in this category.

If you’re interested in 2.X differences, I also suggest finding a
copy of the Fourth Edition of the book
Python Pocket
Reference
described earlier. That book gives both 2.X
and 3.X versions of core language structures, built-in functions and
exceptions, and many of the standard library modules and tools used in
this book. Though not designed to be a reference or version translator per
se, the Fourth Edition of
Learning
Python
similarly covers both 2.X and 3.X, and as
stated, is prerequisite material to this book. The goal of this 3.X-only
Programming
Python
is not to abandon the current vast 2.X user base
in favor of a still imaginary one for 3.X; it is to help readers with the
migration, and avoid doubling the size of an already massive book.

Specific 3.X Changes

Luckily, many of the 2.X/3.X differences that impact this book’s
presentation are trivial. For instance, the
tkinter
GUI toolkit, used extensively in this
book, is shown under its 3.X
tkinter
name and package structure only; its 2.X
Tkinter
module incarnation is not described.
This mostly boils down to different import statements, but only their
Python 3 versions are given here. Similarly, to satisfy 3.X module
naming conventions, 2.X’s
anydbm
,
Queue
,
thread
,
StringIO.StringIO
, and
urllib.open
become
dbm
,
queue
,
_thread
,
io.StringIO
, and
urllib.request.urlopen
, respectively, in both
Python 3.X and this edition. Other tools are similarly renamed.

On the other hand, 3.X implies broader idiomatic changes which
are, of course, more radical. For example, Python 3.X’s new Unicode
awareness has inspired fully Internationalized versions of the PyEdit
text editor and the PyMailGUI email client examples in this edition
(more on this in a moment). Furthermore: the replacement of
os.popen2
with the
subprocess
module required new examples; the
demise of
os.path.walk
in favor of
os.walk
allowed some examples to be
trimmed; the new Unicode and binary dichotomy of files and strings
impacted a host of additional existing examples and material; and new
modules such as
multiprocessing
offer
new options covered in this edition.

Beyond such library changes, core language changes in Python 3 are
also reflected in this book’s example code. For instance, changes to
2.X’s
print
,
raw_input
,
keys
,
has_key
,
map
, and
apply
all required changes here. In addition,
3.X’s new
package
-
relative
import model impacted
a few examples including
mailtools
and expression parsers, and its different flavor of
division
forced some minor math updates in
canvas-based GUI examples such as PyClock, PyDraw, and PyPhoto.

Of note here, I did not change all
%
string formatting
expressions to use the new
str.
format
, since both forms are supported in
Python 3.1, and it now appears that they will be either indefinitely or
forever. In fact, per a “grep” we’ll build and run in
Chapter 11
’s PyEdit example, it seems that this
expression still appears over 3,000 times in Python 3.1’s own library
code. Since I cannot predict Python evolution completely, see the first
chapter for more on this if it ever requires updates in an unexpected
future.

Also because of the 3.X scope, this edition is unable to use some
third-party packages that are still in 2.X form only, as described
earlier. This includes the leading MySQL interface, ZODB, PyCrypto, and
others; as also mentioned, PIL was ported to 3.1 for use in this book,
but this required a special patch and an official 3.X release is still
presently pending. Many of these may be available in 3.X form by the
time you read these words, assuming the Python world can either break
some of the current cross dependencies in 2.X packages or adopt new
3.X-only tools.

Language Versus Library: Unicode

As a book focused on applications instead of core language
fundamentals, language changes are not always obtrusive here. Indeed, in
retrospect the book
Learning Python
may have been
affected by 3.X core language changes more than this book. In most cases
here, more example changes were probably made in the name of clarity or
functionality than in support of 3.X itself.

On the other hand, Python 3.X does impact much code, and the
impacts can be subtle at times. Readers with Python 2.X backgrounds will
find that while 3.X core language changes are often simple to apply,
updates required for changes in the 3.X standard library are sometimes
more far reaching.

Chief among these, Python 3.X’s Unicode strings have had broad
ramifications. Let’s be honest: to people who have spent their lives in
an ASCII world, the impacts of the 3.X Unicode model can be downright
aggravating at times! As we’ll see in this book, it affects file
content; file names; pipe descriptors; sockets; text in GUIs; Internet
protocols such as FTP and email; CGI scripts; and even some persistence
tools. For better or worse, once we reach the world of applications
programming as covered in this book, Unicode is no longer an optional
topic for many or most Python 3.X programmers.

Of course, Unicode arguably never should have been entirely
optional for many programmers in the first place. Indeed, we’ll find
that things that may have appeared to work in 2.X never really
did—treating text as raw byte strings can mask issues such as comparison
results across encodings (see the grep utility of
Chapter 11
’s PyEdit for a prime example of code
that should fail in the face of Unicode mismatches). Python 3.X elevates
such issues to potentially every programmer’s panorama.

Still, porting nontrivial code to 3.X is not at all an
insurmountable task. Moreover, many readers of this edition have the
luxury of approaching Python 3.X as their first Python and need not deal
with existing 2.X code. If this is your case, you’ll find Python 3.X to
be a robust and widely applicable scripting and programming language,
which addresses head-on many issues that once lurked in the shadows in
2.X.

Python 3.1 Limitations: Email, CGI

There’s one exception that I should call out here because of its
impact on major book examples. In order to make its code relevant to the
widest possible audience, this book’s major examples are related to
Internet
email
and have much new support in this
edition for Internationalization and Unicode in this domain.
Chapter 14
’s PyMailGUI and
Chapter 16
’s PyMailCGI, and all the prior
examples they reuse, fall into this category. This includes the PyEdit
text editor—now Unicode-aware for files, display, and greps.

On this front, there is both proverbial good news and bad. The
good news is that in the end, we will be able to develop the
feature-rich and fully Internationalized PyMailGUI email client in this
book, using the
email
package as it
currently exists. This will include support for arbitrary encodings in
both text content and message headers, for both viewing and composing
messages. The less happy news is that this will come at some cost in
workaround complexity in Python 3.1.

Unfortunately, as we’ll learn in
Chapter 13
, the
email
package in Python 3.1 has a number of
issues related to
str
/
bytes
combinations in Python 3.X. For example,
there’s no simple way to guess the encoding needed to convert mail
bytes
returned by the
poplib
module to the
str
expected by the
email
parser. Moreover, the
email
package is currently broken altogether
for some types of messages, and it has uneven or type-specific support
for some others.

This situation appears to be temporary. Some of the issues
encountered in this book are already scheduled to be repaired (in fact,
one such fix in 3.2 required a last-minute patch to one of this book’s
3.1 workarounds in
Chapter 13
).
Furthermore, a new version of
email
is being developed to accommodate the 3.X Unicode/bytes dichotomy more
accurately, but it won’t materialize until long after this book is
published, and it might be backward-incompatible with the current
package’s API, much like Python 3.X itself. Because of that, this book
both codes workarounds and makes some assumption along the way, but
please watch its website (described ahead) for required updates in
future Pythons. One upside here is that the dilemmas posed neatly
reflect those common in realistic programming—an underlying theme of
this text.

These issues in the
email
package are also inherited by the
cgi
module for CGI file uploads, which are in large measure broken in 3.1.
CGI scripts are a basic technique eclipsed by many web frameworks today,
but they still serve as an entry-level way to learn Web fundamentals and
are still at the heart of many larger toolkits. A future fix seems
likely for this 3.1 flaw as well, but we have to make do with nonbinary
CGI file uploads for this edition in Chapters
15
and
16
,
and limited email attachments in PyMailCGI. This seems less than ideal
nearly two years after 3.0’s release, but such is life in the dynamic
worlds of both software development at large and books that aim to lead
the curve instead of following it.

BOOK: Programming Python
13.67Mb size Format: txt, pdf, ePub
ads

Other books

Eye of the Beholder by Dana Marie Bell
Whiplash by Yvie Towers
Never Surrender by Jewel, Deanna
Mr. In-Between by Neil Cross
THE ALPHAS Box Set by A.J. Winter
The Welsh Girl by Peter Ho Davies
Crawl by Edward Lorn