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

Beginning Python - From Novice To Professional (2005)

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

580 I N D E X

cookie-cutter code

Python Database API 285

automating 367–368

connections and cursors 287–289

cookielib module 302

exceptions 287

copy method

global variables 286

dictionaries 73, 74

types 289

Cormen, Thomas H. et al

DatabaseError exception class 287

Introduction to Algorithms 387

DataError exception class 287

count method

datetime module 227, 251

lists 43

deallocating objects 366

CREATE TABLE command 485

Debian Linux

create_socket method

installing Python 5

dispatcher class 458

def statements 194, 549

ctypes tool

commenting 112

speeding up programs 361

defining functions 549

cursor function 288

defining generator-function 194

cursor object

default arguments 550

attributes 289

parameters 120

methods 288

del statements 42, 103, 104, 168, 567

dictfetchall method 487, 488

delattr function 559

dictfetchone method 486

deque type

execute method 486

collections module 225–226

fetchall method 486, 488

description attribute

fetchone method 486

cursor objects 289

cursor method

Design Patterns

connection object 288, 486

Gang of Four 387

custom exception classes 162

dict function 69, 559

D

dictfetchall() method

cursor object 487, 488

Dalke, Andrew

dictfetchone() method

Sorting Mini-HOWTO 49

cursor object 486

data structures 31

dictionaries 67

sequences 31

basic operations 69–70

database application example 291–292

telephone book example 70–71

creating and populating tables 292–293

creating 549

searching and dealing with results 294–295

iterating over 96

database support 285

methods 72, 562

database application example 291–292

clear method 72–73

creating and populating tables 292–293

copy method 73–74

searching and dealing with results

fromkeys method 74

294–295

get method 74–75 has_key method 75 items method 75 iteritems method 75 iterkeys method 76 itervalues method 77 keys method 76

pop method 76 popitem method 76 setdefault method 76

telephone book example 78 update method 77

values method 77 string formatting 71, 72 syntax 68

dict function 69 uses 67, 68

difflib module 251 dir function 211, 559 dispatcher class

bind method 458 create_socket method 458

handle_accept method 458, 461, 466 listen method 458

subclassing 457 distribution operators

*operator 123–124

**operator 123–124 Distutils toolkit 373

compiling extensions 378–379 installation 374–376 installers 378

introduction 373–374 py2exe extension

creating executable programs 379 wrapping up 376–378

division

adding statement to program 11 operators 11

I N D E X 581

divmod function 559 docstrings

adding to functions 112 doctest tool 344–346

Document Object Model (DOM) 424 documentation, modules 213–214 DOM (Document Object Model) 424 double-clicking 22

draw method

Group class 530, 532

RenderUpdates class 529

E

eight queens example 195 backtracking 196

base case 197–198 finding conflicts 197 recursive case 198–199 state representation 196 the problem 196 wrapping it up 200

elif clauses 88 else clauses 87

exceptions 166

using in loops 100–101 email module 302 empty lists 38

enable function cgitb module 324

encapsulation 143–145, 552 privacy 145–146

end method

adding to handler superclass 398 Match Objects 242

endless loop trap 189

implementing properties with old-style classes 188, 189

enter method ChatSession class 466

582 I N D E X

enumerate function 559 numbered iteration 97

environ mapping os module 217

environment variables setting 209

equality operator 89, 547 compared to identity 552

error class

pygame module 529 Error exception class 287

error messages. See tracebacks escape function

re module 239, 241 escaping quotes 23–24 eval function 559

eval statements 105–106 event handling

wxPython GUI toolkit 278 except clause

adding to try/except statement 164 catching all exceptions 165–166 catching the object 165

catching two exceptions with one block 165 using more than one 164

Exception class 161

catching all exceptions 167

creating custom exception classes 162

examples of using with raise statement 160–161

exception handling SimpleXMLRPCServer class 337

exceptions 159

built-in exceptions 161 catching exceptions 162 all exceptions 165–167

catching two exceptions with one block 164

raise statement 163

catching the object 165 custom exception classes 162 finally clause 168

functions and 168–169 introduction 159–160 objects 159–160

raise statement 160–161 using else clauses 166

using more than one except clause 164 Zen of 169–170

exec statements 104–105, 569 execfile function 559 execute method

cursor object 289, 486 executemany method

cursor object 289 executing programs 19–20 exit function

sys module 215 exponentiation operator (**) 12

compared to pow function 16 expression statements 566 expressions 10–12

compared to statements 14 Python reference 557–566

extend method lists 44

deque type 226 extending Python

introduction 357–358

Jython and IronPython 358–360 writing C extensions 360

hacking it on your own 365–370 SWIG 361–365

tools 360 extendleft method deque type 226

Extreme Programming 341

I N D E X 583

F

Factory class

twisted.internet.protocol module 309 fail method

TestCase class 348 failIf method

TestCase class 348 failIfAlmostEqual method TestCase class 348

failIfEqual method TestCase class 348

failUnless method TestCase class 348

failUnlessAlmostEqual method TestCase class 348

failUnlessEqual method TestCase class 348 failUnlessRaises method TestCase class 348

Fault class

xmlrpclib module 510 fetchall method

cursor object 289, 486, 488 fetchmany method

cursor object 289 fetchone method

cursor object 289, 486 Fibonacci numbers computation

example of using abstraction 109–110 field width

conversion specifiers 57, 58 file function 559

file iterators 266–267 file object

close method 261

read and write methods 258 reading and writing lines 260–261 seek and tell methods 260

file sharing project adding GUI 517

first implementation 518–521 further exploration 525

goals 517 preparations 518

second implementation 521–525 tools 517

file sharing with XML-RPC 499 first implementation 501–508 further exploration 516

goals 500 preparations 501 problem 499–500

second implementation 509 client interface 509–510 exceptions 510

trying it out 511–516 validating file names 510–511

tools 501 fileinput module 219

example 220–221 functions 219

lazy line iteration 265 file-like objects 255 filelineno function

fileinput module 219 filename function

fileinput module 219 files 255

basic methods 257 examples 262–263

reading and writing 258–260 reading and writing lines 260–261

closing 261

iterating over file contents 263 byte by byte 264

file iterators 266–267 lazy line iteration 265

584 I N D E X

one line at a time 264 reading everything 265

opening 255–257

filter function 134, 135, 559 filterwarnings function 160 finally clause

exceptions 168 find method 60, 61 findall function

re module 239, 240 flatten generator 193

rewritten as plain function 195 flip function

pygame.display module 529, 531, 532 float function 560

floats 10 Font function

pygame.font module 529 for loops 95

compared to list comprehension 101 range function 548

for statements 570 forking

multiple connections 304 SocketServer module 305

<form> tag

action attribute 476 format function 488 --formats switch 377 found_terminator method

async_chat class 460, 461 framework for extensions 367–368 from module import function 18

reasons not to use 19 fromkeys method

dictionaries 74 frozenset type

sets module 223 ftplib module 302

functions 16, 549–550 and exceptions 168–169

compared to methods 149 creating 111

definitions 570 documenting 112

example of module containing a function 205

functional programming 133 apply function 137

filter function 134–135 map function 134 reduce function 135–137

parameters 113 changing 114–119 collecting 121–123 distributing 123–124

keyword parameters and defaults 119–121

supplying and receiving example 124–126

values 113

Python functions 112–113

G

Gamma, Erich et al

Design Patterns 387

Gang of Four

Design Patterns 387 garbage collection 366–367 gathering operators

*operator 121

**operator 122 generator-function 194 generator-iterator 194 generators 191

avoiding 194–195

eight queens example 195 backtracking 196

base case 197–198

finding conflicts 197 problem 196 recursive case 198–199

state representation 196 wrapping it up 200

in general 194

making a generator 191–192 recursive generators 192–193

making safer 193–194 Gentoo Linux

installing Python 5 get function

pygame.event module 530, 532 get method

dictionaries 74, 75 get_surface function

pygame.display module 529, 531 getattr function 560

Cmd class 464

working with __getattr__ method 189 getdefaultencoding function

sys module 436 gethostname function socket module 298

getopt module 252 getrandbits function

random module 228 Getting Started with HTML

Dave Raggett 391 global statements 569 global variables

rebinding 127–128 globals function 560 gopherlib module 302

Graphical User Interfaces. See GUI

graphics project. See painting a pretty picture graphs and trees 195

Group class

draw method 530, 532 pygame.sprite module 530

I N D E X 585

update method 532 group method

Match Objects 242

GUI (Graphical User Interfaces) 269

text editor application example 269–270 toolkits 270

wxPython GUI toolkit

creating windows and components 273–274

downloading and installing 271–272 event handling 278

finished program 278–280 labels and positions 274–277 writing programs 272–273

H

handle_accept method dispatcher class 458, 461, 466

handle_charref method HTMLParser module 318

handle_close method async_chat class 461

handle_comment method HTMLParser module 318

handle_data method HTMLParser module 318, 319

handle_decl method HTMLParser module 318

handle_endtag method HTMLParser module 318, 319

handle_entityref method HTMLParser module 318

handle_pi method HTMLParser module 318

handle_startendtag method HTMLParser module 318

handle_starttag method HTMLParser module 318, 319

has_key method dictionaries 75

586I N D E X

hasattr function 560

working with __getattr__ method 189 working with __setattr__ method 189

head method

NNTP class 441, 442 heapify function

heapq module 224 heappop function

heapq module 224 heappush function

heapq module 223 heapq module

introduction 223–225 heapreplace function

heapq module 225 help function 212–213, 560 --help switch 343, 374

--help-commands switch 374 hex function 560 hexadecimal numbers 13 hotshot module

profiling 353 HTML

automatically marking up plain text 391 parsing 315

Tidy fixes common errors 315–316 HTMLDestination class

creating 445, 447 HTMLParser

using 318–319 HTMLParser module

methods 318 using 318

HTMLRenderer class

as subclass of handler superclass 398 httplib module 302

Hunt, Andrew and Thomas, David

Pragmatic Programmer, The 387

I

id function 560

identity compared to equality 552 IDLE

saving and executing programs 19 if statements 16, 569

catching exceptions 162 conditional execution 87

imaplib module 302 immutability

parameters 118–119 import command 18, 19 import statements 568

as clause 83

fetching functions from external modules 203

importing something as something else 82–83

import-only-once behavior modules 204, 205

in operator 91, 558 membership 39–40

include_dirs variable setting 291

indentation 85, 86 Index class 180 index method

lists 44 IndexError class 161 indexing lists 548

indexing sequences 33–34 infinite recursion 129 inheritance 146–147

classes 153–154 list, dict, and str 183

multiple inheritance 553

superclasses and multiple inheritance 155 init function

pygame module 529, 531

initialization 38

naming functions 370 inplace parameter

fileinput module 220

input compared to raw_input 26 input function 548, 560

fileinput module 219 insert method

lists 45

inspect module 156, 408 install command 375 instant markup project 391

first implementation 394 adding markup 395–396

goals for project 392 preparation 392–393 problem defined 391 second implementation 396

components 396

constructing rules and filters 402–403 filters 401

handler superclass 398–399 handlers 397

Parser class 401

putting it all together 403–408 Rule superclass 400

rules 399–400 tools 392

int function 548, 560 integers 10

large integers 12–13 IntegrityError exception class 287 InterfaceError exception class 287 interfaces

and introspection 155–156 polymorphism 155–156

InternalError exception class 287 interpreter, interactive 8–9

I N D E X 587

Introduction to Algorithms

Cormen, Thomas H. et al 387

IOError class 161

IronPython 571

alternative Python distribution 7 extending 358–360

is not operator 558

is operator 90, 91, 558 isfirstline function

fileinput module 219, 220 isinstance function 560 isstdin function

fileinput module 219 issubclass function 560 issubclass method

built-in method 153 item access 179

basic sequence and mapping protocol 180–182

subclassing list, dict and str 182–184 items method

dictionaries 75 iter function 560 iteration utilities

numbered iteration 97 parallel iteration 96–97

iterators

__iter__ method 173 iterator protocol 190

making sequences from iterators 191 iteritems method

dictionaries 75, 96 iterkeys method

dictionaries 76, 96 itertools module 252 itervalues method

dictionaries 77, 96

588 I N D E X

J

Java Swing GUI toolkit 271, 573 example of using with Jython 282–283

join method 61–62

as inverse of split method 63 JUnit test framework

unittest module 347 Jython 571

alternative Python distribution 7

example of using with Java Swing GUI toolkit 282–283

extending 358–360

K

KeyError class 161 keys method

dictionaries 76 keyword arguments

using with wx constructors 274 keyword parameters 119–121 Knuth, Donald

Art of Computer Programming, The 387

L

lambda expressions 133 lambda operator 558

LATEX

introduction 392

len function 40, 551, 560 library_dirs variable

setting 291 line method

Canvas class 413 lineno() function

fileinput module 219 LinePlot class

reportlab.graphics.charts.lineplots package 418

LineReceiver class

Twisted framework 455

LineReceiver protocol twisted.protocols.basic module 310

lines generator

instant markup project 394 linesep variable

os module 217 Linux

installing Python 3–4 installing Python with RPM 4–5

list comprehension 101–102 list function 41, 560

listen method dispatcher class 458 socket class 298

listenTCP function twisted.internet.reactor module 309

lists 31, 40, 548 assigning to slices 42 changing 41 changing lists 41 deleting elements 42 list function 41 methods 43, 562

advanced sorting 48–49 append method 43 count method 43 extend method 44 index method 44 insert method 45

pop method 45–46 remove method 46 reverse method 46–47 sort method 47–48

load function

pygame.image module 530 locals function 560

localtime function time module 441

localtime function time module 227

logging 385–387 logging module 252 long function 560 long strings 27 loop method

asyncore module 458 loops 93, 94

breaking out of break statement 98

continue statement 99 else clauses 100–101

while true/break idiom 99–100 for loops 95

iteration utilities numbered iteration 97 parallel iteration 96–97

reversed and sorted iteration 98 while loops 94

lower method 62, 91

M

MacPython 571

Macs

installing Python 6 magic methods 173, 553

constructors 174–175

calling unbound superclass constructor 177–178

overriding 175–177 item access 179

basic sequence and mapping protocol 180–182

subclassing list, dict and str 182–184 iterators

iterator protocol 190

making sequences from iterators 191 setting up Python 209

super function 178–179

I N D E X 589

mailbox module 302 mailcap module 302 MainLoop method wx.App class 273

maketrans function 60, 64 MANIFEST.in file 376 map function 134, 560

markup project. See instant markup project Martelli, Alex

boy/girl pairing solution 102 Python Cookbook 93

match function re module 239

MatchObjects and groups re module 241

math module

sqrt function 554 Matplotlib/pylab

alternative to PYX and ReportLab 420 max function 40, 560 MAX_HISTORY_LENGTH constant

setting to 6 504 McMillan installer

installing Distutils toolkit 378 md5 module 251

membership 39

in operator 39–40 memory leaks 366 methods

compared to functions 149 creating 150–151 definitions 148

mhlib module 302 min function 40, 561 minimum field width

conversion specifiers 56 mktime function

time module 227