100% found this document useful (3 votes)
24 views

Download ebooks file Dive Into Python 1st Edition Mark Pilgrim (Auth.) all chapters

Pilgrim

Uploaded by

erliesbiazin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
24 views

Download ebooks file Dive Into Python 1st Edition Mark Pilgrim (Auth.) all chapters

Pilgrim

Uploaded by

erliesbiazin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Download Full ebookname - Read Now at ebookname.

com

Dive Into Python 1st Edition Mark Pilgrim (Auth.)

https://ebookname.com/product/dive-into-python-1st-edition-
mark-pilgrim-auth/

OR CLICK BUTTON

DOWLOAD EBOOK

Discover More Ebook - Explore Now at ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Developing with PDF Dive into the Portable Document Format


1st Edition Leonard Rosenthol

https://ebookname.com/product/developing-with-pdf-dive-into-the-
portable-document-format-1st-edition-leonard-rosenthol/

ebookname.com

Dive into Close Reading Strategies for Your 3 5 Classroom


1st Edition Diane Lapp

https://ebookname.com/product/dive-into-close-reading-strategies-for-
your-3-5-classroom-1st-edition-diane-lapp/

ebookname.com

Programming Python 4th Edition Mark Lutz

https://ebookname.com/product/programming-python-4th-edition-mark-
lutz/

ebookname.com

Pediatric Emergency Medicine Secrets 2nd Edition Steven M.


Selbst

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

Electronics Circuits and Systems Fourth Edition Owen


Bishop

https://ebookname.com/product/electronics-circuits-and-systems-fourth-
edition-owen-bishop/

ebookname.com

Sociology 5th Edition Giddens

https://ebookname.com/product/sociology-5th-edition-giddens/

ebookname.com

Theory and Language Acquisition Susanne Niemeier

https://ebookname.com/product/theory-and-language-acquisition-susanne-
niemeier/

ebookname.com

Geometry and Physics of Branes Series in High Energy


Physics Cosmology and Gravitation 1st Edition U Bruzzo

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)

'l1s a complete newbie to the language, I most certainly valued the


approach employed, and I constantly had those little thoughts like
'This is the way a programming language should be taught.'"
-Lasse Koskela, JavaRanch (www.javaranch.com)

"It [Dive Into Python] is a very impressive piece of work-it manages


to explain the Python language and, more importantly, the idioms
and customs ofPython programming, through a series of well-chosen
and interesting examples."
-Kevin Marks, Epeus' epigone {http://epeus.blogspot.com)

"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)

"Dive Into Python is just what I needed to get started on becoming a


guru in the language."
-Blane Warrene (www.practicalapplications.net)
Dive Into Python
MARK PILGRIM

APress Media, LLC


Dive Into Python
Copyright © 2004 by Mark Pilgrim
Originally published by Apress in 2004

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

Chapter 1 Installing Python ................................... 1


Chapter 2 Your First Python Program ......................... 11
Chapter 3 Native Datatypes ................................... 23
Chapter 4 The Power of Introspection ........................ 49
Chapter 5 Objects and Object-Orientation .................... 71
Chapter 6 Exceptions and File Handling ...................... 97
Chapter 7 Regular Expressions ............................... 121
Chapter 8 HTML Processing .................................... 141
Chapter 9 XML Processing ..................................... 171
Chapter 10 Scripts and Streams ............................... 197
Chapter 11 HTTP Web Services ................................. 221
Chapter 12 SOAP Web Services ................................. 249
Chapter 13 Unit Testing ....................................... 271
Chapter 14 Test-First Programming ............................ 2B7
Chapter 15 Refactoring ........................................ 309
Chapter 16 Functional Programming ............................ 331
Chapter 17 Dynamic Functions ................................. 349
Chapter 18 Performance Tuning ................................ 367
Appendix A Python License ..................................... 387
Appendix B GNU Free Documentation License .................. 393

Index .............................................................. 401

v
Contents
About the Author ................................................. xiii
Introduction ....................................................... xv

Chapter 1 Installing Python ............................... 1


Which Python Is Right for You? ................................... 1
Python on Windows .................................................. 1
Python on Mac as X................................................. 3
Python on Mac as 9 ................................................. 6
Python on Red Hat Linux ............................................ 6
Python on Debian GNU/Linux ........................................ 7
Python Installation from Source .................................. 8
The Interactive Shell ............................................. 10
Summary ............................................................ . 10

Chapter 2 Your First Python Program ................... 11


Diving In .......................................................... 11
Declaring Functions ............................................... 12
Documenting Functions ............................................. 14
Everything Is an Object .......................................... 15
Indenting Code ..................................................... 18
Testing Modules: The if _name_ Trick ......................... 19
Summary ............................................................. 21

Chapter 3 Native Datatypes ............................... 23


Introducing Dictionaries ......................................... 23
Introducing Lists ................................................. 27
Introducing Tuples ................................................ 34
Declaring Variables ............................................... 36
Formatting Strings ................................................ 39
Mapping Lists ...................................................... 41
Joining Lists and Splitting Strings ............................ 43
Summary ............................................................. 46

vii
Contents

Chapter 4 The Power of Introspection ................. 49


Diving In .......................................................... 49
Using Optional and Named Arguments .............................. 51
Using type, str, dir, and Other Built-in Functions ........... 52
Getting Object References with getattr ......................... 56
Filtering Lists ................................................... 59
Understanding the Peculiar Nature of and and or ............... 61
Using lambda Functions ........................................... 64
Putting It All Together .......................................... 66
Summary ............................................................. 69

Chapter 5 Objects and Object-Orientation ............ 71


Diving In .......................................................... 71
Importing Modules Using from module import .................... 74
Defining Classes .................................................. 76
Instantiating Classes ............................................. 80
Exploring UserDict: A Wrapper Class ............................ 82
Using Special Class Methods ...................................... 86
Introducing Class Attributes ..................................... 91
Using Private Functions .......................................... 93
Summary ............................................................. 95

Chapter 6 Exceptions and File Handling ............... 97


Handling Exceptions ............................................... 97
Working with File Objects ....................................... 101
Iterating with for Loops ........................................ 106
Using sys. modules ................................................ 109
Working with Directories ........................................ 112
Putting It All Together ......................................... 117
Summary ............................................................ 118

Chapter 7 Regular Expressions .......................... 121


Diving In ......................................................... 121
Case Study: Street Addresses ................................... 121
Case Study: Roman Numerals ...................................... 124
Verbose Regular Expressions ..................................... 131
Case Study: Parsing Phone Numbers .............................. 133
Summary ............................................................ 139

viii
Contents

Chapter 8 HTML Processing ............................... 141


Diving In ......................................................... 141
Introducing Python's sgmllib. py ................................ 147
Extracting Data from HTML Documents ........................... 150
Understanding locals() and globals() .......................... 157
Using Dictionary-based String Formatting ...................... 161
Quoting Attribute Values ........................................ 163
Parsing HTML One Tag at a Time ................................. 165
Putting It All Together ......................................... 168
Summary ............................................................ 170

Chapter 9 XML Processing ................................. 171


Diving In ......................................................... 171
Importing Python Packages ....................................... 180
Parsing XML ....................................................... 182
Dealing with Unicode ............................................. 185
Searching for XML Elements ...................................... 191
Accessing XML Element Attributes ............................... 194
Summary ............................................................ 196

Chapter 10 Scripts and Streams .......................... 197


Abstracting Input Sources ....................................... 197
Using Standard Input, Output, and Error ....................... 202
Introducing Some XML Processing Techniques .................... 207
Handling Command-Line Arguments ................................ 212
Putting It All Together ......................................... 217
Summary ............................................................ 219

Chapter 11 HTTP Web Services ............................ 221


Diving In ......................................................... 221
How Not to Fetch Data Over HTTP ................................ 224
Supporting HTTP Features ........................................ 225
Debugging HTTP Web Services ..................................... 228
Setting the User-Agent .......................................... 229
Handling Last-Modified and ETag ................................ 232
Handling Redirects ............................................... 236
Handling Compressed Data ........................................ 241
Putting It All Together ......................................... 244
Summary ............................................................ 247

ix
Contents

Chapter 12 SOAP Web Services .............................. 249


Diving In ............................................................ 249
Installing the SOAP Libraries .................................... 251
Taking Your First Steps with SOAP ............................... 253
Introducing WSDL .................................................... 257
Searching Google .................................................... 261
Troubleshooting SOAP Web Services ............................... 265
Summary .............................................................. 270

Chapter 13 Unit Testing ..................................... 271


Diving In ............................................................ 271
Introducing the Test Suite ....................................... 272
Constructing Test Cases ........................................... 277
Summary .............................................................. 285

Chapter 14 Test-First Programming ....................... 287


Diving ~n (Stage 1) ................................................ 287
Converting Roman Numerals, Stage 2 .............................. 292
Converting Roman Numerals, Stage 3 .............................. 297
Converting Roman Numerals, Stage 4 .............................. 301
Converting Roman Numerals, Stage 5 .............................. 304
Summary .............................................................. 307

Chapter 15 Refactoring ...................................... 309


Diving In ............................................................ 309
Handling Changing Requirements ................................... 312
Refactoring for Performance ...................................... 321
Postscript ........................................................... 326
Summary .............................................................. 328

x
Contents

Chapter 16 Functional Programming ...................... 331


Diving In ......................................................... 331
Finding the Path ................................................. 333
Filtering Lists Revisited ....................................... 336
Mapping Lists Revisited ......................................... 338
Data-centric Programming ........................................ 340
Dynamically Importing Modules .................................. 341
Putting It All Together ......................................... 343
Summary ............................................................ 347

Chapter 17 Dynamic Functions ............................. 349


Diving In ......................................................... 349
Pluralizing Nouns J Stage 1 ...................................... 350
Pluralizing Nouns J Stage 2 ...................................... 353
Pluralizing Nouns J Stage 3 ...................................... 355
Pluralizing Nouns J Stage 4 ...................................... 356
Pluralizing Nouns J Stage 5 ...................................... 359
Pluralizing Nouns J Stage 6 ...................................... 361
Summary ............................................................ 365

Chapter 18 Performance Tuning ........................... 367


Diving In ......................................................... 367
Using the timeit Module ......................................... 370
Optimizing Regular Expressions ................................. 372
Optimizing Dictionary Lookups .................................. 376
Optimizing List Operations ...................................... 380
Optimizing String Manipulation ................................. 383
Summary ............................................................ 385

Appendix A Python License ................................. 387


A. History of the Software ...................................... 387
B. Terms and Conditions for Accessing or Otherwise
Using Python .................................................. 388

xi
Contents

Appendix B GNU Free Documentation License ........... 393


o. Preamble ....................................................... 393
1. Applicability and Definitions ............................... 393
2. Verbatim Copying .............................................. 395
3. Copying in Quantity .......................................... 395
4. Modifications ................................................. 396
5. Combining Documents .......................................... 398
6. Collections of Documents ..................................... 398
7. Aggregation with Independent Works ......................... 398
8. Translation ................................................... 399
9. Termination ................................................... 399
10. Future Revisions of This License .......................... 399
Addendum: How to Use This License for Your Documents ........ 400

Index ............................................................ 401

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:

Chapter 1, Installing Python: No matter what platform you're using,


Python runs on your computer. You'll step through how to install it on
Windows, Mac as x, Mac as 9, Red Hat Linux, and Debian GNU I Linux.
After downloading, you can be up and running and experimenting in
the Python interactive shell in five minutes.
Chapter 2, Your First Python Program: As promised, you'll dive into
your first complete Python program, odbchelper. py, and explore every line
of it in depth. This will show you how to declare functions, document your
code with the Python equivalent of JavaDoc, and test individual functions
from the Python interactive shell.

Chapter 3, Native Datatypes: Python has a rich set of native datatypes.


You'll learn how to manipulate lists, tuples, and dictionaries; how to
format strings; and how to use list comprehensions.

xv
Introduction

Chapter 4, The Power of Introspection: This chapter explores apihelper. py,


a short but powerful program that displays information about a module,
function, or class from the Python interactive shell. Diving into this pro-
gram will show you how to define functions with optional arguments, call
functions with named arguments, get references to Python objects with
the getattr() function, and use lambda to quickly define one-off functions.

Chapter 5, Objects and Object-Orientation: Python supports first-class


objects with a complete inheritance model. This chapter introduces a
class, Filelnfo, that encapsulates reading metadata from different types
of files. You'll see how to define classes and create instances of classes.
You'll also explore Python's powerful special class methods, which allow
you to define behavior in your custom classes that enables you to oper-
ate on them as if they were built-in datatypes. This chapter steps through
defining a class that behaves like a dictionary, but other special class
methods could allow classes to be added and subtracted like numbers,
or appended and sliced like lists.

Chapter 6, Exceptions and FOe Handing: Most modern programming


languages have some form of exception handling, and Python is no
exception. In this chapter, you'll see how Python's exception handling
works. Then you'll use it to safely work with files, directory listings, and
other external resources that can fail at any moment.

Chapter 7, Regular Expressions: This chapter introduces the concept of


regular expressions, which are a powerful pattern-matching technique. If
you have used Perl, this concept will be old hat, but few people use them
in other languages (even though all modern programming languages offer
regular expression libraries). This chapter steps through a series of mini-
studies to explore different forms of regular expression syntax to work
with street addresses, Roman numerals, and phone numbers.

Chapter 8, IITML Processing: One of Python's best-kept secrets is sgmllib,


a built-in library for dealing with HfML and similar forms of markup. This
chapter is centered around dialect. py, a program that takes HfML pages
and transforms the text into different dialects while leaving the HfML
markup intact. To understand this will require an in-depth look at Python's
sgmllib. There are also digressions to learn about dictionary-based string
formatting, and the localsO and globalsO functions for accessing all the
defined variables in the Python environment as a native data structure.

xvi
Introduction

Chapter 9, XML Processing: This chapter covers Python's built-in XML


processing libraries. You'll see kgp. py, a program that takes XML docu-
ments that define context-free grammars and parses them to generate
randomized output. This will take you through parsing XML documents,
accessing arbitrary elements and attributes within them, and recon-
structing XML files after they've been parsed. There is also a thorough
explanation of Unicode, which is central to XML text processing and is
also used within Python to store strings in multiple languages.

Chapter 10, Scripts and Streams: Another one of Python's strengths


is the concept of a me-like object, which allows you to get data from
multiple input sources the same way you would read from a file on disk.
This chapter's example is openanything. py, a utility library that lets you
handle an in-memory buffer as a file, the output of another running
process, or even a remote web page. There is also a discussion of han-
dling command-line arguments and flags in scripts that are run from
the command line.

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.

Chapter 14, Test-First Programming: Continuing where Chapter 13 left


off, this chapter will explore what it feels like to write code that already
has unit tests for it. When all your tests pass, stop coding.

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.

Chapter 16, Functional Programming: This chapter covers Python's


functional programming methods: map, filter, and lambda. Using a sam-
ple program to build and run regression test suites, you'll learn about
the joys of data-centric programming, dynamically importing modules,
and manipulating path information on the command line.

Chapter 17, Dynamic Functions: Python 2.3 introduces a new language


feature: generators. This chapter uses a staged approach to build the
same program in six different ways. By stage six, you'll see how genera-
tors can radic~y reduce your program's complexity, while actually
making it more flexible in the long run. Along the way, you'll learn about
string substitution with regular expressions, closures, and functions that
behave like first-class objects.

Chapter 18, Performance Tuning: Python is an interpreted scripting


language, but that doesn't mean you can ignore performance altogether.
This chapter explores some common mistakes that can make your pro-
gram run up to ten times slower than it could. You'll learn how to use
the built-in timeit module to optimize regular expressions, dictionary
lookups, list operations, and string manipulation.

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.

Which Python Is Right for You?


The first thing you need to do with Python is install it. Or do you? If you're using an
account on a hosted server, your ISP may have already installed Python. Most pop-
ular Linux distributions come with Python in the default installation. Mac OS X 10.2
and later versions come with a command-line version of Python, although you'll
probably want to install a version that includes a more Mac-like graphical interface.
Windows does not come with any version of Python, but don't despair! There
are several ways to point-and-click your way to Python on Windows.
As you can see already, Python runs on many operating systems. The full list
includes Windows, Mac OS, Mac OS X, and all varieties of free UNIX-compatible
systems like Linux. There are also versions that run on Sun Solaris, AS/400, Amiga,
OS/2, BeOS, and a plethora of other platforms you've probably never even heard of.
What's more, Python programs written on one platform can, with a little care,
run on any supported platform. For instance, I regularly develop Python programs
on Wmdows and later deploy them on Linux.
So back to the question that started this section, "Which Python is right for
you?" The answer is whichever one runs on the computer you already have.

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.

Option 1: Installing ActivePython


Here is the procedure for installing ActivePython:

1. Download ActivePython from


http://www.activestate.com/Products/ActivePython/.

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.

3. Double-click the installer, ActivePython-2.2.2-224-win32-ix86.msi.

4. Step through the installer program.

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.
»>

Option 2: Installing Python from Python.org


Here is the procedure for installing Python from http://www.python.org/:

2
Installing Python

1. Download the latest Python Windows installer by going to


http://www. python. org/ftp/python/ and selecting the highest version
number listed, and then downloading the . exe installer.

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.)

3. Step through the installer program.

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.

Running the Preins tailed Version of Python on Mac OS X


To use the preinstalled version of Python, follow these steps:

1. Open the 1Applications folder.

2. Open the Utilities folder.

3. Double-click Terminal to open a terminal window and get to a com-


mandline.

4. 'JYpe python at the command prompt.

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%

Installing the Latest Version of Python on Mac OS X


Follow these steps to download and install the latest version of Python:

1. Download the MacPython-OSX disk image from


http://homepages.cwi.nl/-jack/macpython/download.html.

2. If your browser has not already done so, double-click


MacPython-OSX - 2.3 -1. dmg to mount the disk image on your desktop.

3. Double-click the installer, Macpython-OSX.pkg.

4. The installer will prompt you for your administrative username and
password.

4
Installing Python

5. Step through the installer program.

6. After installation is complete, close the installer and open the


/ Applications folder.

7. Open the MacPython-2.3 folder.

8. Double-click PythonIDE to launch 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:

Python 2.3 (#2, Jul 30 2003, 11:45:28)


[GCC 3.1 20020420 (prerelease)]
Type "copyright", "credits" or "license" for more information.
MacPython IDE 1.0.1
»>

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.

Listing 1-1. Two Versions of Python

[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% /usr/local/bin/python
Python 2.3 (#2, Jul 30 2003, 11:45:28)
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits", or "license" for more information.
»> [press Ctrl+D to get back to the command prompt]
[localhost:-] you%

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:

1. Download the MacPython23full.bin file from


http://homepages.cwi.n1/-jack/macpython/down1oad.htm1.

2. If your browser does not decompress the file automatically, double-click


MacPython23full. bin to decompress the file with Stuffit Expander.

3. Double-click the installer, MacPython23fu11.

4. Step through the installer program.

5. After installation is complete, close the installer and open the


I Applications folder.

6. Open the MacPython-OS9 2.3 folder.

7. Double-click Python IDE to launch 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). You'll see the following:

Python 2.3 (#2, Jul 30 2003, 11:45:28)


[GCC 3.1 20020420 (prerelease)]
Type "copyright", "credits" or "license" for more information.
MacPython IDE 1.0.1
»>

Python on Red Hat Linux


Installing under UNIX-compatible operating systems such as Linux is easy if
you're willing to install a binary package. Prebuilt binary packages are available
for most popular Linux distributions, or you can always compile from source.
Download the latest Python RPM by going to
http://www . python. orglftp/pythonl and selecting the rpmsl directory within that.
Then download the RPM with the highest version number. You can install it with
the rpm command, as shown in Listing 1-2.

6
Installing Python

Listing 1-2. Installing on RedHat Lima 9

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]

[root@localhost root]# rpm -Uvh python2.3-2.3-Spydotorg.i386.rpm


Preparing... ########################################### [100%]
1:python2.3 ########################################### [100%]
[root@localhost root]# python (1)
Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright", "credits", or "license" for more information.
»> [press Ctrl+D to exit]
[root@localhost root]# python2.3 (2)
Python 2.3 (#1, Sep 12 2003, 10:53:56)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits", or "license" for more information.
»> [press Ctrl+D to exit]
[root@localhost root]# which python2.3 (3)
/usr/bin/python2.3

(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.

Python on Debian GNU/Linux


If you are lucky enough to be running Debian GNU/LimlX, you install Python
through the apt command, as shown in Listing 1-3.

7
Chapter 1

Listing 1-3. Installing on Debian GNUlLinux

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]

Python Installation from Source


If you prefer to build from source, you can download the Python source code
from http://WtM •python. orgl ftp/python/. Select the highest version number listed,
download the . tgz file, and then do the usual configure, make, make install dance,
as shown in listing 1-4.

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:

Blotting paper.—Place the stain between clean white blotters,


cleansing tissues, or paper towels, and press with a warm iron,
changing the blotters as they become soiled. Then sponge with
carbon tetrachloride or other grease solvent.

Denatured alcohol.—If a color stain remains, sponge with liquid


made up of 1 cup denatured alcohol and 2 cups water.

Candy
Launder in warm soapy water if the material is washable. Otherwise,
sponge with clear warm water.

If dye or chocolate stains remain, follow instructions given under


Dyes and Running Colors, page 14, or Chocolate and Cocoa, page
13.

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.

Carbon tetrachloride, kerosene, or turpentine.—Soak the stain


in carbon tetrachloride, kerosene, or turpentine. If kerosene is used,
wash in warm soapy water afterwards.

Chocolate and Cocoa


It may be necessary to try more than one method to remove
chocolate and cocoa stains, since they usually contain other
substances such as fat, milk, starch, and sugar. First scrape off as
much of the stain as possible with a dull knife; then try one of the
following:

Soap and warm water.—If the material is washable the regular


laundering in warm soapy water will often remove this stain.

Hydrogen peroxide and sodium perborate.—Sponge stubborn


stains with hydrogen peroxide. Or use a mixture of 1 level teaspoon
sodium perborate to 1 pint hydrogen peroxide. If the stain still
shows, cover the dampened spot with powdered sodium perborate
and let stand an hour. Rinse thoroughly. Be sure to test for color
change on a sample of the cloth or on the inside of hem or seam of
the garment before using these bleaches on the stain.

Carbon tetrachloride and pepsin.—If the cloth is not washable


sponge with carbon tetrachloride to dissolve the grease. Dry
thoroughly, then sponge with warm water, and dust with pepsin
powder. Work the powder into the cloth, let stand for 30 minutes or
longer, then sponge with water.

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:

Grease solvents.—Sponge or dip fresh cod-liver oil stains in carbon


tetrachloride, benzene, Stoddard solvent, or gasoline. When the cod-
liver oil has been removed, sponge with warm soapy water.

Glycerine.—For washable materials, pour either glycerine or 14


one of the soapless shampoos on the fresh stain. Rub lightly
between the hands to loosen the stain, rinse well in water, and then
wash in warm soapsuds.

Coffee and Tea


Water and glycerine.—If the stains are on wool or silk, sponge
with lukewarm water. Then apply glycerine and rub lightly between
the hands. Let stand for half an hour and rinse thoroughly with
water. If a grease spot from cream remains, sponge with carbon
tetrachloride.

Boiling water.—Remove fresh stains from washable materials by


pouring boiling water on the stain from a height of 2 or 3 feet, then
wash in warm soapy water. If a trace of stain remains, dry in the sun
or bleach with hydrogen peroxide and sodium perborate.
Hydrogen peroxide and sodium perborate.—Sponge with clear
water and then with a solution of 1 teaspoon sodium perborate to 1
pint hydrogen peroxide. If the stain still shows, sprinkle powdered
sodium perborate on the stain and let stand half an hour. Rinse well
with water.

Dyes and Running Colors


As there are many different kinds of dyes, no one remover will
successfully take out all dye stains. In fact, it may be impossible to
remove some of these stains completely. Proceed as follows:

Water and sunlight.—If the material is washable, rinse the stains


in cold or lukewarm water (soak for 10 to 12 hours if necessary),
wash in heavy soapsuds, and then dry in the sun. Spots on wool or
silk materials sometimes come out by soaking or washing in cold
water.

Bleaches.—If a stain remains, try one of the following:

Hydrosulfite.—Apply one of the hydrosulfites available at drug stores


as a color remover. Follow directions on the package.

Javelle water.—For stains on uncolored linen, cotton, or rayon, dip in


Javelle water for 1 minute (no longer), remove the chlorine from the
cloth with a sodium thiosulfate solution, rinse well in water. (See p. 6
for more detailed instructions.) Do not use Javelle water to remove
stains from silk or wool.

Hydrogen peroxide.—For stains on any white material, add a few


drops of ammonia water to hydrogen peroxide. Soak the stains until
they disappear and rinse thoroughly in water. One teaspoon sodium
perborate added to 1 pint hydrogen peroxide makes a good bleach,
but it must be made fresh, as it soon loses its strength.
Egg
Scrape away as much of the stain as possible with a blunt knife.
Then sponge with cold water. Never use hot water—heat makes egg
stains harder to remove.

Pepsin.—If cold water does not remove the stain completely,


sprinkle pepsin powder over the spot. Work it in well and let stand
for half an hour. Rinse well.

Grease solvents.—For nonwashable materials, sponge first 15


with cold water. Let dry and then sponge with carbon
tetrachloride, gasoline, or other grease solvent.

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.

Grease solvent and banana oil (amyl acetate).—Use this


treatment on any material including acetate rayon and vinyon. First
wet the stain well with carbon tetrachloride or gasoline; then apply a
drop of banana oil to the stain. Brush lightly with a soft cloth, using
an upward motion to pick up the dissolved polish. For heavy stains
use dry-cleaning soap with the banana oil.

Bleaches.—To remove any color remaining after the polish itself has
been dissolved, apply a bleach. Test the cloth for change in color
first.

Hydrogen peroxide and sodium perborate.—Sponge with clear water


and then with a solution of 1 teaspoon sodium perborate to 1 pint
hydrogen peroxide. If the stain still shows, sprinkle powdered
sodium perborate on the stain and let stand half an hour. Rinse well
with clear water.

Hydrosulfite.—Apply one of the hydrosulfites available at drug stores


as a color remover. Follow directions on the package.

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.

Fruits and Berries


Treat fruit and berry stains immediately, if possible; they are hard to
remove after they dry. Boiling water (if it does not harm the cloth) or
sometimes even warm water will remove most fruit stains. It is
better not to use soap, as alkalies set some fruit and berry stains.
Use the same methods for removing stains from cooked fruits and
berries as from fresh.

Washing in warm soapy water sometimes removes stains from citrus


fruits, such as grapefruit and lemon. But if the stain is old or the
cloth has been pressed before washing, use one of the bleaches
described below. If the acid in citrus fruit changes the color of the
cloth, restore it with ammonia water or baking soda. (See Acids, p.
10.)

Cold water and glycerine.—For fresh peach, pear, cherry, and


plum stains on cotton and linen and for any fruit stain on wool or silk
materials (either white or colored), first sponge the stain well with
cool water; then work glycerine or a soapless shampoo into the
stain, rubbing lightly between the hands. Do not use soap, as 16
soap sets the stain. Let stand several hours, then apply a few
drops of vinegar or oxalic acid, allow to remain for a minute or two,
then rinse thoroughly in water.

Boiling water.—Boiling water removes from cotton and linen most


fruit stains except peach, pear, plum, and cherry. Never use boiling
water on silk or wool. Stretch the stained part over a bowl, fasten it
with string, and pour boiling water on it from a teakettle held at a
height of 3 or 4 feet so that the water strikes the stain with force.
Rubbing alternated with the boiling water is also helpful. If a stain
remains, squeeze a little lemon juice on it and place in the sun to
dry, or use one of the chemical bleaches.

Bleaches.—If a stain remains, try one of the following:

Hydrogen peroxide and sodium perborate.—Sponge with hydrogen


peroxide-sodium perborate mixture (1 teaspoon sodium perborate to
1 pint peroxide). Rinse thoroughly. If the stain persists, sprinkle
powdered sodium perborate on the dampened area and let stand for
half an hour. Finally rinse well. Always test for change of color on the
inside of a hem or seam before using these bleaches. If the color
fades, do not use them—just dampen the stain with water and
spread in the sun to bleach.

Hydrosulfite.—Hydrosulfites available at drug stores as dye removers


are satisfactory for removing fruit stains from any white material.
Follow directions on package.
Javelle water.—For stains on uncolored linen or cotton material, dip
in Javelle water for 1 minute (no longer), remove the chlorine from
the cloth with a sodium thiosulfate solution, rinse well in water. (See
p. 6 for more detailed instructions.) Do not use Javelle water on silk
or wool.

Glue and Mucilage


Water.—If the material is washable, soak the spot in warm water,
or if it is a stubborn stain you may have to boil it. If the stain is
known to be casein glue, soak it in cold water.

Acetic acid.—For nonwashable materials, sponge the spot with


water, then with acetic acid (10-percent solution) or white vinegar.
Rinse well.

Grass and Other Fresh Garden Foliage


Hot water and soap.—If the material is washable, use hot water
and soap, rubbing the stain well. If this does not completely remove
the stain, use a bleach.

Bleaches.—Try one of the following:

Javelle water.—For stains on uncolored linen, cotton, or rayon, dip in


Javelle water for 1 minute (no longer), remove the chlorine from the
cloth with a sodium thiosulfate solution, rinse well in water. (See p. 6
for more detailed instructions.) Do not use Javelle water to remove
stains from silk or wool.

Hydrogen peroxide and sodium perborate.—Sponge with clear 17


water and then with a solution of 1 teaspoon sodium
perborate to 1 pint hydrogen peroxide. If the stain still shows,
sprinkle powdered sodium perborate on the stain and let stand half
an hour. Rinse well with clear water. Always test for change of color
on a sample of the cloth before using these bleaches.

Hydrosulfite.—Hydrosulfites available at drug stores as dye removers


are satisfactory in removing grass stains from any white materials.
Follow directions on the package.

Benzene or denatured alcohol.—On materials that soap and


water might injure, sponge the stains with benzene or alcohol. Test
them first to be sure they do not change the color of the material.
Do not use alcohol on acetate rayon or vinyon unless you dilute it—1
cup denatured alcohol with 2 cups water.

Grease and Oils


Fresh grease spots usually are the pure fat or oil. Old grease spots
or stains from automobile, wheel, or machine greases usually
contain also more or less dust, dirt, or fine bits of metal. (For road
oil and axle grease, see p. 27.) First scrape or wipe off as much of
the grease as possible; then treat the stain by one of the following
methods:

Soap and water.—If the material is washable, wash in warm sudsy


water. Be sure to use plenty of soap on the stained part and rub well
between the hands. The soaplike washing agents (soapless
shampoos, oils, and lathers) are good to soften grease stains.
Place a grease or oil stain between paper towels or
cleansing tissues and press with a warm iron.

Absorbents.—Use cornstarch, French chalk, or white talcum


powder for fine materials; corn meal or salt for carpets, rugs, and
other coarse materials. Dust the powder or salt over the spot, 18
let stand until it absorbs the grease or oil, then brush off.
Another method is to place the stained part between blotting papers
and press lightly with a warm iron. Change the blotting paper as it
becomes soiled. Or use cleansing tissues or paper towels in the
same way. The advantage of using absorbents is that they do not
wet the material or leave rings as water or grease solvents are apt
to do.

Grease solvents.—Remove common grease and oils with carbon


tetrachloride, gasoline, or benzene. Place a pad of clean cloth or a
white blotter beneath the stain and sponge with a clean cloth,
moistened with the grease solvent. Work from the wrong side of the
material in order to push the dirt and grease out rather than to rub it
into the material. Use light, brushing motions, work from the outside
of the spot toward the center and spread or “feather out” the
solvent into the cloth around the stain until there is no definite edge.
Then pat dry with a clean, dry cloth.

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.

Another method is to make a paste by mixing cornstarch or talcum


with carbon tetrachloride or other dry-cleaning fluid. Spread the
paste over the spot; when dry brush it off. Repeat if necessary. The
solvent does not spread and is less likely to form a ring if used in
this way.

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.

Cold or lukewarm water.—If the material is washable and the


stain contains no highly colored fruit or chocolate, sponge with cold
or lukewarm water; then wash in warm soapsuds.

Carbon tetrachloride.—For nonwashable materials, sponge with


carbon tetrachloride to remove the greasy part of the stain. Let it
dry; then sponge with cold water to remove any stains from the egg
and sugar in the ice cream. If this does not remove the stain
completely, follow with a pepsin treatment.

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

Denatured alcohol, carbon tetrachloride, and benzene.—


Place a pad of cloth or blotter under the stain and sponge with one
of these solvents. Then rub glycerine (use glycerine only with
alcohol) or a dry-cleaning soap into the stain and finally rinse out
with the solvent. If this does not remove the stain, let the stain dry;
then wet with water and rub in a synthetic detergent (nonsoap
cleaner) to help soften the stain. Or use strong soapsuds to which a
few drops of ammonia water have been added. Alcohol must not be
used on acetate rayon or colored materials.

Printing ink

Use one of the following agents for removing printing-ink stains:

Lard or petroleum jelly.—Rub the stain with lard or petroleum


jelly; work it into the cloth. If material is washable, wash with soap
and water; otherwise sponge with carbon tetrachloride, gasoline, or
other grease solvent.
Turpentine.—Soak the stain for a few minutes in turpentine and
then sponge with carbon tetrachloride, alcohol, or other dry-cleaning
fluid. Do not use alcohol on acetate rayon or colored materials.

Kerosene.—To remove printing from flour bags and other bags,


soak in kerosene for several hours. Then wash thoroughly in soap
and hot water and spread on the grass in the sun to dry.

Writing ink

In removing writing-ink stains it usually is necessary to try various


methods. Always start with the simplest method and the one least
likely to harm the cloth.

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.

Glycerine and water or soap and water.—If the material is


washable, pour either glycerine or one of the soapless shampoos on
the fresh stain. Rub lightly between the hands, rinse, and apply
glycerine again as long as any ink comes from the stain. Rinse with
clear water. Washing with soap and warm water will remove some
types of ink.

Bleaches.—If the above treatments do not remove the stain, try a


bleach. But use bleaches sparingly on colored materials.

Oxalic acid.—Soak the stain for a few seconds in a solution of oxalic


acid (3 tablespoons of the crystals of the acid to a pint of water). Or
sponge the stain well with cold water, then stretch the stain over a
bowl of hot water, and apply crystals of oxalic acid directly to the
stain. Rinse by dipping in the hot water and finally in water to which
a few drops of ammonia water have been added. Do not use on
weighted silk.

Hydrosulfite.—Sponge with a hydrosulfite solution and rinse quickly.

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.

Denatured alcohol.—On materials that water would injure, sponge


with alcohol. On acetate rayon and colored materials be sure to
dilute the alcohol—1 cup denatured alcohol to 2 cups water.

Sodium thiosulfate (“hypo”).—Sponge the stain or dip in a


solution of 1 tablespoon of the “hypo” to 1 pint of water. Rinse well
in water.

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.

Lemon juice.—Spread the stain over a pan of boiling water and


then squeeze lemon juice on it. After a few minutes rinse; then
repeat the process. This method is rather slow, but does not harm
delicate white cottons or linens. Another method is to sprinkle the
stain with salt, squeeze lemon juice on it, and spread in the sun to
dry. Add more lemon juice if the stain still shows. Rinse well.

Oxalic acid.—Spread the stained article over a bowl of hot water


and apply a few drops of oxalic acid solution (3 tablespoons of the
crystals to 1 pint of water). Or put the crystals of acid directly on the
stain and moisten with hot water. Rinse in hot water, and repeat until
the stain disappears. Do not use on weighted silk.

Cream of tartar.—Boil the stained article in a liquid made up of 4


teaspoons of cream of tartar to 1 pint of water. Rinse thoroughly.

Hydrosulfite.—Hydrosulfites available at drug stores as color


removers or dye-stripping agents also will remove rust stains. Follow
directions given on the package. Do not use on weighted silks.

Linseed Oil
Sponge with carbon tetrachloride or benzene. Treat as for cod-liver
oil stains.

Lipstick and Rouge


Petroleum jelly and carbon tetrachloride.—If water spots the
cloth, work petroleum jelly or lard into the stain. Then either sponge
with carbon tetrachloride or dip the stained part in a bowl of the
solvent. If a trace of color remains, sponge with denatured alcohol.
On acetate rayon and colored materials, dilute the alcohol—1 cup of
denatured alcohol to 2 cups water.

Glycerine, soap, and water.—If the material is washable, first


loosen the stain as above with glycerine or petroleum jelly. Then
launder. If soap or other alkalies are applied before the stain is
loosened, they are apt to set it.

Hydrogen peroxide and sodium perborate.—Sponge with 21


sodium perborate-hydrogen peroxide mixture (1 teaspoon
sodium perborate to 1 pint peroxide). Rinse thoroughly. If the stain
persists, sprinkle powdered sodium perborate on the dampened area
and let stand for half an hour. Finally rinse well. Be sure to test the
cloth for colorfastness before using these bleaches.

Meat Juice or Gravy


Sponge meat-juice or gravy stains with cold or lukewarm water.
Never use hot water; it sets the stain. If a grease spot remains,
launder washable materials in warm soapy water. If the cloth is not
washable, use an absorbent powder or a grease solvent.

Absorbents.—Dust the powder over the stain, let it stand until it


absorbs the grease, then brush off.

Solvents.—Sponge with carbon tetrachloride, gasoline, or benzene.

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.

Boiling water.—For washable materials, pour boiling water on the


stain from a height of 3 or 4 feet, as for fruit stains, or launder in
warm soapy water.

Denatured alcohol or carbon tetrachloride.—Some color stains


can be sponged or soaked out with alcohol. Sponge greasy stains
with carbon tetrachloride. A dry-cleaning soap helps to loosen them.
Finally sponge with fresh carbon tetrachloride.

Bleaches.—Use bleaches only on white materials. Try one of the


following:

Hydrosulfite.—Use one of the hydrosulfite dye-stripping agents


available at drug stores. Follow instructions on the package.

Javelle water.—For stains on linen, cotton, or rayon, dip in Javelle


water for 1 minute (no longer), remove the chlorine from the cloth
with a sodium thiosulfate solution, rinse well in water. (See p. 6 for
more detailed instructions.) Do not use Javelle water on silk or wool.

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.

Acetic acid.—If a stain remains after the above treatment, apply


10-percent acetic acid with a medicine dropper; then rinse well in
water.

Bleaches.—If the above treatments do not completely remove the


stain, use a bleach.

Javelle water.—For stains on uncolored linen, cotton, or rayon, dip in


Javelle water for 1 minute (no longer), remove the chlorine from the
cloth with a sodium thiosulfate solution, rinse well in water. (See p. 6
for more detailed instructions.) Do not use Javelle water on silk or
wool.

Sodium perborate.—Sponge with a sodium perborate solution (4


tablespoons of the perborate in a pint of lukewarm water) or
dampen the stain with water and dust the powdered sodium
perborate on it. Rinse thoroughly. Always test for the effect on the
color of the cloth before using bleaches.

Sodium hydrosulfite.—This color remover is available at drug stores.


It may be used safely on most white materials. Follow directions on
the package.

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.

Mercury or quicksilver removes lead or solder stains from rugs or


clothing. First scrape off as much of the lead as possible with a dull
knife. Then pour mercury on the stain and work with a stick until the
mercury absorbs the stains.

Mildew
Mildew spots must be treated when fresh, before the mold growth
has a chance to weaken the cloth.

Soap and water.—On washable material, soap and water will


remove very fresh stains. Drying on the grass in the sun helps to
bleach the spots.

Bleaches.—Try a bleaching agent if soap and water do not remove


the stain. Be sure to test for colorfastness on a hidden part of the
garment.

Lemon juice.—Moisten the stain with lemon juice and salt and place
in the sun. This often removes slight stains.

Javelle water.—Old stains on cotton, linen, or rayon may be 23


bleached out with Javelle water. Dip the stain in the Javelle
water for 1 minute (no longer), remove the chlorine from the cloth
with a sodium thiosulphate solution, rinse well in water. (See p. 6 for
more detailed instructions.) Do not use Javelle water on silk or wool.
Sodium perborate.—Soak the stain in a sodium perborate solution (4
tablespoons perborate to 1 pint lukewarm water). Or dampen the
stain with water and sprinkle the perborate powder directly on the
stain. Rinse after either treatment.

Milk and Cream


See Ice Cream, p. 18.

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.

Denatured alcohol.—If water spots the cloth, sponge the stain


with alcohol. Since alcohol makes some colors run, test a sample of
the cloth to be sure it does not harm the color. On acetate rayon
sponge with dilute alcohol—1 cup denatured alcohol to 2 cups water.

Bleaches.—Try one of the following, but use sparingly on colored


materials and do not use on weighted silks.

Hydrosulfite.—Sponge with a hydrosulfite solution (2 teaspoons in 1


pint of warm water) and rinse quickly.

Oxalic acid.—Apply oxalic acid solution with a medicine dropper (see


p. 7) and rinse well with clear water. Sponge with weak ammonia
water, borax or sodium perborate solution, to neutralize the acid.

24

Paints—Oil Paints, Varnishes, Enamels


Treat oil paint, varnish, and enamel stains quickly, since a dried or
hardened paint stain is almost impossible to remove. Scrape off as
much of the paint or varnish as possible before using any remover. If
the stain has hardened, apply a solvent on both sides and give time
for it to soften. Do not rub too hard; rubbing roughens the cloth.
Use one of the following methods:

Soap and water.—If the material is washable, remove fresh stains


by washing with plenty of soap. If the stain has dried, soften it first
by rubbing oil, lard, or petroleum jelly into it.
Turpentine or other solvents.—Sponge the stain with pure
turpentine or, if the spots are large or scattered, wash the whole
article in it. Or soak in a liquid of equal parts ammonia water and
turpentine, rinse several times in fresh turpentine, wash in soapy
water. Carbon tetrachloride, kerosene, alcohol, or benzene may be
applied in the same way as turpentine. Benzene is good for the
usual type of spar varnish. Alcohol will remove stains of shellac
varnish, but never use alcohol on acetate rayon or vinyon.

Paint and varnish remover.—Equal parts of benzene, carbon


tetrachloride, and amyl acetate (banana oil) make a very good paint
remover. Apply the remover and rub in a dry-cleaning soap to help
loosen the stain. Finally rinse out with carbon tetrachloride.

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:

Denatured alcohol.—Soak the stain in alcohol. If carbon marks


remain, sponge with soap and water. Do not use alcohol on acetate
rayon. Test all dyed cloth for colorfastness.

Bleaches.—Remove the dye with a bleaching agent.

Javelle water.—For stains on uncolored cotton, linen, or rayon, dip in


Javelle water for 1 minute (no longer), remove the chlorine from the
cloth with a sodium thiosulfate solution, rinse well in water. (See p. 6
for more detailed instructions.) Do not use Javelle water on silk or
wool.
Hydrogen peroxide and sodium perborate.—For other materials
sponge with a mixture of 1 teaspoon sodium perborate to 1 pint
peroxide. Rinse well.

Lead pencil

A soft eraser sometimes will remove the marks, especially on stiff or


starched materials. If the material is washable, rub soapsuds into
the stain and launder as usual. Sponge woolen materials with clear
water or with a solution of equal parts alcohol and water.

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.

To remove perspiration odors, sponge the stained part with warm


water to which a few drops of vinegar have been added, sprinkle
powdered pepsin over the stain, work it well into the cloth, and let
stand 1 to 2 hours, keeping the spot moist. Then brush off the
powder and rinse well.

Yellowish perspiration stains on white material can be removed by:

Soap and water.—If the material is washable, bleach in the sun


after washing in soap and water.

Bleaches.—For a stubborn stain, try a bleach.


Hydrogen peroxide.—Sponge with hydrogen peroxide or a mixture of
1 teaspoon sodium perborate to 1 pint peroxide. Rinse with water.

Sodium hydrosulfite.—Quickly dip the stain into a sodium


hydrosulfite solution (2 teaspoons sodium hydrosulfite to 1 pint
water). Rinse immediately. First test the colorfastness of the cloth to
this bleach.

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:

Soap and water.—Sponge delicate, washable materials with


lukewarm water. Do not use hot water if egg or cream was used in
making the salad dressing. Use soap if it will not harm the cloth.

Grease solvents.—Sponge the stain with lukewarm water, let dry,


and then sponge with carbon tetrachloride, Stoddard solvent,
gasoline, or benzene.

Absorbents.—Absorbent powders are particularly good for splatters


of salad oil. Dust cornstarch or talcum powder over the spot, allow it
to absorb the oil or grease, then brush off. Or make a thick paste by
mixing the powder with carbon tetrachloride or other grease solvent,
spread it on the spot, let dry, and brush off. Repeat if necessary.
Another method is to put the stained cloth between cleansing tissues
and press with a warm iron.

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.

Hydrogen peroxide.—If the stained material is white, use


hydrogen peroxide. Dampen a white cotton cloth with the peroxide
and lay it on the stain. Cover with a clean dry cloth; then press with
a medium warm iron. If the hydrogen peroxide soaks through the
top cloth, replace with a dry one. Ironing directly on the cloth
moistened with peroxide or on the dampened stain itself, after the
cloth has been removed, will cause rust stains on the garment.
Repeat the treatment, until the stain is completely removed. Rinse
well.

Light scorch stains may be removed also by sponging with hydrogen


peroxide to which sodium perborate has been added (1 teaspoon
sodium perborate to 1 pint peroxide). Rinse well with water.

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.

Solvents.—Sponge well with carbon tetrachloride or turpentine.


Glycerine, lard, or petroleum jelly worked into the stain first helps to
loosen it. For liquid dressings and for stains on wool, sponge with
denatured alcohol. Do not use alcohol on acetate rayon or colored
cloth.

Bleaches.—If a dye stain remains, remove with a bleaching agent.

Hydrosulfite.—Apply one of the hydrosulfites available at drug stores


as a color remover. Follow directions on the package.

Hydrogen peroxide or sodium perborate.—Sponge the stain with


hydrogen peroxide or with a sodium perborate solution (p. 7) or
sprinkle sodium perborate powder on the moistened stain directly.
Rinse well.

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.

Soot and Smoke


Absorbents with solvents.—First brush the stain; then sprinkle
with an absorbent powder—French chalk, cornstarch, corn meal, or
salt. Work the powder around until soiled and brush it off. 27
Then if the material is washable, sponge or wash with soap
and water. If water harms the cloth, first use an absorbent; then
sponge the stain with one of the grease solvents—carbon
tetrachloride, Stoddard solvent, or gasoline.

Another method is to make a paste by mixing an absorbent powder


with carbon tetrachloride or other solvent, spread it on the stain,
then brush it off when dry.

To remove the odor of smoke from a garment, have it dry-cleaned.

Sugar Sirups
If the material is washable, wash out sugar-sirup stains with soap
and water. For more delicate materials, sponge with clean water.

Tar, Road Oil, Asphalt, Axle Grease, Pitch


Stains made by tarlike substances are hard to remove, especially
from cotton material. First rub in petroleum jelly or lard to soften the
stain, then sponge with one of the grease solvents—carbon
tetrachloride, Stoddard solvent, gasoline, benzene—or dip the article
in the liquid and rub lightly between the hands. Repeat the
treatment until the stain is removed. If the material is washable, use
warm soapy water after rubbing in the petroleum jelly or lard.

For stains on carpets or rugs, scrape off as much as possible with a


dull knife. Then sponge with the grease solvent, using a brushing
motion so that you do not rub the stain into the carpet.
Tobacco
Treat stains from the tarry substances in the stem of a pipe in the
same way as tar. Use one of the following methods to remove
tobacco juice stains:

Cold water and glycerine.—Sponge with cold water; then work


warm glycerine into the stain. Let stand for half an hour, and wash
with soap and water. If the stain cannot be completely removed by
washing, bleach it in the sun. Moistening it with lemon juice makes it
disappear more quickly.

Wood or denatured alcohol.—To remove traces of color


remaining on wool materials after the above treatment, sponge with
alcohol.

Bleaches.—Try one of the following to remove remaining tobacco


stains:

Hydrogen peroxide or sodium perborate.—Sponge with hydrogen


peroxide or with sodium perborate solution (4 tablespoons to a pint
of water). Or sprinkle powdered sodium perborate on the moistened
stain. Rinse thoroughly.

Javelle water.—For stains on cotton or linen, dip the stain in Javelle


water for 1 minute (no longer), remove the chlorine from the cloth
with a sodium thiosulfate solution, rinse well in water. (See p. 6 for
more detailed instructions.) Do not use Javelle water on silk or wool
materials. Be sure to test the cloth for colorfastness before applying
this bleach.

28

Tomato Juice and Catsup


Cold water and glycerine.—Sponge the stain thoroughly with cold
water to remove all the loose foodstuff. Next work glycerine into the
stain, and let stand for half an hour. Then wash with soap and water.

Hydrogen peroxide or sodium perborate.—Remove any


remaining stain by sponging with hydrogen peroxide or with sodium
perborate solution (4 tablespoons to 1 pint of water). Sponge or
rinse with cold water.

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.)

Warm water followed by salt and water.—Sponge with warm


water. Warm salt water may be used (about ½ cup salt to 1 quart
water). Apply and let stand 15 minutes; then sponge with clear
water.

Hydrogen peroxide or sodium perborate.—Apply a few drops of


hydrogen peroxide (see p. 7) or sponge with a mixture of 1 level
teaspoon sodium perborate to 1 pint peroxide. Powdered sodium
perborate may be sprinkled on the dampened stain. Rinse
thoroughly in water.

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:

Javelle water.—Mix Javelle water with an equal amount of hot


water. Soak the stained place for 1½ hours in this solution, and rinse
thoroughly. Then treat with oxalic acid solution (1 teaspoon oxalic
acid to 1 pint water), and rinse again. This will remove a week-old
stain and will not seriously injure the material. Soaking the stain in
Javelle water of full strength, however, rots the material. Do not use
Javelle water on silk or wool.

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

White Sauces, Cream Soups


If the material is washable, laundering in warm soap and water will
remove these spots. For nonwashable materials, sponge with warm
water, let dry, then sponge with a grease solvent—carbon
tetrachloride, gasoline, or benzene.

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.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookname.com

You might also like