Download ebooks file Dive Into Python 1st Edition Mark Pilgrim (Auth.) all chapters
Download ebooks file Dive Into Python 1st Edition Mark Pilgrim (Auth.) all chapters
com
https://ebookname.com/product/dive-into-python-1st-edition-
mark-pilgrim-auth/
OR CLICK BUTTON
DOWLOAD EBOOK
https://ebookname.com/product/developing-with-pdf-dive-into-the-
portable-document-format-1st-edition-leonard-rosenthol/
ebookname.com
https://ebookname.com/product/dive-into-close-reading-strategies-for-
your-3-5-classroom-1st-edition-diane-lapp/
ebookname.com
https://ebookname.com/product/programming-python-4th-edition-mark-
lutz/
ebookname.com
https://ebookname.com/product/pediatric-emergency-medicine-
secrets-2nd-edition-steven-m-selbst/
ebookname.com
Contending for the Faith Southern Baptists in New Mexico
1938 1995 1st Edition Daniel R. Carnett
https://ebookname.com/product/contending-for-the-faith-southern-
baptists-in-new-mexico-1938-1995-1st-edition-daniel-r-carnett/
ebookname.com
https://ebookname.com/product/electronics-circuits-and-systems-fourth-
edition-owen-bishop/
ebookname.com
https://ebookname.com/product/sociology-5th-edition-giddens/
ebookname.com
https://ebookname.com/product/theory-and-language-acquisition-susanne-
niemeier/
ebookname.com
https://ebookname.com/product/geometry-and-physics-of-branes-series-
in-high-energy-physics-cosmology-and-gravitation-1st-edition-u-bruzzo/
ebookname.com
Getting Started with Knockout js for NET Developers 1st
Edition Akinshin
https://ebookname.com/product/getting-started-with-knockout-js-for-
net-developers-1st-edition-akinshin/
ebookname.com
Praise for Dive Into Python:
"Whether you're an experienced programmer looking to get into
Python or grizzled Python veteran who remembers the days when
you had to import the string module, Dive Into Python is your 'desert
island' Python book."
-Joey deVilla, Slashdot contributor (www.slashdot.org)
"It's not just on the technical side that the book scores high marks.
Mention must be made of the writing style, which is both accessible,
technically competent and riddled with peppery asides and wise
cracks. It works well, and it really is a pleasure to read. What's more,
the book succeeds in enthusing the reader. Highly recommended."
-(www.techbookreport.com)
Permission is granted to copy, distribute and/ or modify this document under the terms of the
GNU Free Documentation Ucense, Version 1.1 or any later version published by the Free Software
Foundation; with no Invariant Sections, no Front-CoverTexts, and no Back-CoverTexts. A copy of
the license is included in the section Appendix B, entitled "GNU Free Documentation Ucense."
AII rights reserved. No part of this work may be reproduced or transmitted in any form or by any
means, electronic or mechanical, including photocopying, recording, or by any information
storage or retrieval system, without the prior written permission of the copyright owner and the
publisher.
ISBN 978-1-59059-356-1 ISBN 978-1-4302-0700-9 (eBook)
DOI 10.1007/978-1-4302-0700-9
Trademarked names may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, we use the names only in an editorial fashion and to the
benefit of the trademark owner, with no intention of infringement of the trademark.
Lead Editor: Chris Mills
Technical Reviewer: Anna Ravenscroft
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis,
Jason Gilmore, Jonathan Hassle, Chris Mills, Dominic Shakeshaft, Jim Sumser
Project Manager: Kylie Johnston
Copy Edit Manager: Nicole LeClerc
Copy Editor: Marilyn Smith
Production Manager: Karl Brooks
Production Editor: Katie Stence
Compositor: Kinetic Publishing Services, lLC
Proofreader: Unda Seifert
Indexer: Rebecca Plunkett
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
The information in this book is distributed on an "as is" hasis, without warranty. Although every
precaution bas been taken in the preparation of this work, neither the author(s) nor Apress shall
have any liability to any person or entity with respect to any loss or damage caused or alleged to
be caused directly or indirectly by the information contained in this work.
For Ethan
Contents at a Glance
About the Author ................................................. xiii
Introduction ....................................................... xv
v
Contents
About the Author ................................................. xiii
Introduction ....................................................... xv
vii
Contents
viii
Contents
ix
Contents
x
Contents
xi
Contents
xii
About the Author
By DAY, MARK PILGRIM is an accessibility architect. By night, he is a husband and
father who lives in North Carolina with his wife, his son, and his dog. He would
like to thank his wife and his son, who have been exceedingly patient while he
worked on this book. The dog was not all that patient, but two out of three isn't
bad. Mark spends his copious free time sunbathing, skydiving, and reading
Immanuel Kant's The Critique of Pure Reason in the original Klingon.
xiii
Introduction
IF YOU ARE a developer interested in getting started quickly with the hottest
open-source language, this book is for you. Experience in other languages is
recommended but not required. If you have experience in Perl, Java, or Visual
Basic, the first few chapters will go even faster. Core programming structures
are shared among all languages, and this book contains many analogies to
help you become familiar with Python's syntax and move on to higher-level
concepts.
But even if you don't have a lot of experience with other modern pro-
gramming languages, this book is still a good place to start. It won't bore
you with long-winded treatises on the history of computer science. You'll
get down to business immediately and see how Python's interactive shell is
the perfect breeding ground for experimenting with programming concepts
before getting your feet wet with actual code.
This book is divided into a series of isolated case studies. Each chapter
focuses on a single piece of real code that does something meaningful and useful.
As each example brings up related issues, there are digressions to explore them,
but the discussion always comes back to the code in the end. This does not mean
that every chapter is an island. Many of the later chapters assume you have mas-
tered basic concepts covered in earlier chapters. These cases are clearly marked
in the text, with references and quick reviews to get you up and running quickly.
Here is an overview of what you'll find in this book:
xv
Introduction
xvi
Introduction
Chapter 11, HTfPWeb Services: This chapter dives into HTTP with an
expanded version of the openanything.py module introduced in Chapter 10.
With HTTP web services, a proper implementation of HTTP is essential,
and this chapter will teach you the five features of HTTP you need to know:
setting the User-Agent, handling temporary and permanent redirects, tak-
ing advantage of the Last -Modified header, using Etags, and supporting
compressed data.
Chapter 12, SOAP Web Services: Python comes with a lot of libraries
(Python fans like to say that it comes with "batteries included"). However,
it doesn't yet come with a SOAP library, so the first step to using SOAP
web services is to install three libraries: PyXMl, fpconst, and SOAPpy. This
chapter steps you through that, and then dives into parsing WSDL files
and calling remote functions over SOAP. The chapter culminates in a
simple program that returns Google search results with the Google
SOAP API.
Chapter 13, Unit Testing: Unit testing is the idea that you should write
small, independent tests to check each of your functions. Python comes
with a unit testing framework, and this chapter will use the example of
validating Roman numerals to show you how to write unit tests in Python.
xvii
Introduction
Chapter 15, Refactorlng: Continuing where Chapter 14 left off, this final
chapter on testing will show how to update your unit tests to handle
changing requirements, how to integrate new unit tests to cover newly
reported bugs, and how to refactor for performance without worrying
about breaking old code.
I hope this book makes you love Python as much as I do. Let's dive in.
xviii
CHAPTER 1
Installing Python
WELCOME TO PYTHON. Let's dive in. In this chapter, you'll install the version of
Python that's right for you.
Python on Windows
On Windows, you have a couple choices for installing Python.
ActiveState makes a Windows installer for Python called ActivePython,
which includes a complete version of Python, an IDE with a Python-aware code
editor, plus some Windows extensions for Python that allow complete access to
Windows-specific services, APls, and the Windows Registry.
ActivePython is freely downloadable, although it is not open source. It is the
IDE I used to learn Python, and I recommend you try it unless you have a specific
reason not to use it. One such reason might be that ActiveState is generally several
months behind in updating the ActivePython installer when new versions of Python
1
Chapter 1
are released. If you absolutely need the latest version of Python, and ActivePython is
still a version behind as you read this, you'll want to use the second option for
installing Python on Wmdows.
The second option is the "official" Python installer, distributed by the people
who develop Python itself. It is freely downloadable and open source, and it is
always current with the latest version of Python.
2. If you are using Windows 95, Windows 98, or Windows Me, you will
also need to download and install Windows Installer 2.0, from http: I I
download.microsoft.com/download/WindowsInstaller/Insta11/2.0/W9XMel
EN-US/InstMsiA.exe, before installing ActivePython.
5. If space is tight, you can do a custom installation and deselect the docu-
mentation, but I don't recommend this unless you absolutely can't spare
the 14MB.
After the installation is complete, close the installer and choose Start ~
Programs ~ ActiveState ActivePython 2.2 ~ PythonWin IDE. You'll see the
following:
PythonWin 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) -
see 'Help/About PythonWin' for further copyright information.
»>
2
Installing Python
2. Double-click the installer, Python- 2. xxx. yyy. exe. (The name will depend
on the version of Python available when you read this; at time of writing,
the latest version was Python 2.3.3.)
4. If disk space is tight, you can deselect the HTML Help file, the utility
scripts (Tools/), and/or the test suite (Lib/test/).
5. If you do not have administrative rights on your machine, you can select
Advanced Options, and then choose Non-Admin Install. This just affects
where Registry entries and Start menu shortcuts are created.
After the installation is complete, close the installer and select Start ~
Programs ~ Python 2.3 ~ IDLE (Python GUI). You'll see the following:
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
Type "copyright", "credits" or "licenseO" for more information.
****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************
IDLE 1.0
»>
Python on Mac OS X
On Mac as X. you have two choices for installing Python: install it or don't install
it. You probably want to install it.
Mac as x 10.2 and later comes with a command-line version of Python pre-
installed. If you are comfortable with the command line, you can use this version
for the first third of the book. However, the preinstalled version does not come
3
Chapter 1
with an XML parser, so when you get to the XML chapter, you'll need to install
the full version.
Rather than using the preinstalled version, you'll probably want to install the
latest version, which also comes with a graphical interactive shell.
Try it out:
Welcome to Darwin!
[localhost:~] you% python
Python 2.2 (#1, 07/14/02, 23:25:09)
[GCC Apple cpp-precomp 6.14] on darwin
Type "help", "copyright", "credits", or "license" for more information.
»> [press Ctrl+D to get back to the command prompt]
[localhost:~] you%
4. The installer will prompt you for your administrative username and
password.
4
Installing Python
The MacPython IDE should display a splash screen, and then take you to the
interactive shell. If the interactive shell does not appear, select Wmdow ~ Python
Interactive (or press Command-O). The opening screen will show the following
information:
Note that once you install the latest version, the preinstalled version is still
present. If you are running scripts from the command line, you need to be aware
of which version of Python you are using, which you can check as shown in
listing 1-1.
5
Chapter 1
Python on Mac OS 9
Mac OS 9 does not come with any version of Python, but installation is very simple,
and there is only one choice.
Follow these steps to install Python on Mac OS 9:
The MacPython IDE should display a splash screen, and then take you to the
interactive shell. If the interactive shell does not appear, select Wmdow ~ Python
Interactive (or press Command-O). You'll see the following:
6
Installing Python
localhost:-$ su -
Password: [enter your root password]
[root@localhost root]# wget http://python.org/ftp/python/2.3/rpms/redhat-9/
python2.3-2.3-Spydotorg.i386.rpm
Resolving python.org ... done.
Connecting to python.org[194.109.137.226]:80 ... connected.
HTTP request sent, awaiting response ... 200 OK
Length: 7,495,111 [application/octet-stream]
(1) Whoops! Just typing python gives you the older version of Python-the
one that was installed by default. That's not the one you want.
(2) At the time of this writing, the newest version is called python2.3. You'll
probably want to change the path on the first line of the sample scripts to point
to the newer version.
(3) This is the complete path of the newer version of Python that you just
installed. Use this on the #! line (the first line of each script) to ensure that scripts
are running under the latest version of Python, and be sure to type python2. 3 to
get into the interactive shell.
7
Chapter 1
localhost:-$ su -
Password: [enter your root password]
localhost:-# apt-get install python
Reading Package Lists ••• Done
Building Dependency Tree ••. Done
The following extra packages will be installed:
python2.3
Suggested packages:
python-tk python2.3-doc
The following NEW packages will be installed:
python python2.3
o upgraded, 2 newly installed, 0 to remove and 3 not upgraded.
Need to get OB/2880kB of archives.
After unpacking 9351kB of additional disk space will be used.
Do you want to continue? [V/n] V
Selecting previously deselected package python2.3.
(Reading database ••• 22848 files and directories currently installed.)
Unpacking python2.3 (from ••• /python2.3_2.3.1-1_i386.deb)
Selecting previously deselected package python.
Unpacking python (from .•• /python_2.3.1-1_all.deb)
Setting up python (2.3.1-1) •.•
Setting up python2.3 (2.3.1-1) •.•
Compiling python modules in lusr/lib/python2.3
Compiling optimized python modules in lusr/lib/python2.3
localhost:-# exit
logout
localhost:-$ python
Python 2.3.1 (#2, Sep 24 2003, 11:39:14)
[GCC 3.3.2 20030908 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
»> [press Ctrl+D to exit]
8
Random documents with unrelated
content Scribd suggests to you:
Scrape away as much wax as possible with a dull knife. Then treat
as follows:
Candy
Launder in warm soapy water if the material is washable. Otherwise,
sponge with clear warm water.
13
Carbon Paper
Carbon-paper stains usually can be removed by washing in a heavy
suds of soap and water. Sponge unwashable materials with a liquid
of 1 cup alcohol and 2 cups water; then sponge with cold water.
Chewing Gum
Use one of the following methods:
Ice.—If the material will not water-spot, rub the gum stain with ice.
Then scrape and rub the hardened gum out of the cloth. This
method is particularly good for rugs and other heavy materials.
Egg white.—If the material is washable, soften the gum stain with
egg white and then wash.
Cod-liver Oil
Fresh cod-liver oil stains are almost colorless and are easy to
remove. But old stains, especially if the material has been washed or
ironed, are a light brown and are almost impossible to remove, even
with bleaches. Treat fresh stains with either of the following:
Fingernail Polish
Acetone or nail-polish removers.—On any material except
acetate rayon or vinyon, sponge the stain with acetone or a
commercial nail-polish remover.
Bleaches.—To remove any color remaining after the polish itself has
been dissolved, apply a bleach. Test the cloth for change in color
first.
Fish Slime
Soak or sponge the stain with a solution made of ½ cup salt and 1
cup vinegar in 2 quarts of water. Rinse well in water; then wash in
warm soapsuds.
Flypaper, Sticky
Sponge the stain with carbon tetrachloride or benzene. If the
material is washable, soak in kerosene and then wash in warm soapy
water.
If the grease spot contains dirt or fine bits of metal, first loosen the
stain by rubbing a little lard, petroleum jelly, or dry-cleaning soap
into it. Then sponge with the grease solvent or dip the stain into a
small bowl of the solvent.
Ice Cream
Ice cream stains contain milk or cream, sugar, sometimes egg, and
often coloring. If after trying the following methods, a fruit or
chocolate stain remains, follow instructions under Fruits and Berries,
page 15, or under Chocolate and Cocoa, page 13.
Pepsin.—First sponge the stain with cold water, then sprinkle pepsin
on the dampened stain, and let it stand half an hour. Brush it off and
rinse the spot well. For best results, be sure the material is free from
soap or other alkali before applying the pepsin.
Ink
Because inks differ in composition, it is impossible to find removers
that are equally effective for all types of ink spots. Each of the
methods mentioned below is satisfactory with some type of ink. For
most ink spots, it is necessary to try several methods, beginning
always with the simplest and that least likely to harm the cloth.
19
India or drawing ink
Printing ink
Writing ink
Absorbents.—If the stain is still wet, spread corn meal, salt, French
chalk, cornstarch, or talcum powder on the stain to remove any
excess ink and to keep it from spreading. Work the powder into the
stain. Shake it off as it becomes soiled and repeat the process.
When the dry absorbent fails to take up more ink, make the
absorbent into a paste with water or with a mixture of 1 part water
and 1 part alcohol and apply again. Let dry and brush off.
20
Iodine
Soap and water.—If the material is washable, soap and water will
often remove a fresh stain. Or moisten with water and place either
in the sun, over a warm radiator, or hold in the steam from a boiling
teakettle.
Iron Rust
Use any of the methods given below to remove iron-rust stains from
white materials. Test remover on sample of cloth before using on
colored materials.
Linseed Oil
Sponge with carbon tetrachloride or benzene. Treat as for cod-liver
oil stains.
Medicines
Because of the great number and variety of substances used in
medicines, it is not possible to give methods for removing all such
stains. If you know what the medicine is made of, it will aid in
choosing the remover. For instance, a tarry or gummy medicine can
be treated in the same way as a tar spot (see p. 27); a medicine
containing much iron can be removed in the same way as iron rust
(see p. 20). Medicines in a sugar sirup usually can be washed out
with water; those dissolved in alcohol sometimes can be removed by
sponging the stain with alcohol. Many of the medicines used in
swabbing sore throats contain silver nitrate and should be sponged
with a solution of sodium thiosulfate (“hypo”)—1 teaspoon of the
crystals in 1 cup of water.
If you cannot find out what kind of medicine caused the stain, you
may have to try several methods to find one that will do the job.
Each of the following methods will remove certain medicine stains.
22
Mercurochrome
Mercurochrome stains are very hard to remove unless you treat
them promptly. Proceed as follows:
Denatured alcohol, glycerine, and laundering.—First sponge
the stain well with a liquid made of equal parts of alcohol and water.
(On acetate rayon and colored materials use 1 part alcohol and 2
parts water). Next work glycerine into the cloth to help loosen the
stain, and continue using as long as any color bleeds from the stain.
Then wash well in soapsuds, and rinse with water to which a few
drops of ammonia water have been added.
Metallic Stains
The tarnish of copper, brass, tin, and other metals often stains
textiles. To remove, apply vinegar, lemon juice, or a 10-percent
solution of acetic acid. Rinse well as soon as the stain has dissolved.
Do not use chlorine bleaches or sodium perborate to remove these
stains.
Mildew
Mildew spots must be treated when fresh, before the mold growth
has a chance to weaken the cloth.
Lemon juice.—Moisten the stain with lemon juice and salt and place
in the sun. This often removes slight stains.
Mimeograph-Correction Fluid
Follow instructions given under Fingernail Polish, page 15.
Mucus
Soak in lukewarm salt water (about 2 cups salt to 1 gallon water) or
in weak ammonia water (2 tablespoons ammonia water to each
gallon water). Rinse well with cold water and launder as usual.
Mud
Let the mud stain dry, then brush well. Sponge with clear water, or
use soap and water if it will not harm the cloth. Sponging with
alcohol will help to remove the last traces of the stain. On colored
materials and acetate rayon dilute the alcohol—1 cup denatured
alcohol to 2 cups water.
Mustard
Glycerine and soap and water.—If the material is washable,
work glycerine into the stain, rub lightly between the hands, and
then wash the article in soap and water.
24
Pencil Marks
Indelible pencil
Do not use water on indelible pencil marks as this spreads the dye
and makes the stain harder to remove. Use one of the following:
Lead pencil
25
Perspiration Stains
Perspiration of the body is usually acid, so you can sometimes
restore colors changed by a perspiration stain by treating with an
alkali. Dampen the stain with water and hold it over the fumes from
an open ammonia water bottle. (See Acids, p. 10.) Old stains may be
alkaline; then try vinegar. (See Alkalies, p. 11.) However, colors
changed by perspiration cannot always be restored, particularly if
the stain is an old one.
Rubber Cement
To remove rubber cement either sponge or dip the cloth in carbon
tetrachloride, Stoddard solvent, gasoline, or benzene. If the stain
has dried, rub in petroleum jelly or dry-cleaning soap to loosen it.
Then apply the carbon tetrachloride or other solvent.
Salad Dressings
The acid of the vinegar or lemon juice in salad dressings may injure
the color of the material. Apply a mild alkali such as baking soda or
weak ammonia water immediately to restore the color. (See Acids, p.
10.) Then use one of the following to remove the stain:
26
Scorch
You can usually remove light scorch stains from cotton and linen
materials, but wool and silk can seldom be restored to their original
condition. Brushing with emery paper may improve wool, however.
Try the following:
Soap and water.—If the cloth is washable, soap and water will
remove very slight stains. After washing, place the article in the sun
for a day or two; it may bleach out any remaining traces of the stain.
Shoe Dressings
Soap and water.—If the material is washable, remove fresh stains
from one of the paste dressings by sponging or washing thoroughly
with plenty of soap. For spots caused by white dressings, sponge
first with water, then with soap and water.
Soap
Ironing material from which the soap has not been well rinsed may
cause a stain much like iron rust. Washing with soap and water
usually removes it. Be sure to rinse well. Bleaching in the sun
afterwards is sometimes helpful.
Sugar Sirups
If the material is washable, wash out sugar-sirup stains with soap
and water. For more delicate materials, sponge with clean water.
28
Urine
These stains differ so in composition that it is impossible to give
methods which will be successful in all cases. If the color of the cloth
is not destroyed but only changed, it may be restored. Normal
human urine is usually acid, as is also that of all meat-eating
animals. Therefore sponge such stains with a weak ammonia or soda
solution. (See Acids, p. 10.) If the stain is alkaline, sponge with
lemon juice or vinegar. (See Alkalies, p. 11.)
Walnut (Black)
Soap solution.—Boil washable materials in soapy water (a half-inch
cube of laundry soap to each cup of water). This will completely
remove fresh stains on cotton or linen. If this treatment leaves a
gray color, as it sometimes does with an old stain, treat with Javelle
water as follows:
Water Spots
Some silks, rayons, and wools are spotted by water. To remove such
spots, dampen the entire material evenly, either by sponging with
clean water or by shaking in the steam from a briskly boiling
teakettle. Then press it while still damp. Scratching with the
fingernail or a stiff brush or rubbing the cloth between the hands will
sometimes remove the spot.
29
30
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.
ebookname.com