Download Python - How to Program PDF

TitlePython - How to Program
File Size25.0 MB
Total Pages1103
Table of Contents
                            toc_2.pdf
pythonhtp1.pdf
	c.jpg
	pythonhtp1_01.pdf
	pythonhtp1_02.pdf
	pythonhtp1_03.pdf
	Pythonhtp1_04.pdf
	pythonhtp1_05.pdf
	pythonhtp1_06.pdf
	pythonhtp1_07.pdf
	pythonhtp1_08.pdf
	pythonhtp1_09.pdf
	pythonhtp1_10.pdf
	pythonhtp1_11.pdf
	pythonhtp1_12.pdf
	pythonhtp1_13.pdf
	pythonhtp1_14.pdf
	pythonhtp1_15.pdf
	pythonhtp1_16.pdf
	pythonhtp1_17.pdf
pythonhtp1_19.pdf
pythonhtp1_20.pdf
pythonhtp1_21.pdf
pythonhtp1_22.pdf
pythonhtp1_23.pdf
pythonhtp1_24.pdf
pythonhtp1_25.pdf
pythonhtp1_26.pdf
pythonhtp1_27.pdf
pythonhtp1_28.pdf
pythonhtp1_29.pdf
                        
Document Text Contents
Page 1

Python How to Program, 1/e
Table of Contents

1. Introduction to Computers, Internet and the World Wide Web.
2. Introduction to Python Programming.
3. Control Structures.
4. Functions.
5. Tuples, Lists, and Dictionaries.
6. Introduction to the Common Gateway Interface (CGI).
7. Object-Based Programming: Classes and Data Abstraction.
8. Object-Oriented Programming: Inheritance and Polymorphism.
9. Operator Overloading.
10. Graphical User Interface Components: Part 1.
11. Graphical User Interface Components: Part 2.
12. Exception Handling.
13. Strings Manipulation and Regular Expressions.
14. File Processing and Serialization.
15. Extensible Markup Language (XML).
16. Python XML Processing.
17. Python Database Application Programming Interface (DB-API).
18. Process Management.
19. Multithreading.
20. Networking.
21. Security.
22. Data Structures.
23. Case Study: Multi-Tier Online Bookstore.
24. Multimedia.
25. Accessibility.
26. Bonus: Introduction to XHMTL: Part I.
27. Bonus: Introduction to XHTML: Part II.
28. Bonus: Cascading Style Sheets™ (CSS).
29. Bonus: Introduction to PHP.
Appendix A. Operator Precedence Chart.
Appendix B. ASCII Character Set.
Appendix C. Number Systems.
Appendix D. Python Development Environments.
Appendix E. Python 2.2 Resources.
Appendix F. Career Opportunities.
Appendix G. Unicode®.

Page 551

Chapter 16 Python XML Processing 549

Visitors to the message forum are greeted initially by the Web page that default.py
(Fig. 16.23) generates, which displays links to all forums and provides forum management
options. Initially, only two links are active—one to view the Feedback forum (i.e., the
sample forum) and one to create a forum. In the chapter exercises, we ask the reader to
enhance the message forum by adding functionality for modifying and deleting forums.

11 <message timestamp = "Wed Jun 27 12:53:22 2001">
12 <user>Jessica</user>
13 <title>Nice forums!</title>
14 <text>These forums are great! Well done, all.</text>
15 </message>
16
17 </forum>

1 <?xml version = "1.0"?>
2
3 <!-- Fig. 16.22: forums.xml -->
4 <!-- XML document containing all forums -->
5
6 <?xml:stylesheet type = "text/xsl" href = "formatting.xsl"?>
7
8 <forums>
9

10 <forum filename = "feedback.xml">
11 <name>Feedback</name>
12 </forum>
13
14 </forums>

Fig. 16.22Fig. 16.22Fig. 16.22Fig. 16.22 XML document containing data for all available forums.

1 #!c:\Python\python.exe
2 # Fig. 16.23: default.py
3 # Default page for message forums.
4
5 import os
6 import sys
7 from xml.dom.ext.reader import PyExpat
8
9 def printHeader( title, style ):

10 print """Content-type: text/html
11
12 <?xml version = "1.0" encoding = "UTF-8"?>
13 <!DOCTYPE html PUBLIC
14 "-//W3C//DTD XHTML 1.0 Strict//EN"
15 "DTD/xhtml1-strict.dtd">
16 <html xmlns = "http://www.w3.org/1999/xhtml">
17

Fig. 16.23Fig. 16.23Fig. 16.23Fig. 16.23 Default page for the message forum. (Part 1 of 3.)

Fig. 16.21Fig. 16.21Fig. 16.21Fig. 16.21 XML document representing a forum containing one message. (Part 2 of 2.)

pythonhtp1_16.fm Page 549 Wednesday, December 19, 2001 2:46 PM

Page 552

550 Python XML Processing Chapter 16

18 <head>
19 <title>%s</title>
20 <link rel = "stylesheet" href = "%s" type = "text/css" />
21 </head>
22
23 <body>""" % ( title, style )
24
25 # open XML document that contains the forum names and locations
26 try:
27 XMLFile = open( "../htdocs/XML/forums.xml" )
28 except IOError:
29 print "Location: /error.html\n"
30 sys.exit()
31
32 # parse XML document containing forum information
33 reader = PyExpat.Reader()
34 document = reader.fromStream( XMLFile )
35 XMLFile.close()
36
37 # write XHTML to browser
38 printHeader( "Deitel Message Forums", "/XML/site.css" )
39 print """<h1>Deitel Message Forums</h1>
40 <p style="font-weight:bold">Available Forums</p>
41 <ul>"""
42
43 # determine client-browser type
44 if os.environ[ "HTTP_USER_AGENT" ].find( "MSIE" ) != -1:
45 prefix = "../XML/" # Internet Explorer
46 else:
47 prefix = "forum.py?file="
48
49 # add links for each forum
50 for forum in document.getElementsByTagName( "forum" ):
51
52 # create link to forum
53 link = prefix + forum.attributes.item( 0 ).value
54
55 # get element nodes containing tag name "name"
56 name = forum.getElementsByTagName( "name" )[ 0 ]
57
58 # get Text node's value
59 nameText = name.childNodes[ 0 ].nodeValue
60 print '<li><a href = "%s">%s</a></li>' % ( link, nameText )
61
62 print """</ul>
63 <p style="font-weight:bold">Forum Management</p>
64 <ul>
65 <li><a href = "addForum.py">Add a Forum</a></li>
66 <li>Delete a Forum</li>
67 <li>Modify a Forum</li>
68 </ul>
69 </body>
70

Fig. 16.23Fig. 16.23Fig. 16.23Fig. 16.23 Default page for the message forum. (Part 2 of 3.)

pythonhtp1_16.fm Page 550 Wednesday, December 19, 2001 2:46 PM

Page 1102

Index 1

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 9/28/01

Symbols
- 1409
!= operator 1406
$ 1396
$ metacharacter 1409
* 1409
. 1400, 1409
/i 1409
/s 1409
/x 1409
< 1406
<= 1406
<?php 1396
== operator 1406
=> operator 1403
> 1406
>= 1406
[[:<:]] 1409
[[:>]] 1409
[] 1402, 1409
\ character 1416
^ 1409

A
append 1424
array 1402
array function 1402
arrays.php 1403
as 1403
assignment operator 1401

B
bracket expression 1409
business logic 1416, 1418
business rule 1416

C
caret metacharacter (^) 1409
CDT>fig29_21.php 1432
character 1410
character class 1410
chop function 1425
compare.php 1406
comparison operator 1405
concatenation operator 1400
cookie 1430
cookies.html 1431
cookies.php 1432
count function 1402

D
data.html 1426

data.php 1398
database connectivity 1425
database handle 1429
database.php 1427
delimiter 1425
die function 1418, 1429
double 1397

E
environment variable 1410, 1412
environment variables (PHP) 1412
equality operator 1405
ereg function 1407, 1408
ereg_replace function 1410
eregi function 1409
Examples

Array manipulation 1403
Displaying the cookie’s

contents 1434
Displaying the environment

variables 1411
fig29_01.php 1396
fig29_03.php 1398
fig29_04.php 1400
fig29_06.php 1403
fig29_07.php 1406
fig29_08.php 1407
fig29_11.php 1411
fig29_13.html 1413
fig29_14.php 1416
fig29_15.html 1418
fig29_16.php 1421
fig29_18.html 1426
fig29_19.php 1427
fig29_20.html 1431
fig29_21.php 1432
fig29_24.php 1434
Form to query a MySQL

database 1426
Gathering data to be written as

a cookie 1431
Obtaining user input through

forms 1416
Querying a database and

displaying the results 1427
Simple PHP program 1396
Type conversion 1398
Using PHP’s arithmetic

operators 1400
Using regular expressions

1407
Using the string comparison

operators 1406
Verifying a username and

password 1421

Writing a cookie to the client
1432

XHTML form for gathering
user input 1413

XHTML form for obtaining a
username and password 1418

expression.php 1407

F
fclose function 1425
feof function 1425
fgets function 1425
fig29_01.php 1396
fig29_03.php 1398
fig29_04.php 1400
fig29_06.php 1403
fig29_07.php 1406
fig29_08.php 1407
fig29_11.php 1411
fig29_13.html 1413
fig29_14.php 1416
fig29_15.html 1418
fig29_16.php 1421
fig29_18.html 1426
fig29_19.php 1427
fig29_20.html 1431
fig29_24.php 1434
file handle 1425
first.php 1396
fopen function 1424, 1425
for repetition structure 1402,

1429
foreach structure 1403, 1429
form.html 1413
form.php 1416
fputs function 1424

G
$GLOBALS variable 1410
globals.php 1411

H
HTTP connection 1410
HTTP host 1410
$HTTP_COOKIE_VARS 1434,

1435

I
index value 1429
integer 1397
interpolation 1397
isset function 1420

pythonhtp1_29IX.fm Page 1 Friday, September 28, 2001 2:23 PM

Page 1103

2 Index

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 9/28/01

K
key function 1403
keyword 1402

L
literal character 1408
logical negation (!) operator 1425

M
metacharacter 1409, 1410
method = "post" 1415
MySQL 1395, 1426
mysql_connect function 1429
mysql_error function 1429
mysql_fetch_row function

1429
mysql_query function 1429
mysql_selectdb function

1429

N
newline character (\n) 1424
next function 1403

O
operator precedence chart 1435
operators.php 1400

P
parenthetical memory in PHP

1409
password.html 1418
password.php 1421
Perl (Practical Extraction and

Report Language) 1396
Perl-compatible regular

expression 1407
PHP comment 1397
.php extension 1398
PHP keyword 1402
PHP quantifier 1409
Portable Operating System

Interface (POSIX) 1407
POSIX extended regular

expression 1407
post request type 1415
Practical Extraction and Report

Language (Perl) 1396
preg_match function 1407
print function 1397
print statement 1396

private Web site 1418

Q
quantifier 1409

R
Rasmus Lerdorf 1395
read 1424
regular expression 1405, 1407
relational operator 1406
reset function 1403

S
setcookie function 1431
settype function 1399
split function 1425
strcmp function 1405
string 1397

T
time function 1431

V
validation 1416

W
Web server 1426, 1430
while loop 1425
write 1424

pythonhtp1_29IX.fm Page 2 Friday, September 28, 2001 2:23 PM

Similer Documents