Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Python - From Novice To Professional (2005)

.pdf
Скачиваний:
139
Добавлен:
17.08.2013
Размер:
13.91 Mб
Скачать

590I N D E X

mod_python

CGI handler 330–331 configuring Apache 330 installing 329 installing on Unix 329

installing on Windows 329 PSP 331–332

Publisher 332–334 mode argument

open function 256–257

modulator tool

speeding up programs 361 modules 17, 18, 203, 554

example of module containing a function 205

example of module with conditional test code 206

exploring 211

__all__ Variable 212 dir function 211

documentation 213–214 help function 212–213 using source code 214

making available 207 location 207–208

telling interpreter where to look 208–209 naming 210

packages 210–211 standard library 215 cmd module 252 csv module 251

datetime module 251 deques 225–226 difflib module 251

fileinput module 219–221 getopt module 252 heaps 223–225

itertools module 252 logging module 252

md5 and sha modules 251

optparse module 252 os module 216–218 profile module 251 random module 228 re module 235–251 Sets 221–223

shelve module 231–235 sys module 215–216 time module 226–227 timeit module 251 trace module 251

using to define things 205–207 writing 203–204

modules mapping sys module 215

modulus operator 11 Monty Python

Self-Defense Against Fresh Fruit 527 multiple connections 303

asynchronous I/O 305–308

forking and threading with SocketServers 305

multiple exceptions catching 165

multiple inheritance 553 superclasses 155

multiple superclasses 154–155 multiplication of sequences example 38–39 multiplication operator (*) 37

MySQL database

creating for bulletin board project 485 suitability for bulletin board project 484

MySQLdb module 573

N

named arguments 550 NameError class 161 namespaces 151–152 nested scopes 128 nesting lists 548

nesting blocks 88

Network News Transfer Protocol. See NNTP network programming 297

multiple connections 303 asynchronous I/O 305–308

forking and threading with SocketServers 305

socket module 298–300 SocketServer module 302–303 standard library modules 302 Twisted framework 308

downloading and installing 308 writing a Twisted server 309–311

urllib and urlib2 300 opening remote files 300 retrieving remote files 301

virtual tea party 455–472 newnews method

NNTP class 441–442 news project 439

first implementation 441–444 further exploration 452

goals 440 preparations 440–441 problem 439

second implementation 444–452 tools 440

nextfile function

fileinput module 219, 220 nextset method

cursor objects 289 nlargest function

heapq module 225

NNTP (Network News Transfer Protocol)

introduction 439

NNTP class methods 441

NNTP server

access required to use nntplib 440

I N D E X 591

nntplib library 439, 440 nntplib module 302 NNTPSource class

creating 447 None 38

not in operator 558 not operator 558

NotImplementedError exception class 217 NotSupportedError exception class 287 nsmallest function

heapq module 225

Nullsoft Scriptable Install System

installing Distutils toolkit 378

numbered iteration 97 enumerate function 97

numbers 10–12

cmath module and complex numbers 18 hexadecimal numbers 13

octal numbers 13 NumPy tool

speeding up programs 361

O

object-oriented design 156–157 objects 139–140

encapsulation 143–145 privacy 145–146

inheritance 146–147 polymorphism 140–142

forms of 142–143 oct function 561 octal numbers 13

Official Python Distribution 571 online reources 571

distributions 571 documentation 572

newsgroups and mailing lists 573 toolkits and modules 572–573

592I N D E X

open function buffering 257

mode argument 256–257 opening files 255–256 shelve module 231 webbrowser module 218

open functions 83, 561 OperationalError exception class 287 operator module

add function 142 optparse module 252 or logical operator 551 or operator 558

OR operator (|)

finding union of two sets 222 ord function 561

os module example 217–218

functions and variables 216 urandom function 228

os.path module 501

P

packages 210–211 packaging programs 373

Distutils toolkit 373 installation 374–376 introduction 373–374 wrapping up 376–378

painting a pretty picture project

creating line diagram from sunspots data 411–420

further exploration 420 goals 412

problems 411 tools 412

visualizing data from text file 411 palindromes 362–365

detartrated 369–370

parallel iteration 96–97 zip function 97

parameters changing 114–115

reasons for 115–118 collecting 121–123 distributing 123–124 immutability 118–119

keyword parameters and defaults 119–121 supplying and receiving example 124–126 values 113

paramstyle module property Python DB API 286

parse function xml.sax module 425

Parser class

addFilter method 402 addRule method 402 instant markup project 401 methods 401

pass statements 102, 567 password handling

sha module 478–479 path variable

sys module 215 patterns

greedy and nongreedy patterns 243 pdfgen module

generating PDFs 413

PDFs, generating with ReportLab 413 peer-to-peer interaction 499

pickle 285

pipe characters (|) 259 PlainDestination class creating 445, 447

platform variable sys module 215

playful programming 381 flexibility 381–382

configuration 383–385 prototyping 382–383

introduction 381 learning more 387 logging 385–387

minimum requirements 387 project structure 388

plus sign

concatenating sequences 551 poll function

asynchronous I/O 306–308 PolyLines, creating 415 polymorphism 140–142

forms of 142–143 interfaces 155–156

pop method dictionaries 76 lists 45, 46

popen functions

running the Tidy program 317 popitem method

dictionaries 76 sequence unpacking 84

poplib module 302 positional parameters 119 PostgreSQL database

creating for bulletin board project 485 using for bulletin board project 484

pound bang #! 21 pow function 16, 561

Pragmatic Programmer, The

Andrew Hunt and David Thomas 387 precision 54

conversion specifiers 57 print statements 14, 16, 567 using with commas 81–82

I N D E X 593

procedures 112

profile module 251, 353

profiling as alternative to unit testing 353–354

ProgrammingError exception class 287 programs

importing as module 203 packaging 373–380

properties 184–185

__getattr__, __setattr__, and others 188–189

property function 185–186

static methods and class methods 187–188 working on old style classes 173

property function 173 creating properties 185–186

protocols description 179

prototyping 382–383

PSP (Python Server Pages) 331–332

Psyco tool

speeding up programs 360 psycopg module 573

importing 484 Publisher 332–334 pump function

pygame.event module 530 push method

async_chat class 461 PuTTY software 457 Py_DECREF macro 366–367 Py_INCREF macro 366–368 py_modules directive 376 PyChecker tool 351

importing as module 351 using 352–353

Pygame

using for arcade game 528 web site 527

594I N D E X

pygame module 528, 573 error class 529

init function 529, 531 Surface function 528 pygame.display module 529

blit method 529

flip function 529, 531, 532 get_surface function 529 set_caption function 529 set_mode function 529, 531 update function 529, 532

pygame.event module get function 530, 532 pump function 530

pygame.font module Font function 529 pygame.image module load function 530

pygame.locals module 529 importing constants from 532

pygame.mouse module set_visible function 531

pygame.sprite module Group class 530 Sprite class 530

PyGTK GUI toolkit 271, 573 PyLint tool 351

importing as module 351 using 352, 353

PyQt GUI toolkit 271, 573 Pyrex tool

speeding up programs 361 pysqlite

downloading and installing 290–291 getting started 291

Python

__future__ module 19 algorithms 9–10

alternative distributions 6–7 basics 547

cmath module 18 command prompt 20 comments 22

compiling from source 5–6 extending 357–570 functions 16, 549–550

getting input from user 15–16 hexadecimal numbers 13 installing on Debian Linux 5 installing on Gentoo Linux 5 installing on Linux 3–4 installing on Linux with RPM 4–5 installing on Macs 6

installing on Windows 1–2 interpreter 8–9

keeping up to date 8 large integers 12–13 loose ends 554

making scripts behave like normal programs 21–22

modules 17–18

numbers and expressions 10–12 objects 550–554

octal numbers 13 reference 557

expressions 557–566 statements 566–570

saving and executing programs 19–20 statements 14–15

strings 23

concatenating strings 24

input compared to raw input 26 long strings 27

raw strings 27–28

single-quoted strings and escaping quotes 23–24

string representations 25 Unicode strings 29

variables 13

Python C API 365

hand-coded palindrome module 369–370 python command 3

Python Cookbook

Alex Martelli 93

Python Database API 285 connections and cursors 287–289 exceptions 287

global variables 286 types 289

constructors and special values 289 Python functions 112–113

Python Server Pages 331–332 python-list@python.org mailing list 573 PYTHONPATH environment variable

including module directory in 208 PythonWin GUI toolkit 271, 573

PYX package 412 PyXML module 573

Q

-Qnew command-line switch 11 quote function

urllib module 301 quote_plus function urllib module 301

R

Raggett, Dave

Getting Started with HTML 391 raise statement

catching exceptions 163

examples of using with Exception class 160–161

raise statements 568 random module 501 examples 229–231

introduction 228 random function

random module 228

I N D E X 595

randomString function 510 randrange function

random module 228 range function 561

checking for arguments 213 for loops 548

raw strings 27–28 raw_input 23

compared to input 26 raw_input function 241, 548, 561

rawDataReceived event handler 310 Raymond, Eric S.

Art of UNIX Programming, The 387

RDF Site Summary. See RSS

re module

content of 238–241 examples 244–251 importing 160

MatchObjects and groups 241 support for regular expressions 235

using group numbers and functions in substitutions 242–243

read method file object 258

readline method

file object 260–261 using in a while loop 264

Really Simple Syndication. See RSS recursion 129

binary search 131–133 factorial and power 130–131 parts 129–130

recursive generators 192–193 making safer 193–194

reduce function 135, 136, 137, 561

Refactoring

Kent Beck et al 387 refactoring, description 439 reference counting 366–367

596I N D E X

regex module importing 159

register_function method SimpleXMLRPCServer class 501

register_instance method SimpleXMLRPCServer class 501

regular expressions

alternatives and subpatterns 237 beginning and end of string 238 changing plain text to markup 391 character sets 236

content of re module 238–241 escaping special characters 236 introduction 235

optional and repeated subpatterns 237–238 support for in re module 235

wildcard 236 reload function 561

remote editing with CGI project 473 first implementation 474–475 further exploration 481

goals 473–474 preparations 474 problem 473

second implementation 476 edit.cgi 476–478 index.html 476

running the editor 479–481 save.cgi 478–479

tools 474 remove method

lists 46

Set class 223 RenderUpdates class

draw method 529 replace method 62 reportlab module 573

importing 412

ReportLab package 412

creating graphics and documents in PDF format 411

drawing with 413–415

PYX as alternative to 420 workings of 412

reportlab.graphics package 413 reportlab.graphics.charts.lineplots package

LinePlot class 418 repr function 25, 561

requirement specification 342–343 return statements 194, 567

returning values from functions 549 reverse method

lists 46, 47, 98

reversed function 47, 98, 561 Rich Site Summary. See RSS robotparser module 302 rollback method

connection object 288 Rossum, Guido 19 round function 17, 561 rowcount attribute

cursor objects 289 rpm --install command installing RPMs 423 RPM package manager

installing Python on Linux 4, 5 RPMs

installing with rpm --install command 423 RSS

introduction 335–336 rule objects

action method 401 condition method 401 methods 400

I N D E X 597

S

safe_substitute method 55 sample function

random module 228 saving programs 19–20 SAX

parser 422

processing XML files 421 workings of 424

scoping 126–127 nested scopes 128

rebinding global variables 127 screen scraping

Beautiful Soup module 319–320 HTMLParser 318–319 introduction 313–314

Tidy 314

getting Tidy library 316 introduction 315–316

using command-line Tidy 317 XHTML 317

sdist command 376 search function

re module 239 security

CGI security risks 323 seek method

file object 260 select function

asynchronous I/O 306, 307 select module 304, 456

select module

polling event constants 307 select function 304, 456

self parameter 149

Self-Defense Against Fresh Fruit 527 sep variable

os module 217

sequence multiplication example 38–39

sequence unpacking assignment statements 83–84 popitem method 84

sequences

common sequence operations 32 adding sequences 37

indexing 33–34

length, minimum, and maximum 40 longer steps 36–37

membership 39–40 multiplication 37

None, empty lists, and initialization 38 shortcut 35

slicing sequences 34–35 introduction 31–32

serve_forever method SimpleXMLRPCServer class 501

ServerProxy object 338

Set class

add method 223 remove method 223 sets module 221–223 union method 222

set_caption function pygame.display module 529

set_mode function pygame.display module 529, 531

set_terminator method async_chat class 460–461

set_visible function pygame.mouse module 531

setattr function 561 setdefault method

dictionaries 76, 102 setdefaultencoding function

sys module 436 setinputsizes method cursor objects 289

598 I N D E X

setoutputsize method

Slashdot

cursor objects 289

as example of bulletin board 483

sets module

sleep function

frozenset type 223

time module 227

Set class 221–223

slice assignments

set type 221

assigning lists to slices 42

SetSizer method

inserting elements 42

wx.Panel class 276

slicing lists 548

sha module 251

slicing sequences 34–35

password handling 478–479

SmallTalk language

shadowing 127

privacy of object state 145

shebang 21

smtpd module 302

Shelve class

smtplib module 302

close method 231

socket class

shelve module 285

bind method 298

example 232–235

listen method 298

introduction 231

socket module 298

open function 231

socket module

potential problem 231–232

gethostname function 298

Show method

network programming 298

wx.Frame class 273

socket class 298

shuffle() function

SocketServer class 297

random module 228

SocketServer module

Simple API for XML. See SAX

classes 303

simple statements 566

forking and threading 305

Simple Wrapper and Interface Generator. See

network programming 302–303

SWIG

sort method 47, 48, 98

 

SimpleWebSource class

advanced sorting 48, 49

 

creating 447

sorted function 98, 561

 

SimpleXMLRPCServer class

Sorting Mini-HOWTO 49

 

allow_reuse_address attribute 509

Andrew Dalke 49

 

handling of exceptions 337

span method

 

register_function method 501

Match Objects 242

 

register_instance method 501, 505

special characters

 

registering Node with 503

character sets 237

 

serve_forever method 501

parsing in XML files 436

 

SimpleXMLRPCServer module 302, 501, 573

split method 63

 

importing 501

Join as inverse of 61–62

 

single-quoted strings 23–24

 

I N D E X 599

split function

print statements 14, 16

re module 239, 240

using with commas 81–82

Sprite class

static methods

pygame.sprite module 530

creating with staticmethod objects 187

sqrt function 18

stderr variable

math module 554

sys module 215, 258

Stackless Python

stdin variable

alternative Python distribution 6

iteratorability of 266

StandardError exception class 287

sys module 215, 258

start method

stdout class

adding to handler superclass 398

sys module 215, 258

Match Objects 242

write method 310

start_paragraph method

StopIteration exception 190

adding to handler superclass 398

str function 25, 561

statements 14–15

StreamRequestHandler class 303

assignment statements 83–85

strftime function

blocks 85–86

time module 227, 441

conditional statements 86–87

string formatting

elif clauses 88

dictionaries 71, 72

else clauses 87

string module 501

if statements 87

Template class 72

nesting blocks 88

template strings 54

conditions 86

values from 60

assertions 93

string representations 25

boolean statements 91–92

strings

comparison operators 89–91

comparing strings and sequences 91

del statements 103–104

concatenating strings 24

eval statements 105–106

input compared to raw_input 26

exec statements 104–105

long strings 27

if statements 16

methods 563–565

import statements 82–83

raw strings 27, 28

loops 93–94

single-quoted strings and escaping quotes

breaking out of 98–100

23–24

 

for loops 95

string formatting 53–56

 

iterating over dictionaries 96

conversion types 56–57

 

iteration utilities 96–98

signs, alignment, and zero-padding

58–59

while loops 94

simple conversion 56

pass statements 102

width and precision 57