You are on page 1of 858

:

ISBN 978-5-93286-157-8

9 785932 861578

Python
Essential Reference
Fourth Edition

David Beazley

Python

-
2010

High tech

Python.
.

.

.
.
.
.
.
.

.
Python. . . . .: -, 2010.
864 ., .
ISBN 978-5-93286-157-8
Python. Python. ; ,
.
, Python. ,
, - .
Python,
, , , .
, , ,

.
,
. ,
Python 2.6 Python 3.
ISBN 978-5-93286-157-8
ISBN 978-0-672-32978-4 ()
-, 2010
Authorized translation of the English edition 2009 Pearson Education, Inc. This
translation is published and sold by permission of Pearson Education, Inc., the owner
of all rights to publish and sell the same.
, . , , .

-. 199034, -, 16 , 7,
. (812) 324-5353, www.symbol.ru. N 000054 25.12.98.
30.07.2010. 70100 1/16. .
54 . . 1200 .

199034, -, 9 , 12.

,
.


............................................................................... 15
........................................................................ 17
................................................................................19

I. Python .......................................... 21
1. ........................................................... 23
........................................................... 23
.............................. 25
............................................................. 28
- ..........................................29
................................................................................ 30
................................................................................ 32
.............................................................................. 33
........................................................................... 35
............................................................................... 36
................................................................ 37
.............................................................................39
.......................................................................... 40
....................................................................... 41
................................................................. 43
........................................................................ 44
............................................................................... 46
............................................................... 47

2. ........................... 48
..................................................... 48
........................... 49
.............................................................. 50
............................................................. 51
......................................................................... 54
, ...................... 54
..................................................... 55
......................................................................... 55
..................................56

3. ....................................................... 57
...................................................................... 57
................................................... 58
.............................................59
....................................................................60
........................................................61
.................................63

......................................... 75

............................ 80
.............................. 84

4. .....................................................96
..........................................................96
.......................................99
........................................................ 103
..................... 105
...................................................... 108
................................................. 109
........................... 109
(.)........................................... 110
() ................................................. 110
.................................................... 111
...................... 112
....................................... 113
........................................................... 113
.......................................................... 114

5. ... 116
.................................. 116
....................................................... 117
.............................................................. 117
...................................................................... 120
with .............................. 126
__debug__ ....................... 128

6. ............... 130
........................................................................... 130
...................... 133
............................................................. 134
...................................... 136
....................................................................... 139
yield ............................................. 141
yield........................................... 143

9
.............................. 146
............................................................ 148
- ....................................................... 150
........................................ 151
lambda ................................................................. 152
........................................................................... 153
................................................... 154
............................................................. 155
eval(), exec() compile() .......................................... 156

7. - .... 158
class ................................................................ 158
............................................................. 159
............................................................. 160
..................................................................... 160
,
.................................................. 165
................................... 165
............................................................................ 167
...................................................................... 170
............................ 171
.............................................. 172
.................... 176
__slots__ ........................................................................... 177
........................................................ 178
............................ 180
................................................ 182
....................................................................... 184

8. , ........................................ 189
import ................................................ 189
......................... 191
.............. 193
........................................................... 194
........................................... 195
................................ 196
.............................................................................. 197
Python ......... 200
........................................... 203

9. ..................................................................... 205
................................... 205
...................................................... 207
..................................................... 207
, ......... 211

10

print ............................................................... 212


print() .................................................................. 213
........................ 213
.............................................. 214
.................................................... 215
- ........................................................... 218
pickle ..................................... 223

10. ........................................................... 226


............................... 226
........................................................ 229
Python..................................... 230
.................. 231
........................... 232
........................................ 232
....................................................... 234

11. , , .. 236
doctest ............................ 236
unittest ............................. 239
Python pdb .............................................. 242
................................................ 247
.................................................... 248

II. Python ........................................ 257


12. ........................................................ 259
................................................ 259
...................................................... 273
................................................ 278
future_builtins ........................................................ 279

13. Python ................................ 280


atexit ..................................................................... 280
copy ....................................................................... 280
gc .......................................................................... 281
inspect ................................................................... 283
marshal .................................................................. 288
pickle ..................................................................... 289
sys ......................................................................... 292
traceback ................................................................ 300
types ...................................................................... 301
warnings................................................................. 303
weakref .................................................................. 305

11

14. ..................................................................... 309


decimal ................................................................... 309
fractions ................................................................. 317
math ...................................................................... 319
numbers ................................................................. 321
random ................................................................... 322

15. ,
.......................................... 326
abc ......................................................................... 326
array ...................................................................... 328
bisect ..................................................................... 331
collections ............................................................... 332
contextlib ............................................................... 339
functools ................................................................ 339
heapq ..................................................................... 341
itertools .................................................................. 342
operator.................................................................. 346

16. ............................................. 349


codecs .................................................................... 349
re .......................................................................... 354
string ..................................................................... 362
struct ..................................................................... 366
unicodedata ............................................................. 369

17. ..................................................... 375


... 375
sqlite3 .................................................................... 383
DBM ............................. 391
shelve ..................................................................... 393

18. ........................................ 395


bz2 ........................................................................ 395
filecmp ................................................................... 396
fnmatch .................................................................. 398
glob ....................................................................... 399
gzip ....................................................................... 400
shutil ..................................................................... 400
tarfile .................................................................... 402
tempfile .................................................................. 407
zipfile .................................................................... 409
zlib ........................................................................ 413

19. ....................................... 415


commands ............................................................... 416
ConfigParser configparser....................................... 416

12

datetime ................................................................. 421


errno ...................................................................... 430
fcntl....................................................................... 434
io........................................................................... 437
logging ................................................................... 445
mmap .................................................................... 463
msvcrt .................................................................... 467
optparse ................................................................. 469
os .......................................................................... 475
os.path ................................................................... 496
signal ..................................................................... 499
subprocess............................................................... 503
time ....................................................................... 507
winreg .................................................................... 511

20. ............................................... 516


.............................................................. 516
Python ............................ 518
multiprocessing ....................................................... 519
threading ................................................................ 545
queue (Queue) .......................................................... 556
............................................... 559

21. .................................................... 561


............................... 561
asynchat ................................................................. 564
asyncore ................................................................. 568
select ..................................................................... 572
socket..................................................................... 586
ssl.......................................................................... 608
SocketServer ........................................................... 611

22. - ................................... 619


ftplib ..................................................................... 619
http ......................................................................... 623
smtplib ................................................................... 639
urllib........................................................................ 640
xmlrpc...................................................................... 651

23. - ................................................... 660


cgi ......................................................................... 662
cgitb ...................................................................... 670
WSGI................................................................ 671
wsgiref ..................................................................... 673

13

24. .............. 677


base64 .................................................................... 677
binascii ................................................................... 680
csv ......................................................................... 681
email ........................................................................ 685
hashlib ................................................................... 694
hmac ...................................................................... 695
HTMLParser ........................................................... 696
json ....................................................................... 699
mimetypes .............................................................. 703
quopri .................................................................... 704
xml .......................................................................... 706

25. .................................... 725


Python ............................................ 725
................................................................. 726
............ 727
.................................................................................. 727
...................... 728
........................................................ 728
.................................................... 728
.............................................................. 729

III. ............................................... 731


26. Python........... 733
........................................................... 734
Python ..................................... 754
ctypes..................................................................... 759

.................................................. 768
Jython IronPython ............................................................ 769

A. Python 3 ........................................................ 770


Python 3? ...................................... 770
................................................... 771
.............................................................. 780
2to3 ............................ 788

......................................................... 794


. (David M. Beazley) Python,
1996 .
, , SWIG, , C/C++, , Python, Perl, Ruby, Tcl Java. , PLY, lex1 yacc2 Python.
- Python
.
,
. ,
, ,
, Python . http://www.dabeaz.com.

. . .

. . .


(Noah Gift) Python For UNIX and Linux System
Administration ( OReilly).1 Google App Engine In Action ( Manning).
, , . , IBM developerWorks, Red
Hat Magazine, OReilly MacTech. http://
www.giftcs.com, ,
http://noahgift.com. , Twitter.
,
-, , .
Apple LPI.
, Caltech, Disney Feature Animation, Sony
Imageworks Turner Studios.
Weta Digital, .
, , .

. , . Python UNIX Linux.


. . .: -, 2009.


.
, ,
.
(Kurt Grandis) .
(Timothy Boronczyk), (Paul
DuBois), (Mats Wichmann), (David Ascher)
(Tim Bell) ,
. (Guido van Rossum), (Jeremy Hylton), (Fred Drake), (Roger
Masse) (Barry Warsaw) ,
1999 . ,
, . ,
, ,
. Addison-Wesley Pearson
Education . (Mark
Taber), (Michael Thurston), (Seth Kerney)
(Lisa Thibault) , . (Robin Drake),
. ,
, .


, , . , , , ,
.

,
, .
, , , -
, ,
.

, , .

, .
. :

feedback@developers-library.info

Mark Taber
Associate Publisher
Pearson Education
800 East 96th Street
Indianapolis, IN 46240 USA


-
informit.com/register, ,
.


Python.. Python , .
, Python Python.
, Python
, C Java.. , (,

)
.
Python http://www.python.org.
,
UNIX, Windows Macintosh. , - Python
, .
Python.
Python.. Python 2.6 Python 3.0. Python 3 .
: Python 3.0 Python 2.x, ?
, C,
. , - ,
, , .
,
. , Python 2, Python 3.
Python 3,
(,
). , ,
Python .

20

Python. ,
.1 Python
;
. , -
,
, , , , . , Python,
. ,
. , .
, , Python .
, . ,
. -, Python
.
,
, . ,
.
,
,
Python . Python,, , .
.
(David Beazley)
,
, 2009

. Python. . . . :
, 2000. . .

Python
1.

2.

3.

4.

5.

6.

7.

8.

9.

10.
11. , ,


Python. ,
Python,
. ,
, , , , , , , . .
. ,
.
Python Python 3, , Python 2.6.
, Python 3 ,
, . Python 3.


Python . python. (, Jython, IronPython, IDLE, ActivePython,
Wing IDE, pydev ),
. , , -. , ,
>>>, , :

24

1.
Python 2.6rc2 (r26rc2:66504, Sep 19 2008, 08:50:24)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type help, copyright, credits or license for more information.
>>> print ,
,
>>>


SyntaxError, , Python 3. - ,
, print Python 3 . ,
. :
>>> print(, )
,
>>>

Python 2, , .
Python.
, , , ,
Python 2 Python 3.

Python
.
. , , Python .
:
>>> 6000 + 4523.50 + 134.12
10657.620000000001
>>> _ + 8192.32
18849.940000000002
>>>

Python _, .
.
, .
,
, , :
# helloworld.py
print ,

25

Python .py. # , .
helloworld.py,
, :
% python helloworld.py
,
%

Python Windows,
.py
(Run command),
(Start). , . ,
( , ).
Python, IDLE.
UNIX
#!, :
#!/usr/bin/env python
print ,

,
. EOF (end of file )
Exit () Python. UNIX
EOF Ctrl+D; Windows Ctrl+Z. , SystemExit.
>>> raise SystemExit


1.1 ,
.
1.1.
principal = 1000
#
rate = 0.05
#
numyears = 5
#
year = 1
while year <= numyears:
principal = principal * (1 + rate)
print year, principal
# Python 3: print(year, principal)
year += 1

26

1.
1
2
3
4
5

1050.0
1102.5
1157.625
1215.50625
1276.2815625

Python ,

. . , ,

. Python C, ,
, ,
. Python
1.1 principal.
. :
principal = principal * (1 + rate)

principal. principal 1000,


( rate ,
).
principal
. , , ,
principal, ,
.
. , ,
:
principal = 1000; rate = 0.05; numyears = 5;

while ,
. ,
while. , , , .
, 1.1 , while.
Python ,
, .
( ).
, 1.1, .

27

,
principal
. . (%), :
print %3d %0.2f % (year, principal)
print(%3d %0.2f % (year, principal)) # Python 3

, :
1
2
3
4
5

1050.00
1102.50
1157.63
1215.51
1276.28


, %d, %s %f. ,
. , . , %3d
, 3 , %0.2f
, .
printf() C 4 .
,
format(). :
print format(year,3d),format(principal,0.2f)
print(format(year,3d),format(principal,0.2f)) # Python 3

format() ,
(%). , 3d , 3 , 0.2f
,
. , format(), . :
print {0:3d} {1:0.2f}.format(year,principal)
print({0:3d} {1:0.2f}.format(year,principal)) # Python 3

{0:3d} {1:0.2f}
format(),
.

28

1.


if else. :
if a < b:
print
else:
print

if else . else .
, , pass, :
if a < b:
pass #
else:
print


or, and not:
if product == and type == \
and not (age < 4 or age > 8):
print !


. , , (\) ,
. , , , .

Python ,
switch case. , elif, :
if suffix == .htm:
content = text/html
elif suffix == .jpg:
content = image/jpeg
elif suffix == .png:
content = image/png
else:
raise RuntimeError( )

True False
Boolean. :
if spam in s:
has_spam = True

29

else:
has_spam = False

, < >, True False. in, ,


, , . True False, , :
has_spam = spam in s

-
:
f = open(foo.txt)
line = f.readline()
while line:
print line,
# print(line,end=)
line = f.readline()
f.close()

#
# readline()
# ,
# Python 3

open() .
, . readline() ,
. .

foo.txt. , (, , , ), .
, Python
for, . , :
for line in open(foo.txt):
print line,

, print >> ,
:
f = open(out,w) #
while year <= numyears:
principal = principal * (1 + rate)
print >>f,%3d %0.2f % (year,principal)
year += 1
f.close()

>> Python 2.
Python 3 print , :
print(%3d %0.2f % (year,principal),file=f)

30

1.

, write(), . , print :
f.write(%3d %0.2f\n % (year,principal))

,
. , , sys.stdin.
, sys.stdout,
print. :
import sys
sys.stdout.write( :)
name = sys.stdin.readline()

Python 2 ,
:
name = raw_input( :)

Python 3 raw_input() input(),


.

, ,
, :
a = , !
b = Python
c =

, .
, ,
. ,
, :
print Content-type: text/html

<h1> Hello World </h1>


Click <a href=http://www.python.org>here</a>.

,
, . ,
s[i], :

31

a = ,
b = a[4] # b =

, s[i:j].
s k i
<= k < j. - , , :
c = a[:6] # c =
d = a[8:] # d =
e = a[3:9] # e = ,

(+):
g = a +

Python ,
(
, Perl PHP). , +
:
x = 37
y = 42
z = x + y # z = 3742 ( )

, ,
int() float(). :
z = int(x) + int(y) # z = 79 ( )


str(), repr() format(). :
s = x: + str(x)
s = x: + repr(x)
s = x: + format(x,4d)

str() repr() ,
.
str() , print, repr() ,
. :
>>> x = 3.4
>>> str(x)
3.4
>>> repr(x)
3.3999999999999999
>>>

3.4 Python.
, - .

32

1.


format(). :
>>> format(x,0.5f)
3.40000
>>>

. ,
:
names = [ Dave, Mark, Ann, Phil ]

,
, .
:
a = names[2]
# , Ann
names[0] = Jeff # Jeff

append():
names.append(Paula)

insert():
names.insert(2, Thomas)


:
b = names[0:2]
c = names[2:]
names[1] = Jeff
names[0:2] = [Dave,Mark,Jeff]

#
#
#
#
#

[Jeff, Mark]
[Thomas, Ann, Phil, Paula]
Jeff

.

(+) :
a = [1,2,3] + [4,5] # [1,2,3,4,5]

:
names = []
#
names = list() #

, ,
:
a = [1,Dave,3.14, [Mark, 7, 9, [100,101]], 10]

, :

33

a[1]
# Dave
a[3][2]
# 9
a[3][3][1] # 101

1.2
. ,
, .
1.2.
import sys
# sys
if len(sys.argv) != 2 : # :
print ,
raise SystemExit(1)
f = open(sys.argv[1]) # ,
lines = f.readlines() #
f.close()

#
fvalues = [float(line) for line in lines]

#
print : , min(fvalues)
print : , max(fvalues)

import sys Python.. .


open() , sys.argv. readlines() .
[float(line) for line in lines] , float() .
, , . for, ,
, :
fvalues = [float(line) for line in open(sys.argv[1])]

, , min()
max() .

, . ,
:
stock = (GOOG, 100, 490.10)

34

1.
address = (www.python.org, 80)
person = (first_name, last_name, phone)

Python , :
stock = GOOG, 100, 490.10
address = www.python.org,80
person = first_name, last_name, phone

, , 0 1 ,
:
a = ()
# ( )
b = (item,) # ( )
c = item, # ( )

, .
, :
name, shares, price = stock
host, port = address
first_name, last_name, phone = person

, (
, ),
(
, ).
, ,
,
.
, .
, ,
( ), , . , ,
, ,
.
, , . . .
. ,
, ,
:
# name,shares,price
filename = portfolio.csv
portfolio = []

35

for line in open(filename):


fields = line.split(,) #
name = fields[0]
#
shares = int(fields[1])
price = float(fields[2])
stock = (name,shares,price)
portfolio.append(stock)

# (name, shares, price)


#

split() . portfolio, , .
, :
>>> portfolio[0]
(GOOG, 100, 490.10)
>>> portfolio[1]
(MSFT, 50, 54.23)
>>>

:
>>> portfolio[1][1]
50
>>> portfolio[1][2]
54.23
>>>


:
total = 0.0
for name, shares, price in portfolio:
total += shares * price


. set(), , :
s = set([3,5,9,10]) #
t = set(Hello)
#

, . , .
, , , :
>>> t
set([H, e, l, o])

, l.

36

1.

,
, , .
:
a
b
c
d

=
=
=
=

t
t
t
t

|
&

s
s
s
s

#
#
#
#
#

t s
t s
(, t, s)
(, t s,
)

add() update()
:
t.add(x)
#
s.update([10,37,42]) # s

remove():
t.remove(H)

, ,
, . , ({}),
:
stock = {
name : GOOG,
shares : 100,
price : 490.10
}

:
name = stock[name]
value = stock[shares] * stock[price]


:
stock[shares] = 75
stock[date] = June 7, 2007

, ,
Python, . ,
, , .
,
, . ,
, ,
. ,
:

37


prices = {
GOOG
AAPL
IBM
MSFT
}

:
:
:
:

490.10,
123.50,
91.50,
52.13

:
prices = {}
#
prices = dict() #

in, :
if SCOX in prices:
p = prices[SCOX]
else:
p = 0.0


:
p = prices.get(SCOX,0.0)

,
:
syms = list(prices) # syms = [AAPL, MSFT, IBM, GOOG]

del:
del prices[MSFT]

, ,
Python. , , .


for, .
Python.. , , .
:
for n in [1,2,3,4,5,6,7,8,9]:
print 2 %d = %d % (n, 2**n)

n [1,2,3,4,,9].
,
:

38

1.
for n in range(1,10):
print 2 %d = %d % (n, 2**n)

range(i,j [,stride]) , i j-1. ,


. stride . :
a
b
c
d

=
=
=
=

range(5)
range(1,8)
range(0,14,3)
range(8,1,-1)

#
#
#
#

a
b
c
d

=
=
=
=

0,1,2,3,4
1,2,3,4,5,6,7
0,3,6,9,12
8,7,6,5,4,3,2

range() Python 2,
.
. Python xrange(). :
for i in xrange(100000000): # i = 0,1,2,...,99999999

xrange() ,
.
. Python 3 xrange() range(),
range() .
for
,
, , ,
. :
a = ,
# a
for c in a:
print c

b = [Dave,Mark,Ann,Phil]
#
for name in b:
print name

c = { GOOG : 490.10, IBM : 91.50, AAPL : 123.15 }


#
for key in c:
print key, c[key]

#
f = open(foo.txt)
for line in f:
print line,

for Python,
- , . -

39

, 6 .

def,
:
def remainder(a, b):
q = a // b
# // .
r = a - q*b
return r

,
, , : result = remainder(37, 15). ,
, :
def divide(a, b):
q = a // b
# a b , q
r = a - q*b
return (q,r)

, ,
:
quotient, remainder = divide(1456, 33)


:
def connect(hostname, port, timeout=300):
#

- ,
. - ,
. :
connect(www.python.org, 80)

, .
, . :
connect(port=80, hostname=www.python.org)

, .
, , . ,
global:

40

1.
count = 0
...
def foo():
global count
count += 1

# count

, yield,
. :
def countdown(n):
print !
while n > 0:
yield n # (n)
n -= 1

, yield, . - , next()
( __next__() Python 3). :
>>> c = countdown(5)
>>> c.next()
!
5
>>> c.next()
4
>>> c.next()
3
>>>

next() - ,
yield. next() , yield,
. next() , ,
yield. ,
.
, next() ,
. - for, :
>>> for i in countdown(5):
...
print i,
!
5 4 3 2 1
>>>

. , - tail -f, -

41

UNIX
:
# ( tail -f)
import time
def tail(f):
f.seek(0,2)
#
while True:
line = f.readline() #
if not line:
# ,
time.sleep(0.1) #
continue
#
yield line

,
:
def grep(lines, searchtext):
for line in lines:
if searchtext in line: yield line

, :
# tail -f | grep python
# Python
wwwlog = tail(open(access-log))
pylines = grep(wwwlog,python)
for line in pylines:
print line,

, ,
. , , for item in s,
s , , - , . s

.

. , ,
, .
,
yield, (yield), :
def print_matches(matchtext):
print , matchtext
while True:
line = (yield)
#

42

1.
if matchtext in line:
print line

, , (yield),
send(). :
>>> matcher = print_matches(python)
>>> matcher.next()
# (yield)
python
>>> matcher.send(Hello World)
>>> matcher.send(python is cool)
python is cool
>>> matcher.send(yow!)
>>> matcher.close()
# matcher
>>>

,
send(). ,
(yield)
. , (yield)
. , ,
close(), .
, -, ,
. . ,
:
#
matchers = [
print_matches(python),
print_matches(guido),
print_matches(jython)
]

# next()
for m in matchers: m.next()

# .
# : ,
# - .
wwwlog = tail(open(access-log))
for line in wwwlog:
for m in matchers:
m.send(line) #

6.

43


, , .
, . ,
, . :
items = [37, 42] #
items.append(73) # append()

dir()
.
:
>>> items = [37, 42]
>>> dir(items)
[__add__, __class__, __contains__, __delattr__, __delitem__,
...
append, count, extend, index, insert, pop,
remove, reverse, sort]
>>>

,
append() insert(). , ,
.
. , __add__() +:
>>> items.__add__([73,101])
[37, 42, 73, 101]
>>>

-
class. , , push(),
pop() length():
class Stack(object):
def __init__(self):
#
self.stack = [ ]
def push(self,object):
self.stack.append(object)
def pop(self):
return self.stack.pop()
def length(self):
return len(self.stack)

class Stack(object) , Stack . ,


; Stack
object, Python.. , def. -

44

1.

.
self. ,
, self. ,
, . , __init__() .
:
s = Stack()
s.push(Dave)
s.push(42)
s.push([3,4,5])
x = s.pop()
y = s.pop()
del s

#
#

# x [3,4,5]
# y 42
# s

, .
. list :
class Stack(list):
# push()
# : pop().
def push(self,object):
self.append(object)

, , ( ).
, , C++
Java:
class EventHandler(object):
@staticmethod
def dispatcherThread():
while (1):
#
...

EventHandler.dispatcherThread() #

@staticmethod . @staticmethod ,
6.

, , :
Traceback (most recent call last):
File foo.py, line 12, in <module>
IOError: [Errno 2] No such file or directory: file.txt

45

(:
( ):
foo.py, 12, <>
IOError: [Errno 2] : file.txt
)

,
.
. try except, :
try:
f = open(file.txt,r)
except IOError as e:
print e

IOError, e except. -
, (
). , except
.
,
except. , .
raise.
raise , :
raise RuntimeError( )

,
5 .
, , , ,
. with, .
, :
import threading
message_lock = threading.Lock()
...
with message_lock:
messages.add(newmessage)

with message_lock.
with, .
,
, with. , -

46

1.

,
.
with , ,
, . . 3
.

, .
Python
, . ,
, . ( ,
.py.) :
# : div.py
def divide(a, b):
q = a/b
# a b , q
r = a - q*b
return (q, r)


import:
import div
a, b = div.divide(2305, 29)

import , .py.
, , div.divide()
.
,
import as, :
import div as foo
a,b = foo.divide(2305,29)


, from:
from div import divide
a,b = divide(2305,29)

# div

,
:

47

from div import *

dir() , :
>>> import string
>>> dir(string)
[__builtins__, __doc__, __file__, __name__, _idmap,
_idmapL, _lower, _swapcase, _upper, atof, atof_error,
atoi, atoi_error, atol, atol_error, capitalize,
capwords, center, count, digits, expandtabs, find,
...
>>>


Python . -, help()
Python.. help()
help(_), . help() ,
.
Python , . ,
__doc__. :
>>> print issubclass.__doc__
issubclass(C, B) -> bool

Return whether class C is a subclass (i.e., a derived class) of class B.


When using a tuple as the second argument issubclass(X, (A, B, ...)),
is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).
(
, C ( ) B.
issubclass(X, (A, B, ...)),
issubclass(X, A) or issubclass(X, B)
or ... ( . .).
)
>>>

, Python pydoc,
Python.
pydoc topic .

,
Python.
, , , , , .


.
, (\), :
a = math.cos(3 * (x - n)) + \
math.sin(3 * (y - n))

, , .
, (...), [...],
{...} ,
,
.

, , , .
,
. :
if a:
1 #
2

49

else:
3
4 # ()

, ,
, , :
if a: 1
else: 2

pass.
:
if a:
pass
else:

,
. Python ( ) .
Python , , ,
8 (,
11,
16). Python -t,
, , . -tt,
TabError.
, (;).
, ,
, .
# , . , # ,
.
, ,
.
.


, , , , .
-
(_), .

50

2.

AZ
Z
Z az
z
z ISO-Latin.
-Latin.
Latin.. , FOO foo .
,
$, % @. , , if, else for .
:
and

del

from

nonlocal

try

as

elif

global

not

while

assert

else

if

or

with

break

except

import

pass

yield

class

exec

in

print

continue

finally

is

raise

def

for

lambda

return

, , . , , , _foo, from module import *. ,


, __init__,
. , , __bar, ,
7 - .
.


Python :

True False ,
1 0 . , 1234,
. , ,
0, 0x 0b (, 0644, 0x100fea8
0b11101010).
Python
, , ,
, , : 12345678901234567890.

51

1
, l (
L ) L, : 12345678901234567890L. L , Python
, , ,
. Python,
,
L. . , ,
( L).
, 123.34 1.2334e+02, . ,
j J, 12.34J,
. ,
,
: 1.2 + 12.34J.



, (), () ( ). , , ,
. , ( ,
, ). (
, ), hello world, , : helloworld.
(\)
, ,
, .
. 2.1.
.
2.1.

\\

, repr(). . .

52

2.

2.1 ()

\a

\b

\e

\0

\n

\v

\t

\r

\f

\OOO

( \000 \377)

\uxxxx

( \u0000 \uffff)

\Uxxxxxxxx

( \U00000000 \Uffffffff)

\N{ }

\xhh

( \x00 \xff)

\OOO \x
, (
, ,
, ).
, . ,
Jalapeo, ,
Jalape\xf1o, \xf1 .
Python 2 8-
.
.
Python 2
. ,
, u .
:
s = uJalape\u00f1o

Python 3 ( ), . Python 2
, -U ( , u ).

53

Python \u, \U \N . 2.1 .


, U+XXXX, XXXX . ( :
Python,, .) , U+00F1.
\u U+0000 U+FFFF (, \u00f1). \U
U+10000 (, \U00012345). \U
, U+10000
, .
3 .
, . ,
\N{ }. :
s = uJalape\N{LATIN SMALL LETTER N WITH TILDE}o


http://www.unicode.org/charts.
r
R, : r\d. , ,
,
.
, ,
- . , re, Windows (,
rc:\newdata\tests).

, r\.
\uXXXX -
, , \, . , ur\u1234 U+1234,
ur\\u1234 ,
,
u1234. , Python 2.2 r u, . Python 3.0 u .

,

54

2.

, UTF-8 UTF-16. ,
UTF-8, Jalape\xc3\xb1o, U+004A, U+0061, U+006C, U+0061, U+0070, U+0065,
U+00C3, U+00B1, U+006F, , , .
, UTF-8 \xc3\
xb1 U+00F1, U+00C3 U+00B1.
, b, : bJalape\xc3\xb1o.
, .
,
decode(). 3 4
.
, Python 2.6,
.
Python 3 bytes,
(. A, Python 3).

, [...], (...) {...}


, , ,
, :
a = [ 1, 3.4, hello ] #
b = ( 10, 20, 30 )
#
c = { a: 3, b: 42 } #

, ,
(\). , . :
a = [ 1,
3.4,
hello,
]

,

Python :
+
^
-=

~
*=

*
<
/=

**
>
//=

/
<=
%=

//
>=
**=

%
==
&=

<<
!=
|=

>>
<>
^=

&
+=
>>=

|
<<=

55

, ,
:
( ) [ ] { } , : . ` = ;

, (=) , (,)
, . (.) , (...) .
, :

$ ? , .


, , ,
, :
def fact(n):

if (n <= 1): return 1
else: return n * fact(n - 1)

.
__doc__ , :
>>> print fact._ _doc__

>>>

, . ,

. .

, , , ,
, .
@ , , .
:

56

2.
class Foo(object):
@staticmethod
def bar():
pass

,
. :
@foo
@bar
def spam():
pass

6
7
- .


Python 7- ASCII. ,
, , .
Python ,

, :
#!/usr/bin/env python
# -*- coding: UTF-8 -*

s = Jalapeo # UTF-8.

coding: , , .
Python,, , ASCII.


, Python,
.
, , , . . ,

.
Python
. 4 . 7
- .

, Python, . , ( )
. , a = 42,
42.
,
, a ,
.
, ,
, .
.
.
, .
, . , , , .

. , .

58

3.

, , , . (.), :
a = 3 + 4j
r = a.real

#
# ()

b = [1, 2, 3] #
b.append(7) # append
#


, ,
id().
, , Python,, .
is . type()
.
:
#
def compare(a,b):
if a is b:
# a b

if a == b:
# a b

if type(a) is type(b):
# a b

,
. . is. ,
. , list, dict file, . :
if type(s) is list:
s.append(item)

if type(d) is dict:
d.update(t)

,
isinstance(object, type). :
if isinstance(s,list):
s.append(item)

59

if isinstance(d,dict):
d.update(t)

isinstance() ,

Python.
,
, . -,
.
-, , . ,
isinstance(s,list) , ,
,
, , , list. ,
,
. 7.


.
,
,
, , :
a = 37
# 37
b = a
# 37
c = []
c.append(b) # 37

, 37.
a, , . a b, b
,
1. , b , .
, 37. .
, del
( ). :
del a
# 37
b = 42
# 37
c[0] = 2.0 # 37

sys.getrefcount(). :
>>> a = 37
>>> import sys

60

3.
>>> sys.getrefcount(a)
7
>>>

,
. ,
, ,
.
, .
, . :
a = { }
b = { }
a[b] = b
b[a] = a
del a
del b

# a b
# b a

del
a b , . , ( ). ,
,
.
, , .
gc (. 13,
Python ).


, a = b, b. ,
, b.
, ,
. :
>>> a = [1,2,3,4]
>>> b = a
# b a
>>> b is a
True
>>> b[2] = -100
# b
>>> a
# , a
[1, 2, -100, 4]
>>>

61

a b , , ,
. ,
.
, , : . ,
, .
:
>>> a = [ 1, 2, [3,4] ]
>>> b = list(a)
>>> b is a
False
>>> b.append(100)
>>> b
[1, 2, [3, 4], 100]
>>> a
[1, 2, [3, 4]]
>>> b[2] [0] = -100
>>> b
[1, 2, [-100, 4], 100]
>>> a
[1, 2, [-100, 4]]
>>>

# a.

# b.

# , a
# b

# , a

a b , ,
, . a b.
, . Python
, . copy.deepcopy(), :
>>>
>>>
>>>
>>>
>>>
[1,
>>>
[1,
>>>

import copy
a = [1, 2, [3, 4]]
b = copy.deepcopy(a)
b[2] [0] = -100
b
2, [-100, 4]]
a
# , a
2, [3, 4]]


Python . , , , . , , -

62

3.

, , . ,
, :
items = {
number : 42
text : Hello World
}

,
. :
items[func] = abs
import math
items[mod] = math
items[error] = ValueError
nums = [1,2,3,4]
items[append] = nums.append

# abs()
#
#
#

, ,
. , . :
>>> items[func](-45)
#
45
>>> items[mod].sqrt(4)
#
2.0
>>> try:
...
x = int(a lot)
... except items[error] as e: #
...
print(Couldnt convert)
...
Couldnt convert
>>> items[append](100)
#
>>> nums
[1, 2, 3, 4, 100]
>>>

abs(-45)
math.sqrt(4)

except ValueError as e

nums.append(100)

, Python , ,
. , , ,
GOOG,100,490.10, ,
. :
( ) :
>>> line = GOOG,100,490.10
>>> field_types = [str, int, float]
>>> raw_fields = line.split(,)
>>> fields = [ty(val) for ty,val in zip(field_types,raw_fields)]
>>> fields
[GOOG, 100, 490.10000000000002]
>>>

63


Python , .
,
. 3.1. ,
, isinstance().
Python 2, ( Python 3
).
3.1.

None

type(None)

None.

int

long
float


( Python 2)

complex

bool


(True False)

str

list

(
Python 2)

tuple

xrange

unicode

,
xrange() ( Python 3
range)

dict

set

frozenset

None
None (,
). Python , None. , .
None
, , . None False.

64

3.


Python : ,
, ,
. , . .
: True False. True
False 1 0 . 2147483648 2147483647 (
).

( ). , ,
Python (, Python 3 ). ,

,
, , . . Python 2 isinstance(x, int) False, x
.
Python (64 ). ,
IEEE 754, 17 ,
308 308. double C. Python 32-
.
, numpy (
: http://numpy.sourceforge.net).
. z
z.real z.imag. z.conjugate() z ( a+bj a-bj).
,
.
( fractions) numerator denominator.
real imag, conjugate().
, , as_integer_ratio(). is_integer()
,

65

. hex() fromhex() .
. decimal . fractions .
14 .


, . , . ,
.
; , .
.

,
. 3.2 , . i-
s s[i].

s[i:j] s[i:j:stride] (
4).
len(s). min(s)
max(s). , ,
( ). sum(s)
, .
.. 3.3 , , .
3.2. ,

s[i]

i- s

s[i:j]

s[i:j:stride]

len(s)

min(s)

max(s)

66

3.

3.2 ()

sum(s [,initial])

all(s)

, s, True

any(s)

, s, True

3.3. ,

s[i] = v

s[i:j] = t

s[i:j:stride] = t

del s[i]

del s[i:j]

del s[i:j:stride]

, , . 3.4.
list(s) .
s , , . s.append(x) x. s.index(x)
x. ValueError. s.remove(x)
x ValueError, . s.extend(t) s, t.
s.sort()

.
.
,
. , ,
. s.reverse()
. ,
sort() reverse(), None.

67

3.4.

list(s)

s .

s.append(x)

x s.

s.extend(t)

t s.

s.count(x)

x s.

s.index(x [,start [,stop]])

i,
s[i] == x. start stop

, .
s.insert(i,x)

x i.

s.pop([i])

i- .
i ,
.

s.remove(x)

s x .

s.reverse()

s
.

s.sort([key [, reverse]])

s. key ,
. reverse .
key reverse .

Python 2 . , 8- .
NULL.
, 16- . 65 536 .
1 ,
Python
. , (4-) , ;
. Python
32- . , Python
U+000000
U+110000. , , .
, . 3.5.
, -

68

3.

, . , s.capitalize(), s.center() s.expandtabs(),


s . ,
s.isalnum() s.isupper(), True False, . , False .
s.find(), s.index(), s.rfind() s.rindex()
. s. , , find()
-1, index() ValueError.
s.replace() .
, . re.
s.split() s.rsplit() , . s.partition() s.rpartition() - s :
, - .
start
end, , s.
, .
s.translate()
, ,
. , -- . 8-
256- .
s, s[n]
, n.
s.encode() s.decode()
,
: ascii, utf-8 utf-16. , - 9
. , Python 3 encode()
, decode() bytes.
s.format() .
. s {item}
. {0},
{1} . . {name},
name . :

69


>>> a = {0} {age}
>>> a.format(, age=40)
40
>>>

{item} . {item[n]}, n
, n- item. {item[key]}, key ,
item[key]. {item.attr} attr item. format() 4.
3.5.

s.capitalize()

s.center(width [, pad])

width.
pad ,
.

s.count(sub [,start [,end]])


sub.

s.decode([encoding [,errors]])


( ).

s.encode([encoding [,errors]])


( ).

s.endswith(suffix [,start [,end]])

, suffix.

s.expandtabs([tabsize])

s.find(sub [, start [,end]])

sub
-1.

s.format(*args, **kwargs)

s.

s.index(sub [, start [,end]])

sub
.

s.isalnum()

, - .

s.isalpha()

, .

s.isdigit()

, .

s.islower()

, .

s.isspace()

, .

70

3.

3.5 ()

s.istitle()

,
.

s.isupper()

, .

s.join(t)

, t, s .

s.ljust(width [, fill])

s
width.

s.lower()

s.lstrip([chrs])


,
chrs.

s.partition(sep)

-
sep. (head,sep,tail)
(s, ,), sep
s.

s.replace(old, new [,maxreplace])

old new.

s.rfind(sub [,start [,end]])

s.rindex(sub [,start [,end]])

s.rjust(width [, fill])

s
width.

s.rpartition(sep)

-
sep, .

s.rsplit([sep [,maxsplit]])

, ,
- sep.
maxsplit
. maxsplit , split().

s.rstrip([chrs])


, chrs.

s.split([sep [,maxsplit]])

-
sep. maxsplit .

s.splitlines([keepends])

.
keepnds 1,
.

71

s.startswith(prefix [,start [,end]])

, prefix.

s.strip([chrs])


,
chrs.

s.swapcase()


, .

s.title()

, .

s.translate(table [,deletechars])

table, ,
deletechars.

s.upper()

s.zfill(width)

width.

xrange()
xrange([i,]j [,stride]) , k, i <= k < j. i stride
0 1 . xrange
, , ,
. ,
. xrange .
, Python 3 xrange()
range(). , .

,
. , ,
. .
, ,
. (, , ). , -

72

3.

, ,
( ,
).

m[k], k .
, KeyError.
len(m) , . , , . 3.6.
3.6. ,

len(m)

m.

m[k]

m k.

m[k]=x

m[k] x.

del m[k]

m[k].

k in m

True, k m.

m.clear()

m.

m.copy()

m.

m.fromkeys(s [,value])

, s, value.

m.get(k [,v])

m[k], , v.

m.has_key(k)

True, m k, False. ( in.


Python 2.)

m.items()

(key, value).

m.keys()

m.pop(k [,default])

m[k], ,
; default,
,
KeyError.

m.popitem()

(key, value) .

m.setdefault(k [, v])

m[k], , v
m[k] = v.

m.update(b)

b m.

m.values()

m.

73

, . 3.6, . m.clear()
. m.update(b)
(key,value), b. m.get(k [,v])
v,
. m.setdefault(k [,v]) m.get(),
, v , m[k] = v. v ,
None. m.pop()
. m.popitem() .
m.copy() . m.fromkeys(s [,value]) , s.
, m. None,
value . fromkeys() , ,
, : dict.fromkeys().
m.items() , (key,
value). m.keys() ,
m.values() .
. Python 2
, Python 3 ,
. , Python.
,
. , items = list(m.
items()). ,
keys = list(m).

.
,
.
, . , ,
. : set , frozenset . :
s = set([1,5,10,15])
f = frozenset([a,37,hello])

, set() frozenset(), , , . , . 3.7.

74

3.

3.7. ,

len(s)

s.

s.copy()

s.

s.difference(t)

.
s, t.

s.intersection(t)

. ,
s t.

s.isdisjoint(t)

True, s t .

s.issubset(t)

True, s t.

s.issuperset(t)

True, s t.

s.symmetric_difference(t)

.
, s
t, .

s.union(t)

. ,
s t.

s.difference(t), s.intersection(t), s.symmetric_difference(t) s.union(t)


. , s (set frozenset).
t Python,
. , ,
. , 4.
( set) ,
. 3.8.
3.8. ,

s.add(item)

item s. ,
.

s.clear()

s.

s.difference_update(t)

s, t.

s.discard(item)

item s. , .

s.intersection_update(t)

s t s.

s.pop()


s.

75

s.remove(item)

item s. item ,
KeyError.

s.symmetric_difference_
update(t)

s t
s.

s.update(t)

t s. t
,
,
.

s. t , .



, Python , , . . 3.9 , .
3.9. Python

types.BuiltinFunctionType

type

object

types.FunctionType

types.MethodType

types.ModuleType

object

type

, . 3.9 object type , , , .


,
, . ,
, , , .

76

3.


,
def
lambda. :
def foo(x,y):
return x + y

bar = lambda x,y: x + y

f :

f.__doc__

f.__name__

f.__dict__

f.__code__

f.__defaults__

f.__globals__

f.__closure__

, ,

Python 2
, func_code, func_defaults . Python 2.6 Python 3.

, .
: , :
class Foo(object):
def instance_method(self,arg):

@classmethod
def class_method(cls,arg):

@staticmethod
def static_method(arg):

,
. , self. , . cls. ,

77

. , .

types.MethodType. , ,
, , (.). (.)
, .
, .
f.instance_method(arg) Foo :
f = Foo()
#
meth = f.instance_method # ()
meth(37)
#

meth . , ()
. (self). meth , , , () .
. :
umeth = Foo.instance_method # instance_method Foo
umeth(f,37)
# self

umeth .
, , ,
.
f Foo.
, TypeError. :
>>> umeth(hello,5)
Traceback (most recent call last):
File <stdin>, line 1, in <module>
TypeError: descriptor instance_method requires a Foo object but received a str
(:
( ):
<stdin>, 1, <>
TypeError: instance_method Foo,
str
)
>>>

types.MethodType, .
:

78

3.

m.__doc__

m.__name__

m.__class__

m.__func__

m.__self__

,
(None )

Python 3 , types.MethodType. Foo.instance_method, ,


, , .
, Python 3
self.


, C C++,
types.BuiltinFunctionType. :

b.__doc__

b.__name__

b.__self__

,
( )

, len(), __self__ None, ,


- . ,
x.append, x , __self__
x.


, . class
. __init__() . ,
__call__().
x, x(args) x.__call__(args).

79

,
,
type. :
>>> class Foo(object):
...
pass
...
>>> type(Foo)
<type type>

t type, :

t.__doc__

t.__name__

t.__bases__

t.__dict__

t.__module__

t.__abstractmethods__

(
,
)

, , . :
>>> f = Foo()
>>> type(f)
<class __main__.Foo>

i:

i.__class__

i.__dict__

__dict__ ,, . ,
i.attr = value, value . ,
__slots__, ,
__dict__.
Python 7.

80

3.

, , import. , import foo,


foo . , ,
__dict__. , ( ), . , m.x m.__dict__[x].
, : m.x = y, m.__dict__[x] = y.
:

m.__dict__

m.__doc__

m.__name__

m.__file__

m.__path__

. ,



, , . , , ,
, Ellipsis. . 3.10.
,

.
3.10.

types.CodeType

types.FrameType

types.GeneratorType

types.TracebackType

slice

Ellipsis

81


, , -,
compile(). ,
, , ,
, . c , :

c.co_name

c.co_argcount

(
)

c.co_nlocals

c.co_varnames

c.co_cellvars

c.co_freevars

.
,

c.co_code

c.co_consts

, -

c.co_names

, -

c.co_filename

c.co_firstlineno

c.co_lnotab

c.co_stacksize

(
)

c.co_flags

, . 2 (0x04) , (*args).
3 (0x08) ,
(**kwds).

82

3.

( ). f
, :

f.f_back

( )

f.f_code

f.f_locals

f.f_globals

f.f_builtins

f.f_lineno

f.f_lasti

. -
f_code

( ):

f.f_trace

f.f_exc_type

( Python 2)

f.f_exc_value

( Python 2)

f.f_exc_traceback

( Python 2)


.

sys.exc_info().
, :

t.tb_next

, ( , )

t.tb_frame

t.tb_lineno

t.tb_lasti

( ),

83


- ( 6 ). - , yield.
-.
:

g.gi_code

g.gi_frame

g.gi_running

,
-

g.next()

-,

yield,
( Python 3
__next__())

g.send(value)

value .

yield -.
- ,
yield. send() , yield

g.close()

, GeneratorExit -.
,

g.throw(exc [,exc_value [,exc_tb ]])


yield. exc , exc_value
exc_tb .
, ,
yield


, , : a[i:j:stride], a[i:j, n:m]

84

3.

a[..., i:j]. slice([i,]


j [,stride]). ,
:

s.start

. None, .

s.stop

. None, .

s.step

. None, .

s.indices(length).
(start,stop,stride)
. :
s = slice(10,20) # [10:20]
s.indices(100) # (10,20,1) > [10:20]
s.indices(15)
# (10,15,1) > [10:15]

Ellipsis
Ellipsis (ellipsis)
[].
, Ellipsis. ,
True. Ellipsis
Python,, , [] . , Ellipsis
:
class Example(object):
def __getitem__(self,index):
print(index)

e = Example()
e[3, ..., 4]

# e.__getitem__((3, Ellipsis, 4))


Python
, . , , , ,
, , s[n], len(s) .
. (__). . , x + y
x.__add__(y),
x[k] x.__getitem__(k).

85

,
.
, ,
, . , ( ) , .
,
.


,, .. 3.11, , . __new__() ,
. __init__() . __del__()
. ,
. ,
del x .
7.
3.11.

__new__(cls [,*args [,**kwargs]])

__init__(self [,*args [,**kwargs]])

__del__(self)

__new__() __init__() .
A(args), :
x = A.__new__(A,args)
is isinstance(x,A): x.__init__(args)


__new__() __del__(). __new__() ,
( , , ).
__del__() , , .

86

3.


. 3.12 ,
.
3.12.

__format__(self, format_spec)

__repr__(self)

__str__(self)

__repr__() __str__()
. __repr__() , eval(). ,
. repr().
repr() eval():
a = [2,3,4,5] #
s = repr(a) # s = [2, 3, 4, 5]
b = eval(s) #

- , ,
__repr__() <......>,
:
f = open(foo)
a = repr(f)
# a = <open file foo, mode r at dc030>

__str__() str(), , . __repr__()


, .
__repr__().
__format__() format()
format(). format_spec , . , format(),
, format_spec format(). :
format(x,spec)
# x.__format__(spec)
x is {0:spec}.format(x) # x.__format__(spec)

87

. , 4.


.. 3.13 , . __bool__()
True False.
, __len__(). __hash__() ,

. , . ,
; , .
3.13.

__bool__(self)

False True

__hash__(self)

(<, >, <=, >=, ==, !=). ,


Boolean, Python. , ,
, .
,
. , ,
. 3.14.
3.14. ,

__lt__(self,other)

self < other

__le__(self,other)

self <= other

__gt__(self,other)

self > other

__ge__(self,other)

self >= other

__eq__(self,other)

self == other

__ne__(self,other)

self != other

88

3.

, . 3.14. , == , __eq__().
, , min() max(), , , __lt__().


. 3.15 , , isinstance() b issubclass(). ,
7.
3.15.

__instancecheck__(cls,object)

isinstance(object, cls)

__subclasscheck__(cls, sub)

issubclass(sub, cls)


.. 3.16 , / (.)
del .
3.16.

__getattribute__(self,name)

self.name.

__getattr__(self, name)

self.name,
,
AttributeError.

__setattr__(self, name, value)

self.name = value. , .

__delattr__(self, name)

self.name.

__getattribute__() , . ,
. __getattr__(). __getattr__() AttributeError. __
setattr__(), __delattr__().

89


, , ,
. , , . 3.17. ,

.
3.17. -

__get__(self,instance,cls)

AttributeError

__set__(self,instance,value)

value

__delete__(self,instance)

__get__(), __set__() __delete__()


__getattribute__(),
__setattr__() __delattr__() .. , . ,
,
.
- , , , .
7.


. 3.18 , , .
3.18.

__len__(self)

self

__getitem__(self, key)

self[key]

__setitem__(self, key, value)

self[key] = value

__delitem__(self, key)

self[key]

__contains__(self, obj)

True, obj
self; False

90

3.

:
a = [1,2,3,4,5,6]
len(a)
x = a[2]
a[1] = 7
del a[2]
5 in a

#
#
#
#
#

a.__len__()
x = a.__getitem__(2)
a.__setitem__(1,7)
a.__delitem__(2)
a.__contains__(5)

__len__() len() . ,
__bool__() .
__getitem__(), ,
key.
Python,
. __setitem__() value . __delitem__() ,
del . __contains__()
in.
, __getitem__(), __setitem__() __delidelitem__() , x = s[i:j]. slice. , . :
a = [1,2,3,4,5,6]
x = a[1:5]
# x = a.__getitem__(slice(1,5,None))
a[1:3] = [10,11,12] # a.__setitem__(slice(1,3,None), [10,11,12])
del a[1:4]
# a.__delitem__(slice(1,4,None))

, Python,, ,
. , ,
, :
a = m[0:100:10]
b = m[1:10, 3:20]
c = m[0:100:10, 50:75:5]
m[0:5, 5:10] = n
del m[:10, 15:]

#
#
#
#
#

(=10)




i:j[:stride], stride () .
, . , ( ...) :

91

a = m[..., 10:20] #
m[10:20, ...] = n


, __getgetitem__(), __setitem__() __delitem__() . , Ellipsis. ,
a = m[0:10, 0:100:5, ...]

__getitem__():
a = m.__getitem__((slice(0,10,None), slice(0,100,5), Ellipsis))

, Python , 4. Python,
, .

obj ,
obj.__iter__(), . iter,
, iter.next() (
iter.__next__() Python 3), StopIteration . for , . , for x in s
:
_iter = s.__iter__()
while 1:
try:
x = _iter.next()
# _iter.__next__() Python 3
except StopIteration:
break
# , for
...


.. 3.19 , .
, , 4;
, x + y, x.__add__(y). , r, , . ,
. , x,
x + y, __add__(),
y.__radd__(x).

92

3.

3.19.

__add__(self,other)

self + other

__sub__(self,other)

self - other

__mul__(self,other)

self * other

__div__(self,other)

self / other ( Python 2)

__truediv__(self,other)

self / other (Python 3)

__floordiv__(self,other)

self // other

__mod__(self,other)

self % other

__divmod__(self,other)

divmod(self,other)

__pow__(self,other [,modulo])

self ** other, pow(self, other, modulo)

__lshift__(self,other)

self << other

__rshift__(self,other)

self >> other

__and__(self,other)

self & other

__or__(self,other)

self | other

__xor__(self,other)

self ^ other

__radd__(self,other)

other + self

__rsub__(self,other)

other - self

__rmul__(self,other)

other * self

__rdiv__(self,other)

other / self ( Python 2)

__rtruediv__(self,other)

other / self (Python 3)

__rfloordiv__(self,other)

other // self

__rmod__(self,other)

other % self

__rdivmod__(self,other)

divmod(other,self)

__rpow__(self,other)

other ** self

__rlshift__(self,other)

other << self

__rrshift__(self,other)

other >> self

__rand__(self,other)

other & self

__ror__(self,other)

other | self

__rxor__(self,other)

other ^ self

__iadd__(self,other)

self += other

__isub__(self,other)

self -= other

__imul__(self,other)

self *= other

__idiv__(self,other)

self /= other ( Python 2)

__itruediv__(self,other)

self /= other (Python 3)

__ifloordiv__(self,other)

self //= other

__imod__(self,other)

self %= other

__ipow__(self,other)

self **= other

__iand__(self,other)

self &= other

__ior__(self,other)

self |= other

__ixor__(self,other)

self ^= other

__ilshift__(self,other)

self <<= other

__irshift__(self,other)

self >>= other

__neg__(self)

self

__pos__(self)

+self

__abs__(self)

abs(self)

__invert__(self)

~self

__int__(self)

int(self)

__long__(self)

long(self) ( Python 2)

__float__(self)

float(self)

__complex__(self)

complex(self)

93

__iadd__(), __isub__() . . , ,
a+=b a-=b ( ). . ,
self ,
,
.
, __div__(), __truediv__()
__floordiv__(), (/) (//).
,
Python 2.2, Python 3 /,
. Python 2 / __div__().
. Python 3 /
__truediv__() .
Python 2 , from __future__ import division.
__int__(), __long__(), __float__() __complex__() , -

94

3.

.
, int() float(). . , 3 + x TypeError,
x __int__()
.


, __
call__(self [,*args [, **kwargs]]). x ,
, . x(arg1, arg2,
...) x.__call__(self, arg1, arg2, ...). ,
,
-. :
class DistanceFrom(object):
def __init__(self,origin):
self.origin = origin
def __call__(self, x):
return abs(x - self.origin)

nums = [1, 37, 42, 101, 13, 9, -20]


nums.sort(key=DistanceFrom(10))
# 10

DistanceFrom, .
, sort(), .


with
,
. :
with context [ as var]:

, context , . 3.20.
__enter__() with. ,
, ,
as var. __exit__() , , with. __exit__()
, , .
None.

95

3.20.

__enter__(self)


.
, as
with.

__exit__(self, type, value, tb)

, .
, type, value tb , .
with
, ,
.

,
.
5
.

dir()
dir() .
, dir(),
__dir__(self). ,
. ,
,
__dict__ .


,
Python.. , .


, :

x + y

x y

x * y

x / y

x // y

x ** y

(xy)

x % y

(x mod y)

-x

+x

(//, ,
) , . Python 2
(/) ,
. 7/4 1, 1.75. Python 3 ,

97

.
x // y. , 7 % 4 3.

x // y, x (x // y) * y.
(%) (//) .
,
:

x << y

x >> y

x & y

x | y

x ^ y

~x

,
, . ,
,
. , Python
, .
,
:

abs(x)

divmod(x,y)

(x // y, x % y)

pow(x,y [,modulo])

(x ** y) % modulo

round(x,[n])

10-n (
)

abs() . divmod()

, . pow()
**,
(
). round()
x , 10 n.
n , 0. x
, Python 2

98

4.

, (, 0.5
1.0, -0.5 -1.0). Python 3,
, (, 0.5 0.0,
1.5 2.0).
Python 3.
True, , False, :

x < y

x > y

x == y

x != y

x >= y

x <= y

, : w < x < y < z.


w < x and x < y and y < z. , x < y > z, , ,
( ,
x z ). , ,
TypeError.
, .
:
1. ,
.
2. ,
.
3. .
, , . , , .

99


, , , :

s + r

s * n, n * s

n s, n

v1,v2, vn = s

s[i]

s[i:j]

s[i:j:stride]

x in s, x not in s

for x in s:

all(s)

True, s
,

any(s)

True, s ,

len(s)

min(s)

max(s)

sum(s [, initial])

+ . s * n n . ,
. :
>>> a = [3,4,5]
>>> b = [a]
>>> c = 4*b
>>> c
[[3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5]]
>>> a[0] = -7
>>> c
[[-7, 4, 5], [-7, 4, 5], [-7, 4, 5], [-7, 4, 5]]
>>>

,
c. a b. b . , -

100

4.

a
. . ,
,
a. :
a = [ 3, 4, 5 ]
c = [list(a) for j in range(4)] # list()

,
copy .
. :
items = [ 3, 4, 5 ]
x,y,z = items
# x = 3, y = 4, z = 5

letters = abc
x,y,z = letters

# x = a, y = b, z = c

datetime = ((5, 19, 2008), (10, 30, am))


(month,day,year),(hour,minute,am_pm) = datetime

. , . ,
, 3 ,
. , , .
s[n] n- , s[0] . , , . , s[-1] .
, , IndexError.
s[i:j] ,
, k i <= k < j.
, i j, . / ,
. ,
. i / j
.

101

,
s[i:j:stride],
. . stride,
i j, s[i], s[i+stride], s[i+2*stride] ,
j (
). stride . i, , ,
.
j, , stride
, stride
. :
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

b
c
d
e
f
g
h
i
j

=
=
=
=
=
=
=
=
=

a[::2]
a[::-2]
a[0:5:2]
a[5:0:-2]
a[:5:1]
a[:5:-1]
a[5::1]
a[5::-1]
a[5:0:-1]

#
#
#
#
#
#
#
#
#

b
c
d
e
f
g
h
i
j

=
=
=
=
=
=
=
=
=

[0, 2, 4, 6, 8 ]
[9, 7, 5, 3, 1 ]
[0,2]
[5,3,1]
[0,1,2,3,4]
[9,8,7,6]
[5,6,7,8,9]
[5,4,3,2,1,0]
[5,4,3,2,1]

x in s x s
True False. x not in s x s.
in not in . , hello in hello
world True. , in .
, , re.
for x in s 5 . len(s) . min(s) max(s) ;
,
<
(,
). sum(s) s,
. .
. , sum(items, decimal.Decimal(0)) Decimal ( decimal 14 ).

102

4.

. :

s[i] = x

s[i:j] = r

s[i:j:stride] = r

del s[i]

del s[i:j]

del s[i:j:stride]

s[i] = x i, x x. ,
, , IndexError. s[i:j] = r k, i <= k < j,
r.
, , . s
, r. :
a = [1,2,3,4,5]
a[1] = 6
a[2:4] = [10,11]
a[3:4] = [-1,-2,-3]
a[2:] = [0]

#
#
#
#

a
a
a
a

=
=
=
=

[1,6,3,4,5]
[1,6,10,11,5]
[1,6,10,-1,-2,-3,5]
[1,6,0]

. ,
,
,
. :
a = [1,2,3,4,5]
a[1::2] = [10,11]
# a = [1,10,3,11,5]
a[1::2] = [30,40,50] # ValueError.

del s[i] i
. del s[i:j] , . , : del s[i:j:stride].
<, >, <=,
>=, == !=. . . ,

103

.
,
.
,
. a b, , a < b.
. , ( ASCII ).
, .
,
,
.
, ( unicodedata locale).
, .
Python :
. ,
, ,
.
(, +

== ). Python 3
TypeError, Python 2
. Python 2 .
, , .


(s % d)
, s , d
().
sprintf() C. :
( )
,
.
d , d. d
, ( , ).

104

4.

% , . 4.1.
4.1.

d, i

( ).

[-]m.dddddd.

[-]m.ddddddexx.

[-]m.ddddddExx.

g, G

%e %E,
-4 ; %f.

. str().

, repr().

%.

% :
1. , . , KeyError.
2. :

-, .
.

+,
( ).

0, .

3. , .
,
, (
, ) .
4. , .
5. , ; ,

105

, ,
.
,
(*).
, .
:
a
b
c
d
e
r
r
r
r
r
r
r

=
=
=
=
=
=
=
=
=
=
=
=

42
13.142783
hello
{x:13, y:1.54321, z:world}
5628398123741234
a is %d % a
# r = a is 42
%10d %f % (a,b)
# r = 42 13.142783
%+010d %E % (a,b)
# r = +000000042 1.314278E+01
%(x)-10d %(y)0.3g % d # r = 13 1.54
%0.4s %s % (c, d[z]) # r = hell world
%*.*f % (5,3,b)
# r = 13.143
e = %d % e
# r = e = 5628398123741234

%
,
, ( $var ). , , , :
stock = {
name : GOOG,
shares : 100,
price : 490.10 }

r = %(shares)d %(name)s %(price)0.2f % stock


# r = 100 GOOG 490.10

, . vars() , , .
name =
age = 41
r = %(name)s, %(age)s % vars()


s.format(*args, **kwargs)
. s.
{n}, n , format(). {name}

106

4.

. {, {{, }}
}. :
r
r
r
r

=
=
=
=

{0} {1} {2}.format(GOOG,100,490.10)


{name} {shares} {price}.format(name=GOOG,shares=100,price=490.10)
, {0}, {age} .format(,age=47)
{{ }} .format()

. , {name[n]}, n
, n- name, {name[key]}, key ,
name[key]. {name.attr} name.attr. :
stock = { name : GOOG,
shares : 100,
price : 490.10 }

r = {0[name]} {0[shares]} {0[price]}.format(stock)


x = 3 + 4j
r = {0.real} {0.imag}.format(x)

. , .
.
, (:), : {place:format_spec}. , . :
r = {name:8} {shares:8d} {price:8.2f}.format
(name=GOOG,shares=100,price=490.10)

[[fill[align]][sign][0][width]
[.precision][type], , [], . width , align <, > ^,
, . fill ,
. :
name = Elwood
r = {0:<10}.format(name) # r = Elwood
r = {0:>10}.format(name) # r = Elwood
r = {0:^10}.format(name) # r = Elwood
r = {0:=^10}.format(name) # r = ==Elwood==

type . . 4.2 . type ,

107

s, d
f.
4.2.

d, i

( ).

f, F

[-]m.dddddd.

[-]m.ddddddexx.

[-]m.ddddddExx.

g, G

e E,
-4 ; f.

, g, .

100
f, %.

.
str().

sign +, - . + , . - ,
. ,
.
precision . width 0,
.
:
x
r
r
r
r
y
r
r

=
=
=
=
=
=
=
=

42
{0:10d}.format(x)
{0:10x}.format(x)
{0:10b}.format(x)
{0:010b}.format(x)
3.1415926
{0:10.2f}.format(y)
{0:10.2e}.format(y)

#
#
#
#

r
r
r
r

=
=
=
=

42
2a
101010
0000101010

# r = 3.14
# r = 3.14e+00

108

4.
r = {0:+10.2f}.format(y) # r = +3.14
r = {0:+010.2f}.format(y) # r = +000003.14
r = {0:+10.2%}.format(y) # r = +314.16%

, format().
.
:
y = 3.1415926
r = {0:{width}.{precision}f}.format(y,width=10,precision=3)
r = {0:{1}.{2}f}.format(y,10,3)


. ,
.
, . format()
__format__(self, format_spec) .
format()
. , , .
, str() repr(),
, __format__().
!s
!r. :
name =
r = {0!r:^20}.format(name)

# r =


. :

x = d[k]

key

d[k] = x

key

del d[k]

key

k in d

key

len(d)

, , . ,

109

, , :
d = { }
d[1,2,3] = foo
d[1,0,3] = bar

.
, :
d[(1,2,3)] = foo
d[(1,0,3)] = bar


set frozenset :

s | t

s t

s & t

s t

s t

s t

s ^ t

s t

len(s)

max(s)

min(s)

, . , s frozenset,
frozenset, t
set.


Python
:

x += y

x = x + y

x -= y

x = x - y

x *= y

x = x * y

x /= y

x = x / y

x //= y

x = x // y

x **= y

x = x ** y

x %= y

x = x % y

110

4.

()

x &= y

x = x & y

x |= y

x = x | y

x ^= y

x = x ^ y

x >>= y

x = x >> y

x <<= y

x = x << y

,
. :
a = 3
b = [1,2]
c = Hello %s %s
a += 1
# a = 4
b[1] += 10
# b = [1, 12]
c %= (Monty, Python) # c = Hello Monty Python

. x += y x x + y. , 3
.

(.)
(.). :
foo.x = 3
print foo.y
a = foo.bar(3,4,5)


, : foo.y.a.b.
, : a = foo.bar(3,4,5).spam.
(.). 3 7
- .

()
f(args) f. . , .
.

111

, partial() functools. :
def foo(x,y,z):
return x + y + z

from functools import partial


f = partial(foo,1,2) # x y foo
f(3)
# foo(1,2,3), 6

partial() , ,
. f ,
. . (currying) , , ,
f(x,y), ,
. , x, f ,
, y,
.


.
, . , , .
, .

int(x [,base])

x . x , base
.

float(x)

x .

complex(real [,imag])

x .

str(x)

x .

repr(x)

x .

format(x [,format_spec])

x .

eval(str)

str
.

tuple(s)

s .

list(s)

s .

set(s)

s .

112

4.

()

dict(d)

. d (key, value).

frozenset(s)

s frozenset.

chr(x)

x .

unichr(x)

x
( Python 2).

ord(x)

hex(x)

x .

bin(x)

x
.

oct(x)

x .

, str() repr() . repr() , eval(), ,


str() ( print).
format(x, [format_spec]) , ,
x. format_spec,
, . ord() . .
chr() unichr() .
int(), float()
complex(). eval() , , . :
a
b
b
c

=
=
=
=

int(34)
long(0xfe76214, 16)
float(3.1415926)
eval(3, 5, 6)

#
#
#
#

a
b
b
c

=
=
=
=

34
266822164L (0xfe76214L)
3.1415926
(3,5,6)

, (list(), tuple(), set() ), , , .


and, or
not. :

x or y

x , y,
x.

x and y

x , x,
y.

not x

x , 1, 0.

113

, True , ,
, , . False 0,
None, , , . ,
,
. , a and b b , a . .


(x == y) x y.
, True , . True , x y
.
, ,
(==).
(x is y x is not y) ,
. , x == y, x is not y.
,
, , .
, , .


. 4.3 Python ( ). , (**),
.
, ,
, . ( , , , x * y, x / y, x // y x % y,
.)

114

4.

4.3. ( )

(...), [...], {...}

s[i], s[i:j]

s.attr

f(...)

+x, -x, ~x

x ** y

( )

x * y, x / y, x // y, x % y

, , ,

x + y, x - y

x << y, x >> y

x & y

x ^ y

x | y

x < y, x <= y,

, ,

x > y, x >= y,
x == y, x != y,
x is y, x is not y
x in s, x not in s
not x

x and y

x or y

lambda args: expr

, . 4.3,
x y. ,
, , .



, . :
if a <= b:
minvalue = a
else:
minvalue = b

115

,
. :
minvalue = a if a <=b else b

, . True, a, if.
b, else.
, (
).
-. :
values = [1, 100, 45, 23, 73, 37, 69 ]
clamped = [x if x < 50 else 50 for x in values]
print(clamped)
# [1, 50, 45, 23, 50, 37, 50]




. :
, , .


Python . , , , , , .
Python
. ,
:
if debug:
def square(x):
if not isinstance(x,float):
raise TypeError(Expected a float)
return x * x
else:
def square(x):
return x * x

, , . , ,
import.

117


if, else elif .
:
if :

elif :

elif :

...
else:

, else elif .
,
pass:
if :
pass
#
else:


Python for while.
:
while :

for i in s:

while ,
False. for s, . for , .
,
, , ,
.
, s , ,
for:
it = s.__iter__()
# s
while 1:
try:
i = it.next()
# (__next__ Python 3)
except StopIteration: #

118

5.
break
# i
...

i for i in s .
s. for.
for , . , , .
, , ,
, , :
for x,y,z in s:

s , . x, y z .
, s , s, , ,
.
, ,
, :
i = 0
for x in s:

i += 1

Python
enumerate(), :
for i,x in enumerate(s):

enumerate(s) , (0, s[0]), (1, s[1]), (2, s[2]) .


, , . , ,

, :
# s t
i = 0
while i < len(s) and i < len(t):
x = s[i]
# s
y = t[i]
# t

i += 1

119

, zip().
:
# s t
for x,y in zip(s,t):

zip(s,t) s t
(s[0],t[0]), (s[1],t[1]), (s[2], t[2]) , ,
. , Python 2
zip() s t
. ,
, .
itertools.izip(),
, zip(), , . Python 3
zip() .
break. , , :
for line in open(foo.txt):
stripped = line.strip()
if not stripped:
break
# ,
# stripped
...

(
) continue. , ,

. ,
:
for line in open(foo.txt):
stripped = line.strip()
if not stripped:
continue
#
# stripped
...

break continue
. , . Python goto.
else, :
# for-else
for line in open(foo.txt):

120

5.
stripped = line.strip()
if not stripped:
break
# stripped
...
else:
raise RuntimeError( )

else
, , .
break, else .
else ,
- ,
. , else, :
found_separator = False
for line in open(foo.txt):
stripped = line.strip()
if not stripped:
found_separator = True
break
# stripped
...

if not found_separator:
raise RuntimeError( )


. raise. raise : raise
Exception([value]), Exception , value
. :
raise RuntimeError( )

raise ,
(
).
try except,
:
try:
f = open(foo)
except IOError as e:

121

,
try except,
.
except. except ,
try-except. , try.
try-except, . ,
.
sys.excepthook(), 13 Python
.
as var except
, .
. , isinstance().
, Python except
except ExcType, var, (,). ( )
Python 2.6. as var,
Python 3.
except
, :
try:

except IOError as e:
# -
...
except TypeError as e:
#
...
except NameError as e:
#
...

, :
try:

except (IOError, TypeError, NameError) as e:
# -,
#
...

122

5.

pass, :
try:

except IOError:
pass
# ( ).

, , , , Exception, :
try:

except Exception as e:
error_log.write( : %s\n % e)

,
. , , . , ,
, .
except, , :
try:

except:
error_log.write(\n)

except , , . ,

, .
, try else,
except. ,
try . :
try:
f = open(foo, r)
except IOError as e:
error_log.write( foo: %s\n % e)
else:
data = f.read()
f.close()

finally , , try. :
f = open(foo,r)
try:
#

123

...
finally:
f.close()
# , ,

finally .
, ,
, . try , finally c
c
. ,
finally, , .


.. 5.1 , Python.
5.1.

BaseException

GeneratorExit

.close()

KeyboardInterrupt


( Ctrl-C)

SystemExit

Exception

StopIteration

StandardError

( Python 2). Python 3


,
Exception

ArithmeticError

FloatingPointError

ZeroDivisionError

AssertionError

assert

AttributeError

EnvironmentError

IOError

OSError

EOFError

124

5.

5.1 ()

ImportError

import

LookupError

IndexError

KeyError

MemoryError

NameError

UnboundLocalError

ReferenceError

RuntimeError

NotImplementedError

SyntaxError

IndentationError
TabError



( tt)

SystemError

TypeError

ValueError

UnicodeError

UnicodeDecodeError

UnicodeEncodeError

UnicodeTranslateError

, . 5.1. , ,
except. :
try:

except LookupError: # IndexError KeyError

125

try:

except Exception: #

, . , SystemExit
KeyboardInterrupt Exception, , , .


. , ,
Exception, :
class NetworkError(Exception): pass

, raise, :
raise NetworkError( .)

raise
.
, .
, :
class DeviceError(Exception):
def __init__(self,errno,msg):
self.args = (errno, msg)
self.errno = errno
self.errmsg = msg

# ( )
raise DeviceError(1, )

,
__init__(), , __init__(), self.args, .
.
, , .

. , NetworkError, ,
.
:
class HostnameError(NetworkError): pass
class TimeoutError(NetworkError): pass

126

5.
def error1():
raise HostnameError( )

def error2():
raise TimeoutError( )
try:
error1()
except NetworkError as e:
if type(e) is HostnameError:
# ,
...

except NetworkError , NetworkError.


, type().
sys.exc_info().

with
, , , , . , , , .
with
, ,
. :
with open(debuglog,a) as f:
f.write(\n)

f.write(\n)

import threading
lock = threading.Lock()
with lock:
#

with ,
, .
with , , , .
with obj obj ,
,
. with obj,
obj.__enter__(), ,
. ,
obj.__exit__(type,value,traceback).

with

127

, __exit__() None. ,
, .
__exit__() True False, , ( False, ).
with obj as
var. , obj.__enter__(), var. , var obj.
with , ( __enter__() __exit__()).
, .
:
class ListTransaction(object):
def __init__(self,thelist):
self.thelist = thelist
def __enter__(self):
self.workingcopy = list(self.thelist)
return self.workingcopy
def __exit__(self,type,value,tb):
if type is None:
self.thelist[:] = self.workingcopy
return False


. ,
. . :
items = [1,2,3]
with ListTransaction(items) as working:
working.append(4)
working.append(5)
print(items)
# [1,2,3,4,5]

try:
with ListTransaction(items) as working:
working.append(6)
working.append(7)
raise RuntimeError( !)
except RuntimeError:
pass
print(items) # [1,2,3,4,5]

contextlib -. :
from contextlib import contextmanager
@contextmanager

128

5.
def ListTransaction(thelist):
workingcopy = list(thelist)
yield workingcopy
# ,
thelist[:] = workingcopy

, yield, , __enter__().
__exit__() , yield. ,
-. , , , - .


__debug__
assert .
assert :
assert test [, msg]

test , True False.


test False, assert
AssertionError msg. :
def write_data(file,data):
assert file, write_data: !
...

assert , , , ( -O). ,
assert . assert ,
; ,
, , .
, write_data() , assert
if
.
assert Python __dedebug__, , True,
(
-O). , . -

__debug__

129

__debug__ ,
if . , ,
if __debug__, , if.
if __debug__
.
assert __debug__ ,
. ,
, .
.


. Python
, , . , , , , , , .
, -,
.

def:
def add(x,y):
return x + y

,
.
, , : a = add(3,4). .
,
TypeError.
. :
def split(line,delimiter=,):

, -

131

. , SyntaxError.

,
. :
a = 10
def foo(x=a):
return x

a = 5
foo()

# a.
# 10 ( )


:
def foo(x, items=[]):
items.append(x)
return items

foo(1) # [1]
foo(2) # [1, 2]
foo(3) # [1, 2, 3]

,
. ,
None
, :
def foo(x, items=None):
if items is None:
items = []
items.append(x)
return items

(*), :
def fprintf(file, fmt, *args):
file.write(fmt % args)

# fprintf.
# args (42,hello world, 3.45)
fprintf(out,%d %s %f, 42, hello world, 3.45)

args . args ,
,
*args, :
def printf(fmt, *args):
# args
fprintf(sys.stdout, fmt, *args)

132

6.

, ,
. . :
def foo(w,x,y,z):

#
foo(x=3, y=22, w=hello, z=[1,2])

. ,
. -
,
, TypeError. , Python , .
, :
,

. :
foo(hello, 3, z=[1,2], y=22)
foo(3, 22, w=hello, z=[1,2]) # TypeError. w

**,
(
) .
, , . :
def make_table(data, **parms):
# parms ()
fgcolor = parms.pop(fgcolor,black)
bgcolor = parms.pop(bgcolor,white)
width = parms.pop(width,None)
...
#
if parms:
raise TypeError( %s % list(parms))

make_table(items, fgcolor=black, bgcolor=white, border=1,


borderstyle=grooved, cellpadding=10,
width=400)


,
, , **, :

133

#
def spam(*args, **kwargs):
# args
# kwargs
...


, **kwargs:
def callfunc(*args, **kwargs):
func(*args,**kwargs)

*args **kwargs
. , callfunc()

func().



, , , . Python -
,
, . , ,
, . ( ), , .
:
a = [1, 2, 3, 4, 5]
def square(items):
for i,x in enumerate(items):
items[i] = x * x
#

square(a)

# : [1, 4, 9, 16, 25]


, ,
. ,
( , ).
, .
return .
return,
None. ,
:

134

6.
def factor(a):
d = 2
while (d <= (a / 2)):
if ((a / d) * d == a):
return ((a / d), d)
d = d + 1
return (a, 1)

,
:
x, y = factor(1243)

# x y.

(x, y) = factor(1243) # . .



. , , ,
.
,
. , .
,
.
,
. , NameError.

. :
a = 42
def foo():
a = 13
foo()
# a - 42

a -
42, foo a
.
,
; a , 13, ,
. global.
; ,
. . :

135

a = 42
b = 37
def foo():
global a # a
a = 13
b = 0
foo()
# a 13. b - 37.

Python . :
def countdown(start):
n = start
def display(): #
print(T-minus %d % n)
while n > 0:
display()
n -= 1


. , .
, , , .
, Python 2 , (
) ( global)
. ,
. ,
:
def countdown(start):
n = start
def display():
print(T-minus %d % n)
def decrement():
n -= 1 # Python 2
while n > 0:
display()
decrement()

Python 2 , , , . Python 3
n nonlocal, :
def countdown(start):
n = start
def display():
print(T-minus %d % n)
def decrement():

136

6.
nonlocal n # n ( Python 3)
n -= 1
while n > 0:
display()
decrement()

nonlocal
,
( ). ,
Perl, , nonlocal
, local Perl.
,
, UnboundLocalError.
, :
i = 0
def foo():
i = i + 1
print(i)

# UnboundLocalError

i (
global). i = i + 1 i ,
. i,
.
, . , ,
i i + 1
i; i print(i) i, .


Python . ,
,
.
, :
# foo.py
def callf(func):
return func()

, :
>>> import foo
>>> def helloworld():
...
return , !
...
>>> foo.callf(helloworld)
#

137

, !
>>>

, , ,
. foo.py,
:
# foo.py
x = 42
def callf(func):
return func()

:
>>> import foo
>>> x = 37
>>> def helloworld():
...
return , ! x = %d % x
...
>>> foo.callf(helloworld)
#
, ! x = 37
>>>

, helloworld()
x, ,
helloworld(). x
foo.py helloworld(), helloworld() x.
, , , , .
__globals__, , .
, .
__globals__ :
>>> helloworld.__globals__
{__builtins__: <module __builtin__ (built-in)>,
helloworld: <function helloworld at 0x7bb30>,
x: 37, __name__: __main__, __doc__: None
foo: <module foo from foo.py>}
>>>

,
, . :
import foo
def bar():
x = 13
def helloworld():
return , ! x = %d % x
foo.callf(helloworld) # , ! x = 13

138

6.

, , . :
from urllib import urlopen
# from urllib.request import urlopen (Python 3)
def page(url):
def get():
return urlopen(url).read()
return get

page() .
get(), -. , get(), ,
get(). :
>>> python = page(http://www.python.org)
>>> jython = page(http://www.jython.org)
>>> python
<function get at 0x95d5f0>
>>> jython
<function get at 0x9735f0>
>>> pydata = python() # http://www.python.org
>>> jydata = jython() # http://www.jython.org
>>>

, python jython, , get().


page(), , ,
get()
get().
get() urlopen(url) url,
page(). python jython, , . :
>>> python.__closure__
(<cell at 0x67f50: str object at 0x69230>,)
>>> python.__closure__[0].cell_contents
http://www.python.org
>>> jython.__closure__[0].cell_contents
http://www.jython.org
>>>

. , , :
def countdown(n):
def next():
nonlocal n
r = n
n -= 1
return r
return next

139

#
next = countdown(10)
while True:
v = next() #
if not v: break

n . next() . , , , :
class Countdown(object):
def __init__(self,n):
self.n = n
def next(self):
r = self.n
self.n -= 1
return r

#
c = Countdown(10)
while True:
v = c.next() #
if not v: break

, ,
, , (
50%).
, , , . .

, , .
. @ , :
@trace
def square(x):
return x*x

:
def square(x):
return x*x
square = trace(square)

140

6.

square().
trace(), square.
trace, , :
enable_tracing = True
if enable_tracing:
debug_log = open(debug.log,w)

def trace(func):
if enable_tracing:
def callf(*args,**kwargs):
debug_log.write( %s: %s, %s\n %
(func.__name__, args, kwargs))
r = func(*args,**kwargs)
debug_log.write(%s %s\n % (func.__name, r))
return r
return callf
else:
return func

trace() -,
. ,
square(), debug.log
write() -. callf, trace(), , .
,
enable_
tracing. False, trace()
, - . ,
, .
,
. , . :
@foo
@bar
@spam
def grok(x):
pass

.
, :
def grok(x):
pass
grok = foo(bar(spam(grok)))

, . :
@eventhandler(BUTTON)
def handle_button(msg):
...

yield

141

@eventhandler(RESET)
def handle_reset(msg):
...

:
def handle_button(msg):
...
temp = eventhandler(BUTTON)
#
handle_button = temp(handle_button) # ,

@
. , . :
#
event_handlers = { }
def eventhandler(event):
def register_function(f):
event_handlers[event] = f
return f
return register_function

. :
@foo
class Bar(object):
def __init__(self,x):
self.x = x
def spam(self):

-, ,
. , ,
, : Bar.spam. , -
foo() .

, , . .

yield
yield,
, . ,
. :
def countdown(n):
print( , %d % n)
while n > 0:
yield n
n -= 1
return

142

6.

, ,
. :
>>> c = countdown(10)
>>>

. -, , ,
next() ( __next__(), Python 3). :
>>> c.next()
# Python 3 c.__next__()
, 10
10
>>> c.next()
9

next() - ,
yield. yield
, ,
next(). , , yield.
, next() , for, sum()
. :
for n in countdown(10):

a = sum(countdown(10))

- , StopIteration.

, None.
- ,
, , . , :
for n in countdown(10):
if n == 2: break
statements

for break, . - close(), , . ,


close(). close(),
, :
>>> c = countdown(10)
>>> c.next()
, 10
10

yield

143

>>> c.next()
9
>>> c.close()
>>> c.next()
Traceback (most recent call last):
File <stdin>, line 1, in <module>
StopIteration
>>>

close() -, yield,
GeneratorExit. .
def countdown(n):
print( , %d % n)
try:
while n > 0:
yield n
n = n - 1
except GeneratorExit:
print( %d % n)

GeneratorExit, -

yield. , ,
, close()
.

yield
yield , . :
def receiver():
print( )
while True:
n = (yield)
print( %s % n)

, yield ,
.
. :
>>> r = receiver()
>>> r.next() # yield (r.__next__() Python 3)

>>> r.send(1)
1
>>> r.send(2)
2
>>> r.send()

>>>

144

6.

next() , , yield.
,
send() -
r. , send(), (yield)
.
, yield.
, next() . , .
def coroutine(func):
def start(*args,**kwargs):
g = func(*args,**kwargs)
g.next()
return g
return start

:
@coroutine
def receiver():
print( )
while True:
n = (yield)
print( %s % n)

#
r = receiver()
r.send(, !) # : .next()

,
. close(), :
>>> r.close()
>>> r.send(4)
Traceback (most recent call last):
File <stdin>, line 1, in <module>
StopIteration

,
StopIteration. close()
GeneratorExit ,
. :
def receiver():
print( )
try:
while True:
n = (yield)
print( %s % n)

yield

145

except GeneratorExit:
print( )

throw(exctype [, value [, tb]]), exctype , value tb . :


>>> r.throw(RuntimeError, !)
Traceback (most recent call last):
File <stdin>, line 1, in <module>
File <stdin>, line 4, in receiver
RuntimeError: !

, yield . .
throw(), ,
.
, yield, yield .
:
def line_splitter(delimiter=None):
print( )
result = None
while True:
line = (yield result)
result = line.split(delimiter)

, .
send() . :
>>> s = line_splitter(,)
>>> s.next()

>>> s.send(A,B,C)
[A, B, C ]
>>> s.send(100,200,300)
[100, 200, 300]
>>>

, , . next() (yield result), None


result. send() line , result. ,
send(), , yield.
, , send(),
yield, , ,
send(). -

146

6.

- , , throw().
throw(), ,
yield , throw().
, .


,
.
,
. ,
-
, ,
UNIX.. 1 . , -, , , :
import os
import fnmatch

def find_files(topdir, pattern):


for path, dirname, filelist in os.walk(topdir):
for name in filelist:
if fnmatch.fnmatch(name, pattern):
yield os.path.join(path,name)

import gzip, bz2


def opener(filenames):
for name in filenames:
if name.endswith(.gz): f = gzip.open(name)
elif name.endswith(.bz2): f = bz2.BZ2File(name)
else: f = open(name)
yield f

def cat(filelist):
for f in filelist:
for line in f:
yield line

def grep(pattern, lines):


for line in lines:
if pattern in line:
yield line


:
wwwlogs
files
lines
pylines

=
=
=
=

find(www,access-log*)
opener(wwwlogs)
cat(files)
grep(python, lines)

147

for line in pylines:


sys.stdout.write(line)

, ,
access-log*, , www. accesslog , , .
, python.
for .
.
, ,
, -
.
, . , ,
. , for -, .
, , :
import os
import fnmatch

@coroutine
def find_files(target):
while True:
topdir, pattern = (yield)
for path, dirname, filelist in os.walk(topdir):
for name in filelist:
if fnmatch.fnmatch(name,pattern):
target.send(os.path.join(path,name))

import gzip, bz2


@coroutine
def opener(target):
while True:
name = (yield)
if name.endswith(.gz): f = gzip.open(name)
elif name.endswith(.bz2): f = bz2.BZ2File(name)
else: f = open(name)
target.send(f)

@coroutine
def cat(target):
while True:
f = (yield)
for line in f:
target.send(line)

@coroutine
def grep(pattern, target):

148

6.
while True:
line = (yield)
if pattern in line:
target.send(line)

@coroutine
def printer():
while True:
line = (yield)
sys.stdout.write(line)

, ,
:
finder = find_files(opener(cat(grep(python,printer()))))

#
finder.send((www,access-log*))
finder.send((otherwww,access-log*))

,
target. ,

find_files(). , ,
. ,

close(). , , , send(), .
. ,

, .
, , ,
,

.
20 .


,
. :
nums = [1, 2, 3, 4, 5]
squares = []
for n in nums:
squares.append(n * n)

,
, .
:

149


nums = [1, 2, 3, 4, 5]
squares = [n * n for n in nums]

:
[expression for item1 in iterable1 if condition1
for item2 in iterable2 if condition2
...
for itemN in iterableN if conditionN ]

:
s = []
for item1 in iterable1:
if condition1:
for item2 in iterable2:
if condition2:
...
for itemN in iterableN:
if conditionN: s.append(expression)

:
a = [-3,5,2,-10,7,8]
b = abc

c = [2*s for s in a]
# c = [-6,10,4,-20,14,16]
d = [s for s in a if s >= 0] # d = [5,2,7,8]
e = [(x,y) for x in a
# e = [(5,a),(5,b),(5,c),
for y in b
#
(2,a),(2,b),(2,c),
if x > 0 ]
#
(7,a),(7,b),(7,c),
#
(8,a),(8,b),(8,c)]

f = [(1,2), (3,4), (5,6)]


g = [math.sqrt(x*x+y*y)
for x,y in f]

# f = [2.23606, 5.0, 7.81024]

, ,
,
for, .
.
if ;
,
.
, . , : [(x,y) for x in a for y
in b], : [x,y for x in a for y in b].
, , Python 2 , , . , x, [x for x in
a], , , ,

150

6.


. , Python 3, .

-
- , , , . - , ,
. :
(expression for item1 in iterable1 if condition1
for item2 in iterable2 if condition2
...
for itemN in iterableN if conditionN)

, - . , . :
>>> a = [1, 2, 3, 4]
>>> b = (10*i for i in a)
>>> b
<generator object at 0x590a8>
>>> b.next()
10
>>> b.next()
20
...

- , . ,
, -
, .
. :
#
f = open(data.txt)
#
lines = (t.strip() for t in f) #
#
comments = (t for t in lines if t[0] == #) #
for c in comments:
print(c)

-, , . , .
, for, -

151

.
. ,
.
Python .
, - , . , (, append()). - list():
clist = list(comments)


- , . () . , [x*x for x in a if x > 0]
{ x2 | x a, x > 0 }.
,
, , ,
. , , portportfolio.txt,
.txt,
txt,
, , , :
AA 100 32.20
IBM 50 91.10
CAT 150 83.44
MSFT 200 51.23
GE 95 40.37
MSFT 50 65.10
IBM 100 70.44

, ,
,
:
lines = open(portfolio.txt)
fields = (line.split() for line in lines)
print(sum(float(f[1]) * float(f[2]) for f in fields))

. ,
.
, , ,
, :
total = 0
for line in open(portfolio.txt):
fields = line.split()

152

6.
total += float(fields[1]) * float(fields[2])
print(total)

,
UNIX. , , -,
awk:
% awk { total += $2 * $3} END { print total } portfolio.txt
44671.2
%

, - select
SQL, . ,
, ,
:
fields = (line.split() for line in open(portfolio.txt))
portfolio = [ {name : f[0],
shares : int(f[1]),
price : float(f[2]) }
for f in fields]

#
msft = [s for s in portfolio if s[name] == MSFT]
large_holdings = [s for s in portfolio
if s[shares]*s[price] >= 10000]

,
( 17),
. :
sum(shares*cost for shares,cost in
cursor.execute(select shares, cost from portfolio)
if shares*cost >= 10000)

lambda
lambda , :
lambda args : expression

args , , expression , . :
a = lambda x,y : x+y
r = a(2,3)

# r 5

lambda . lambda , ,
for while. lambda-
, .

153

lambda- ,
. ,
:
names.sort(key=lambda n: n.lower())

. :
def factorial(n):
if n <= 1: return 1
else: return n * factorial(n - 1)

, .
sys.getrecursionlimit(),
sys.setrecursionlimit(). 1000. ,
- ,
. RuntimeError. Python ,
, Scheme.
, - . , :
def flatten(lists):
for s in lists:
if isinstance(s,list):
flatten(s)
else:
print(s)

items = [[1,2,3],[4,5,[5,6]],[7,8,9]]
flatten(items)
# 1 2 3 4 5 6 7 8 9

print yield, . , flatten()


, . :
def genflatten(lists):
for s in lists:
if isinstance(s,list):
for item in genflatten(s):
yield item
else:
yield item

154

6.


. , . :
@locked
def factorial(n):
if n <= 1: return 1
else: return n * factorial(n - 1) # factorial

-
, , .



,
. :
def factorial(n):
n. :

>>> factorial(6)
120
>>>

if n <= 1: return 1
else: return n*factorial(n-1)

__doc__ , .
, . :
def wrap(func):
call(*args,**kwargs):
return func(*args,**kwargs)
return call
@wrap
def factorial(n):
n.
...


factorial(), :
>>> help(factorial)
Help on function call in module __main__:
( call __main__:)

155

call(*args, **kwargs)
(END)
>>>

,
. :
def wrap(func):
call(*args,**kwargs):
return func(*args,**kwargs)
call.__doc__ = func.__doc__
call.__name__ = func.__name__
return call

functools wraps, . , :
from functools import wraps
def wrap(func):
@wraps(func)
call(*args,**kwargs):
return func(*args,**kwargs)
return call

@wraps(func), functools, func .


.
:
def foo():

foo.secure = 1
foo.private = 1

,
__dict__ .

, ,

.
, .
, .
, ,
.

156

6.

, functools.wraps(), :
def wrap(func):
call(*args,**kwargs):
return func(*args,**kwargs)
call.__doc__ = func.__doc__
call.__name__ = func.__name__
call.__dict__.update(func.__dict__)
return call

eval(), exec() compile()


eval(str [,globals [,locals]]) str
. :
a = eval(3*math.sin(3.5+x) + 7.2)

exec(str [, globals [, locals]]) str,


Python.. ,
exec(). :
a = [3, 5, 10, 13]
exec(for i in a: print(i))

, Python 2 exec() .
exec, , : exec
for i in a: print i. - Python 2.6, Python 3. exec().
(
, ). , eval()
exec() , . :
globals = {x: 7,
y: 10,
birds: [Parrot, Swallow, Albatross]
}
locals = { }

# , , ,
#
a = eval(3 * x + 4 * y, globals, locals)
exec(for b in birds: print(b), globals, locals)

, . , - -

eval(), exec() compile()

157

, ,
exec() SyntaxError,
lambda-.
exec() eval()
-. ,
, ,
.
compile(str,filename,kind) -, str
, , filename
, (
). kind : single ,
exec eval .
, compile(), eval() exec(). :
s = for i in range(0,10): print(i)
c = compile(s,,exec)
#
exec(c)
#

s2 = 3 * x + 4 * y
c2 = compile(s2, , eval) #
result = eval(c2)
#

. ,
- . , , C Java. (
3
.)

class
, , , .
(, ), (, ) (,
).
class.
, . :
class Account(object):
num_accounts = 0
def __init__(self,name,balance):
self.name = name
self.balance = balance
Account.num_accounts += 1
def __del__(self):
Account.num_accounts -= 1
def deposit(self,amt):
self.balance = self.balance + amt

159

def withdraw(self,amt):
self.balance = self.balance - amt
def inquiry(self):
return self.balance

, , , ,
. , ,
Account:
Account.num_accounts
Account.__init__
Account.__del__
Account.deposit
Account.withdraw
Account.inquiry

, class (, Accounts). , , .
.
, , . , ,
. self,
. deposit(), withdraw() inquiry(),
, .
, num_accounts, , ( ). Account.


, . , __init__() . ,
__init__(), : self , . :
#
a = Account(, 1000.00) # Account.__init__(a,,1000.00)
b = Account(, 10.00)

__init__() self. , self.name


= name name .
, , , (.),
:

160

7. -
a.deposit(100.00) # Account.deposit(a,100.00)
b.withdraw(50.00) # Account.withdraw(b,50.00)
name = a.name
#

(.) . . ,
a.name name
a. a.deposit deposit
() Account. , ,
.
.


,
, .
. , self.
self.balance, balance.
, :
class Foo(object):
def bar(self):
print(bar!)
def spam(self):
bar(self) # ! bar NameError
self.bar()
#
Foo.bar(self) #


, Python C++ Java. ,
, ,
self Python , this. self , Python
, , , int x float y C. ,
,
.
self ,
self, ,
.

, .
. -

161

.
, .
.
class . object,
. object , Python , __str__(), print.
. , Account, inquiry(),
,
, ,
:
import random
class EvilAccount(Account):
def inquiry(self):
if random.randint(0,4) == 1:
return self.balance * 1.10 # :
else:
return self.balance

c = EvilAccount(, 1000.00)
c.deposit(10.0)
# Account.deposit(c,10.0)
available = c.inquiry() # EvilAccount.inquiry(c)

EvilAccount Account, inquiry().


Python
(.). ,
,
. , . ,
c.deposit() deposit(), Account.
, __init__(). ,
EvilAccount evilfactor:
class EvilAccount(Account):
def __init__(self,name,balance,evilfactor):
Account.__init__(self,name,balance) #
# Account
self.evilfactor = evilfactor
def inquiry(self):
if random.randint(0,4) == 1:

162

7. -
return self.balance * self.evilfactor
else:
return self.balance

__init__(),
__init__() .
, __init__().
, Account.__init__().
__init__(),
. , __init__(), ,
, .
,
.
, self
, :
class MoreEvilAccount(EvilAccount):
def deposit(self,amount):
self.withdraw(5.00)
#
EvilAccount.deposit(self,amount) #

, EvilAccount deposit().
Account. , , , (, EvilAccount deposit()). ,
super(), :
class MoreEvilAccount(EvilAccount):
def deposit(self,amount):
self.withdraw(5.00)
#
super(MoreEvilAccount,self).deposit(amount)#

super(cls, instance) , .
, . (
, ,
). ,
super() . Python 3 super().deposit(amount), , . Python 2
.
Python . . :

163

class DepositCharge(object):
fee = 5.00
def deposit_fee(self):
self.withdraw(self.fee)

class WithdrawCharge(object):
fee = 2.50
def withdraw_fee(self):
self.withdraw(self.fee)

# ,
class MostEvilAccount(EvilAccount, DepositCharge, WithdrawCharge):
def deposit(self,amt):
self.deposit_fee()
super(MostEvilAccount,self).deposit(amt)
def withdraw(self,amt):
self.withdraw_fee()
super(MostEvilAcount,self).withdraw(amt)

, . :
d = MostEvilAccount(Dave,500.00,1.10)
d.deposit_fee() # DepositCharge.deposit_fee(). fee == 5.00
d.withdraw_fee() # WithdrawCharge.withdraw_fee(). fee == 5.00 ??

deposit_fee() withdraw_fee()
. , withdraw_fee() ,
fee, .
, fee ,
. , ? (: DepositCharge.fee.)
,
, . ,
, ,
.
EvilAccount ,
Account, Account. MostEvilAccount, DepositCharge , WithdrawCharge,
. ,
__mro__ . :
>>> MostEvilAccount.__mro__
(<class __main__.MostEvilAccount>,
<class __main__.EvilAccount>,
<class __main__.Account>,
<class __main__.DepositCharge>,
<class __main__.WithdrawCharge>,

164

7. -
<type object>)
>>>


. , ,
, .
-
, - -. C3-, ( A
A Monotonic Superclass Linearization for Dylan
Dylan) ( . (K. Barrett)
, OOPSLA96).
96). ,
Python
TypeError. :
class X(object): pass
class Y(X): pass
class Z(X,Y): pass # TypeError.
#
#

Z,
. , X
Y, . Y ,
X. X,
Y, X.
, ,
, .
, .
-. , , ,
, ( ). , , - . DepositCharge
WithdrawCharge, . , deposit_fee(), ,
. , ,
DepositCharge. ( ,
).

165

: fee, deposit_fee() withdraw_


fee() , ,
self (, DepositChange.fee).

,

(, ,
)
. , . , , obj.attr, attr , , .
, .

, obj. obj.name obj, name. ( : ,
, , , ).
Python,
. , , ,
, .
. , ,
, ,
.
, , . , .


, , , , self. ,
.
, , . .
@staticmethod, :

166

7. -
class Foo(object):
@staticmethod
def add(x,y):
return x + y

,
. - . :
x = Foo.add(3,4) # x = 7


.
__init__(), , :
class Date(object):
def __init__(self,year,month,day):
self.year = year
self.month = month
self.day = day
@staticmethod
def now():
t = time.localtime()
return Date(t.tm_year, t.tm_mon, t.tm_day)
@staticmethod
def tomorrow():
t = time.localtime(time.time()+86400)
return Date(t.tm_year, t.tm_mon, t.tm_day)

#
a
b
c


= Date(1967, 4, 9)
= Date.now()
# now()
= Date.tomorrow() # tomorrow()

, . @classmethod. , ,
cls, . :
class Times(object):
factor = 1
@classmethod
def mul(cls,x):
return cls.factor*x

class TwoTimes(Times):
factor = 2

x = TwoTimes.mul(4) # Times.mul(TwoTimes, 4) -> 8

, TwoTimes mul() . ,
, , .

167

, , , Date,
, :
class EuroDate(Date):
# ,
#
def __str__(self):
return %02d/%02d/%4d % (self.day, self.month, self.year)

Date, . now() tomorrow() . , EuroDate.now(),


EuroDate Date. , :
class Date(object):
...
@classmethod
def now(cls):
t = time.localtime()
#
return cls(t.tm_year, t.tm_month, t.tm_day)

class EuroDate(Date):
...

a = Date.now()
# Date.now(Date) Date
b = EuroDate.now() # Date.now(EuroDate) EuroDate

,
,
. . :
a = Date(1967,4,9)
b = a.now()
# Date.now(Date)

, a.now()
a.
Python,
- , Smalltalk Ruby.
.


, . , . :
class Circle(object):
def __init__(self,radius):
self.radius = radius
# Circles

168

7. -
@property
def area(self):
return math.pi*self.radius**2
@property
def perimeter(self):
return 2*math.pi*self.radius

Circle :
>>> c = Circle(4.0)
>>> c.radius
4.0
>>> c.area
50.26548245743669
>>> c.perimeter
25.132741228718345
>>> c.area = 2
Traceback (most recent call last):
File <stdin>, line 1, in <module>
AttributeError: cant set attribute
(:
( ):
<stdin>, 1, <module>
AttributeError:
)
>>>

Circle c.radius, , c.area c.perimeter, . @property ,


, , (),
, . , , , ,
( AttributeError ).
(Uniform Access Principle).
, , .
, ,
c.radius, , c.area(). , (), , .
.
Python , , . :
class Foo(object):
def __init__(self,name):

169

self.name = name
def spam(self,x):
print(%s, %s % (self.name, x)

, : f = Foo
(), f.spam,
spam, , , ,
,
(). , self
, ().
, .
@staticmethod @classmethod
, , .
, @staticmethod
, , .

. . :
class Foo(object):
def __init__(self,name):
self.__name = name
@property
def name(self):
return self.__name
@name.setter
def name(self,value):
if not isinstance(value,str):
raise TypeError( !)
self.__name = value
@name.deleter
def name(self):
raise TypeError( name)

f = Foo()
n = f.name
f.name =
f.name = 45
del f.name

#
#
#
#

f.name()
name(f,)
name(f,45) -> TypeError
name(f) -> TypeError

@property name , . @name.setter @name.deleter



name.
. , name __name. - , , .

170

7. -

,
property(getf=None, setf=None, delf=None,
doc=None), , . :
class Foo(object):
def getname(self):
return self.__name
def setname(self,value):
if not isinstance(value,str):
raise TypeError( !)
self.__name = value
def delname(self):
raise TypeError( name)
name = property(getname,setname,delname)

- ,
. , get, set delete
.

get, set delete. ,


. ,, .
__get__(), __set__() __delete__()
. :
class TypedProperty(object):
def __init__(self,name,type,default=None):
self.name = _ + name
self.type = type
self.default = default if default else type()
def __get__(self,instance,cls):
return getattr(instance,self.name,self.default)
def __set__(self,instance,value):
if not isinstance(value,self.type):
raise TypeError( %s % self.type)
setattr(instance,self.name,value)
def __delete__(self,instance):
raise AttributeError( )

class Foo(object):
name = TypedProperty(name,str)
num = TypedProperty(num,int,42)

TypedProperty ,
. :

171

f = Foo()
a = f.name
# Foo.name.__get__(f,Foo)
f.name = # Foo.name.__set__(f,Guido)
del f.name
# Foo.name.__delete__(f)

.
, __init__() . ,
, ,
.

name ,
.
, , .


.
, - .
,
. - ,

,
.
, , , __Foo,
_Classname__Foo. ,

. :
class A(object):
def __init__(self):
self.__X = 3
# self._A__X
def __spam(self):
# _A__spam()
pass
def bar(self):
self.__spam()
# A.__spam()

class B(A):
def __init__(self):
A.__init__(self)
self.__X = 37
# self._B__X
def __spam(self):
# _B__spam()
pass

, ,
. ,

172

7. -

, , . , __dir__(),
, dir(), .
, ,
, ,
, .
. , ,
, getattr(), hasattr(),
setattr() delattr(), .
_Classname__name.

.
, (, ,
, ). .


. , A.bar(), ,
A.__spam(), self
__spam() .
, .
(, _name).
import *.
,
,
.


, ,
. :
class Circle(object):
def __init__(self,radius):
self.radius = radius

# Circle
c = Circle(4.0)
d = Circle(5.0)

, __new__(), , __init__(),

173

. , c = Circle(4.0)
:
c = Circle.__new__(Circle, 4.0)
if isinstance(c,Circle):
Circle.__init__(c,4.0)

__new__() .
, __new__(cls,
*args, **kwargs), args kwargs , __init__(). __new__() ,
. __new__
() , __init__() .
__new__() , . -,
,
. , , , ,
__new__() ,
, ,
( __init__() ). :
class Upperstr(str):
def __new__(cls,value=):
return str.__new__(cls, value.upper())

u = Upperstr(hello)

# HELLO

, __new__
(), . .
. ,
. , __del__() .
__del__() .
,
,
.
__del__(), ,
.
, close(), .
del. ,
__del__(). del
__del__().

174

7. -

,
, ,
__del__(), (
__del__() ). , ,
(,
C++), __del__(). , __del__() ,
, , , - . , , ,
.
class Account(object):
def __init__(self,name,balance):
self.name = name
self.balance = balance
self.observers = set()
def __del__(self):
for ob in self.observers:
ob.close()
del self.observers
def register(self,observer):
self.observers.add(observer)
def unregister(self,observer):
self.observers.remove(observer)
def notify(self):
for ob in self.observers:
ob.update()
def withdraw(self,amt):
self.balance -= amt
self.notify()

class AccountObserver(object):
def __init__(self, theaccount):
self.theaccount = theaccount
theaccount.register(self)
def __del__(self):
self.theaccount.unregister(self)
del self.theaccount
def update(self):
print(: %0.2f % self.theaccount.balance)
def close(self):
print( )

#
a = Account(,1000.00)
a_ob = AccountObserver(a)

175

Account,
AccountObserver Account, . Account , AccountObserver . __del__()

( ). . ,
, . ,
( gc) , .
, , , weakref .
, .

, .
:
import weakref
class AccountObserver(object):
def __init__(self, theaccount):
self.accountref = weakref.ref(theaccount) #
theaccount.register(self)
def __del__(self):
acc = self.accountref()
#
if acc:
# ,
acc.unregister(self)
def update(self):
print(: %0.2f % self.accountref().balance)
def close(self):
print( )

#
a = Account(,1000.00)
a_ob = AccountObserver(a)

accountref.
Account, , , . Account, None,
. . Account , __del__, . , gc.
weakref 13
Python .

176

7. -



, __dict__. . :
>>> a = Account(, 1100.0)
>>> a.__dict__
{balance: 1100.0, name: }

, :
a.number = 123456

# a.__dict__ number


__dict__. , __dict__,
.

__class__. , __dict__.
, . :
>>> a.__class__
<class __main__.Account>
>>> Account.__dict__.keys()
[__dict__, __module__, inquiry, deposit, withdraw,
__del__, num_accounts, __weakref__, __doc__, __init__]
>>>

,
__bases__, .
,
.
, obj.name = value
, obj.__setattr__
(name, value). c del obj.
name, obj.__delattr__(name).
__dict__ obj,
.
, , .
, obj.name,
obj.__getattribute__(name).
, ,
, __dict__, . ,
__getattr__() (

__slots__

177

). , AttributeError.

, . :
class Circle(object):
def __init__(self,radius):
self.radius = radius
def __getattr__(self,name):
if name == area:
return math.pi*self.radius**2
elif name == perimeter:
return 2*math.pi*self.radius
else:
return object.__getattr__(self,name)
def __setattr__(self,name,value):
if name in [area,perimeter]:
raise TypeError(%s is readonly % name)
object.__setattr__(self,name,value)

, ,
, , . ,
, .
, . ,
, .
__getattr__(), __setattr__() __delattr__(),
.

__slots__

, __slots__. :
class Account(object):
__slots__ = (name,balance)
...

__slots__,
.
AttributeError.

,
.
__slots__ . .
, __slots__,

178

7. -

. , .
__slots__ , ,

.
, __slots__ -
. __slots__,
__slots__ ( ),
,
. ,
, , __slots__ !
, __slots__ , ,
__dict__. , __dict__
, .
, __slots__ , __getattribute__(), __getattr__() __setattr__(). __slots__. , , __slots__,
, .


Python,
, 3. , Python
, , __add__(),
.
, , .

Python,
.

class Complex(object):
def __init__(self,real,imag=0):
self.real = float(real)
self.imag = float(imag)
def __repr__(self):

179

return Complex(%s,%s) % (self.real, self.imag)


def __str__(self):
return (%g+%gj) % (self.real, self.imag)
# self + other
def __add__(self,other):
return Complex(self.real + other.real, self.imag + other.imag)
# self - other
def __sub__(self,other):
return Complex(self.real - other.real, self.imag - other.imag)

__repr__() ,
eval()
( , Complex(real,imag)).
. __str__(), , ,
( print).
, __add__() __sub__(),
. . ,
__add__() __sub__(), , . ,
, ,
Complex. :
>>> c = Complex(2,3)
>>> c + 4.0
Complex(6.0,3.0)
>>> 4.0 + c
Traceback (most recent call last):
File <stdin>, line 1, in <module>
TypeError: unsupported operand type(s) for +: int and Complex
(:
( ):
<stdin>, 1, <module>
TypeError: +: int Complex
)
>>>

c + 4.0 .
Python .real .imag, .
, .
Complex
, , (
, ).
4.0 + c , Complex.
, Complex , :

180

7. -
class Complex(object):
...
def __radd__(self,other):
return Complex(other.real + self.real, other.imag + self.imag)
def __rsub__(self,other):
return Complex(other.real - self.real, other.imag - self.img)
...

. 4.0 + c , TypeError,
c.__radd__(4.0).
Python , . , Python ,
__coerce__(). Python 2.6 Python 3. , , __int__(), __float__() __complex__().
, int(x)
float(x),
. , ,
.


,
.
isinstance(obj,cname). True,
obj cname , cname. :
class A(object): pass
class B(A): pass
class C(object): pass

a = A()
b = B()
c = C()

# A
# B
# C

type(a)
isinstance(a,A)
isinstance(b,A)
isinstance(b,C)

#
#
#
#

: A
True
True, B A
False, C A

issubclass(A,B) True, A
B. :
issubclass(B,A) # True
issubclass(C,A) # False

, , ,
, -

181

, . :
class Foo(object):
def spam(self,a,b):
pass

class FooProxy(object):
def __init__(self,f):
self.f = f
def spam(self,a,b):
return self.f.spam(a,b)

FooProxy Foo.
, Foo. FooProxy Foo
. :
f = Foo()
# Foo
g = FooProxy(f)
# FooProxy
isinstance(g, Foo) # False

Foo isinstance(), FooProxy.


.
, Foo, .
, ,
isinstance() issubclass(), . :
class IClass(object):
def __init__(self):
self.implementors = set()
def register(self,C):
self.implementors.add(C)
def __instancecheck__(self,x):
return self.__subclasscheck__(type(x))
def __subclasscheck__(self,sub):
return any(c in self.implementors for c in sub.mro())

#
IFoo = IClass()
IFoo.register(Foo)
IFoo.register(FooProxy)

IClass, . register() . __instancecheck__() isinstance(x, IClass). __subclasscheck__()


issubclass(C,IClass).

182

7. -

IFoo
, :
f = Foo()
g = FooProxy(f)
isinstance(f, IFoo)
isinstance(g, IFoo)
issubclass(FooProxy, IFoo)

#
#
#
#
#

Foo
FooProxy
True
True
True

,
. IFoo ,
.
. ,
,
. , .
__instancecheck__() __subsubclasscheck__(). .
Python , .
,
.



isinstance() issubclass(). ,
.
,

.
abc.
(ABCMeta) (@abstractabstractmethod @abstractproperty), :
from abc import ABCMeta, abstractmethod, abstractproperty
class Foo:
# Python 3
__metaclass__ = ABCMeta # class Foo(metaclass=ABCMeta)
@abstractmethod
def spam(self,a,b):
pass
@abstractproperty
def name(self):
pass

ABCMeta, ( -

183

Python 2 3). ,
(
).
,
, @abstractmethod @ababstractproperty Foo.
. Foo, :
>>> f = Foo()
Traceback (most recent call last):
File <stdin>, line 1, in <module>
TypeError: Cant instantiate abstract class Foo with abstract methods spam
(:
( ):
<stdin>, 1, <module>
TypeError: Foo
spam
)
>>>

. ,
, Bar, Foo, , Bar .
,
.
,
,
.
, , . , ,
(get, set delete), .
,
. , ,
. , Foo.spam(a,b)
.

. register(), :
class Grok(object):
def spam(self,a,b):
print(Grok.spam)

184

7. -
Foo.register(Grok)

# Grok,
# Foo

, ( isinstance() issubclass())
True
. , .
.
- , Python . , c
, int float, , object, , -
, .
, ,
, .

,
. ,
, , . collections
, , . numbers
,
. 14 15 ,
.

Python , . :
class Foo(object): pass
isinstance(Foo,object) # True

, , -
Foo. , . , , , .
,
Foo, type().
Foo, , type:
>>> type(Foo)
<type type>

185

class , . -,
, ,
. ,
, , ( __).
,
, . , :
class_name = Foo
#
class_parents = (object,) #
class_body =
#
def __init__(self,x):
self.x = x
def blah(self):
print(Hello World)

class_dict = { }
# class_dict
exec(class_body,globals(),class_dict)

# Foo
Foo = type(class_name,class_parents,class_dict)

,
type(), . , , . , __metaclass__ ( Python 2), metaclass ( Python 3).
class Foo:
# Python 3
__metaclass__ = type # class Foo(metaclass=type)
...

, class ( ). .
class Foo(object): pass

Foo , object.
, class __metaclass__. , .
, class. :
__metaclass__ = type
class Foo:
pass

186

7. -

, __metaclass__ ,
. Python 2 types.ClassType, ,
. , Python 2.2, Python. - ,
. Python 3 type.
, . , type
, __init__() __new__(). , ,
:
class DocMeta(type):
def __init__(self,name,bases,dict):
for key, value in dict.items():
#
if key.startswith(__): continue
#
if not hasattr(value,__call__): continue
#
if not getattr(value,__doc__):
raise TypeError(%s must have a docstring % key)
type.__init__(self,name,bases,dict)

__init__(), . , . - , TypeError.
type.__init__().
, .
, , :
class Documented:
# Python 3
__metaclass__ = DocMeta # class Documented(metaclass=DocMeta)

, , . :
class Foo(Documented):
spam(self,a,b):
spam -
pass

, . , .

187

, ,
, .
- , __new__(), .
, ,
. TypedProperty, :
class TypedProperty(object):
def __init__(self,type,default=None):
self.name = None
self.type = type
if default: self.default = default
else:
self.default = type()
def __get__(self,instance,cls):
return getattr(instance,self.name,self.default)
def __set__(self,instance,value):
if not isinstance(value,self.type):
raise TypeError( %s % self.type)
setattr(instance,self.name,value)
def __delete__(self,instance):
raise AttributeError( )

name None. . :
class TypedMeta(type):
def __new__(cls,name,bases,dict):
slots = [ ]
for key,value in dict.items():
if isinstance(value,TypedProperty):
value.name = _ + key
slots.append(value.name)
dict[__slots__] = slots
return type.__new__(cls,name,bases,dict)

# ,
class Typed:
# Python 3
__metaclass__ = TypedMeta # class Typed(metaclass=TypedMeta)


TypedProperty. name
slots. __slots__ __new__() type, .
:
class Foo(Typed):
name = TypedProperty(str)
num = TypedProperty(int,42)

188

7. -

,
, . , .


,
.
, ,
.
. , . :
registry = { }
def register(cls):
registry[cls.__clsid__] = cls
return cls

register __clsid__.
, , . ,
. :
@register
class Foo(object):
__clsid__ = 123-456
def bar(self):
pass

, , .
:
class Foo(object):
__clsid__ = 123-456
def bar(self):
pass
register(Foo) #

,
- , - ,
.

,
Python . ,
Python.. . ,
.

import
Python
. spam.py:
# spam.py
a = 37
def foo():
print( foo(), a = %s % a)
def bar():
print( bar(), foo())
foo()
class Spam(object):
def grok(self):
print( Spam.grok)

, ,
import spam. import , :
1. ,
, .
, , , global.
2. .

190

8. ,

3. ,
. ,
:
import spam
x = spam.a
spam.foo()
s = spam.Spam()
s.grok()
...

#
#
#
#

spam
spam
spam
spam.Spam()

, import . ,
,
.
, , . , spam.py
Spam, spam.Spam.
, import , :
import socket, os, re

, ,
as. :
import spam as sp
import socket as net
sp.foo()
sp.bar()
net.gethostname()

, ,
,
import. -
.

, . , , , xmlreader.py csvreader.py, read_data(filename), ,
. , , :
if format == xml:
import xmlreader as reader
elif format == csv:
import csvreader as reader
data = reader.read_data(filename)

Python . , ,
,

191

. , reader . ,
. __dict__
, ,
, .
import .

, import, .
import , import. , , , sys.modules.
. , , import .



from. from import, ,
, , , , :
from spam import foo #
#
foo()
#
spam.foo()
#

spam foo

spam.foo()
NameError: spam

from , . :
from spam import foo, bar

, . import . :
from spam import (foo,
bar,
Spam)

, from as, . :
from spam import Spam as Sp
s = Sp()

, , ,
, ,
(*). :

192

8. ,
from spam import * #

from module import * . ,


- ,
(, -, , ,
).
,
from module import *, __all__. :
# module: spam.py
__all__ = [ bar, Spam ] #
# from spam import *

,
from, . , :
from spam import foo
a = 42
foo()
# foo(), a = 37

foo(), spam.py, a. foo()


, .
, ,
,
. . ,
bar(),
spam.foo(), foo(), :
from spam import bar
def foo():
print( foo)
bar()
# bar() foo(),
# spam.foo(), foo(),

, from, .
:
from spam import a, foo #
a = 42
#
foo()
# foo(), a = 37
print(a)
# 42

,
Python . a, .
, 42, a

193

. a , , .
from , ,
C Fortran.. , , import
( spam.a).



Python
. import .
.
, :
% python spam.py

__name__,
. ,
, . __main__. ,

, ,
__main__.
, ,
__main__. ,
- , ,
. , :
# ,
if __name__ == __main__:
#

else:
# ,

, , , . ,
, , if, , , , .
,
.

194

8. ,


, ,
sys.path. sys.
path , . sys.path
, .zip .egg. sys.path . ,
.
,
zip-,
-, Python.. . , , , foo.py
bar.py, zip- mymodules.zip. Python, :
import sys
sys.path.append(mymodules.zip)
import foo, bar

, zip-.
-. , zip-
- . :
sys.path.append(/tmp/modules.zip/lib/python)

.zip-, .egg. .egg , setuptools. ,


Python. .egg , ,
zip-, (,
, ). ,
.egg, , zip-.
zip-,
, . -,
.py, .pyw, .pyc .pyo.
, C,
, , setuptools,
( C ). , Python
.pyc .pyo, .py ( ). ,
,
.

195


,
Python.. , import,
:

Python ( .py)

C C++,
DLL

, C Python

(, foo)
, sys.path, ( , ):
1. foo, .
2. foo.pyd, foo.so, foomodule.so foomodule.dll (
).
3. foo.pyo ( -O -OO).
4. foo.pyc.
5. foo.py ( Windows
.pyw).
; 26 Python.
.py, , - .pyc.
-,
- .py (
.pyc ). .pyo , -O. - ,
assert .
.
-O -OO, . .pyo,
.
, sys.path, , . , ImportError.
.pyc .pyo import.
. -

196

8. ,

, , , .py , (, -
zip-).
, -B.
.pyc .pyo, .py . ,
, .pyc. , Python .
,
. , ,
.pyc . .pyc,
Python,, .
import ,
, , ,
Windows OS X ( ). import foo foo.py
FOO.PY. .


Python , .
sys.modules, .
,
import. ,
, , ,
, .

. , sys.modules
import, ,
, .
,
import, . ,
, .
Python reload(),
. ( ), -

197

. Python 3
. .
, , Python,
C/C++,
.
, .
Python.


.
, . ,
__init__.py. , . ,
:
Graphics/
__init__.py
Primitive/
__init__.py
lines.py
fill.py
text.py
...
Graph2d/
__init__.py
plot2d.py
...
Graph3d/
__init__.py
plot3d.py
...
Formats/
__init__.py
gif.py
png.py
tiff.py
jpeg.py

import :

import Graphics.Primitive.fill
Graphics.Primitive.fill. ,, , , Graphics.Primitive.fill.
floodfill(img,x,y,color).

198

8. ,

from Graphics.Primitive import fill


fill,
; , fill.
floodfill(img,x,y,color).

from Graphics.Primitive.fill import floodfill


fill,
floodfill ; , floodfill(img,x,y,color).

- , __init__.py.
, ,
. __init__.py,
import . import Graphics.Primitive.fill, ,
__init__.py Graphics, __init__.py
Primitive.
, , :
from Graphics.Primitive import *

,
, , . - ,
(
), Python , . ,
, __init__.py,
Primitive. , __all__, .
__init__.py , :
# Graphics/Primitive/__init__.py
__all__ = [lines,text,fill]

, from Graphics.Primitive
import *, , .
, , , .
, , Graphics.Primitive.fill Graphics.Primitive.lines.
(, from Graphics.Primi.PrimiPrimitives import lines)
, :
# fill.py
from . import lines

. from . import lines


, . -

199

lines , fill.
py. import module. Python , import module
. Python
, , , ,
. Python 3, import , ,
. .
,
. , Graphics.Graph2D.plot2d Graphics.Primitives.lines,
:
# plot2d.py
from ..Primitives import lines

..
, Primitives , .
from module import symbol.
, import ..Primitives.lines import .lines, . , symbol
. ,
from .. import Primitives.lines, . ,

; , .
, . , :
import Graphics
Graphics.Primitive.fill.floodfill(img,x,y,color) # !

, import Graphics __init__.py


Graphics,
, ,
:
# Graphics/__init__.py
from . import Primitive, Graph2d, Graph3d

# Graphics/Primitive/__init__.py
from . import lines, fill, text, ...

200

8. ,

import Graphics .
, , . , import module,
.
, , __path__, ,
(__path__
sys.path ). __path__ __init__.py .
__path__ ,
.
,
.


Python
,
distutils.
, README,
.
, . , import. ,
, (, python _). , Python:
spam/
README.txt
Documentation.txt
libspam.py
#
spampkg/
#
__init__.py
foo.py
bar.py
runspam.py
# , : python runspam.py

,
Python .
, spam,
, - Python,
.

Python

201

, setup.py
( spam) :
# setup.py
from distutils.core import setup

setup(name = spam,
version = 1.0,
py_modules = [libspam],
packages = [spampkg],
scripts = [runspam.py],
)

py_modules setup()
, packages scripts . ,
(, ). name , version
.
setup() ,
. . 8.1
.. , classifiers, , : [Development Status :: 4 - Beta, Programming Language :: Python]
( http://pypi.python.org).
8.1. setup()

name

()

version

()

author

author_email

maintainer

maintainer_email

url

description

long_description

download_url

classifiers

setup.py
. ,
:

202

8. ,
% python setup.py sdist
...
%

spam/dist ,
spam-1.0.tar.gz spam-1.0.zip. ,
. , :
% unzip spam-1.0.zip
...
% cd spam-1.0
% python setup.py install
...
%


. site-packages Python.
, sys.path. UNIX-
- Python, Windows Scripts ( C:\Python26\
Scripts).
UNIX,
#! python, Python
. ,
Python, /usr/local/
bin/python, , Python
, .
setup.py , . python setup.py bdist,
, .py
.pyc , . , , (,
C,, ). Windows python setup.py bdist_
wininst, .exe.
Windows, ,
,
. ,
Windows ,
.
distutils , Python ( ). , ,

203

Python,
(
, py2exe py2app). ,
Python .
, distutils , . 26 , distutils ,
C C++.

Python,
.egg. setuptools (http://pypi.python.
org/pypi/setuptools). setuptools,
setup.py, :
# setup.py
try:
from setuptools import setup
except ImportError:
from distutils.core import setup

setup(name = spam,
...
)


, Python, Python (Python Package
Index PyPI), http://pypi.python.org. , ,
. , , .
,
, setup.py python setup.py install.
site-packag-packagpackages, Python.
.
, python setup.py
install --user, . , : /Users/beazley/.local/lib/python2.6/site-packages UNIX.

, --prefix setup.py. -

204

8. ,

, python setup.py install --prefix=/home/beazley/pypackages


/home/beazley/pypackages. , sys.path,
.
, Python C
C++. ,
,
C++. UNIX, Linux OS X
. Windows Microsoft Visual Studio.. Windows,, , ,
.
setuptools, easy_install. , easy_install _.
setuptools , PyPI, ,
. , .
PyPI,
python setup.py register.
(
, ).


- Python, , ,
,
pickle.


Python sys.argv. . ,
. ,
:
import sys
if len(sys.argv) != 3:
sys.stderr.write(Usage: python %s inputfile outputfile\n % sys.argv[0])
raise SystemExit(1)
inputfile = sys.argv[1]
outputfile = sys.argv[2]

sys.argv[0] , . sys.stderr SystemExit ,


, .

,
optparse. :
import optparse
p = optparse.OptionParser()

#
p.add_option(-o,action=store,dest=outfile)

206

9.
p.add_option(--output,action=store,dest=outfile)

#
p.add_option(-d,action=store_true,dest=debug)
p.add_option(--debug,action=store_true,dest=debug)

#
p.set_defaults(debug=False)

#
opts, args = p.parse_args()

#
outfile = opts.outfile
debugmode = opts.debug

. -o,
--output, . action=store p.add_option(). -d, --debug, .. action=store_true p.add_option().
dest p.add_option() , . p.set_defaults() .
, .
, None.

:
%
%
%
%

python
python
python
python

prog.py
prog.py
prog.py
prog.py

-o outfile -d infile1 ... infileN


--output=outfile --debug infile1 ... infileN
-h
--help

p.parse_args(). (opts, args), opts ,


, args , . opts.dest, dest
, p.add_option().
, -o --output
opts.outfile, args ,
[infile1, ..., infileN]. optparse h, --help, , . , .

optparse. 19 .

207



os.environ. :
import os
path = os.environ[PATH]
user = os.environ[USER]
editor = os.environ[EDITOR]
... .. ...

,
os.environ. :
os.environ[FOO] = BAR

os.environ , , Python.


open(name [,mode [,bufsize]])
, :
f = open(foo)
# foo
f = open(foo,r) # foo ( )
f = open(foo,w) # foo

r , w a .

\n. , Windows
\n
\r\n ( \r\n \n).
b, : rb wb.

, (,
UNIX, , b, -
).
, -
, rt, wt at, .
, (+), : r+ w+. , , , ,
,
.
w+, . U rU,

208

9.

.

( \n, \r \r\n), , \n ,
-. , , UNIX,, , Windows.
bufsize , 0 , 1 . .
Python 3 open() open(name [,mode [,bufsize [, encoding
[, errors [, newline [, closefd]]]]]]). encoding
, : utf-8 ascii. errors , (
). newline None, , \n, \r \r\n. None,
, \n, \r \r\n, \n.
( ), , ,
. newline ,
. closefd , close(). True. .. 9.1 , file.
9.1.

f.read([n])

n .

f.readline([n])

, n . n , .

f.readlines([size])

.
size
.

f.write(s)

s.

f.writelines(lines)

lines.

f.close()

f.tell()

f.seek(offset [, whence])

209

f.isatty()

1, f
.

f.flush()

f.truncate([size])

size .

f.fileno()

f.next()


StopIteration. Python 3 f.__next__().

read() , , , . readline() ,
; readlines()
. readline() , , n. , n,
n .
. readlines()
size, , .
, , .
, readline() readlines(),

(, \n \r\n). (U rU), \n.
read() readline() , . ,
, :
while True:
line = f.readline()
if not line:
break

for.
:
for line in f:
#
#
...

, Python 2 8- , ( ). Python 3 -

210

9.

, ,
.
write() , writelines()
. write() writelines() , .
, .
, ,
. tell()
. seek()
,
offset whence, , . whence
0 ( ), seek() offset ; whence 1, ; whence 2,
. seek()
. , ,
open(), .
( ). ,
, .
fileno() , ,
-, . ,
fcntl UNIX.
, , , . 9.2.
9.2.

f.closed

, : False
, True .

f.mode

-.

f.name

, open(). .

f.softspace

, , print
. , , ,
, (
Python 2).

211

f.newlines


, ,
. None,
, ; , \n, \r \r\n,
.

f.encoding

, (, latin-1 utf-8). ,
None.

,

, , ,
, sys
sys.stdin, sys.stdout sys.stderr . stdin ,
, . stdout ,
print. stderr , . stdin , stdout stderr .
, , -. ,
:
import sys
sys.stdout.write( : )
name = sys.stdin.readline()

raw_input(prompt),
sys.stdin
:
name = raw_input( : )

, raw_input(),
. sys.stdin,
. Python 3 raw_input() input().
( Ctrl+C) KeyboardInterrupt,
.

212

9.

sys.stdout, sys.stdin sys.


stderr , print .
sys.
stdout, . ,
sys.stdout, sys.stdin sys.stderr, , sys.__stdout__,
sys.__stdin__ sys.__stderr__ .
, sys.stdin, sys.stdout sys.
stderr. , Python IDLE, sys.stdin ,
, , . , read() seek(), .

print
, sys.stdout, Python 2 print. print , , :
print : , x, y, z

str(), . , . ,
print .
print . softspace ,
.
print : , x, y, z, w
# print
print : , x, y, #
print z, w
# z

(%) .format(), 4 . :
print : %d %7.5f %s % (x,y,z) # -
print : {0:d} {1:7.5f} {2}.format(x,y,z)

, print, , >>file, ,
file , . :
f = open(output,w)
print >>f, ,
...
f.close()

213

print()

print()
Python 3 print . Python 2.6 print , , from __future__
import print_function , .
print() , print,
.
, print(), :
print(: , x, y, z)

end=ending. :
print(: , x, y, z, end=) #


file=outfile. :
print(: , x, y, z, file=f) # f

- sep=sepchr. :
print(: , x, y, z, sep=,) #


, ,
, . , Perl PHP,, , ($) (
$name, $address ). Python , ,
.
, , name, item amount, :
# : ,
#
form = \
%(name)s,
, %(item)s $%(amount)0.2f.
,

Joe Python User

print form % { name: ,


item: ,
amount: 50.00,
}

214

9.

:
,

, $50.00.

,
Joe Python User

format() , . :
form = \
{name},
, {item} {amount:0.2f}.
,

Joe Python User

print form.format(name= , item=, amount=50.0)

Template() string, :
import string
form = string.Template(\
$name,
, $item $amount.
,

Joe Python User


)
print form.substitute({name: ,
item: ,
amount: %0.2f % 50.0})

, $,
. form.substitute() . , . -,
, , ,
, .



-.
-. yield write()
print. :

215

def countdown(n):
while n > 0:
yield %d\n % n
n -= 1
yield !\n

, , . , f, :
count = countdown(5)
f.writelines(count)

s, :
for chunk in count:
s.sendall(chunk)

:
out = .join(count)

,
-. , ,
,
-:
chunks = []
buffered_size = 0
for chunk in count:
chunks.append(chunk)
buffered_size += len(chunk)
if buffered_size >= MAXBUFFERSIZE:
outf.write(.join(chunks))
chunks.clear()
buffered_size = 0
outf.write(.join(chunks)

, ,

,
, ,
.
Python,
- (Web Services Gateway Interface
WSGI),
), -.


, -,
, .

216

9.

s, , ,
s.decode([encoding [,errors]]). u u.encode([encoding
[, errors]]). , ,
, 8- . encoding
. :

ascii

7- ASCII

latin-1 iso-8859-1

ISO 8859-1 Latin-1

cp1252

Windows 1252

utf-8

8-

utf-16

16- (
)

utf-16-le

UTF-16,
-16, (little
little endian)

utf-16-be

UTF-16, (big endian)


unicode-escape

ustring

raw-unicode-escape


urstring

, site,
sys.getdefaultencoding().
ascii, ASCII [0x00,0x7f]
[U+0000, U+007F]. utf-8
. , , .
s.decode(), , s
. Python 2 s , ,
Python 3 s bytes.
t.encode() . , ,
Python 2 . ,
Python 2 , decode() encode(),
Python 3 encode(), bytes

217

decode().
Python 2, encode() ,
decode() .
, , UnicodeError. ,
ascii , ,
U+1F28, ,
ASCII.
errors encode() decode()
, . :

strict

UnicodeError.

ignore

replace


(U+FFFD , ?
).

backslashreplace


, Python. , U+1234
\u1234.

xmlcharrefreplace

XML. , U+1234 &#4660;.

strict.
xmlcharrefreplace
ASCII . , Jalape\u00f1o, ASCII xmlcharrefrexmlcharrefreplace,
Jalapeo, .
,
(, + ). Python 3 ,
Python 2 -
,
, .

. .

218

9.

-
. ,
, . ,
, U+HHLL
, LL HH, , HH LL. , , , ,
.
-
, ,
. ,
- , , , .
codecs , , .
, , codecs.open(filename [, mode [, encoding [, errors]]]), :
f = codecs.open(foo.txt,r,utf-8,strict) #
g = codecs.open(bar.txt,w,utf-8)
#

,
, . encoding ,
. errors
: strict, ignore, replace, backslashreplace
xmlcharrefreplace, .
, codecs.
EncodedFile(file, inputenc [, outputenc [, errors]])
, . :
f = open(foo.txt,rb)
...
fenc = codecs.EncodedFile(f,utf-8)

, , , inputenc,
outputenc.
outputenc ,
inputenc. errors , . EncodedFile
.

219


.
,
. ,
XML , <?xml ...>. 3C 3F 78 6D (<?xm), ,
UTF-8. 00 3C 00 3F 3C 00 3F 00, , UTF-16
-16 UTF-16
-16 . ,
MIME . :
<?xml ... encoding=ISO-8859-1 ... ?>

(Byte-Order Markers BOM),


.
U+FEFF. . , ,
(, \xff\xfe UTF-16LE \xfe\xff UTF-16-BE).
BOM . , BOM . , .
, , , :
f = open(somefile,rb)
#
...
# , .
# , BOM ( )
# .
fenc = codecs.EncodedFile(f,encoding)
data = fenc.read()


. 9.3 codecs, .
9.3. , codecs

ascii

ASCII

latin-1 iso-8859-1

Latin-1 ISO 8859-1

220

9.

9.3 ()
cp437

CP437

cp1252

CP1252

utf-8

8-

utf-16

16-

utf-16-le

UTF-16,
(little endian)

utf-16-be

UTF-16,
-16, (big endian)

unicode-escape

ustring

raw-unicode-escape


urstring

ascii
ascii [0x00,0x7f]
[U+0000, U+007F]. , , .

iso-8859-1, latin-1
8- [0x00,0xff]
[U+0000, U+00FF]. [0x00,0x7f] ASCII. [0x80,0xff]
ISO-8859-1,
-8859-1, ASCII.. [0x00,0xff] .

cp437
iso-8859-1 Python , Windows
. , [x80,0xff], , , ,
DOS.

cp1252
iso-8859-1
Windows. [0x800x9f], iso-8859-1 .

221

utf-8
UTF-8 , ,
. ASCII
,
0127. 2 3 .
:

U+0000 - U+007F

0nnnnnnn

U+007F - U+07FF

110nnnnn

10nnnnnn

U+0800 - U+FFFF

1110nnnn

10nnnnnn

10nnnnnn

2- 110. 3-
1110. 10.
, UTF-8
6 .
Python 4- UTF-8,
.
[U+D800, U+DFFF] 20- . : 4- 11110nnn 10nnnnnn 10nmmmm
10mmmmm U+D800 + N, U+DC00 + M, N 10 , M 10 20- ,
4- UTF-8.
-8. - UTF-8
-8 ( 111110 1111110 ) 32- .
Python , , UnicodeError.
UTF-8
-8 , . -, ASCII .
, ASCII UTF-8
-8 ASCII.. -, UTF-8
-8 NULL .
, C,, , 8- , NULL,
UTF-8.
-8. , UTF-8
-8 . , a
b, a < b, UTF-8

222

9.

a < b . , 8- ,
UTF-8.

utf-16, utf-16-be utf-16-le


UTF-16
-16 16- , 16- . ,
(big endian). ,
UTF-16
U+FEFF. U+FEFF
,
, U+FFFE, .
FE FF FF FE . Python
.
utf-16-be UTF-16
-16 (big endian).
utf-16-le UTF-16
-16 (little endian).
UTF-16
-16 , 16 , .

unicode-escape raw-unicode-escape
, Python
. :
s = u\u14a8\u0345\u2a34
t = s.encode(unicode-escape)

# t = \u14a8\u0345\u2a34


- ,
,
, , . unicodedata. ,
unicodedata.category(c). , unicodedata.category(uA) Lu, ,
.
,
. , U+00F1 () U+00F1 U+006e U+0303 (n, ).

223

pickle

,
unicodedata.normalize(). ,
unicodedata.normalize(NFC, s) ,
s .
unicodedata 16 .

pickle

. ,
, .
pickle shelve.
pickle ,
, . pickle , dump() load(). , :
import pickle
obj = SomeObject()
f = open(filename,wb)
pickle.dump(obj, f)
f.close()

# f

:
import pickle
f = open(filename,rb)
obj = pickle.load(f)
f.close()


dump(), . load().
shelve pickle, ,
:
import shelve
obj = SomeObject()
db = shelve.open(filename)
db[key] = obj
...
obj = db[key]
db.close()

#
#
#
#

, shelve, , . -,
. -, , ,
pickle.

224

9.

Python,
, , ,
.
, pickle,
Python.
Python. 1 protocol dump(obj, file, protocol)
pickle. 0.
pickle, Python. Python,
. 1 2
.
, , :
import pickle
obj = SomeObject()
f = open(filename,wb)
pickle.dump(obj,f,2)
# 2
pickle.dump(obj,f,pickle.HIGHEST_PROTOCOL) #
#
f.close()

load() ,
.
shelve
Python
pickle, :
import shelve
db = shelve.open(filename,protocol=2)
...

pickle shelve.
__getstate__()
__setstate__() . __getstate__(), ,
, . ,
__getstate__(), , , . __setstate__() ,
. ,
, .
,

. . .

pickle

225

,
, :
import socket
class Client(object):
def __init__(self,addr):
self.server_addr = addr
self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.sock.connect(addr)
def __getstate__(self):
return self.server_addr
def __setstate__(self,value):
self.server_addr = value
self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.sock.connect(self.server_addr)

pickle ,
Python,
, . , - ,
( ,
).
pickle shelve . 13
Python .

10


,
Python.. , , .


, . , :
python [options] [-c cmd | filename | - ] [args]

. 10.1 :
10.1. ,

-3

,
Python 3.

-B

.pyc .pyo
import.

-E

-h

-i

-m module

module, .

-O

227

-OO


.pyo.

-Q arg

Python 2. arg : -Qold ( ),


-Qnew, -Qwarn -Qwarnall.

-s

sys.path.

-S

site .

-t

-tt

TabError .

-u

stdout stderr.

-U


( Python 2).

-v


import.

-V

-x

-c cmd

cmd, .

-i , .
-m , __main__. -O -OO -;
8 , . -S
site,
.
-t, -tt -v . x
,
Python (, Python).
.
(-),
.
, . , . -c cmd

, : python c print(hello world).

228

10.

,
(-), sys.argv, 9
.
, , . 10.2:
10.2. ,

PYTHONPATH

, ,
.

PYTHONSTARTUP

,
.

PYTHONHOME

Python.

PYTHONINSPECT

i.

PYTHONUNBUFFERED

u.

PYTHONIOENCODING

stdin, stdout stderr. encoding[:errors],


: utf-8 utf-8:ignore.

PYTHONDONTWRITEBYTECODE

B.

PYTHONOPTIMIZE

O.

PYTHONNOUSERSITE

s.

PYTHONVERBOSE

v.

PYTHONUSERBASE

PYTHONCASEOK

,
.

PYTHONPATH , sys.path, 9. PYTHONSTARTUP ,


. PYTHONHOME , Python,, , ,
site-packages, .
, /usr/local,
. , : /usr/
local:/usr/local/sparc-solaris-2.6, , - . PYTHONHOME , Python .
PYTHONIOENCODING
Python 3, -

229

-. ,
Python 3 . , ,
. :
>>> a = Jalape\xf1o
>>> a
Traceback (most recent call last):
File <stdin>, line 1, in <module>
File /tmp/lib/python3.0/io.py, line 1486, in write
b = encoder.encode(s)
File /tmp/lib/python3.0/encodings/ascii.py, line 22, in encode
return codecs.ascii_encode(input, self.errors)[0]
UnicodeEncodeError: ascii codec cant encode character \xf1 in position 7:
ordinal not in range(128)
(:
( ):
<stdin>, 1, <module>
/tmp/lib/python3.0/encodings/ascii.py, 22, encode
return codecs.ascii_encode(input, self.errors)[0]
UnicodeEncodeError: \xf1, 7,
ascii: range(128)
)
>>>

,
PYTHONIOENCODING , ascii:backslashreplace utf-8. :
>>> a = Jalape\xf1o
>>> a
Jalape\xf1o
>>>

Windows , PYTHONPATH, ,
HKEY_LOCAL_MACHINE/Software/Python.


, Python
.
. , ,
PYTHONSTARTUP ( ). ,
( import).
,
, .pythonrc.
. >>> -

230

10.

; ... , . :
>>> for i in range(0,4):
...
print i,
...
0 1 2 3
>>>

,
sys.ps1 sys.ps2.
Python
GNU readline. , Python.
, ,
repr(). sys.dis.disdisplayhook , . :
>>>
...
...
...
>>>
>>>
7
>>>
[0,
>>>

def my_display(x):
r = repr(x)
if len(r) > 40: print(r[:40]+...+r[-1])
else: print(r)
sys.displayhook = my_display
3+4
range(100000)
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 1...]

, ,
(_).
. :
>>> 7 + 3
10
>>> _ + 2
12
>>>

_ displayhook(), . displayhook(),
_, , , .

Python
, , . UNIX
,

231

,
:
#!/usr/bin/env python
# Python code from this point on...
print , !
...

Windows .py, .pyw, .wpy, .pyc


.pyo .
, .pyw (
). , .bat. ,
.bat Python , .bat
:
:: foo.bat
:: foo.py
::
c:\python26\python.exe c:\pythonscripts\foo.py %*



Python
. ,
site. site ,
sys.path. , site
, .
site ,
sys.prefix sys.exec_prefix, :
[ sys.prefix,
# Windows
sys.exec_prefix,
# Windows
sys.prefix + lib/pythonvers/site-packages,
sys.prefix + lib/site-python,
sys.exec_prefix + lib/pythonvers/site-packages,
sys.exec_prefix + lib/site-python ]

, ,
, ( ).
. , sys.path.
- ( .pth). , zip-
.egg, , ,
sys.path. :

232

10.
# foo.pth foo
foo
bar


. .
site , . , sys.path. .
sys.path,
sitecustomize.
, .
sitecustomize
ImportError, .
sitecustomize sys.path. .
, site ,
. ascii. , sitecustomize.py , sys.setdefaultencod.setdefaultencodsetdefaultencoding() , utf-8.
site , , .


, , .
.
UNIX Macintosh ~/.local : ~/.local/lib/python2.6/site-packages. Windows

%APPDATA%, : C:\
Documents and Settings\David Beazley\Application Data. Python\Python26\site-packages.
,
,
.
, --user setup.py. : python setup.py install --user.


,
Python,, , , . from __future__ import. :

233

#
from __future__ import division


. , ,
__future__, , . Python ,
.
. 10.3 ,
:
10.3. __future__

nested_scopes

.
Python 2.1 , Python 2.2.

generators

. Python 2.2
, Python 2.3.

division

, . ,
1/4 0.25 0.
Python 2.2 2.6.
, Python 3.0.

absolute_import

. ,
, import string,
, , sys.path.
, . , import
module
. , import string,
string .
Python 2.5
2.6. ,
Python 3.0.

with_statement

with. Python 2.5 ,


Python 2.6.

print_function

print print(). Python 2.6 ,


Python 3.0.

,
__future__. ,

234

10.

Python, , .


,, , SystemExit (
sys.exit()) SIGTERM SIGHUP
( UNIX).
(
). ,
__del__().
, __del__() .
- ( ,
).
, .
, __del__() , , .

(, close()). ,

atexit, :
import atexit
connection = open_connection(deaddot.com)

def cleanup():
print ...
close_connection(connection)

atexit.register(cleanup)

:
import atexit, gc
atexit.register(gc.collect)

, : __
del__() , . , __del__()
NameError, , :
Exception exceptions.NameError: c in <method Bar.__del__
of Bar instance at c0310> ignored

235

(:
exceptions.NameError: c <method Bar.__del__ Bar
c0310>
)

, , __del__()
. ,
- (, ). , , , ,
. NameError ,
__del__():
import foo
class Bar(object):
def __del__(self, foo=foo):
foo.bar()
# foo


- . os._exit(status). exit(),
Python.
, .

11
.

, ,

, , C Java,
Python ,
.
,
( ). Python . , ,
. ,
, , ,
- , ( ,
).
, ,
Python,, . .

doctest
, ,
. ,
. ,
help(),
Python.. -

doctest

237

,
. :
# splitter.py
def split(line, types=None, delimiter=None):

.
:

>>> split(GOOG 100 490.50)


[GOOG, 100, 490.50]
>>> split(GOOG 100 490.50,[str, int, float])
[GOOG, 100, 490.5]
>>>

,
-,
:

>>> split(GOOG,100,490.50,delimiter=,)
[GOOG, 100, 490.50]
>>>

fields = line.split(delimiter)
if types:
fields = [ ty(val) for ty,val in zip(types,fields) ]
return fields

, , , . , .
doctest.
doctest ,
, . doctest, , .
, ,
splitter.py, testsplitter.py :
# testsplitter.py
import splitter
import doctest

nfail, ntests = doctest.testmod(splitter)

doctest.testmod(module) module
. ,
. ,

. , testmod(module, verbose=True).

238

11. , ,

,
, :
...
if __name__ == __main__:
#
import doctest
doctest.testmod()

, , ,
. , ,
.
doctest , , . . :
def half(x):
x. :

>>> half(6.8)
3.4
>>>

return x/2

doctest, :
**********************************************************************
File half.py, line 4, in __main__.half
Failed example:
half(6.8)
Expected:
3.4
Got:
3.3999999999999999
**********************************************************************
(:
**********************************************************************
half.py, 4, __main__.half
:
half(6.8)
:
3.4
:
3.3999999999999999
**********************************************************************
)

unittest

239

, ,
.
doctest ,
, .
, doctest ,
.

, ( , , 50 , ).
unittest.
, doctest , ,
. ,
.
: http://docs.python.org/library/doctest.html.

unittest
unittest.
(,
, , ). , .
.
,
.
, :
# splitter.py
def split(line, types=None, delimiter=None):

.
...

fields = line.split(delimiter)
if types:
fields = [ ty(val) for ty,val in zip(types,fields) ]
return fields


split(),
testsplitter.py, :

240

11. , ,
# testsplitter.py
import splitter
import unittest

#
class TestSplitFunction(unittest.TestCase):
def setUp(self):
# ( )
pass
def tearDown(self):
# ( )
pass
def testsimplestring(self):
r = splitter.split(GOOG 100 490.50)
self.assertEqual(r,[GOOG,100,490.50])
def testtypeconvert(self):
r = splitter.split(GOOG 100 490.50,[str, int, float])
self.assertEqual(r,[GOOG, 100, 490.5])
def testdelimiter(self):
r = splitter.split(GOOG,100,490.50,delimiter=,)
self.assertEqual(r,[GOOG,100,490.50])

#
if __name__ == __main__:
unittest.main()

, Python, testsplitter.py. :
% python testsplitter.py
...
---------------------------------------------------------------------Run 3 tests in 0.014s

OK

unittest , unittest.TestCase. , test, testsimplestring,


testtypeconvert . ( ,
, , test.)
.
t unittest.TestCase ,
:
t.setUp()
, .
t.tearDown()
.

unittest

241

t.assert_(expr [, msg])
t.failUnless(expr [, msg])
, expr
False. msg , ( ).
t.assertEqual(x, y [,msg])
t.failUnlessEqual(x, y [, msg])
, x y . msg , ( ).
t.assertNotEqual(x, y [, msg])
t.failIfEqual(x, y, [, msg])
, x y . msg , ( ).
t.assertAlmostEqual(x, y [, places [, msg]])
t.failUnlessAlmostEqual(x, y, [, places [, msg]])
, x y places .
x y
places . ,
x y . msg , ( ).
t.assertNotAlmostEqual(x, y, [, places [, msg]])
t.failIfAlmostEqual(x, y [, places [, msg]])
, x y
places . msg , ( ).
t.assertRaises(exc, callable, ...)
t.failUnlessRaises(exc, callable, ...)
, callable
exc.
callable, .
exc .
t.failIf(expr [, msg])
, expr
True. msg , ( ).
t.fail([msg])
. msg , ( ).
t.failureException
, . , , -

242

11. , ,

,
, , , .
, unittest , ,
, . ( , ). , http://docs.python.
org/library/unittest.html,
, .

Python pdb
Python , pdb. pdb , , ,
.

Python.
run(statement [, globals [, locals]])
statement .
. continue
. globals locals , .
runeval(expression [, globals [, locals]])
expression .
- , , continue,
run(). .
runcall(function [, argument, ...])
function . function
. function. - . function.
set_trace()
.
.

Python pdb

243

post_mortem(traceback)
traceback, . traceback
, sys.exc_info().
pm()

traceback, .
, , , , set_trace(). - , set_trace() .
, , ,
. .


(Pdb), :
>>> import pdb
>>> import buggymodule
>>> pdb.run(buggymodule.start())
> <string>(0)?()
(Pdb)

(Pdb) , . ,
. . , h(elp) ,
h help.
[!]statement
statement () . , , statement
. ,
global :
(Pdb) global list_options; list_options = [-l]
(Pdb)

a(rgs)
.
alias [name [command]]
name command. , %1,%2
, command ,

244

11. , ,

. %*
. command ,
.. , Pdb.
:
# ( : pi classInst)
alias pi for k in %1._ _dict_ _.keys(): print %1.,k,=,%1._ _dict_ _[k]
# self
alias ps pi self

b(reak) [loc [, condition]]


loc. loc
, . :

filename:n

function

module.function

loc , , . condition ,
, .
, .
, .
cl(ear) [bpnumber [bpnumber ...]]
bpnumber.
, .
commands [bpnumber]

bpnumber. , end, .
continue, .
bpnumber , commands .
condition bpnumber [condition]
condition bpnumber. condition
, , . condition , .

Python pdb

245

c(ont(inue))
,
.
disable [bpnumber [bpnumber ...]]
. clear, disable
.
d(own)
.
enable [bpnumber [bpnumber ...]]
.
h(elp) [command]
. command, .
ignore bpnumber [count]
count .
j(ump) lineno
.
. , , .
l(ist) [first [, last]]
.
11 ,
(5 5 ). 11 , .
. last first, , .
n(ext)
.
, .
p expression
expression
.
pp expression
, p, pprint.

246

11. , ,

q(uit)
.
r(eturn)
.
run [args]
args ,
sys.argv. .
s(tep)
.
tbreak [loc [, condition]]
, .
u(p)
.
unalias name
.
until
,
, . ,
, until , , .
w(here)
.


, . :
% python -m pdb someprogram.py

,
- . ,
continue. ,
split() , ,
, :
% python m pdb someprogram.py
> /Users/beazley/Code/someprogram.py(1)<module>()

247

-> import splitter


(Pdb) b splitter.split
Breakpoint 1 at /Users/beazley/Code/splitter.py:1
(Pdb) c
> /Users/beazley/Code/splitter.py(18)split()
-> fields = line.split(delimiter)
(Pdb)


.pdbrc, ,
.
, ,
,
( ).


profile
cProfile. ,
, cProfile C,, . ( ,
),
. , , :
% python -m cProfile someprogram.py

, profile:
run(command [, filename])

command exec
. filename . , .
, :
126 function calls (6 primitive calls) in 5.130 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1
0.030
0.030
5.070
5.070 <string>:1(?)
121/1
5.020
0.041
5.020
5.020 book.py:11(process)
1
0.020
0.020
5.040
5.040 book.py:5(?)
2
0.000
0.000
0.000
0.000 exceptions.py:101(__init__)
1
0.060
0.060
5.130
5.130 profile:0(execfile(book.py))
0
0.000
0.000
profile:0(profiler)

248

11. , ,

, run(),
:

primitive calls

ncalls

( )

tottime

,
( )

percall

tottime / ncalls

cumtime

percall

cumtime / primitive calls

filename:lineno(function)

(, 121/1),
(primitive calls),
.
, , , , . , pstats.
, , http://docs.python.org/
library/profile.html.


,
, . , ,
,
.


,
Python,, , - , time UNIX.
, time.clock(),
, time.time(),
. :
start_cpu = time.clock()
start_real= time.time()

249

end_cpu = time.clock()
end_real = time.time()
print( . %f % (end_real start_real))
print( . %f % (end_cpu - start_cpu))

, , , , . timeit(code [,
setup]) timeit. :
>>> from timeit import timeit
>>> timeit(math.sqrt(2.0),import math)
0.20388007164001465
>>> timeit(sqrt(2.0),from math import sqrt)
0.14494490623474121

timeit() , .
,
. timeit() , . number=count timeit().
, timeit repeat(),
. , timeit(), , . :
>>> from timeit import repeat
>>> repeat(math.sqrt(2.0),import math)
[0.20306601524353027, 0.19715800285339355, 0.20907392501831055]
>>>


, ,
. , , sqrt(2.0) math.sqrt(2.0),
0,20388/0,14494 1,41 .
, , 41 %.


sys getsizeof(),
( ),
. :
>>> import sys
>>> sys.getsizeof(1)
14

250

11. , ,
>>> sys.getsizeof(Hello World)
52
>>> sys.getsizeof([1,2,3,4])
52
>>> sum(sys.getsizeof(x) for x in [1,2,3,4])
56

, , , , , , . ,
[1,2,3,4] , ,
( 14 ). , .
sum(), .
, getsizeof()
. ,
, , , ,
. , Python,, C, ,

. .
, , . , , ,
( , ,
), getsizeof(), , , , ,
.

, Python
dis. dis(), , :
>>> from dis import dis
>>> dis(split)
2
0 LOAD_FAST
3 LOAD_ATTR
6 LOAD_FAST
9 CALL_FUNCTION
12 STORE_FAST

0
0
1
1
2

(line)
(split)
(delimiter)
(fields)

251

15 LOAD_GLOBAL
18 JUMP_IF_FALSE
21 POP_TOP

1 (types)
58 (to 79)

>>

>>

>>

22
25
26
29
32
35
38
41
42
45
48
51
54
57
60
63
66
67
70
73
76
79

BUILD_LIST
DUP_TOP
STORE_FAST
LOAD_GLOBAL
LOAD_GLOBAL
LOAD_FAST
CALL_FUNCTION
GET_ITER
FOR_ITER
UNPACK_SEQUENCE
STORE_FAST
STORE_FAST
LOAD_FAST
LOAD_FAST
LOAD_FAST
CALL_FUNCTION
LIST_APPEND
JUMP_ABSOLUTE
DELETE_FAST
STORE_FAST
JUMP_FORWARD
POP_TOP

0
3
2
1
2
2

(_[1])
(zip)
(types)
(fields)

25
2
4
5
3
4
5
1

(to 70)
(ty)
(val)
(_[1])
(ty)
(val)

42
3 (_[1])
2 (fields)
1 (to 80)

>>

80 LOAD_FAST
83 RETURN_VALUE

2 (fields)

>>>

. -, ,
.
. -, , ,
,
,
.
, .



, , ,
Python.


, ,
. , 10
, 10 % , 910 %. -

252

11. , ,

, , ,
.
, .
,
, ,
.

O(n
(n
n log n)) O(n3).

.

, , ,
C
Python.
,
(, , ).
. ,
. , collection.deque
,
. ,

. ,
, . ,
, , .
, , deque:
>>> from timeit import timeit
>>> timeit(s.appendleft(37),
...
import collections; s = collections.deque(),
...
number=1000000)
0.24434304237365723
>>> timeit(s.insert(0,37), s = [], number=1000000)
612.95199513435364

, ,
. . , ,
.

253

,
dict() :
s = dict(name=GOOG,shares=100,price=490.10)
# s = {name:GOOG, shares:100, price:490.10 }

, (
). , .
>>> timeit(s = {name:GOOG,shares:100,price:490.10})
0.38917303085327148
>>> timeit(s = dict(name=GOOG,shares=100,price=490.10))
0.94420003890991211

,
, .
, , Python, .

,

, , . , ,
.
,
, .
,
,
:
class Stock(object):
def __init__(self,name,shares,price):
self.name = name
self.shares = shares
self.price = price


, . :
>>> from timeit import timeit
>>> timeit(s = Stock(GOOG,100,490.10),from stock import Stock)
1.3166780471801758
>>> timeit(s = {name : GOOG, shares : 100, price : 490.10 })
0.37812089920043945
>>>

3,5 .
:

254

11. , ,
>>> timeit(s.shares*s.price,
...
from stock import Stock; s = Stock(GOOG,100,490.10))
0.29100513458251953
>>> timeit(s[shares]*s[price],
...
s = {name : GOOG, shares : 100, price : 490.10 })
0.23622798919677734
>>>

1,2 . ,
, ,
.
. .

__slots__
, __slots__
. :
class Stock(object):
__slots__ = [name,shares,price]
def __init__(self,name,shares,price):
self.name = name
self.shares = shares
self.price = price

__slots__ , . , , . , __slots__,
(
). ,
.
__slots__, -
, .
__slots__
. . ,
, , __dict__. __slots__, __dict__ , , .

(.)
(.) . ,
x.name x
, x

255

name.
,
.
, ,
,
. , , ,
from math import sqrt,
sqrt(x), , math.sqrt(x). , 1.4 .
, (.),
. , , .



, . :
def parse_header(line):
fields = line.split(:)
if len(fields) != 2:
raise RuntimeError( )
header, value = fields
return header.lower(), value.strip()

, ,
. :
def parse_header(line):
fields = line.split(:)
try:
header, value = fields
return header.lower(), value.strip()
except ValueError:
raise RuntimeError( )

, 10 . try ,
, , if.



.
, , , -

256

11. , ,

,
. :
# 1 :
try:
value = items[key]
except KeyError:
value = None

# 2:
if key in items:
value = items[key]
else:
value = None

,
17 ! , , , items.get(key), , in , .



, -, -, , .
- ,
, . ,
, , ,
map() filter(). , .



. , , , ,
. 6 , , .

II

Python
12.
13. Python
14.
15. ,
16.
17.
18.
19.
20.
21.
22. -
23. -
24.
25.

12


Python.
.
. , Python 2
,
Python 3. ;
.


, .
,
, __builtin__, Python 2, builtins, Python 3. , , __builbuiltins__, .
abs(x)
x.
all(s)
True, s
True.
any(s)
True, s True.
ascii(x)
x, ,
repr(), ASCII.. , -

260

12.

ASCII,, .

, . Python 3.
basestring
, Python 2 (str unicode).
. , isinstance(s,basestring)
True, s .
Python 2.
bin(x)
x.
bool([x])
, True False.
x, True,
x ,
( ,
). False. ,
bool() , False.
bool int, True False ,
1 0.
bytearray([x])
, . , x 0 255, 8- , bytes ,
(
0). bytearray , .
, , a[i],
, i. , a[i] = v, v 8-
. , bytearray ,
(, , find(), split(),
replace() ).
b, , . ,
, , : a.split(b,), a.split(,).
bytearray, .
bytearray , a.decode(encoding).
latin-1 encoding
bytearray 8-
.

261

bytearray(s ,encoding)
bytearray
s, encoding ,
.
bytes([x])
, .
Python 2 str(), 8- . Python 3 bytes
,
bytearray, . x
. ,
: , Python 2 bytes, bytes Python 3.
, a , bytes(),
a[i] Python 2 , Python 3 .
bytes(s, encoding)
bytes s, encoding , . Python 3.
chr(x)
x . Python 2
x 0 <= x <= 255, Python 3
x .
x ,
ValueError.
classmethod(func)
func .
, @classmethod. ,
, . ,
f, Foo,
f Foo, f.
cmp(x, y)
x y , x < y, , x > y, 0, x == y. , ,
(,
).
.
compile(string, filename, kind [, flags [, dont_inherit]])
string
exec() eval(). string

262

12.

Python.. ,
(\n),
(, \r\n Windows). filename
, string. kind
exec ,
eval single
. flags , ( __future__) .
, __fufuture__, . ,
,
flags __future__.division.compiler_flag.
flags , 0,
,
Python. flags, ,
, .
dont_inherit , ,
flags, ,
, .
complex([real [, imag]])
, ,
; real imag . imag ,
. real ,
. imag . , 0j.
delattr(object, attr)
attr object. attr .
, del object.attr.
dict([m]) dict(key1 = value1, key2 = value2, ...)
, . . m ( ), , m. , m ,
dict(m) . m
, , (key, value).
. dict()
. , dict(foo=3,
bar=7) { foo : 3, bar : 7 }.
dir([object])
. object , ,
. object , -

263

.
__dict__ , , .
. ,
,
(, ). , . ,
__dir__(),
.
divmod(a, b)
. (a // b, a % b).
(math.floor(a / b), a % b).
.
enumerate(iter[, initial_value)
iter
( enumerate), , ,
iter. , , iter
a, b, c, enumerate(iter) (0,a), (1,b), (2,c).
eval(expr [, globals [, locals]])
expr. expr , compile(). globals
locals , . ,
. globals locals
,
.
exec(code [, global [, locals]])
Python. code ,
, compile(). globals locals
. ,
.
global local, Python. Python 2 exec , Python 3
. : Python 2 , exec, . Python 3 , exec(). ,

264

12.

locals
Python 3 locals(). locals(),
, .
filter(function, iterable)
Python 2 , iterable, function True. Python 3 ,
. function None, iterable False
, , . iterable
, . ,
( 6).
float([x])
, .
x , . x , . 0.0.
format(value [, format_spec])
value ,
format_spec. value.__format__(), .
<, >
^; ( ); d, f s
, .
, d
, 8d
8 , <8d
8 . format() 3
4 .
frozenset([items])
, ,
, items,
. . .
getattr(object, name [,default])
name object. name
, . default , .
, AttributeError. , object.name.

265

globals()
,
. ,
.
hasattr(object, name)
True, name object.
False. name
.
hash(object)
object ( ).
, . ,
, .
, __hash__(),
.
help([object])
. object , , , , . - ,
.
.
hex(x)
x.
id(object)
object.
- (,
, ).
input([prompt])
Python 2 prompt, eval() (
, eval(raw_input(prompt))). Python 3
prompt ,
- .
int(x [,base])
, . x , , 0. ,
. base

266

12.

. Python 2
, x
32- int.
isinstance(object, classobj)
True, object classobj, classobj, classobj. classobj
. , isinstance(s, (list,tuple)) True, s .
issubclass(class1, class2)
True, class1 ( ) class2 class1 class2. class2
, .
, issubclass(A, A) True.
iter(object [,sentinel])
, object.
sentinel , object
__iter__(), , __getitem__(),
, 0. sentinel object .
object , . , , sentinel, . object
, TypeError.
len(s)
, s. s
, , , . s , , TypeError.
list([items])
, . items
, . items ,
. .
locals()
, . , , .

267

long([x [, base]])
, Python 2.
x ,
0. ,
.
0L. long(). int(x) . , x , :
isinstance(x, numbers.Integral).
map(function, items, ...)
Python 2 function items . Python 3
, . map()
, , , function, ,
.
map() Python 2 Python 3 . Python 2
, ,
None. Python 3 ,
. map() -
( ). , map(function, s)
[function(x) for x in s].
max(s [, args, ...])
s, s . s
. ,
.
min(s [, args, ...])
s, s . s
. ,
.
next(s [, default])
s.
, StopIteration,
default.
.
s.next() s
. Python 3

268

12.

s.__next__().
next(), .
object()
Python.
, .
oct(x)
x.
open(filename [, mode [, bufsize]])
Python 2 filename ( 9 ). mode , : r , w a
. , t b,
( ) . , r rt, ,
rb, .
+, ,
( ). w+
. r+ a+
, , . U rU
.
(\n, \r, \r\n)
\n. mode , rt. bufsize , 0 , 1 ,
.
,
( ).
open(filename [, mode [, bufsize [, encoding [, errors [, newline [, closefd]]]]]])
Python 3 filename .
, Python 2, . encoding ,
utf-8. errors : strict,
ignore, replace, backslashreplace xmlcharrefreplace. newline
None, , \n, \r \r\n.
closefd , ,
close(). Python 2, open() Python 3 , , -. , ,
, -,

269

read() write(), , .
- ,
Python 2 3.
A, Python 3.
ord(c)
c. [0,255].
[0,65535]. Python 3 c
,
.
pow(x, y [, z])
x ** y. z , (x ** y) % z. , , y
.
print(value, ... [, sep=separator, end=ending, file=outfile])
Python 3, . , . sep
- ( ). end
( \n). file
.
Python 2, from __future__
import print_function.
property([fget [,fset [,fdel [,doc]]]])
- . fget , , fset ,
fdel . doc .. , property(fget=getX, doc=some text).
range([start,] stop [, step])
Python 2 ,
start stop. step 1. start ( range() ), 0.
step .1 Python 3 range()
range, ( xrange() Python).

start stop. . .

270

12.

raw_input([prompt])
Python 2, (sys.stdin) . prompt ,
(sys.stdout)
. ,
EOF, EOFError.
readline, ,

. Python 3
input().
repr(object)
object. ,
, eval(). ,
Python 3 , ( ). object ASCII, ascii().
reversed(s)
s .
,
__len__() __getitem__(). , s
0. .
round(x [, n])
x 10 n. n , 0. x
, Python 2 , 0 (, 0.5 1.0, -0.5
-1.0). Python 3 , 0,
, , 0, (, 0.5 0.0, 1.5
2).
set([items])
, items.
items . items
, frozenset.
.
setattr(object, name, value)
object name value.
name .. , object.name = value.

271

slice([start,] stop [, step])


, .
,
a[i:j:k]. 3.
sorted(iterable [, key=keyfunc [, reverse=reverseflag]])

iterable. key , ,
, . reverse , ,
. key
reverse ,
sorted(a,key=get_name).
staticmethod(func)
. @staticmethod.
str([object])
, . Python 2
8- , Python 3
. object,
, __str__().
, print. .
sum(items [,initial])

items. initial ,
0. .
super(type [, object])
, type.
.
:
class B(A):
def foo(self):
super(B,self).foo()

object ,
isinstance(object, type) True. object
, type.
7 -
. Python 3 super() , . type
, , object .

272

12.

,
Python 2, , , .
tuple([items])
, . items
, . items ,
- . .
type(object)
Python.. , object. .. , , ,
, int, float, list . . ,
Python,
, types.
type(name, bases, dict)
type (
).
). name , bases ,
dict , ,
. . 7.
unichr(x)
x, 0 <= x <= 65535,
. Python 2. Python 3 chr(x).
unicode(string [,encoding [,errors]])
Python 2.
. encoding .
, , sys.getdefaultencoding(). errors : strict, ignore, replace, backslashreplace xmlcharxmlcharrefreplace. 9 3.
Python 3.
vars([object])
object (
__dict__). , . , , .
.

273

xrange([start,] stop [, step])


, start
stop, stop .
step .. , , . Python 2
xrange() . Python 3
xrange() range(), xrange()
. start, stop step ,
( 32 ).
zip([s1 [, s2 [,..]]])
Python 2 , n- (s1[n],
s2[n], ...). , . . Python 3
, , . Python 2
zip() . ,
itertools.izip().


exceptions,
.
.



:
BaseException
. .
Exception
, , SystemExit, GeneratorExit KeyboardInterrupt. Exception.
ArithmeticError
,, , OverflowError, ZeroDivisionError FloatingPointError.
LookupError
, , IndexError KeyError.

274

12.

EnvironmentError
,
Python, IOError OSError.
. . , :
try:
#
...
except ArithmeticError as e:
#


- ,
. ,
except. :
except IOError as e:
#
# e IOError

e , .
e.args
, .
.
EnvironmentError 2 3
, , , , .

; , Python.
e.message
, ( Python 2).
e.__cause__
( Python 3).
A.
e.__context__
( Python 3). A.
e.__traceback__
,
( Python 3). A.

275


, :
AssertionError
assert.
AttributeError

.
EOFError
. input() raw_input().
, -,
read() readline() , , .
FloatingPointError
. , ,
, ,
, ,
Python .
, , float(nan) float(inf).
ArithmeticError.
GeneratorExit
- . (
, )
close() . , , .
IOError
-.
IOError errno, strerror filename, errno
, strerror filename
. EnvironmentError.
ImportError
, import
from .
IndentationError
. SyntaxError.
IndexError
.
LookupError.

276

12.

KeyError
. LookupError.
KeyboardInterrupt
, ( Ctrl+C).
MemoryError
, .
NameError
.
NotImplementedError
. ,
,
. RuntimeError.
OSError
.
os. ,
IOError. EnvironmentError.
OverflowError
, .
, . , range
xrange, , 32- . ArithmeticError.
ReferenceError
,
.
weakref.
RuntimeError
, .
StopIteration
, . next() -.
SyntaxError
.
filename, lineno, offset text,
.

277

SystemError
. , .
SystemExit
sys.exit().
.
os._exit().
TabError
. ,
Python -tt.
SyntaxError.
TypeError
, .
UnboundLocalError
.
,
. NameError.
UnicodeError
.
ValueError.
UnicodeEncodeError
.
UnicodeError.
UnicodeDecodeError
.
UnicodeError.
UnicodeTranslateError
. UnicodeError.
ValueError
,
, .
WindowsError

Windows. OSError.
ZeroDivisionError
. ArithmeticError.

278

12.


Python warnings,
,
. , :
import warnings
warnings.warn( MONDO , DeprecationWarning)

, .
. Exception.
Warning
. Exception.
UserWarning
, .
Warning.
DeprecationWarning
. Warning.
SyntaxWarning
.
Warning.
RuntimeWarning
,
.
Warning.
FutureWarning
, . Warning.
, warn() , . ,
- .
warnings -W . - , ,
, , , try except. :

future_builtins

279

try:
import md5
except DeprecationWarning:
pass

, .
, , (
warnings). ,
,
Python.

future_builtins
future_builtins, Python 2, , Python 3.
:
ascii(object)
, repr(). .
filter(function, iterable)
. , itertools.ifilter().
hex(object)
,
__hex__() __index__().
map(function, iterable, ...)
. , itertools.imap().
oct(object)
, __oct__() __index__().
zip(iterable, iterable, ... )
. , itertools.izip().
, builtins. , Python 3
raw_input() input(), xrange() range().

13

Python
, Python.. , , (, ), .

atexit
atexit ,
. :
register(func [,args [,kwargs]])
func , . args ,
func. kwargs . func(*args,**kwargs).
, (, , ). ,
, .

copy
copy
, , , .
copy(x)
x x .
.
list(x), dict(x), set(x) ,

gc

281

x ( ,
, , ,
, copy()).
deepcopy(x [, visit])
x x. visit ,
,
. , deepcopy() , .
, , __copy__(self) __
deepcopy__(self, visit) . __deepcopy__() visit,
, . , __deepcopy__() visit,
deepcopy(), ( ).
__getstate__() __setstate__(), pickle, copy .

, , .

,
, , , , , , .
, copy.error.

gc
gc , , ,
, , .
. , ,
.

, , . ,
, -
. , , ,
, , -

282

13. Python

. .
collect([generation])
.
. generation 02,
,
.
disable()
.
enable()
.
garbage
, , ,
,
__del__().
, , , .
, __del__()
, ,
- , .
get_count()
(count0, count1, count2) .
get_debug()
.
get_objects()
,
. .
get_referrers(obj1, obj2, ...)
,
obj1, obj2 .
, ,
, .
get_referents(obj1, obj2, ...)
, obj1, obj2
. , obj1 , , .
get_threshold()
.

inspect

283

isenabled()
True, .
set_debug(flags)
,, . flags ,
DEBUG_STATS, DEBUG_COLLECTABLE, DEBUG_UNCOLLECTABLE, DEBUG_INSTANCES,
DEBUG_OBJECTS, DEBUG_SAVEALL DEBUG_LEAK. DEBUG_LEAK , , ,
,
.
set_threshold(threshold0 [, threshold1[, threshold2]])
. , 0 ,
2 . , ,
, . 2, . threshold0 ,
,
0. threshold1 ,
0,
1. threshold2 ,
1,
2. (700,10,10). threshold0 0,
.

, __
del__(),
gc.garbage (, ).
- ,
.

get_referrers() get_referents() , . , . .

inspect
inspect , , ,
, .

284

13. Python

cleandoc(doc)
doc,
, ,
.
currentframe()
, .
formatargspec(args [, varags [, varkw [, defaults]]])
, , getargspec().
formatargvalues(args [, varargs [, varkw [, locals]]])
, , getargvalues().
getargspec(func)
func ArgSpec(args,
varargs, varkw, defaults), args
func, varargs , * ( ),
varkw , ** ( ), defaults None, . func
, defaults n args, n
len(defaults).
getargvalues(frame)
,
frame. ArgInfo(args, varargs, varkw, locals), args
, varargs , * ( ), varkw , **
( ), locals .
getclasstree(classes [, unique])
classes , .
, , , . 2 (cls,
bases), cls , bases . unique True,
. , , .
getcomments(object)
, , object

inspect

285

Python.. , , .
None.
getdoc(object)
object.
cleandoc().
getfile(object)
, object.
TypeError, (, ).
getframeinfo(frame [, context])
Traceback(filename, lineno, function, code_
context, index), frame . filename lineno
. context , .
code_context , . index , .
getinnerframes(traceback [, context])
traceback
. 6 (frame, filename, lineno, funcname,
code_context, index). filename, lineno, context, code_context index , , getframeinfo().
getmembers(object [, predicate])
object.
__dict__ ,
, (, __doc__, __name__ ).
(name, value).
predicate , True False. ,
predicate True. predicate , isfunction() isclass().
getmodule(object)
, object ( ).
getmoduleinfo(path)
, Python path. path Python, None. ModuleInfo(name, suffix,
mode, module_type), name , suffix

286

13. Python

, mode module_type , .
imp :

imp.PY_SOURCE

Python

imp.PY_COMPILED

imp.C_EXTENSION

,
C

imp.PKG_DIRECTORY

imp.C_BUILTIN

imp.PY_FROZEN

getmodulename(path)
, path. path Python, None.
getmro(cls)
, cls.
7 - .
getouterframes(frame [, context])

. , .
6 (frame, filename, lineno,
funcname, code_context, index), ,
, getinnerframes(). context
, getframeinfo().
getsourcefile(object)
Python,
object.
getsourcelines(object)
(sourcelines, firstline), object. sourcelines , firstline . , IOError.
getsource(object)
object . , IOError.

inspect

287

isabstract(object)
True, object .
isbuiltin(object)
True, object .
isclass(object)
True, object .
iscode(object)
True, object .
isdatadescriptor(object)
True, object . , : __get__() __set__().
isframe(object)
True, object .
isfunction(object)
True, object .
isgenerator(object)
True, object .
isgeneratorfunction(object)
True, object -. isgenerator() , , , .
, .
ismethod(object)
True, object .
ismethoddescriptor(object)
True, object .. , __get__(), __set__().
ismodule(object)
True, object .
isroutine(object)
True, object .
istraceback(object)
True, object
.

288

13. Python

stack([context])
, .
6 (frame, filename, lineno,
funcname, code_context, index), ,
getinnerframes(). context , .
trace([context])
, . , , . context ,
.

marshal
marshal , Python. marshal pickle shelve,
. ( pickle).
marshal
.
dump(value, file [, version])
value file . value
, ValueError. version ,
. marshal.ver.verversion 2. 0 , Python.
dumps(value [,version])
, dump(). value , ValueError.
version .
load(file)
file . , EOFError, ValueError TypeError.
.
loads(string)
string.

pickle

289

None, , , , , , ,
, , . ,

. , .

, int ,
64 ,
, 32- .

marshal , .

marshal ,
pickle, .

pickle
pickle Python , ,
. -: . Python .
, .
dump(object, file [, protocol ])
object
file. protocol .
0 ( ) ,
Python. 1 ,
Python.
2 , . 3
Python 3 .
protocol , . pickle.HIGHEST_PROTOCOL. , pickle.PicklingError.
dumps(object [, protocol])
, dump(), , .

290

13. Python

,
:
f = open(myfile, wb)
pickle.dump(x, f)
pickle.dump(y, f)
... ...
f.close()

.
load(file)
file
. ,
. , , pickle.UnpicklingError. EOFError.
loads(string)
, load(),
.
,
:
f = open(myfile, rb)
x = pickle.load(f)
y = pickle.load(f)
... ...
f.close()

. .
,
dump() load(),
. ,
load() dump(), .
, dump() load() ,
,
. .
Pickler Unpickler.

pickle

291

Pickler(file [, protocol ])
, file , protocol . p Pickler p.dump(x), x file .
x .1 x
p.dump(), , . p.clear_memo() ,
.
, (,
dump()).
Unpickler(file)
, file .
u Unpickler u.load(), . Unpickler , ,
, Pickler. u.load() .
pickle
Python, :

None

, , , pickle

,
, . ,
,
. ,
, ,
. ,
__init__() . .
, , ,
( ). , __main__, ,
( ,
1

. . .

292

13. Python

__main__ ).
- , pickle.
, __getstate__() __setstate__(). __getstate__() , ( ),
. __setstate__()

. , __dict__ . , , copy .

Python 2 cPickle,
pickle C.. pickle, Pickler Unpickler. Python 3
, C,
( pickle ).

, pickle,
Python -
, XML.

, , marshal pickle, pickle ,



.

- , , , .

pickle , , ,
.
, pickle. , __reduce__() __reduce_ex__(),
pickle .

sys
sys , .

sys

293

.
api_version
C API Python.. .
argv
, .
argv[0] .
builtin_module_names
, Python.
byteorder
, : litlittle , big .
copyright
, .
__displayhook__
displayhook().
dont_write_bytecode
, , Python
- ( .pyc .pyo) . True, -B.
.
dllhandle
Python DLL ( Windows).
__excepthook__
excepthook().
exec_prefix
, Python.
executable
, .
flags
, , Python.
flags
, . .

294

13. Python

flags.debug

-d

flags.py3k_warning

-3

flags.division_warning

-Q

flags.division_new

-Qnew

flags.inspect

-i

flags.interactive

-i

flags.optimize

-O OO

flags.dont_write_bytecode

-B

flags.no_site

-S

flags.ignore_environment

-E

flags.tabcheck

-t -tt

flags.verbose

-v

flags.unicode

-U

float_info
,
.. float.h C.

float_info.epsilon

1.0 1.0.

float_info.dig

,
.

float_info.mant_dig


, float_info.radix.

float_info.max

float_info.max_exp


,
float_info.radix.

float_info.max_10_exp


10.

float_info.min

float_info.min_exp


,
float_info.radix.

295

sys

float_info.min_10_exp


10.

float_info.radix

float_info.rounds

(-1 , 0 , 1 , 2 , 3
).

hexversion
,
, sys.version_info.
.
last_type, last_value, last_traceback
, . last_type ,
last_value , last_trace_tracetraceback . ,
,
sys.exc_info().
maxint
( Python 2).
maxsize
, size_t C .
, , .
maxunicode
, , . 65 535
16- UCS-2. Python
UCS-4, .
modules
, .
path
, . , , ( ).
8 .

296

13. Python

platform
, , linux-i386.
prefix
, Python.
ps1, ps2
,
. ps1 >>> ,
ps2 ... .
str() .
py3kwarning
Python 2 True,
-3.
stdin, stdout, stderr
, , .
stdin raw_input() input(). stdout
print raw_input() input() . stderr
.
, write(),
.
__stdin__, __stdout__, __stderr__
, stdin, stdout stderr, .
tracebacklimit
, .
1000. 0 , .
version
.
version_info
(major, minor, micro, releaselevel, serial). , releaselevel,
alpha, beta, candidate final.
warnoptions
W, .

sys

297

winver
,
Windows.

, sys:
_clear_type_cache()
.
,
1024 . , ,
. ,
, . ,
, .
_current_frames()
, , ,
. (
). , , ,
. ,
, .
displayhook([value])
, .
repr(value)
__builtin__._.
displayhook, .
excepthook(type,value,traceback)
.
type , value
, raise, traceback .

. ( , CGI).
exc_clear()
, .
, .

298

13. Python

exc_info()
(type, value, traceback) ,
. type , value raise,
traceback , .
, None.
exit([n])
, SystemExit. n .. 0 ( );
. n , ,
sys.stderr, 1.
getcheckinterval()
, ,
, .
getdefaultencoding()
, . ,
ascii utf-8. site.
getdlopenflags()
, dlopen(),
C, UNIX.
dl.
getfilesystemencoding()
,
. Windows mbcs, Macintosh OS X utf-8. UNIX
,
CODESET. None, .
_getframe([depth])
. depth
, . ,
. , _getframe(1) . depth ValueError.
getprofile()
,
setprofile().

sys

299

getrecursionlimit()
.
getrefcount(object)
object.
getsizeof(object [, default])
object . __sizeof__() . , TypeError,
default. __sizeof__() ,
, . ,
, .
gettrace()
, settrace().
getwindowsversion()
(major,minor,build,platform,text),
Windows. major . , 4 Windows
NT 4.0, 5 Windows 2000 Windows XP. minor
. , 0 Windows 2000,1 1 Windows XP. build . platform
: 0 (Win32s Windows 3.1),
1 (Windows 95, 98 Me), 2 (Windows NT, 2000, XP) 3 (Windows CE).
text ,
Service Pack 3.
setcheckinterval(n)
Python,, , ,
. 10.
setdefaultencoding(enc)
. enc , ascii utf-8. site. sitecustomize.
setdlopenflags(flags)
,
dlopen(), C,, 1

5. . .

300

13. Python

UNIX.. .
flags , , ,
dl, sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL).
setprofile(pfunc)
,
.
setrecursionlimit(n)
. 1000. ,
, Python
Segmentation
Segmentation Fault
( ) Access
Access Violation
( ).
settrace(tfunc)
, . Python
11.

traceback
traceback .
, sys.exc_info().
, Python .
print_tb(traceback [, limit [, file]])
file limit traceback
. limit , . file , sys.stderr.
print_exception(type, value, traceback [, limit [, file]])
file . type ,
value . limit file ,
print_tb().
print_exc([limit [, file]])
, print_exception(), , sys.exc_info().

types

301

format_exc([limit [, file]])
, ,
print_exc().
print_last([limit [, file]])
, print_exception(sys.last_type, sys.last_value, sys.last_traceback, limit, file).
print_stack([frame [, limit [, file]]])
,
. frame . limit file , print_tb().
extract_tb(traceback [, limit])
, print_tb().
(filename, line, funcname, text), , . limit .
extract_stack([frame [, limit]])
, print_
stack(), frame. frame ,
. limit .
format_list(list)
.
list , extract_tb()
extract_stack().
format_exception_only(type, value)
.
format_exception(type, value, traceback [, limit])

.
format_tb(traceback [, limit])
, format_list(extract_tb(traceback, limit)).
format_stack([frame [, limit]])
, format_list(extract_stack(frame, limit)).
tb_lineno(traceback)
, .

types
types , , , , -

302

13. Python

. , ,
isinstance()
.

BuiltinFunctionType

CodeType

FrameType

FunctionType

lambda-

GeneratorType

GetSetDescriptorType

getset

LambdaType

FunctionType

MemberDescriptorType

MethodType

ModuleType

TracebackType

, ,
. , , , .
, , , 3.
FunctionType(code, globals [, name [, defarags [, closure]]])
.
CodeType(argcount, nlocals, stacksize, flags, codestring, constants, names,
varnames, filename, name, firstlineno, lnotab [, freevars [, cellvars ]])
.
MethodType(function, instance, class)
.
ModuleType(name [, doc])
.

types
, , . Python 2 types , IntType DictType.
int dict.
, Python 3 types
.

303

warnings

warnings
warnings , . , , ,
.
warnings , ,
Python. :
>>> import regex
__main__:1: DeprecationWarning: the regex module is deprecated; use the re
(: regex , re)
module
>>>

, , . , :

Warning

UserWarning

DeprecationWarning

SyntaxWarning

RuntimeWarning

FutureWarning

__builtin__, exceptions. , Exception. .


warn(). :
warnings.warn( X.)
warnings.warn( Y .,
RuntimeWarning)

. ,
. filterwarnings(). :
warnings.filterwarnings(action=ignore,
message=.*regex.*,
category=DeprecationWarning)
import regex
#

304

13. Python

W . :
% python Wignore:the\ regex:DeprecationWarning

warnings :
warn(message[, category[, stacklevel]])
. message , category ( DeprecationWarning), stacklevel
, ,
. category
UserWarning, stacklevel 1.
warn_explicit(message, category, filename, lineno[, module[, registry]])
warn(). message category
, warn(). filename, lineno
module .
registry , .
registry , .
showwarning(message, category, filename, lineno[, file])
message file. file ,
sys.stderr.
formatwarning(message, category, filename, lineno)
,
.
filterwarnings(action[, message[, category[, module[, lineno[, append]]]]])
. action error, ignore, always, default, once module. :

error

ignore

always

default

module

once

, ,

message ,
. category , DeprecationError. module

weakref

305

, . lineno 0,
. append ,
(
). . - ,
, .
resetwarnings()
.
, filterwarnings(), W.

warnings.filters.

, . , DeprecationWarning
DeprecationWarning.

W. :
-Waction:message:category:module:lineno

, filterwarning(). message module


, , .

weakref
weakref .
,
, . , . ,
. , - ,
,
(Observer), ,
. 7.
weakref.ref(),
:
>>> class A: pass
>>> a = A()
>>> ar = weakref.ref(a) # a

306

13. Python
>>> print ar
<weakref at 0x135a24; to instance at 0x12ce0c>


,
. , . None, ,
. :
>>> print ar()
#
<__main__.A instance at 12ce0c>
>>> del a
#
>>> print ar()
# a , None
None
>>>

weakref :
ref(object[, callback])
object.
callback , object. , .
.
callback ,
. object ,
. ,
None. ref() ReferenceType,

.
proxy(object[, callback])
- object. , .
, - . - weakref.ReferenceError, ,
. callback , , ref(). -
ProxyType, CallableProxyType, ,
.
getweakrefcount(object)
-,
object.
getweakrefs(object)
-,
object.

weakref

307

WeakKeyDictionary([dict])
, . ,
. dict , WeakKeyDictionary.
, .
, . ,
__hash__(), .
WeakKeyDictionary , iterkeyrefs()
keyrefs(), .
WeakValueDictionary([dict])
, .
, .
dict ,
WeakValueDictionary. WeakValueDictionary , itervaluvaluerefs() valuerefs(), .
ProxyTypes
(ProxyType, CallableProxyType),
-, proxy(), isinstance(object, ProxyTypes).

. , ,
, . :
_resultcache = { }
def foocache(x):
if resultcache.has_key(x):
r = _resultcache[x]() #
if r is not None: return r
r = foo(x)
_resultcache[x] = weakref.ref(r)
return r

, , , , , , , -

308

13. Python

,
(, ,
). ,
, , , .

WeakKeyDictionary WeakValueDictionary, , ,
, .

, ref() proxy(), ,
.

,
. ,
, , , .

, .
, ,

.

14

, . decimal, .

decimal
float Python ( IEEE 754).
, , 0.1,
0.10000000000000001. ,
,
(, 3 * 0.1 == 0.3 False).
decimal IBM General Decimal
Arithmetic Standard, . ,
,
. ,
. ,
Python,, -.
decimal : Decimal, , Context,
, , . , :

310

14.
import decimal
x = decimal.Decimal(3.4)
y = decimal.Decimal(4.5)

# ,
a = x * y
# a = decimal.Decimal(15.30)
b = x / y
# b = decimal.Decimal(0.7555555555555555555555555556)

#
decimal.getcontext().prec = 3
c = x * y
# c = decimal.Decimal(15.3)
d = x / y
# d = decimal.Decimal(0.756)

#
with decimal.localcontext(decimal.Context(prec=10)):
e = x * y
# e = decimal.Decimal(15.30)
f = x / y
# f = decimal.Decimal(0.7555555556)

Decimal
Decimal :
Decimal([value [, context]])
value ,
, , ,
4.5, (sign, digits, exponent). , 0 sign , 1
; digits
exponent .

Infinity, -Infinity,
(Not a Number, NaN) NaN sNaN. sNaN
,
. float
, (
decimal). context Context, . context , , , NaN.
,
:
a
b
c
d
e

=
=
=
=
=

decimal.Decimal(42)
# Decimal(42)
decimal.Decimal(37.45)
# Decimal(37.45)
decimal.Decimal((1,(2,3,4,5),-2)) # Decimal(-23.45)
decimal.Decimal(Infinity)
decimal.Decimal(NaN)

Decimal
int float. , ,
. -

311

decimal

Decimal Python. ,

. context, ,
. , .

x.exp([context])

e ** d

x.fma(y, z [, context])

x * y + z x*y

x.ln([context])

( e) x

x.log10([context])

x.sqrt([context])

Context
,
, Context:
Context(prec=None, rounding=None, traps=None, flags=None, Emin=None, Emax=
None, capitals=1)
.
. prec , , rounding
, traps
,
(, ). flags
,
(, ). flags . Emin Emax ,, .
.. capitals , , , E
e, . 1 (E).
Context .
getcontext() localcontext(), Context.
.
. , ,
.
rounding,
:

312

14.

ROUND_CEILING

. , 2.52 2.6, 2.58


2.5.

ROUND_DOWN

. , 2.58
2.5, 2.58 -2.5.

ROUND_FLOOR

. , 2.52 2.5, 2.58


2.6.

ROUND_HALF_DOWN

, ,
. , 2.58 2.6, 2.55
2.5, 2.58 2.6.

ROUND_HALF_EVEN

, ROUND_HALF_DOWN,
, , ,
. ,
2.65 2.6, 2.55
2.6.

ROUND_HALF_UP

, ROUND_HALF_DOWN,
,
. ,
2.55 2.6, 2.55 2.6.

ROUND_UP

. , 2.52
2.6, 2.52 2.6.

ROUND_05UP

,
0 5, . , 2.54
2.6, 2.64
2.6.

traps flags Context() . ,


. listed traps
, . . :

Clamped

DivisionByZero

0.

Inexact

InvalidOperation

313

decimal

Overflow

Emax.
Inexact Rounded.

Rounded

. , (, 1.00 1.0).

Subnormal

Emin.

Underflow

.
0. Inexact
Subnormal.

,
. :
try:
x = a/b
except decimal.DivisionByZero:
print

, :
ArithmeticError ( )
DecimalException
Clamped
DivisionByZero
Inexact
Overflow
Underflow
InvalidOperation
Rounded
Overflow
Underflow
Subnormal
Underflow

Overflow Underflow
, (, Underflow
Subnormal). , decimal.DivisionByZero
DivisionByZero.
. , , .
, . :
ctxt = decimal.getcontext() #
x = a + b
if ctxt.flags[Rounded]:
print !

314

14.

,
clear_flags(). .
c Context :
c.capitals
1 0 , , E e,
.
c.Emax
, .
c.Emin
, .
c.prec
, .
c.flags
, , . , c.flags[Rounded] , Rounded.
c.rounding
. ROUND_HALF_EVEN.
c.traps
, True/False, , . , c.traps[DivisionByZero]
True, c.traps[Rounded] False.
c.clear_flags()
( c.flags).
c.copy()
c.
c.create_decimal(value)
Decimal, c. ,
, .


decimal .
getcontext()
.
,
.

decimal

315

localcontext([c])
, c
,
with.
. ,
:
with localcontext() as c:
c.prec = 5

setcontext(c)
c
.
BasicContext
. ROUND_HALF_UP; Emin
-999999999; Emax 999999999; , Inexact, Rounded Subnormal.
DefaultContext
, (
).
28 ; ROUND_HALF_EVEN;
Overflow, InvalidOperation DivisionByZero.
ExtendedContext
. ROUND_HALF_EVEN; Emin
-999999999; Emax 999999999;
. ,
NaN Infinity.
Inf
, Decimal(Infinity).
negInf
, Decimal(-Infinity).
NaN
, Decimal(NaN).

,
:
>>> a = Decimal(42.5)
>>> b = Decimal(37.1)

316

14.
>>> a + b
Decimal(79.6)
>>> a / b
Decimal(1.145552560646900269541778976)
>>> divmod(a,b)
(Decimal(1), Decimal(5.4))
>>> max(a,b)
Decimal(42.5)
>>> c = [Decimal(4.5), Decimal(3), Decimal(1.23e3)]
>>> sum(c)
Decimal(1237.5)
>>> [10*x for x in c]
[Decimal(45.0), Decimal(30), Decimal(1.230e4)]
>>> float(a)
42.5
>>> str(a)
42.5

:
>>> getcontext().prec = 4
>>> a = Decimal(3.4562384105)
>>> a
Decimal(3.4562384105)
>>> b = Decimal(5.6273833)
>>> getcontext().flags[Rounded]
0
>>> a + b
9.084
>>> getcontext().flags[Rounded]
1
>>> a / Decimal(0)
Traceback (most recent call last):
File <stdin>, line 1, in ?
decimal.DivisionByZero: x / 0
>>> getcontext().traps[DivisionByZero] = False
>>> a / Decimal(0)
Decimal(Infinity)

Decimal Context , ,

. ,

.
, : http://docs.python.org/library/
decimal.html.

. , .

fractions

317

, , Decimal(sNaN).
.
, .
, ,
. , , , sNaN .

0 (
Decimal(0) Decimal(-0)). .

, , - , . ,
.


.
. ,
What Every Computer Scientist Should
Know About Floating-Point Arithmetic (
) (David Goldberg),
Computing
Computing Surveys,
, Association for Computing Machinery, 1991 ( ,
).1

IBM
IBM General Decimal Arithmetic Specification
.

fractions
fractions Fraction, .
, :
Fraction([numerator [,denominator]])
. (numerator)
(denominator)
0 1 .

, : http://www.vbstreets.ru/VB/Articles/66541.aspx http://
www.ibm.com/developerworks/ru/library/j-jtp0114/. . .

318

14.

Fraction(fraction)
fraction numbers.Rational,
, fraction.
Fraction(s)
s , , 3/7
-4/7, . s , 1.25, ,
( Fraction(5,4)).
, Fraction :
Fraction.from_float(f)
, f .
Fraction.from_decimal(d)
, d Decimal.
:
>>> f = fractions.Fraction(3,4)
>>> f
Fraction(3, 4)
>>> g = fractions.Fraction(1.75)
>>> g
Fraction(7, 4)
>>> h = fractions.Fraction.from_float(3.1415926)
Fraction(3537118815677477, 1125899906842624)
>>>

f Fraction .
f.numerator f.denominator . , :
f.limit_denominator([max_denominator])
, f. max_denominator
.
1000000.

Fraction ( , ):
>>> f + g
Fraction(5, 2)
>>> f * g
Fraction(21, 16)
>>> h.limit_denominator(10)
Fraction(22, 7)
>>>

319

math

, fractions :
gcd(a, b)
a b. , b, ; a .

math
math .
, (
cmath).
. , .

acos(x)

x.

acosh(x)

x.

asin(x)

x.

asinh(x)

x.

atan(x)

x.

atan2(y,x)

(y / x).

atanh(x)

x.

ceil(x)


x.

copysign(x,y)

x , y.

cos(x)

x.

cosh(x)

x.

degrees(x)

x .

radians(x)

x .

exp(x)

e ** x.

fabs(x)

x.

factorial(x)

x.

floor(x)


x.

fmod(x,y)

x % y, fmod() C.

frexp(x)


x .

320

14.

()

fsum(s)

s. .

hypot(x,y)

, sqrt(x * x + y * y).

isinf(x)

True, x .

isnan(x)

True, x NaN.

ldexp(x,i)

x * (2 ** i).

log(x [, base])

x base. base , .

log10(x)

x.

log1p(x)

x+1.

modf(x)

x .
, x.

pow(x,y)

x ** y.

sin(x)

x.

sinh(x)

x.

sqrt(x)

x.

tan(x)

x.

tanh(x)

x.

trunc(x)

x.

math :

pi

e.

+inf, -inf nan float() , : float(inf),


float(-inf) float(nan).

math.fsum() , sum(), ,
,
. s = [1, 1e100, -1e100].
sum(s), -

numbers

321

0.0 ( 1
1e100). math.sum(s)
1.0. , math.fsum(), Adaptive Precision Floating-Point Arithmetic and Fast
Robust Geometric Predicates (
)
(Jonathan
Jonathan Richard Shewchuk)) -
(Carnegie Mellon University. School of Computer Science Technical Report)
CMU-CS-96-140, 1996.

numbers
numbers , . , .
Number
, .
Complex
, .
real imag.
Number.
Real
, .
Complex.
Rational
, .
numerator denominator. Real.
Integral
, . Rational.
.
. :
if isinstance(x, numbers.Number) # x

if isinstance(x, numbers.Integral) # x

- True, , x , ,
, complex(), float() int().

322

14.

,
. ,
. :
>>> class Foo(numbers.Real): pass
...
>>> f = Foo()
Traceback (most recent call last):
File <stdin>, line 1, in <module>
TypeError: Cant instantiate abstract class Foo with abstract methods
(:
( ):
<stdin>, 1, <>
TypeError: Foo

)
__abs__, __add__, __div__, __eq__, __float__, __floordiv__, __le__, __lt__,
__mod__, __mul__, __neg__, __pos__, __pow__, __radd__, __rdiv__, __rfloordiv__,
__rmod__, __rmul__, __rpow__, __rtruediv__, __truediv__, __trunc__
>>>

7
- .


Python (Python
Enhancement Proposal, PEP) PEP 3141 (http://www.python.org/dev/peps/
pep-3141).

random
random , , , .
random(), , [0.0, 1.0).

:
seed([x])
. None x . , x
, x. x
, , -

random

323


hash(x).
getstate()
, .
setstate(), .
setstate(state)
, getstate().
jumpahead(n)
, random()
n . n
.


.
getrandbits(k)
, k .
randint(a,b)
x a <= x <= b.
randrange(start,stop [,step])
(start,stop,step). stop.


.
choice(seq)
seq.
sample(s, len)
len, s, . ,
.
shuffle(x [,random])
x.
random .

[0.0, 1.0).

324

14.


.
,
.
.
random()
[0.0, 1.0).
uniform(a,b)
. [a, b).
betavariate(alpha, beta)
-. 0 1. alpha > -1
beta > -1.
cunifvariate(mean, arc)
. mean
, arc
. , 0 pi. (mean - arc/2, mean + arc/2).
expovariate(lambd)
. lambd ,
1.0 . [0, +Infinity).
gammavariate(alpha, beta)
-. alpha > -1, beta > 0.
gauss(mu, sigma)
1 mu sigma. , normalvariate().
lognormvariate(mu, sigma)
, , mu sigma.
normalvariate(mu, sigma)
mu sigma.
paretovariate(alpha)
alpha.

. . .

random

325

triangular([low [, high [, mode]]])


[low, high) mode. low 0, high 1.0,
mode low high.
vonmisesvariate(mu, kappa)
,1 mu 0 2 *
pi, kappa .
kappa ,
0 2 * pi.
weibullvariate(alpha, beta)
alpha beta.


.
, .

( ,
) 2**199371.

, , ,
.


random.Random random(), seed(), getstate(), setstate() jumpahead().
Random. .

WichmannHill SystemRandom. , , . WichmannHill


- (Wichmann-Hill),
Wichmann-Hill),
-Hill),
Hill),
), Python. SystemRandom ,
os.urandom().

. . .

15

,

, , ,
, , . , Python.
,
.

abc
abc ,
.
ABCMeta
, . ,
, ABCMeta. :
import abc
class Stackable:
# Python 3
__metaclass__ = abc.ABCMeta # class Stackable(metaclass=abc.ABCMETA)
...

, ,
:

-, @abstractmethod @abstractproperty,
,
, .

abc

327

-, register(subclass),
. , , isinstance(x, AbstractClass)
True, x .

,
__subclasshook__(cls, subclass).
True, subclass cls,
False subclass cls,
NotImplemented, subclass .

abstractmethod(method)
, method . , , , , method.
, register()
.
abstractproperty(fget [, fset [, fdel [, doc]]])
. ,
property(). , , , , .
:
from abc import ABCMeta, abstractmethod, abstractproperty
class Stackable:
# Python 3
__metaclass__ = ABCMeta # class Stackable(metaclass=ABCMeta)
@abstractmethod
def push(self,item):
pass
@abstractmethod
def pop(self):
pass
@abstractproperty
def size(self):
pass

, Stackable:
class Stack(Stackable):
def __init__(self):
self.items = []
def push(self,item):
self.items.append(item)
def pop(self):
return self.items.pop()

328

15. ,

, Stack:
>>> s = Stack()
Traceback (most recent call last):
File <stdin>, line 1, in <module>
TypeError: Cant instantiate abstract class Stack with abstract methods size
(:
( ):
<stdin>, 1, <>
TypeError: Stack
size
)
>>>

,
size() Stack. ,
Stack, :
class CompleteStack(Stack):
@property
def size(self):
return len(self.items)

:
>>> s = CompleteStack()
>>> s.push(foo)
>>> s.size
1
>>>

.
7 - , numbers 14 (. 321) collections ,
(. 332).

array
array array,
, ,
- .
, . 15.1.
15.1.


( )

8-

char

8-

signed char

329

array


( )

8-

unsigned char

PY_UNICODE

2 4

16-

short

16-

unsigned short

int

4 8

unsigned int

4 8

long

4 8

unsigned long

4 8


float

double

( 32- 64- ).
L I, Python 2
, .
:
array(typecode [, initializer])
typecode. initializer , . a array :

a.typecode

, .

a.itemsize

( ).

a.append(x)

x.

a.buffer_info()

(address, length),
,
.

a.byteswap()


, . .

330

15. ,

()

a.count(x)

x a.

a.extend(b)

b a. b
,
a.

a.fromfile(f, n)

n ( ) f . f
. , , , n,
EOFError.

a.fromlist(list)

list a. list .

a.fromstring(s)

s , s ,
fromfile().

a.index(x)

x a. x ValueError.

a.insert(i, x)

x
i.

a.pop([i])

i
. i , .

a.remove(x)

x . x ValueError.

a.reverse()

a.tofile(f)

f. .

a.tolist()

a.tostring()

,
tofile().

a.tounicode()

. ValueError, ,
u.

, , ,
TypeError.
array ,
, . ,
10 , 160 ,
10 40 . -

bisect

331

, , , , .
, . ,
, ,
.
-. :
a = array.array(i, [1,2,3,4,5])
b = array.array(a.typecode, (2*x for x in a)) # a


, , . enumerate() , :
a = array.array(i, [1,2,3,4,5])
for i, x in enumerate(a):
a[i] = 2*x

15 , , -.

, , . , . ,
,
numpy, http://numpy.sourceforge.net/. ,
.
+=. *=.

.
struct (. 366).

bisect
bisect . .

332

15. ,

bisect(list, item [, low [, high]])


list, item,
. low
high .
items , .
bisect_left(list, item [, low [, high]])
list, item,
. low
high .
items , .
bisect_right(list, item [, low [, high]])
, bisect().
insort(list, item [, low [, high]])
item list .
item , .
insort_left(list, item [, low [, high]])
item list .
item ,
.
insort_right(list, item [, low [, high]])
, insort().

collections
collections
,
. .

deque defaultdict
collections : deque defaultdict.
deque([iterable [, maxlen]])
, ( deque
). iterable ,
deque. .
, (O(1)). , -

collections

333

, ,
. maxlen,
deque
. ,
, , .
d deque :
d.append(x)
x d.
d.appendleft(x)
x d.
d.clear()
d.
d.extend(iterable)
d, iterable.
d.extendleft(iterable)
d, iterable. - , , , iterable
d .
d.pop()
d.
, IndexError.
d.popleft()
d. , IndexError.
d.remove(item)
item. ValueError, .
d.rotate(n)
n . n , .
. .
-, , , . , , .
,

334

15. ,

, .
pickle.
defaultdict([default_factory], ...)
, , .. ,, ,, default_factory. ,
.
defaultdict() ,
dict(). defaultdict
, . default_factory , , .
defaultdict . , ,
s. ,
defaultdict:
>>> from collections import defaultdict
>>> s = yeah but no but yeah but no but yeah
>>> words = s.split()
>>> wordlocations = defaultdict(list)
>>> for n, w in enumerate(words):
...
wordlocations[w].append(n)
...
>>> wordlocations
defaultdict(<type list>, {yeah:[0, 4, 8],but: [1, 3, 5, 7],no: [2, 6]})
>>>

wordlocations[w]
, .
KeyError list,
default_factory, .
setdefault(), , , . , ,
, wordlocations.setdefault(w,[]).append(n).
, defaultdict.


. ,
: addr = (hostname, port).
, , : addr[0] addr[1]. ,

collections

335

( ).
collections namedtuple(),
, , .
namedtuple(typename, fieldnames [, verbose])
tuple typename. fieldnames .
Python.
,
, [hostname,port]. ,
fieldnames , hostname
port hostname, port.
, typename. . verbose True,
.
:
>>> from collections import namedtuple
>>> NetworkAddress = namedtuple(NetworkAddress,[hostname,port])
>>> a = NetworkAddress(www.python.org,80)
>>> a.hostname
www.python.org
>>> a.port
80
>>> host, port = a
>>> len(a)
2
>>> type(a)
<class __main__.NetworkAddress>
>>> isinstance(a, tuple)
True
>>>

NetworkAddress,
, , : a.hostname
a.port.
. .
, . , , :
class Stock(object):
def __init__(self,name,shares,price):
self.name = name
self.shares = shares
self.price = price

336

15. ,

:
import collections
Stock = collections.namedtuple(Stock,name shares price)

. , s.name, s.shares .

, (, ,
for, : for name, shares, price
in stockList).
.
, s.shares
, s .

Python, , ,
,
. ,
, .
,
,
. , , , (
, , , , ).
, . ,
,
,
.


collections .
, , , .
. -,
, , . -, . ,
, s , isinstance(s, collections.Sequence).
Container
.
__contains__(), in.

collections

337

Hashable
, -. __hash__().
Iterable
, . __iter__().
Iterator
.
next(), Iterable
__iter__(), .
Sized
, . __len__().
Callable
, ,
. __call__().
Sequence
, . Container, Iterable Sized,
__getitem__() __len__(). ,
__contains__(), __iter__(), __reversed__(), index()
count(), __getgetitem__() __len__().
MutableSequence
. Sequence __setitem__() __delitem__().
, append(), reverse(),
extend(), pop(), remove() __iadd__().
Set
, .
Container, Iterable Sized __len__(),
__iter__() __contains__(). , __le__(), __lt__(), __eq__(), __ne__(),
__gt__(), __ge__(), __and__(), __or__(), __xor__(), __sub__() isdisjoint().
MutableSet
. Set add() discard(). ,
clear(), pop(), remove(), __ior__(), __iand__(), __ixor__
() __isub__().
Mapping
, (). Sized, Iterable Container -

338

15. ,

__getitem__(), __len__() __iter__().


__contains__(), keys(), items(), values(), get(), __eq__()
__ne__().
MutableMapping
. Mapping __setitem__() __delitem__().
, pop(), popitem(),
clear(), update() setdefault().
MappingView
.. , . ,
, , ,
. A PyPython 3.
KeysView
.
MappingView Set.
ItemsView
.
MappingView Set.
ValuesView
(key,
MappingView Set.

item) .

Python
. , ,
, . :
#
if isinstance(x, collections.Sequence):
last = x[-1]

# ,
if isinstance(x, collections.Iterable) and isinstance(x, collections.Sized):
for item in x:

#
if isinstance(x, collections.MutableSet):
x.add(item)

.
7 - .

contextlib

339

contextlib
contextlib
, with.
contextmanager(func)
, -
func. , :
@contextmanager
def foo(args):
statements
try:
yield value
except Exception as e:
error handling (if any)

with foo(args) as value, - , yield. ,


yield, value. with.
with -.
with - , -, .
-,
.
with 5.
nested(mgr1, mgr2, ..., mgrN)
, mgr1, mgr2
, . , with.
with nested(m1,m2) as (x,y): ,
with m1 as x: with m2 as y: . , - , .
closing(object)
, object.close() with. ,
with, object.

functools
functools , .

340

15. ,

partial(function [, *args [, **kwargs]])


partial, , function args, kwargs,
, . args,
kwargs,
(
). partial() , ,
. :
from functools import partial
mybutton = partial(Button, root, fg=black,bg=white,font=times,size=12)
b1 = mybutton(text=Ok)
# Button() text=Ok
b2 = mybutton(text=Cancel) # ,
b3 = mybutton(text=Restart)# partial()

p , partial(),
:

p.func

, p.

p.args

, ,
p.func . .

p.keywords

, , p.func .
.

partial
. , ,
, . ,
partial() ,
, , .
reduce(function, items [, initial])
function items
. function
,
items. ,
items. initial ,
function, items. reduce(), Python 2 .
functools.

heapq

341

update_wrapper(wrapper, wrapped [, assigned [, updated]])


. wrapped - wrapper,
. assigned , (__name__,__module__,__doc__). updated
, , -.
(__dict__,).
wraps(function [, assigned [, updated ]])
, , update_wrapper(). assigned updated .
. :
from functools import wraps
def debug(func):
@wraps(func)
def wrapped(*args,**kwargs):
print( %s % func.__name__)
r = func(*args,**kwargs)
print( %s % func.__name__)
return wrapped

@debug
def add(x,y):
return x+y

.
6 .

heapq
heapq , . ,
, . , heap[n]
<= heap[2*n+1] heap[n] <= heap[2*n+2] n, n = 0.
heap[0] .
heapify(x)
x .
heappop(heap)
heap
, . IndexError,
heap .
heappush(heap, item)
item ,
.

342

15. ,

heappushpop(heap, item)
item . , heappush() heappop().
heapreplace(heap, item)
heap.
item. , . , heappop() heappush(). ,
, .
item.
IndexError, heap .
merge(s1, s2, ...)
,
s1, s2 . , , , .
nlargest(n, iterable [, key])
, n items. . key ,
iterable, .
nsmallest(n, iterable [, key])
, n items. . key ,
.


, .

itertools
itertools , ,
. , for , , - -.
chain(iter1, iter2, ..., iterN)
(iter1,... , iterN) ,
. -

itertools

343

iter1 .
iter2. , iterN.
chain.from_iterable(iterables)
,
iterables ,
.
, :
for it in iterables:
for x in it:
yield x

combinations(iterable, r)
,
r , iterable. , iterable. , iterable [1,2,3,4], combinations([1,2,3,4], 2)
[1,2], [1,3], [1,4], [2,3], [3,4].
count([n])
,
, n. n ,
0. ( ,
.
sys.maxint , -sys.maxint - 1.)
cycle(iterable)
, iterable.
iterable. .
dropwhile(predicate, iterable)
, iterable,
predicate(item) True.
predicate False, iterable.
groupby(iterable [, key])
, iterable, .
. ,
iterable ,
. ,
. key ,
; -

344

15. ,

,
. , , (key, group), key
, group , ,
.
ifilter(predicate, iterable)
,
iterable, predicate(item) True. predicate None, iterable True .
ifilterfalse(predicate, iterable)
,
iterable, predicate(item) False. predicate None, iterable False .
imap(function, iter1, iter2, ..., iterN)
, function(i1,i2, ... iN),
i1, i2,..., iN , iter1, iter2, ...,
iterN . function None,
imap() (i1, i2, ..., iN). , .
islice(iterable, [start,] stop [, step])
, , iterable[start:stop:step]. start ,
stop. step
. , start, stop step . start ,
0. step , 1.
izip(iter1, iter2, ... iterN)
, (i1, i2, ..., iN), i1, i2, ..., iN iter1, iter2, ..., iterN . , - . ,
, , zip().
izip_longest(iter1, iter2, ..., iterN [,fillvalue=None])
, izip(), , , , iter1, iter2 . , ,
None, fillvalue.

itertools

345

permutations(iterable [, r])
, r
, iterable. r , ,
iterable.
product(iter1, iter2, ... iterN, [repeat=1])
, , iter1, iter2 . repeat .
repeat(object [, times])
, object. times .
, .
starmap(func, iterable)
, func(*item), item iterable. ,
iterable , .
takewhile(predicate, iterable)
, iterable, predicate(item) True. , predicate False.
tee(iterable [, n])
n iterable. n .. n 2.
.. , , ,
. iterable tee(). .

,
itertools:
from itertools import *
# 0,1,...,10,9,8,...,1
#
for i in cycle(chain(range(10),range(10,0,-1))):
print i

# a
a = [1,4,5,4,9,1,2,3,4,5,1]

346

15. ,
a.sort()
b = [k for k,g in groupby(a)]

# b = [1,2,3,4,5,9]

# x y
x = [1,2,3,4,5]
y = [10,11,12]
for r in product(x,y):
print(r)
# (1,10),(1,11),(1,12), ... (5,10),(5,11),(5,12)

operator
operator , , 3 . ,
add(3, 4) 3 + 4. , , ,
iadd(x,y), x += y.
, operator, :

add(a, b)

a + b

sub(a, b)

a - b

mul(a, b)

a * b

div(a, b)

a / b ( )

floordiv(a, b)

a // b

truediv(a, b)

a / b ( )

mod(a, b)

a % b

neg(a)

pos(a)

+a

abs(a)

inv(a), invert(a)

lshift(a, b)

a << b

rshift(a, b)

a >> b

and_(a, b)

a & b ( )

or_(a, b)

a | b ( )

xor(a, b)

a ^ b ( )

not_(a)

not a

lt(a, b)

a < b

le(a, b)

a <= b

347

operator
eq(a, b)

a == b

ne(a, b)

a != b

gt(a, b)

a > b

ge(a, b)

a >= b

truth(a)

True, a ,
False

concat(a, b)

a + b

repeat(a, b)

a * b a b

contains(a, b)

a in b

countOf(a, b)

b a

indexOf(a, b)

b a

getitem(a, b)

a[b]

setitem(a, b, c)

a[b] = c

delitem(a, b)

del a[b]

getslice(a, b, c)

a[b:c]

setslice(a, b, c, v)

a[b:c] = v

delslice(a, b, c)

del a[b:c]

is_(a, b)

a is b

is_not(a, b)

a is not b

, , , ,
, . , , ,
lambda. ,
, functools.reduce():
>>> from timeit import timeit
>>> timeit(reduce(operator.add,a),import operator; a = range(100))
12.055853843688965
>>> timeit(reduce(lambda x,y: x+y,a),import operator; a = range(100))
25.012306928634644
>>>

, , operator.add, , lambda x,y: x+y.

348

15. ,

, operator , ,
.
attrgetter(name [, name2 [, ... [, nameN]]])
f, f(obj)
obj.name. ,
f(obj) . , f attrgetter(name,shares), f(obj) (obj.
name, obj.shares). name
. , name address.
hostname, f(obj) obj.address.hostname.
itemgetter(item [, item2 [, ... [, itemN]]])
f, f(obj)
obj[item]. ,
f(obj) (obj[item], obj[item2], ..., obj[itemN]).
methodcaller(name [, *args [, **kwargs]])
f, f(obj)
obj.name(*args, **kwargs).

, , , . , ,
sorted(rows, key=lambda r: r[2])
sorted(rows, key=itemgetter(2)). ,
, lambda.

16


Python,
.
, , .

codecs
codecs , - .
, UTF-8, UTF-16 .

, .
, .

codecs
, utf-8
big5. .
lookup(encoding)
. encoding ,
utf-8. , LookupError. CodecInfo.
c CodecInfo :
c.encode(s [, errors])
, s
(bytes, length_consumed). bytes 8-
-

350

16.

, , .
length_consumed , s,
. errors

strict.
c.decode(bytes [, errors])
, bytes (s, length_consumed). s , length_
consumed bytes, . errors strict.
c.streamreader(bytestream [, errors])
StreamReader,
. bytestream , , .
errors
strict. r StreamReader :

r.read([size [, chars [, firstline]]])

chars
. size
,

,
.
firstline .
, , .

r.readline([size [, keepends]])


. keepends ,
( True).

r.readlines([size [, keepends]])

r.reset()

c.streamwriter(bytestream [, errors])
StreamWriter, . bytestream ,
, .
errors
strict. w -

351

codecs

StreamWriter :

w.write(s)

s.

w.writelines(lines)

lines .

w.reset()

c.incrementalencoder([errors])
IncrementalEncoder, . errors strict. e
IncrementalEncoder :

e.encode(s [,final])

s
. final True
encode().

e.reset()

c.incrementaldecoder([errors])
IncrementalDecoder, . errors strict. d
IncrementalDecoder :

d.decode(bytes [,final])

,
bytes.
final
True decode().

d.reset()

-
codecs , -
.
codecs, .

352

16.

open(filename, mode[, encoding[, errors[, buffering]]])


filename mode / , encoding. errors : strict,
ignore, replace, backslashreplace xmlcharrefreplace.
strict. buffering ,
open().
, mode, . Python 3 codecs.open()
open().
EncodedFile(file, inputenc[, outputenc [, errors]])
,
file, .
inputenc,
outputenc. , , inputenc. outputenc ,, inputenc. errors ,
open(), strict.
iterencode(iterable, encoding [, errors])
-, iterable encoding. errors
strict.
iterdecode(iterable, encoding [, errors])
-, iterable encoding. errors strict.


codecs , , . ,
,
.

BOM


(BOM_BE BOM_LE)

BOM_BE

(big-endian)
(\xfe\xff)

BOM_LE

(little-endian)
(\xff\xfe)

BOM_UTF8

UTF-8 (\xef\xbb\xbf)

353

codecs

BOM_UTF16_BE

(big-endian)
16- UTF-16 (\xfe\xff)

BOM_UTF16_LE

(little-endian)
16- UTF-16 (\xfe\xff)

BOM_UTF32_BE

(big-endian)
32- UTF-32 (\x00\x00\xfe\xff)

BOM_UTF32_LE

(little-endian)
little-endian)
-endian)
endian)) 32- UTF-32 (\xff\xfe\x00\x00)


. ,
, open() lookup(). codecs (http://
docs.python.org/library/codecs).

ascii

7- ASCII

cp437

ASCII MS-DOS

cp1252

ASCII Windows

latin-1, iso-8859-1

ASCII, Latin

utf-16

UTF-16

utf-16-be

UTF-16

utf-16-le

UTF-16

utf-32

UTF-32

utf-32-be

UTF-32

utf-32-le

UTF-32

utf-8

UTF-8

codecs
9 .

, ,
.

encode() decode(). encode()


, decode() . Python 2
, Python 3
. , Python 2 -

354

16.

,
(, bz2). Python 3
, .

re
re . , .
,
.
, , , r(?P<int>\
d+)\.(\d*). .


, :
()

text

text.

,
.

;
.

;
.

*?

;
.

+?

;
.

??

;
.

{m}

m .

{m, n}

m n . m ,
0. n , .

355

re
()

{m, n}?

m n ; .

[...]

, , r[abcdef] r[a-zA-z].
, *, .

[^...]

,
, r[^0-9].

A|B

A, B, A B .

(...)

,
, . group() MatchObject,
.

(?aiLmsux)

a, i, L, m, s, u x
, re.A, re.I, re.L, re.M,
re.S, re.U, re.X, re.compile().
a Python 3.

(?:...)

, .

(?P<name>...)

,
,
name. Python.

(?P=name...)

,
name.

(?#...)

. .

(?=...)

,
. , rHello
(?=World) Hello ,
World.

(?!...)

,

. , rHello (?!World)
Hello , World.

(?<=...)

,

. , r(?<=abc)def
def,
abc.

356

16.

()
()

(?<!...)

,
. , r(?<!abc)def def, abc.

(?(id|name)ypat|npat)

,
id name.
, ypat.
npat. , r(Hello)?(?(1) World|Howdy) Hello World Howdy.

, \n \t,
(,
r\n+
). , , , , . , r\* *.
, , :
()

,
. 1 99,
.

\A

\b


.
- , - .

\B

\d

. , r[0-9].

\D

. ,
r[^0-9].

\s

. ,
r[\t\n\r\f\v].

\S

. ,
r[^\t\n\r\f\v].

\w

- .

357

re
()

\W

, \w.

\Z

\\

\d, \D, \s, \S, \w \W . , . , \d , , , ,


.

, :
compile(str [, flags])
.
pattern , . , . flags
, :

A ASCII

8- ASCII ( Python 3).

I IGNORECASE

L LOCALE

\w, \W, \b
\B .

M MULTILINE

^ $ ,
. ( ^ $
.)

S DOTALL

(.) , .

U UNICODE

\w, \W, \b \B
. ( Python 2.. Python 3 .)

X VERBOSE

358

16.

escape(string)
, - .
findall(pattern, string [,flags])

pattern string, .
, , .
, , .
flags , compile().
finditer(pattern, string, [, flags])
, findall(), .
MatchObject.
match(pattern, string [, flags])
pattern string.
MatchObject,
None. flags , compile().
search(pattern, string [, flags])
string pattern. flags , compile().
MatchObject; ,
None.
split(pattern, string [, maxsplit = 0])
string pattern.
, , ,
. maxsplit . .
sub(pattern, repl, string [, count = 0])
repl
pattern string. repl
. , MatchObject, . repl ,
, \6.

\g<name>. count . .
flags, compile(), , (?iLmsux),
.

re

359

subn(pattern, repl, string [, count = 0])


, sub(), , .


r,
compile(), :
r.flags
flags, , 0, .
r.groupindex
, ,
r(?P<id>), .
r.pattern
, .
r.findall(string [, pos [, endpos]])
findall(). pos endpos .
r.finditer(string [, pos [, endpos]])
finditer(). pos endpos .
r.match(string [, pos] [, endpos])
pattern string. pos endpos .
MatchObject,
None.
r.search(string [, pos] [, endpos])
string pattern. pos endpos .
MatchObject;
, None.
r.split(string [, maxsplit = 0])
split().
r.sub(repl, string [, count = 0])
sub().
r.subn(repl, string [, count = 0])
subn().

360

16.

MatchObject
MatchObject search() match()
, . m MatchObject
:
m.expand(template)
, template. , \1 \2, , \g<n> \g<name>,
. ,
, : r\1
\\1.
m.group([group1, group2, ...])
. . , . ,
, , . , .
, IndexError.
m.groups([default])
, . default , ,
( None).
m.groupdict([default])
, .
default , ,
( None).
m.start([group])
m.end([group])

. group , . , , None.
m.span([group])
(m.start(group), m.end(group)).
group ,
(None, None). group ,
.
m.pos
pos, search() match().

re

361

m.endpos
endpos, search() match().
m.lastindex
. , None .
m.lastgroup
.
, None.
m.re
, match() search()
MatchObject.
m.string
, match() search().

re
, .
import re
text = Guido will be out of the office from 12/15/2012 - 1/3/2013.

#
datepat = re.compile((\d+)/(\d+)/(\d+))

#
for m in datepat.finditer(text):
print(m.group())

#
monthnames = [None,Jan,Feb,Mar,Apr,May,Jun,
Jul,Aug,Sep,Oct,Nov,Dec]
for m in datepat.finditer(text):
print (%s %s, %s % (monthnames[int(m.group(1)], m.group(2), m.group(3)))

# (//)
def fix_date(m):
return %s/%s/%s % (m.group(2),m.group(1),m.group(3))
newtext = datepat.sub(fix_date, text)

#
newtext = datepat.sub(r\2/\1/\3, text)

362

16.

, Mastering
Mastering Regular Expressions (Jeffrey Friedl) (OReilly & Associates, 1997).1

, re, .
, Kodos (http://kodos.sourceforget.net).

string
string ,
. , .

, .

ascii_letters

, ASCII

ascii_lowercase

abcdefghijklmnopqrstuvwxyz

ascii_uppercase

ABCDEFGHIJKLMNOPQRSTUVWXYZ

digits

0123456789

hexdigits

0123456789abcdefABCDEF

letters

lowercase uppercase

lowercase

, ,

octdigits

01234567

punctuation

ASCII

printable

letters,
digits, punctuation whitespace

uppercase

, ,

whitespace

, .
: , , ,
,

, (,
letters uppercase) .

. . 3- . . . .: , 2008. . .

string

363

Formatter
str.format() . 3 4 , ,
. string
Formatter,
. , , .
Formatter()
Formatter. f Formatter
.
f.format(format_string, *args, **kwargs)
format_string. , format_string.format(*args, **kwargs). ,
f.format({name} is {0:d} years old, 39,name=Dave) Dave is 39
years old.
f.vformat(format_string, args, kwargs)
, f.format(). args ,
kwargs .
, ,
.
f.parse(format_string)
format_string.

(literal_text, field_name, format_spec, conversion). literal_text , , { ... }. ,
- . field_name
. ,
{0:d} 0. format_spec
, , d. ,
. conversion ( ).
None, , {0!s:d}, conversion s.
field_name, format_spec conversion None.
f.get_field(fieldname, args, kwargs)
args kwargs , fieldname. fieldname ,
0 name, parse(),

364

16.

, field_name. (value, key), value fieldname, key ,


args kwargs. key ,
args. ,
kwargs. fieldname ,
0.name 0[name]. . key
0.
f.get_value(key, args, kwargs)
args kwargs, key. key , args.
kwargs.
f.check_unused_args(used_args, args, kwargs)
, format().
used_args (
get_field()), .
args kwargs , format(). TypeError, .
f.format_value(value, format_spec)
value
format_spec.
format(value, format_spec).
f.convert_field(value, conversion)
value, get_
field(), conversion.
conversion None,
value. s r, value str() repr() .
, Formatter .
, Formatter, , .
3 4.

Template
string Template, .
9.

string

365

, :
Template(s)
s , Template .
t Template :
t.substitute(m [, **kwargs])
m (, ) t. $$
$, $key ${key}
m[key] kwargs[key], . key Python.. $key, KeyError.
t.safe_substitute(m [, **kwargs])
, substitute(), , . $key .
t.template
, Template().
Template ,
delimiter idpattern. , , $ @,
:
class MyTemplate(string.Template):
delimiter = @
#
idpattern = [A-Z]* #


string
, .
capwords(s)
s, .
maketrans(from, to)
,
from , , to.
from to .
,
translate().

366

16.

struct
struct Python
( ) .
, C,, , .


, . , Struct, .
pack(fmt, v1, v2, ...)
v1, v2 , ,
fmt.
pack_into(fmt, buffer, offset, v1, v2 ...)
v1, v2 , ,
, offset.
, .
array.array bytearray.
unpack(fmt, string)
string
fmt. .
string , calcsize().
unpack_from(fmt, buffer, offset)
buffer fmt, offset.
.
calcsize(fmt)
,
fmt.

Struct
struct Struct, .
,
.
Struct(fmt)
Struct, ,
. s Struct -

367

struct

, , :

s.pack(v1, v2, ...)

s.pack_into(buffer, offset, v1, v2, ...)


s.unpack(bytes)

s.unpack_from(buffer, offset)

s.format

s.size


, struct, , :

Python

char

signed char

unsigned char

_Bool (C99)

short

unsigned short

int

unsigned int

long

unsigned long

long long

unsigned long long

float

double

char[]

char[]

void *

, (, 4i , iiii).

368

16.

s ,
10s 10 . 0s
. p ,
,
.
, Pascal,, Macintosh. ,
255 .
I
L, . , P , .
, :

<

>

()

, ,
.. , , ,
C,, .
. ,
short 2 , int 4 ,
long 4 , float 32 double 64
. P
, .


, .
, . , llh0l , 4- (, long 4- ).
short, h,
. , , -

369

unicodedata

, .
q Q , C,
Python, long long.

.
array (. 328) ctypes (. 759).

unicodedata
unicodedata ,
.
bidirectional(unichr)
unichr , .
:

LRE

, c
,

LRO

, c
,

AL

, c

RLE

,
,

RLO

,
,

PDF

EN

ES

ET

AN

CS

NSM

BN

370

16.

()

WS

ON

category(unichr)
, , unichr. :

Lu

Ll

Lt

, ,

Mn

Mc

Me

Nd

Nl

No

Zs

Zl

Zp

Cc

Cf

Cs

Co

Cn

Lm

Lo

Pc

Pd

Ps

Pe

Pi

371

unicodedata

Pf

Po

Sm

Sc

Sk

So

combining(unichr)
,
unichr 0, .
:

, , ,

(Nukta, )

10-199

200

202

204

208

210

212

214

216

218

220

222

224

226

228

230

372

16.

()

232

233

234

240

( )

decimal(unichr [, default])
unichr.
unichr ,
default ValueError, .
decomposition(unichr)
, unichr
, . ,
, . , decomposition(u\u00fc) ( )
0075 0308, u (). , , :

<font>

(, )

<noBreak>

<initial>

( )

<medial>

( )

<final>

( )

<isolated>

( )

<circle>

<super>

<sub>

<vertical>

<wide>

( ) ( )

<narrow>

( ) ( )

<small>

( )

<square>

<fraction>

<compat>

unicodedata

373

digit(unichr [, default])
unichr. unichr , default
ValueError, . decimal() , , , .
east_asian_width(unichr)
, unichr .
lookup(name)
. , lookup(COPYRIGHT SIGN)
.
http://www.unicode.org/charts.
mirrored(unichr)
1, unichr
, 0. ,
. , ( ,
).
name(unichr [, default])
unichr. ValueError, , default,
. , name(u\xfc) LATIN SMALL
LETTER U WITH DIAERESIS.
normalize(form, unistr)
unistr
form. form NFC, NFKC, NFD NFKD.
. , resum uresum\u00e9 uresume\
u0301.
, e, ().
NFC unistr ,
(,
). NFD unistr
, (, e, ). NFKC NFKD
, NFC NFD, ,

,
. , , -

374

16.

I, V, M . NFKC NFKD
.
numeric(unichr [, default])
. ,
default ValueError. ,
U+2155 (, 1/5) 0.2.
unidata_version
, (, 5.1.0).


: http://www.unicode.org.

17


, Python
, -. ,
, . , MySQL Oracle,
. , ,
, .



Python Da Python , Python
tabase API Specification V2.0, PEP 249 (
http://www.python.org/dev/peps/pep-249/). ( MySQL, Oracle )
, . , .

,
, SQL .
: Connection,
, Cursor, .

connect(parameters), .

376

17.

,
: , , , . ,
dsn, user, password, host database .
connect() , :
connect(dsn=hostname:DBNAME,user=michael,password=peekaboo)

Connection.
c Connection :
c.close()
.
c.commit()
.
, .
, .
c.rollback()
, - . , ,
, . , , , , .
c.cursor()
, Cursor, . ,
SQL . .

- , c, c.cursor()
Cursor. cur Cursor
,
:
cur.callproc(procname [, parameters])
procname. parameters
, .
, parameters. parameters, , -

377

. , ,
fetch*(), .
cur.close()
, -
.
cur.execute(query [, parameters])
query . query , ( SQL),
), parameters ,
, query (
).
cur.executemany(query [, parametersequence])
query. query , parametersquence
.
,
execute(), .
cur.fetchone()
,
execute() executemany(). ,
.
None. ,
, .
cur.fetchmany([size])
(
). size ,
.
cur.arraysize.
.
, .
cur.fetchall()
( ),
.
cur.nextset()

( ).
, None; True, fetch*() .
cur.setinputsize(sizes)
,
execute*(). sizes

378

17.

( )
,
.
,
, . execute*().
cur.setoutputsize(size [, column])
. column
, size .
, , , ,
BLOB LONG, , execute*().
column , size .
.
cur.arraysize
, fetchmany()
.
, , .
cur.description

. (name, type_code, display_size,
internal_size, precision, scale, null_ok).
. type_code ,
. None,
.
cur.rowcount
,
execute*(). -1 ,
,
.
,
Cursor . , , execute*(), , for row in cur:.
, ,
sqlite3
, :

379

import sqlite3
conn = sqlite3.connect(dbfile)
cur = conn.cursor()

#
cur.execute(select name, shares, price from portfolio where account=12345)

#
while True:
row = cur.fetchone()
if not row: break
#
name, shares, price = row
...

# ( )
cur.execute(select name, shares, price from portfolio where account=12345)
for name, shares, price in cur:
#
...



SQL,, execute*() .
, . ,
:
symbol = AIG
account = 12345

cur.execute(select shares from portfolio where name=%s and account=%d %


(symbol, account))

, , .
SQL, ,
. ,
symbol EVIL LAUGH; drop table portfolio;-- , , , .
. , , , , :
symbol = AIG
account = 12345

cur.execute(select shares from portfolio where name=? and account=?,


(symbol, account))

380

17.

?
(symbol, account).
,
. paramstyle,
, . :

qmark

,
? . : cur.execute(... where name=?
and account=?, (symbol, account)).
.

numeric

, :n n. : cur.execute(...
where name=:0 and account=:1,(symbol, account)).

named

, :name
. . :
cur.execute(... where name=:symbol and account=:account,
{symbol:symbol, account: account}).

format

printf,
, %s, %d . : cur.execute(... where name=%s and account=%d, (symbol, account)).

pyformat

Python,
%(name)s. named.
,
.


,
, , . ,
, , , .
,
-, .
Date(year, month, day)
, .
Time(hour, minute, second)
, .

381

Timestamp(year, month, day, hour, minute, second)


, .
DateFromTicks(ticks)
, , .
ticks ,
, time.time().
TimeFromTicks(ticks)
, , .
TimestampFromTicks(ticks)
, , .
Binary(s)
, , s.
. ,
type_code cur.description, .

STRING

BINARY

, BLOB

NUMBER

DATETIME

ROWID


Error
, .
, , :

InterfaceError

, , .

DatabaseError

, .

DataError

,, .. , ,
.

OperationalError

,, .. , .

382

17.

()

IntegrityError

, .

InternalError

. ,
.

ProgrammingError

SQL.

NotSupportedError

,
.

Warning,
, , .

, ,
.
, .
threadsafety
, . :
0

. - .

, .

,
.

, .


,
, . , , , , ,

.
, -. :

sqlite3

383

def generate_dicts(cur):
import itertools
fieldnames = [d[0].lower() for d in cur.description ]
while True:
rows = cur.fetchmany()
if not row: return
for row in rows:
yield dict(itertools.izip(fieldnames,row))

#
cur.execute(select name, shares, price from portfolio)
for r in generate_dicts(cur):
print r[name],r[shares],r[price]

, , .
,
.


,
,
.
PEP-249.
-249. , .

sqlite3
sqlite3 SQLite
(http://www.sqlite.org). SQLite C,, ,
. ,
. ,
,
( ,
). ,
, ( ),
,
.
,
, ,
. , ,
sqlite3.

384

17.


sqlite3 :
connect(database [, timeout [, isolation_level [, detect_types]]])
SQLite. database
, . :memory:;
( , , Python,
). timeout
-,
.
5 . SQL, INSERT UPDATE, , . isolation_
level SQL BEGIN, .
: ( ), DEFERRED, EXCLUSIVE IMMEDIATE. :

( )

DEFFERED.

DEFFERED

,

.

EXCLUSIVE

,
,
.

IMMEDIATE

, , .
.

detect_types
(
SQL) . 0
( ).
, PARSE_DECLTYPES PARSE_COLNAMES
. PARSE_DECLTYPES,
, integer
number(8),
. PARSE_COLNAMES,
colname [typename] (-

sqlite3

385

), colname , typename , register_converter(), .


SQLite
colname, , . , , select price as price [decimal] from portfolio, select price as price from portfolio,
decimal.
register_converter(typename, func)

detect_types connect(). typename
, ,
, func ,
Python.
, sqlite3.register_converter(decimal,
3.register_converter(decimal,
register_converter(decimal,
_converter(decimal,
converter(decimal,
(decimal,
decimal,
, decimal.Dec.DecDecimal), , Decimal, : select price as price [decimal]
from stocks.
register_adapter(type, func)
type Python,

. func ,
type int, float, UTF-8,
-8, . , Decimal, sqlite3.register_adapter(decimal.Decimal,float).
complete_statement(s)
True, s SQL, .
,
.
enable_callback_tracebacks(flag)
, , , register_converter() register_adapter().
. flag
True, sys.stderr.

Connection
c Connection connect() , ,
.
, sqlite3.

386

17.

c.create_function(name, num_params, func)



SQL. name ,
num_params , , func Python, .
:
def toupper(s):
return s.upper()
c.create_function(toupper,1,toupper)

#
c.execute(select toupper(name),foo,bar from sometable)

, Python,, int, float, str, unicode, buffer None.


c.create_aggregate(name, num_params, aggregate_class)

SQL. name ,
num_params , . aggregate_class , . step(params),
, num_params,
finalize(), .
:
class Averager(object):
def __init__(self):
self.total = 0.0
self.count = 0
def step(self,value):
self.total += value
self.count += 1
def finalize(self):
return self.total / self.count

c.create_aggregate(myavg,1,Averager)

#
c.execute(select myavg(num) from sometable)

step() finalize() .
c.create_collation(name, func)
SQL. name
, func ,
-1, 0 1, ,

sqlite3

387

, . SQL,
select * from table order by colname collate name.
c.execute(sql [, params])

c.cursor() execute() . sql SQL, params .
c.executemany(sql [, params])

c.cursor() executemany() .
sql SQL, params
.
c.executescript(sql)

c.cursor() executescript() .
sql SQL.
c.interrupt()
, .
.
c.iterdump()
,
SQL,, . , ,
.
c.set_authorizer(auth_callback)
, .
, : auth_callback(code, arg1, arg2, dbname, innername).
, , : SQLITE_OK , SQLITE_DENY , SQLITE_IGNORE Null. code .
arg1 arg2 , . dbname
( main), innername , , None,
.

arg1 arg2:

388

17.

arg1

arg2

SQLITE_CREATE_INDEX

SQLITE_CREATE_TABLE

None

SQLITE_CREATE_TEMP_INDEX

SQLITE_CREATE_TEMP_TABLE

None

SQLITE_CREATE_TEMP_TRIGGER

SQLITE_CREATE_TEMP_VIEW

None

SQLITE_CREATE_TRIGGER

SQLITE_CREATE_VIEW

None

SQLITE_DELETE

None

SQLITE_DROP_INDEX

SQLITE_DROP_TABLE

None

SQLITE_DROP_TEMP_INDEX

SQLITE_DROP_TEMP_TABLE

None

SQLITE_DROP_TEMP_TRIGGER

SQLITE_DROP_TEMP_VIEW

None

SQLITE_DROP_TRIGGER

SQLITE_DROP_VIEW

None

SQLITE_INSERT

None

SQLITE_PRAGMA

None

SQLITE_READ

SQLITE_SELECT

None

None

SQLITE_TRANSACTION

None

None

SQLITE_UPDATE

SQLITE_ATTACH

None

SQLITE_DETACH

None

SQLITE_ALTER_TABLE

SQLITE_REINDEX

None

SQLITE_ANALYZE

None

SQLITE_CREATE_VTABLE

SQLITE_DROP_VTABLE

SQLITE_FUNCTION

None

sqlite3

389

c.set_progress_handler(handler, n)
,
n- SQLite.. handler , .
.
c.row_factory
, ,
.
: ,
, .
c.text_factory
, ,
. UTF-8.
-8. - . .
c.total_changes
, .
,
, . :
conn = sqlite.connect(somedb)
with conn:
conn.execute(insert into sometable values (?,?), (foo,bar)))

with,
, commit().
- ,
rollback(), .


sqlite3, , cursor() . execute(),
executemany() executescript() SQL.
. , .
, , , SQL , .

390

17.


, :
import sqlite3
conn = sqlite3.connect(mydb)
cur = conn.cursor()
cur.execute(create table stocks (symbol text, shares integer, price real))
conn.commit()


SQLite: text, integer, real blob. blob
, text UTF-8.


,
:
import sqlite3
conn = sqlite3.connect(mydb)
cur = conn.cursor()
cur.execute(insert into stocks values (?,?,?),(IBM,50,91.10))
cur.execute(insert into stocks values (?,?,?),(AAPL,100,123.45))
conn.commit()


?, . ? , .
executemany(), :
stocks = [ (GOOG,75,380.13),
(AA,60,14.20),
(AIG,125, 0.99) ]
cur.executemany(insert into stocks values (?,?,?),stocks)


, :
cur.execute(update stocks set shares=? where symbol=?,(50,IBM))

,
SQL ? .


, :
cur.execute(delete from stocks where symbol=?,(SCOX,))

DBM

391


, :
#
for row in cur.execute(select * from stocks):

#
for shares, price in cur.execute(select shares,price from stocks):

# ,
for row in cur.execute(select * from stocks where symbol=?,(IBM,))

# , ,
for row in cur.execute(select * from stocks order by shares):

# , ,
#
for row in cur.execute(select * from stocks order by shares desc):

# (symbol)
for row in cur.execute(select s.symbol, s.shares, p.price
from stocks as s, prices as p using(symbol)):

DBM
Python ,
UNIX DBM.. . UNIX DBM dbm.
gdbm GNU dbm (http://www.
gnu.org/software/gdbm). dbhash
, Berkeley DB (http://www.oracle.
com/database/berkeley-db/index.html). dumbdbm, Python,
DBM.
, , .
, Python, ,
.
open().
open(filename [, flag [, mode]])
filename
. flag r -

392

17.

, w , c , , n,
. mode
, , ( UNIX 0666).
, open(), :

d[key] = value

value

value = d[key]

del d[key]

d.close()

key in d

key

d.sync()

(
).
,
DBM,, , . , dbm gdbm Windows. Python -
DBM .
Python anydbm, DBM.. open(), ,
. DBM
( dbhash, ). dumbdbm,
.
whichdb,
whichdb(filename). DBM,
.
, , , . , DBM , ,
, Python ,
DBM..

393

shelve

, Python ,
(
sqlite3).

shelve
shelve ,
. , , ,
, -,
dbhash, dbm gdbm. , , . , pickle. shelve.open().
open(filename [,flag=c [, protocol [, writeback]]])
. , .
filename .
flag , DBM, :
r, w, c n. protocol , , .
, pickle. writeback . True, , ,
.
False. .
:

d[key] = data

key.
.

data = d[key]

key.

del d[key]

key.

d.has_key(key)

key .

d.keys()

d.close()

d.sync()

. , , pickle.

394

17.

Shelf(dict [, protocol [, writeback]])


-,
dict.
, dict. protocol writeback , shelve.open().
shelve anydbm DBM. Python
dbhash,
Berkeley DB.

18


Python,
. ,
gzip bzip2, , zip
tar,, (, , , ,
). 19 . ,
, XML HTML, 24
.

bz2
bz2 , bzip2.
BZ2File(filename [, mode [, buffering [, compresslevel]]])
.bz2 filename , . mode r,
, w, .
, ,
mode rU. buffering
. 0 ( ). compresslevel 1 9. 9 ( )
, . , , close(), read(), readline(), readlines(), seek(), tell(), write() writelines().

396

18.

BZ2Compressor([compresslevel])
, . compresslevel , 1 9 ( ).
c BZ2Compressor :
c.compress(data)
c.
, . ,
,
compress(). ,
, ,
, flush().
c.flush()
, .
compress() .
BZ2Decompressor()
, .
d BZ2Decompressor :
d.decompress(data)
data,
. , .

, .
EOFError.
compress(data [, compresslevel])
data.
compresslevel 1 9 ( ).
decompress(data)
,
data.

filecmp
filecmp ,
:
cmp(file1, file2 [, shallow])
file1 file2 True, , False. -

filecmp

397

, os.stat() . shallow False, .


cmpfiles(dir1, dir2, common [, shallow])
, common, dir1
dir2. (match, mismatch,
errors). match , mismatch
errors ,
- . shallow
, cmp().
dircmp(dir1, dir2 [, ignore [, hide]])
,
dir1 dir2.
ignore ,
,
[RCS,CVS,tags]. hide ; [os.curdir, os.pardir] ([., ..]
UNIX).
d, dircmp(), :
d.report()
dir1 dir2 sys.stdout.
d.report_partial_closure()
dir1 dir2,
. sys.stdout.
d.report_full_closure()
dir1 dir2, , . sys.stdout.
d.left_list
dir1.
hide ignore.
d.right_list
dir2.
hide ignore.
d.common
, dir1
dir2.
d.left_only
, dir1.
d.right_only
, dir2.

398

18.

d.common_dirs
, dir1 dir2.
d.common_files
, dir1 dir2.
d.common_funny
dir1 dir2 ,

os.stat().
d.same_files
dir1 dir2.
d.diff_files
dir1 dir2.
d.funny_files
, dir1 dir2, - (, - ).
d.subdirs
, d.common_dirs
dircmp.

dircmp ,
. , , .

fnmatch
fnmatch , UNIX.
, , , glob.
:
()

[seq]

seq

[!seq]

, seq

glob

399

:
fnmatch(filename, pattern)
True False, ,
filename pattern.
( , Windows, ).
fnmatchcase(filename, pattern)
filename pattern c .
filter(names, pattern)
fnmatch() ,
names, , pattern.

fnmatch(foo.gif, *.gif)
# True
fnmatch(part37.html, part3[0-5].html) # False

#
# os.walk(), fnmatch
def findall(topdir, pattern):
for path, files, dirs in os.walk(topdir):
for name in files:
if fnmatch.fnmatch(name,pattern):
yield os.path.join(path,name)

# .py
for pyfile in findall(.,*.py):
print pyfile

glob
glob , ,
UNIX ( fnmatch).
glob(pattern)
, pattern.
iglob(pattern)
, glob(), .

htmlfile = glob(*.html)
imgfiles = glob(image[0-5]*.gif)

400

18.

() , ;
.
, os.path.expanduser() os.path.expandvars()
, glob().

gzip
gzip GzipFile,
, GNU gzip.
GzipFile ,
, .
GzipFile([filename [, mode [, compresslevel [, fileobj]]]])
GzipFile . filename , mode : r, rb, a, ab,
w wb. rb. compresslevel 1 9, .
1
; 9 ( )
. fileobj .
, filename.
open(filename [, mode [, compresslevel]])
GzipFile(filename, mode, compresslevel).
mode rb, compresslevel
9.

close() GzipFile , fileobj. .

, UNIX compress, .

zlib.

shutil
shutil
, , . . , ,
, .
, ,
(, , ).

shutil

401

copy(src,dst)
src dst, .
src dst .
copy2(src, dst)
copy(),
.
copyfile(src, dst)
src dst. src dst
.
copyfileobj(f1, f2 [, length])
f1
f2. length .. length ,
, ( , ).
copymode(src, dst)
src dst.
copystat(src, dst)
, src dst. dst,
.
copytree(src, dst, symlinks [,ignore]])
src. dst ( ). copy2(). symlinks
,
.
symlinks , ,
. ignore , .

, .
- ,
Error. (srcname,
dstname, exception), .
ignore_patterns(pattern1, pattern2, ...)
, , pattern1, pattern2 . .
, .
, -

402

18.

. ,
ignore copytree().
os.walk().
move(src, dst)
src dst. src , .
rmtree(path [, ignore_errors [, onerror]])
. ignore_errors , , , .
, onerror.
(func, path excinfo), func ,
(os.remove() os.rmdir()), path , , excinfo , sys.exc_info(). onerror ,
.

tarfile
tarfile tar. ,
tar.
is_tarfile(name)
True, name tar,
.
open([name [, mode [, fileobj [, bufsize]]]])
TarFile. name
, mode , tar. mode filemode[:compression].
:

.. , . .

r:

r:gz

gzip.

r:bz2

bzip2.

a, a:

w, w:

w:gz

gzip.

w:bz2

bzip2.

403

tarfile

TarFile, ( ), :

r|

r|gz

gzip

r|bz2

bzip2

w|

w|gz

gzip

w|bz2

bzip2

fileobj, .
name fileobj.
bufsize tar.. 20512 .
t TarFile, open(),
:
t.add(name [, arcname [, recursive]])
tar. name (, ).
arcname .
recursive ,, ,, .. True.
t.addfile(tarinfo [, fileobj])
tar. tarinfo
TarInfo .. fileobj , . size tarinfo.
t.close()
tar ,
.
t.debug
, .
0, ,
3 . sys.stderr.
t.dereference
True,
,

404

18.

. False, .
t.errorlevel
,
. 0, . 1, OSError IOError.
2, TarError.
t.extract(member [, path])
member .
member ,
TarInfo. path , .
t.extractfile(member)
member , , read(), readline(), readlines(), seek() tell().
member ,
TarInfo. member , , .
t.getmember(name)
name
TarInfo . ,
KeyError. name, (, , ).
t.getmembers()
TarInfo .
t.getnames()
.
t.gettarinfo([name [, arcname [, fileobj]]])
TarInfo, name
fileobj. arcname .. TarInfo
, add().
t.ignore_zeros
True,
. False ( ), , .
True
.

405

tarfile

t.list([verbose])
sys.stdout. verbose
. False,
. ( ).
t.next()
. TarInfo None.
t.posix
True, tar-
- POSIX 1003.1-1990.
( 256 , 8 ). False, GNU ,
. False.
TarInfo.
ti TarInfo.

ti.gid

ti.gname

ti.isblk()

True,

ti.ischr()

True,

ti.isdev()

True, (, FIFO)

ti.isdir()

True,

ti.isfifo()

True, FIFO

ti.isfile()

True,

ti.islnk()

True,

ti.isreg()

, isfile()

ti.issym()

True,

ti.islinkname

ti.mode

ti.mtime

ti.name

ti.size

406

18.

()

ti.type

, : REGTYPE, AREGTYPE,
LNKTYPE, SYMTYPE, DIRTYPE, FIFOTYPE, CONTTYPE, CHRTYPE, BLKTYPE
GNUTYPE_SPARSE

ti.uid

ti.uname

tarfile :
TarError
.
ReadError
tar-
- (, , tar).
CompressionError
, .
StreamError
,
TarFile (, ,
).
ExtractError

( errorlevel 2).

# tar
t = tarfile.open(foo.tar,w)
t.add(README)
import glob
for pyfile in glob.glob(*.py):
t.add(pyfile)
t.close()

# tar
t = tarfile.open(foo.tar)
for f in t:
print(%s %d % (f.name, f.size))

# tar
# README
t = tarfile.open(foo.tar)
for f in t:

tempfile

407

if os.path.basename(f.name) == README:
data = t.extractfile(f).read()
print(**** %s **** % f.name)

tempfile
tempfile .
mkdtemp([suffix [,prefix [, dir]]])
,
, .
suffix , , prefix ,
, dir ,
.
mkstemp([suffix [,prefix [, dir [,text]]]])
(fd, pathname), fd , os.open(), pathname . suffix , , prefix , ,
dir , ,
text , ,
( ).
, ( ), O_EXCL
os.open().
mktemp([suffix [, prefix [,dir]]])
. suffix
, , prefix ,
, dir , . .

, , . , mkstemp().
gettempdir()
,
.
gettempprefix()
,
.
.

408

18.

TemporaryFile([mode [, bufsize [, suffix [,prefix [, dir]]]]])


mkstemp() , , ,
. mode
; w+b. bufsize
,
open(). suffix, prefix dir , mkstemp(). , ,
, file.
, , .
NamedTemporaryFile([mode [, bufsize [, suffix [, prefix [, dir [, delete ]]]]]])
, TemporaryFile(), , . name . ,

, . delete
True ( ), , .
SpooledTemporaryFile([max_size [, mode [, bufsize [, suffix [, prefix [, dir]]]]]])
, TemporaryFile(), ,
, max_size.
, StringIO,
. - - , fileno(),
, ,
, TemporaryFile. , , SpooledTemporaryFile(),
rollover(),
.
. .
.

tempdir

, , mktemp().

template

,
mktemp().
template,
.

zipfile

409

tempfile . , UNIX
: /tmp, /var/tmp /usr/tmp. Windows
: C:\TEMP, C:\TMP, \TEMP \TMP.
,
TMPDIR, TEMP TMP. -
, .

zipfile
zipfile zip ( , PKZIP;
). Zip-
- Python, . , zip-
Python sys.path,
, ,
import (
zipimport, ).
, .egg (
setuptools), zip- (
.egg zip-
).
, zipfile:
is_zipfile(filename)
, filename zip-.
-. True, filename zip-, False
.
ZipFile(filename [, mode [, compression [,allowZip64]]])
zip- filename ZipFile.
mode r,
, w
, a . a, filename zip-, . filename
zip-, .
compression , , : ZIP_STORED
ZIP_DEFLATED. ZIP_STORED. allowZip64
ZIP64,
64, zip-,
-, 2 . False.

410

18.

PyZipFile(filename [, mode[, compression [,allowZip64]]])


zip-, ZipFile(),
PyZipFile, writepy(),

Python.
ZipInfo([filename [, date_time]])
ZipInfo . ,
z.writestr() ZipFile ( ). filename date_time
filename date_time, .
z ZipFile PyZipFile
:
z.close()
.. , zip- .
z.debug
0 (
) 3 ( ).
z.extract(name [, path [, pwd ]])
.
name , , ZipInfo. path ,
, , pwd , .
z.extractall([path [members [, pwd]]])
. path
, pwd . members
, , namelist() ( ).
z.getinfo(name)
name ZipInfo ( ).
z.infolist()
ZipInfo .
z.namelist()
.
z.open(name [, mode [, pwd]])
name ,
, . name -

zipfile

411

ZipInfo,
. mode ,
,
, r, rU U. pwd ,
. read(), readline()
readlines(),
for.
z.printdir()
sys.stdout.
z.read(name [,pwd])
name . name ZipInfo,
. pwd , .
z.setpassword(pwd)
,
.
z.testzip()
, , .
None,
.
z.write(filename[, arcname[, compress_type]])
filename arcname. compress_type
ZIP_STORED ZIP_DEFLATED. ,
ZipFile() PyZipFile().
w a.
z.writepy(pathname)
PyZipFile Python
( *.py).
Python .
pathname ,
.py.
.pyo, .pyc .py ( ). pathname , Python,
.pyo, .pyc .py,
. ,
, ,
. - , .

412

18.

z.writestr(arcinfo, s)
s zip-. arcinfo
, ,
ZipInfo, , .
i ZipInfo, ZipInfo() z.getinfo() z.infolist(), :

i.filename

i.date_time

(year,month,day,hours,minutes,seconds),
. month day
112 131 . 0.

i.compress_type

.
ZIP_STORED ZIP_DEFLATED.

i.comment

i.extra

, .
, , .

i.create_system

, ,
. : 0 (MS-DOS FAT), 3
(UNIX), 7 (Macintosh) 10 (Windows NTFS).

i.create_version

PKZIP,
.

i.extract_version

, .

i.reserved

. 0.

i.flag_bits

zip-, ,
.

i.volume

i.internal_attr

.
1, ASCII. .

i.external_attr

,
.

i.header_offset

i.file_offset

i.CRC

CRC .

i.compress_size

i.file_size

zlib

413

zip-
- PKZIP, http://www.pkware.com/appnote.html.

zlib
zlib , zlib.
adler32(string [, value])
Adler-32 string. value (, ). .
compress(string [, level])
string. level
1 9, , 1
( ), 9 ( ) . 6.
,
- .
compressobj([level])
. level ,
compress().
crc32(string [, value])
CRC string.
value, , ,
. .
decompress(string [, wbits [, buffsize]])
string. wbits
, buffsize . - , .
decompressobj([wbits])
, . wbits .
c :
c.compress(string)
string. ,
string.

c.compress(). .

414

18.

c.flush([mode])
.
mode Z_SYNC_FLUSH, Z_FULL_FLUSH
Z_FINISH ( ). Z_SYNC_FLUSH Z_FULL_FLUSH
. Z_FINISH .
d, , :
d.decompress(string [,max_length])
string ,
string. c.decompress().
. max_length .
d.unconsumed_tail.
d.flush()

. ,
, .
d.unconsumed_tail
, ,
decompress(). , , - .

decompress().
d.unused_data
.

zlib http://www.zlib.net.

19


, ,
, -,
. ,
, ,
,
. 18
, . , ,
.
Python,, , POSIX.
POSIX ,
. UNIX POSIX, , Windows, .
,
. UNIX Linux Mac OS X. Windows Windows, .
, , . ,
, , , The C Programming Language .
(Brian W. Kernighan) . (Dennis M. Ritchie) (Prentice Hall,
1989).1
1

., . C, 2- . . .
, 2008.

416

19.

Advanced Programming in the UNIX Environment . (W. Richard Stevens) (Stephen Rago) (Addison Wesley,
2005).1 . ,
, , ,
, .

commands
commands ,
. UNIX.
, , (`)
UNIX. , x = commands.getoutput(ls l) x=`ls l`.
getoutput(cmd)
cmd ,
.
getstatusoutput(cmd)
getoutput() , (status, output), status , os.wait(), output ,
getoutput().

Python 2. Python 3 subprocess.



,
subprocess,
.

.
subprocess (. 503).

ConfigParser configparser
ConfigParser ( Python 3 configparser)
Windows INI.
1

., . UNIX. , 2- . . . .: -, 2007.

ConfigParser configparser

417

, , :
#
;
[section1]
name1 = value1
name2 = value2

[section2]
;
name1: value1
name2: value2
...

ConfigParser
ConfigParser :
ConfigParser([defaults [, dict_type]])
ConfigParser. defaults ,
,
%(key)s, key defaults. dict_type
, .
dict ( ).
c ConfigParser :
c.add_section(section)
.
section .
c.defaults()
.
c.get(section, option [, raw [, vars]])
option section .
,
, %(option)s. option
, ConfigParser defaults.
raw , ,
option .
vars
%.
c.getboolean(section, option)
option section,
. , 0, true,

418

19.

yes, no, on off, . ( c.get()).


c.getfloat(section, option)
option section,
,
.
c.getint(section, option)
option section,
, .
c.has_option(section, option)
True, section option.
c.has_section(section)
True, section.
c.items(section [, raw [, vars]])
(option, value) section. raw
, ,
True. vars %.
c.options(section)
section.
c.optionxform(option)
option ,
.
.
c.read(filenames)
filenames .
filenames , , ,
. - ,
. ,
.
.
c.readfp(fp [, filename])
fp, , . filename ,
fp ( ). fp.name <???>,
.
c.remove_option(section, option)
option section.

ConfigParser configparser

419

c.remove_section(section)
section.
c.sections()
.
c.set(section, option, value)
option, section,
value. value .
c.write(file)
, ,
file. file ,
.

ConfigParser , ,
.
, , , . ,
optparse.
, Python.
,
ConfigParser. .ini:
# appconfig.ini
# mondo

[output]
LOGFILE=%(LOGDIR)s/app.log
LOGGING=on
LOGDIR=%(BASEDIR)s/logs

[input]
INFILE=%(INDIR)s/initial.dat
INDIR=%(BASEDIR)s/input

, :
from configparser import ConfigParser # Use from ConfigParser in Python 2

#
defaults = {
basedir : /Users/beazley/app

420

19.
}

# ConfigParser .ini
cfg = ConfigParser(defaults)
cfg.read(appconfig.ini)

, get(). :
>>> cfg.get(output,logfile)
/Users/beazley/app/logs/app.log
>>> cfg.get(input,infile)
/Users/beazley/app/input/initial.dat
>>> cfg.getboolean(output,logging)
True
>>>

. , . ,
logfile, , , logfile, LOGFILE
LogFile. -,
, %(BASEDIR)s %(LOGDIR)s, .
. , , . , LOGFILE,
appconfig.ini, LOGDIR,
. ,
, Python . , on LOGGING cfg.getboolean(), True.
, . , , :
; userconfig.ini
;
;

[output]
logging=off

[input]
BASEDIR=/tmp

. :
>>> cfg.read(userconfig.ini)
[userconfig.ini]
>>> cfg.get(output,logfile)
/Users/beazley/app/logs/app.log
>>> cfg.get(output,logging)
off

datetime

421

>>> cfg.get(input,infile)
/tmp/input/initial.dat
>>>

, , . ,
, , . ,
BASEDIR input , ,
INFILE. , Python.

ConfigParser .
RawConfigParser , ConfigParser, . SafeConfigParser ,
ConfigParser, ,
, (, %).

datetime
datetime .
. , .
,
Python,
.

date
date year, month day. date:
date(year, month, day)
date. year
datetime.MINYEAR datetime.MAXYEAR. month 1 12, day
1 month.
date, year, month day, .

422

19.

date.today()
, date,
.
date.fromtimestamp(timestamp)
, date,
timestamp. timestamp ,, time.time().
date.fromordinal(ordinal)
, date, , ordinal ( 1 1
1, 1 2006
732312).

, date.
date.min
, (datetime.
date(1,1,1)).
date.max
, (datetime.date(9999,12,31)).
date.resolution

date (datetime.timedelta(1)).
d date d.year, d.month d.day, , :
d.ctime()
,
time.ctime().
d.isocalendar()
(iso_year, iso_week, iso_weekday),
iso_week 1 53, iso_weekday
1 () 7 (). 1 iso_
week , . ISO 8601.
d.isoformat()
YYYY-MM-DD,
ISO 8601, .
d.isoweekday()
1 () 7 ().

datetime

423

d.replace([year [, month [, day ]]])


date, . , d.replace(month=4)
date, month 4.
d.strftime(format)
, , time.
strftime(). 1900 .
, ,
date ( , ), .
d.timetuple()
time.struct_time, time. ,
(, , ), 0.
d.toordinal()
d. 1 1 1.
d.weekday()
0 () 6 ().

time
time ,
, .
:
time(hour [, minute [, second [, microsecond [, tzinfo]]]])
time, , : 0 <= hour < 24,
0 <= minute < 60, 0 <= second < 60 0 <= microsecond < 1000000.
tzinfo tzinfo, ,
,
. time hour, minute, second,
microsecond tzinfo, .
time time:
time.min
,
(datetime.time(0,0)).
time.max
,
(datetime.time(23,59,59, 999999)).

424

19.

time.resolution

time (datetime.timedelta(0,0,1)).
t time, t.hour, t.minute, t.second,
t.microsecond t.tzinfo, :
t.dst()
t.tzinfo.dst(None).
timedelta. ,
None.
t.isoformat()
HH:MM:SS.mmmmmm.
, .mmmmmm .
, (,
HH:MM:SS.mmmmmm+HH:MM).
t.replace([hour [, minute [, second [, microsecond [, tzinfo ]]]]])
time, . , t.replace(second=30)
time, seconds 30.
, time(), .
t.strftime(format)
, , time.
strftime() time. time
, , .
t.tzname()
t.tzinfo.tzname().
, None.
t.utcoffset()
t.tzinfo.utcoffset(None). timedelta. , None.

datetime
datetime .
datetime:
datetime(year, month, day [, hour [, minute [, second [, microsecond [, tzinfo]]]]])
datetime, date time. ,
date() time().

datetime

425

datetime.combine(date,time)
, datetime,
date, date, time, time.
datetime.fromordinal(ordinal)
, datetime, ,
ordinal (
, datetime.min). ,
, 0, tzinfo None.
datetime.fromtimestamp(timestamp [, tz])
, datetime,
timestamp, time.time().
tz, tzinfo, .
datetime.now([tz])
, datetime,
. tz, tzinfo, .
datetime.strptime(datestring, format)
, datetime datestring, format. strptime() time.
datetime.utcfromtimestamp(timestamp)
, datetime,
timestamp, time.gmtime().
datetime.utcnow()
, datetime,
.

,
datetime.
datetime.min
,
(datetime.datetime(1,1,1,0,0)).
datetime.max
,
(datetime.datetime(9999,12,31,23,59,59,999999)).
datetime.resolution

(datetime.timedelta(0,0,1)).

426

19.

d datetime ,
date time . , :
d.astimezone(tz)
datetime, tz. ,
tz.
d.date()
date .
d.replace([year [, month [, day [, hour [, minute [, second [, microsecond [,
tzinfo]]]]]]])
datetime,
. .
d.time()
time . .
d.timetz()
time .
d.utctimetuple()
time.struct_time, , .

timedelta
timedelta
. datetime
(-). , :
timedelta([days [, seconds [, microseconds [, milliseconds [, minutes [, hours
[, weeks ]]]]]]])
timedelta,
. days, seconds microseconds,
. , , , .
days, seconds microseconds timedelta.

, timedelta:
timedelta.min
, timedelta (timedelta(-999999999)).

427

datetime

timedelta.max
,, timedelta (timedelta(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999)).
timedelta.resolution
timedelta (timedelta(microseconds=1)).


datetime . , date datetime, :

td = date1 - date2

timedelta

date2 = date1 + td

timedelta date

date2 = date1 - td

timedelta date

date1 < date2

date1 <= date2


date1 == date2
date1 != date2
date1 > date2
date1 >= date2

,
.. , ,
;
TypeError.
, , . timedelta :

td3 = td2 + td1

td3 = td2 - td1

td2 = td1 * i

td2 = i * td1
td2 = td1 // i

td2 = -td1

428

19.

()

td2 = +td1
abs(td)

td1 < td2

td1 <= td2


td1 == td2
td1 != td2
td1 > td2
td1 >= td2

:
>>> today = datetime.datetime.now()
>>> today.ctime()
Thu Oct 20 11:10:10 2005
>>> oneday = datetime.timedelta(days=1)
>>> tomorrow = today + oneday
>>> tomorrow.ctime()
Fri Oct 21 11:10:10 2005
>>>

, date, datetime, time timedelta


. ,
, .

tzinfo
datetime
tzinfo, . tzinfo . , tzinfo, :
tz.dst(dt)
timedelta,
, . , None. dt
datetime, None.
tz.fromutc(dt)
dt datetime datetime.
astimezone() datetime.
tzinfo, .

datetime

429

tz.tzname(dt)
(, US/Central).
dt datetime, None.
tz.utcoffset(dt)
timedelta, .
, , , . dt datetime, None.
:
#
#
#
#
#
#
#

,
TZOFFSET .
, US/CST -6
DSTNAME ,

STDNAME - ,

class SomeZone(datetime.tzinfo):
def utcoffset(self,dt):
return datetime.timedelta(hours=TZOFFSET) + self.dst(dt)
def dst(self,dt):
# is_dst() , ,
# ,
# .
if is_dst(dt):
return datetime.timedelta(hours=1)
else:
return datetime.timedelta(0)
def tzname(self,dt):
if is_dst(dt):
return DSTNAME
else:
return STDNAME


datetime.




datetime. datetime datetime.strptime(), .
, ,
, ( time.strptime()). , s=Aug 23, 2008, d = datetime.
datetime.strptime(s, %b %d, %Y).

430

19.

,
, .
Python (http://pypi.python.org) datetime.
, datetime.

.
time (. 507).

errno
errno
, ,
, os socket. errno OSError IOError. os.strerror().
:
errorcode

( EPERM).

POSIX
,
POSIX .
, , UNIX,, Macintosh OS-X
-X
X Windows.. UNIX .
. ,
errorcode, .

E2BIG

EACCES

EADDRINUSE

EADDRNOTAVAIL

EAFNOSUPPORT

EAGAIN

EALREADY

EBADF

431

errno

EBUSY

ECHILD

ECONNABORTED

ECONNREFUSED

ECONNRESET

EDEADLK

EDEADLOCK

EDESTADDRREQ

EDOM

EDQUOT

EEXIST

EFAULT

EFBIG

EHOSTDOWN

EHOSTUNREACH

EILSEQ

EINPROGRESS

EINTR

EINVAL

EIO

-.

EISCONN

EISDIR

ELOOP

EMFILE

EMLINK

EMSGSIZE

ENETDOWN

ENETRESET

ENETUNREACH

ENFILE

ENOBUFS

ENODEV

432

19.

()

ENOENT

ENOEXEC

ENOLCK

ENOMEM

ENOPROTOOPT

ENOSPC

ENOSYS

ENOTCONN

ENOTDIR

ENOTEMPTY

ENOTSOCK

ENOTTY

ENXIO

EOPNOTSUPP

EPERM

EPFNOSUPPORT

EPIPE

EPROTONOSUPPORT

EPROTOTYPE

ERANGE

EREMOTE

EROFS

ESHUTDOWN

ESOCKTNOSUPPORT

ESPIPE

ESRCH

ESTALE

NFS.

ETIMEDOUT

ETOOMANYREFS

, .

EUSERS

EWOULDBLOCK

EXDEV

433

errno

Windows
, ,
Windows.

WSAEACCES

WSAEADDRINUSE

WSAEADDRNOTAVAIL

WSAEAFNOSUPPORT

WSAEALREADY

WSAEBADF

WSAECONNABORTED

WSAECONNREFUSED

WSAECONNRESET

WSAEDESTADDRREQ

WSAEDISCON

WSAEDQUOT

WSAEFAULT

WSAEHOSTDOWN

WSAEHOSTUNREACH

WSAEINPROGRESS

WSAEINTR

WSAEINVAL

WSAEISCONN

WSAELOOP

WSAEMSGSIZE

WSAENAMETOOLONG

WSAENETDOWN

WSAENETRESET

WSAENETUNREACH

WSAENOBUFS

WSAENOPROTOOPT

WSAENOTCONN

WSAENOTEMPTY

WSAENOTSOCK

434

19.

()

WSAEOPNOTSUPP

WSAEPFNOSUPPORT

WSAEPROCLIM

WSAEPROTONOSUPPORT

WSAEPROTOTYPE

WSAEREMOTE

WSAESHUTDOWN

WSAESOCKTNOSUPPORT

WSAESTALE

WSAETIMEDOUT

WSAETOOMANYREFS

WSAEUSERS

WSAEWOULDBLOCK

WSANOTINITIALISED

WSASYSNOTREADY

WSAVERNOTSUPPORTED

Winsock.dll .

fcntl
fcntl -
UNIX.
fileno() .
fcntl(fd, cmd [, arg])
cmd fd. cmd . arg cmd,
. arg
, .
arg ,
,
, . arg 1024 , .
:

435

fcntl

F_DUPFD

. arg
, .
os.dup().

F_SETFD

close-on-exec , arg (0 1). ,


exec().

F_GETFD

close-on-exec.

F_SETFL

,
arg, , , :
O_NDELAY - (System V)
O_APPEND (System V)
O_SYNC (System V)
FNDELAY - (BSD)
FAPPEND (BSD)
FASYNC SIGIO,
- (BSD).

F_GETFL

, F_SETFL.

F_GETOWN

, SIGURG SIGIO (BSD).

F_SETOWN

, SIGIO SIGURG (BSD).

F_GETLK

flock, .

F_SETLK

, -1,
.

F_SETLKW

, -
, , .

fcntl() , IOError.
F_GETLK F_SETLK lockf().
ioctl(fd, op, arg [, mutate_flag])
fcntl() ,
, op,
termios. mutate_flag
, arg .
. ioctl()
-

436

19.

,
. , .
flock(fd, op)
op fd . op ,
, fcntl,
:

LOCK_EX

.
,
.

LOCK_NB

. , IOError,
.

LOCK_SH

. (LOCK_EX),
.

LOCK_UN

. .


IOError, .
, os.open().
os.
lockf(fd, op [, len [, start [, whence]]])
. op
, flock(). len , .
start whence. 0 whence , 1 2 .

import fcntl

#
f = open(foo,w)

# close-on-exec f
fcntl.fcntl(f.fileno(), fcntl.F_SETFD, 1)

#
fcntl.flock(f.fileno(), fcntl.LOCK_EX)

437

io
# 8192 ( )
try:
fcntl.lockf(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB, 8192, 0, 0)
except IOError,e:
print , e

, fcntl(), . fcntl
100 .

, .
, .
, .

io
io -, open(), Python 3.
Python 2.6.
io , -. , , ,
. ,
.

-
io , , . IOBase. f IOBase -:

f.closed

, , .

f.close()

f.fileno()

f.flush()

- ( ).

f.isatty()

True, f .

f.readable()

True, f .

438

19.

()

f.readline([limit])

. limit .

f.readlines([limit])

f
. limit, ,
, .
,
, .

f.seek(offset, [whence])


, whence. offset
. 0 whence , 1 2
.

f.seekable()

True, f
.

f.tell()

f.truncate([size])

size .
size , 0.

f.writable()

True, f .

f.writelines(lines)

f.
,
.

-
- - .
FileIO, , read() write().
FileIO(name [, mode [, closefd]])
, -
. name
, , os.open() fileno() . mode
: r ( ); w a, ,
. mode +,
,
. closefd , , close() . -

439

io

True, False,
FileIO , . name
, open().

. f FileIO
-, ,
:

f.closefd

,
f.close() ( ).

f.mode

, ( ).

f.name

( ).

f.read([size])

size . size , ,
f.readall().
, size, len(). None, .

f.readall()


. .
, .

f.write(bytes)

bytes f, . , .
,
bytes.

, FileIO
, ,
read() write(). , , ,
f.read() f.write() .
-,
, , fcntl.
FileIO ,
, .
, f.readline() f.readlines(), IOBase, Python, ,
f.read(). .
, Python 2.6 f.readline() f FileIO

440

19.

750 , f.readline()
, open().

-
-
, , .
,
-, FileIO,
. , , BufferedIOBase.
BufferedReader(raw [, buffer_size])
BufferedReader,
, raw.
buffer_size . , DEFAULT_BUFFER_SIZE
(8192 ). f BufferedReader , IOBase,
:

f.peek([n])

n -. .
n , . ,
, .
, , , n.

f.read([n])

n . n ( ). , .
,
, BlockingIOError.

f.read1([n])

n .
- , . read() . f.read(),
, ,
.

f.readinto(b)

len(b) b
bytearray.
. ,
, BlockingIOError.

441

io

BufferedWriter(raw [, buffer_size [, max_buffer_size]])


BufferedWriter,
, raw.
buffer_size , ,
-. DEFAULT_BUFFER_SIZE.
max_buffer_size
,
, buffer_size.
,
, -. f BufferedWriter :

f.flush()

-.

, BlockingIOError (,
).

f.write(bytes)

bytes -
.

, BlockingIOError.

BufferedRWPair(reader, writer [, buffer_size [, max_buffer_size]])


BufferedRWPair,
/ -.
reader , , writer
, . ,
. buffer_size max_buf_bufbuffer_size , BufferedWriter().
BufferedRWPair , BufferedReader BufferedWriter.
BufferedRandom(raw [, buffer_size [, max_buffer_size]])
BufferedRandom,
/ -, ( ).
raw , , . buffer_size
max_buffer_size , BufferedWriter().
BufferedRandom , BufferedReader BufferedWriter.

442

19.

BytesIO([bytes])
, -. bytes . b BytesIO , BufferedReader
BufferedWriter. , b.getvalue(), .
FileIO, , , , , . , (, Python 2.6
50 ,
, open()). , -

flush() .. , f.seek()
, f.flush(), , .
, , , ,
, . , f.write(data) f,
data .
data , .

, , write(). , io ,
.

-
-
, . ,
,
- ,
. TextIOBase.
TextIOWrapper(buffered [, encoding [, errors [, newline [, line_buffering]]]])
. buffered
-, ,
. encoding , ascii utf-8. errors
strict (

443

io

9 ). newline , ,
None, , \n, \r \r\n.
None, ,
\n,
os.linesep. newline ,
\n .
line_buffering , ,
flush() ,
. False.
f TextIOWrapper ,
IOBase, :

f.encoding

f.errors

f.line_buffering

, .

f.newlines

None,
.

f.read([n])

n
. n .
.
f.encoding.

f.readline([limit])

. .
limit ,
.

f.write(s)

s .
f.encoding.

StringIO([initial [, encoding [, errors [, newline]]]])


, ,
, TextIOWrapper. initial .
, TextIOWrapper(). s
StringIO s.getvalue(),
.

open()
io open(), open() Python 3:

444

19.

open(file [, mode [, buffering [, encoding [, errors [, newline [, closefd]]]]]])


file . file
-, .
-, io, mode buffering. mode , r, w, a U,
TextIOWrapper. mode ,
rb wb, buffering.
buffering 0,
FileIO, - .
buffering ,
BufferReader, BufferedWriter BufferedRandom, mode. encoding, errors newline ,
; TextIOWrapper. closefd ,
file ,
FileIO.


io ,

-:

IOBase

-.

RawIOBase

, - . IOBase.

BufferedIOBase

, - . IOBase.

TextIOBase

, -
.
IOBase.

. .

io , Python 3 Python 2.6.



, ,

445

logging

- .
Python 2, open()
, io. Python 3, .
, -
-,
C, Python 2.

logging
logging , ,
. , , ,
.
.


logging .
, . , :

CRITICAL

50

ERROR

40

WARNING

30

INFO

20

DEBUG

10

NOTSET


logging. , , , , .


logging
, . ,

446

19.

, , . :
basicConfig([**kwargs])
.
logging.
:

filename

filemode

. a ( ).

format

datefmt

level

. ,
.
.

stream

,
.
std.stderr.
filename.

. format , , , . datefmt , time.strftime().


, ISO8601.
format :

%(name)s

%(levelno)s

%(levelname)s

%(pathname)s

,
.

%(filename)s

,
.

%(funcName)s

, .

%(module)s

, .

447

logging

%(lineno)d

, .

%(created)f

, .
, time.
time().

%(asctime)s

ASCII,
.

%(msecs)s

, .

%(thread)d

%(threadName)s

%(process)d

%(message)s

( ).

, ,
INFO :
import logging
logging.basicConfig(
filename = app.log,
format = %(levelname)-10s %(asctime)s %(message)s
level = logging.INFO
)

Hello World
CRITICAL app.log, .
CRITICAL 2005-10-25 20:46:57,126 Hello World

Logger
,
Logger. , .

Logger
Logger :
getLogger([logname])
Logger logname. ,
Logger. logname ,
, (, app
app.net). logname Logger
.

448

19.

Logger ,
. Logger
getLogger()
logname. getLogger()
Logger . - , ,
.
,
Logger .
, , getLogger(),
Logger.


, ,
getLogger() . , app, getLogger(app) , . :
import logging
log = logging.getLogger(app)

, getLogger(app.net)
getLogger(app.user), . , :
import logging
log = logging.getLogger(app.+__name__)

,
.


, log Logger
( getLogger(), ),
:

CRITICAL

log.critical(fmt [, *args [, exc_info [, extra]]])

ERROR

log.error(fmt [, *args [, exc_info [, extra]]])

WARNING

log.warning(fmt [, *args [, exc_info [, extra]]])

INFO

log.info(fmt [, *args [, exc_info [, extra]]])

DEBUG

log.debug(fmt [, *args [, exc_info [, extra]]])

logging

449

fmt ,
. args fmt. %. ,
. , %. ,
, .
, :
log = logging.getLogger(app)
# ,
log.critical(Cant connect to %s at port %d, host, port)

# ,
parms = {
host : www.python.org,
port : 80
}
log.critical(Cant connect to %(host)s at port %(port)d, parms)

exc_info True, , sys.


exc_info(). , sys.exc_info(),
. extra ( ). , exc_info extra, .
,
(
, , logging). :
log.critical(Cant connect to %s at port %d % (host, port))

log.critical(),
.
logging , . ,
,
,
.
, ,
log Logger,
.

450

19.

log.exception(fmt [, *args ])
ERROR
. except.
log.log(level, fmt [, *args [, exc_info [, extra]]])
level. , ,
.
log.findCaller()
(filename, lineno, funcname) , .
, , ,
.


log Logger , , .

:
log.setLevel(level)
log, level.
level. . level
logging.NOTSET, .
log.isEnabledFor(level)
True, level .
,
, , , . :
log.addFilter(filt)
filt .
log.removeFilter(filt)
filt .
filt Filter.
Filter(logname)
,
logname . , logname
app, ,

451

logging

app, app.net app.user, , spam, .


, Filter filter(record),
record True False, , . record,
, :

record.name

record.levelname

record.levelno

record.pathname

record.filename

record.module

record.exc_info

record.lineno

record.funcName

record.created

record.thread

record.threadName

record.process

(PID)

, :
class FilterFunc(logging.Filter):
def __init__(self,name):
self.funcName = name
def filter(self, record):
if record.funcName == self.funcName: return False
else: return True

log.addFilter(FilterFunc(foo)) # foo()
log.addFilter(FilterFunc(bar)) # bar()


Logger .
Logger , app.net.
client. Logger app, app.net app.net.client. - -

452

19.


,
. , ,
app.net.client, app.net, app .

log Logger.
log.propagate
, ,
. True.
log.getEffectiveLevel()
.. setLevel(), .
setLevel() ( logging.NOTSET),
. ,
.
, , . , , app.net.client,
, :
import logging
logging.getLogger(app.net.client).propagate = False

:
import logging
logging.getLogger(app.net.client).setLevel(logging.CRITICAL)

,
Logger, ,
, - . ,
, ,
,
.
.
,
, ,
,
. :

logging

453

import logging
# app .
log = logging.getLogger(app)
log.setLevel(logging.CRITICAL) # CRITICAL

# app.net
net_log = logging.getLogger(app.net)
net_log.setLevel(logging.ERROR) # ERROR
# , app.net,
#
# app, ,
# CRITICAL.


,
.
,
, basicConfig().


. Logger , . log Logger.
log.addHandler(handler)
Handler .
log.removeHandler(handler)
Handler .
logging , , ,
. . , .
import logging
import sys

# app
app_log = logging.getLogger(app)
app_log.setLevel(logging.INFO)
app_log.propagate = False

# app
app_log.addHandler(logging.FileHandler(app.log))
app_log.addHandler(logging.StreamHandler(sys.stderr))

# . app.log
# sys.stderr
app_log.critical(Creeping death detected!)
app_log.info(FYI)

454

19.

. ( app
).

Handler
logging . Logger addHandler().
, .


.
logging.handlers, , , .
handlers.DatagramHandler(host,port)
UDP host
port.
LogRecord pickle. , , 4- ( ),
. , , , pickle logging.makeLogRecord(). UDP , .
FileHandler(filename [, mode [, encoding [, delay]]])
filename. mode
a.
encoding . delay
; True, . False.
handlers.HTTPHandler(host, url [, method])
HTTP, HTTP GET
POST. host , url URL,
method HTTP, GET ( ) POST. LogRecord URL urllib.urlencode().
handlers.MemoryHandler(capacity [, flushLevel [, target]])
,
target. capacity .

logging

455

flushLevel .. ,
. ERROR. target Handler, . target ,
- setTarget(),
.
handlers.NTEventLogHandler(appname [, dllname [, logtype]])
Windows NT, Windows 2000 Windows XP
. appname , . dllname
.DLL
DLL .EXE,
EXE,, . , dllname
win32service.pyd. logtype
: Application, System Security. Application. ,
Win32 Python.
handlers.RotatingFileHandler(filename [, mode [, maxBytes [, backupCount [,
encoding [, delay]]]]])
filename.
maxBytes, filename.1
filename. backupCount
.. backupCount 0. filename.1,
filename.2, ..., filename.N, filename.1 , filename.N . mode
. mode a. maxBytes 0 ( ),

. encoding delay ,
FileHandler.
handlers.SMTPHandler(mailhost, fromaddr, toaddrs, subject [, credentials])
. mailhost SMTP, .
, , (host, port). fromaddr , toaddrs subject .
credentials (username, password)
.
handlers.SocketHandler(host, port)
TCP. host
port .
, DatagramHandler. DatagramHandler, .

456

19.

StreamHandler([fileobj])
fileobj.
sys.stderr.
handlers.SysLogHandler([address [, facility]])
UNIX.. address (host, port).
, (localhost, 514).
facility ; SysLogHandler.LOG_USER.
SysLogHandler.
handlers.TimedRotatingFileHandler(filename [, when [, interval [, backupCount
[, encoding [, delay [, utc]]]]]])
, RotatingFileHandler,
,
. interval , , when , .
when : S (), M (), H (), D (), W ()
midnight ( ). ,
interval 3, when D,
. backupCount . utc , ,
( ) (UTC).
handlers.WatchedFileHandler(filename [, mode [, encoding [, delay]]])
, FileHandler, (inode)
inode)) . ,
filename.
, .
UNIX.


h Handler . :
h.setLevel(level)
.
level , ERROR CRITICAL.
h.addFilter(filt)
filt Filter. addFilter() Logger.

logging

457

h.removeFilter(filt)
filt Filter.
,
Logger, . :
import logging
import sys

# , CRITICAL
# stderr
crit_hand = logging.StreamHandler(sys.stderr)
crit_hand.setLevel(logging.CRITICAL)

# app
app_log = logging.getLogger(app)
app_log.setLevel(logging.INFO)
app_log.addHandler(logging.FileHandler(app.log))
app_log.addHandler(crit_handler)

app INFO. ,
(crit_handler) CRITICAL. INFO ,
, CRITICAL.


.
h.flush()
.
h.close()
.


Handler , logging. ,
, , . ,
.


, Formatter:

458

19.

Formatter([fmt [, datefmt]])
Formatter. fmt
. fmt , basicConfig(). datefmt ,
time.strftime(). ,
ISO8601.
Formatter,
. h.setFormatter()
h Handler.
h.setFormatter(format)
, h Handler . format
Formatter.
, :
import logging
import sys

#
format = logging.Formatter(%(levelname)-10s %(asctime)s %(message)s)

# , CRITICAL
# stderr
crit_hand = logging.StreamHandler(sys.stderr)
crit_hand.setLevel(logging.CRITICAL)
crit_hand.setFormatter(format)


crit_hand. , Creeping death detected., :
CRITICAL 2005-10-25 20:46:57,126 Creeping death detected.



.
. -,
( log.critical(), log.warning() ) extra, .
Formatter. :
import logging, socket
logging.basicConfig(
format = %(hostname)s %(levelname)-10s %(asctime)s %(message)s
)

logging

459

#
netinfo = {
hostname : socket.gethostname(),
ip
: socket.gethostbyname(socket.gethostname())
}
log = logging.getLogger(app)

#
log.critical(Could not connect to server, extra=netinfo)

,
.
LogAdapter
.
LogAdapter(log [, extra])
log Logger. extra , . LogAdapter
, Logger. ,
,
, extra.
LogAdapter:
import logging, socket
logging.basicConfig(
format = %(hostname)s %(levelname)-10s %(asctime)s %(message)s
)

#
netinfo = {
hostname : socket.gethostname(),
ip
: socket.gethostbyname(socket.gethostname())
}

#
log = logging.LogAdapter(logging.getLogger(app), netinfo)

# .
# LogAdapter
log.critical(Could not connect to server)


logging :
disable(level)

level.
, ,
.

460

19.

addLevelName(level, levelName)
. level
, levelName . , .
getLevelName(level)
, level.
shutdown()
, , .


logging :
1. getLogger()
Logger. , .
2. ( FileHandler, StreamHandler, SocketHandler ) .
3. Formatter Handler setFormatter().
4. addHandler() Handler Logger.
,
,
, . , applogconfig.py, :
# applogconfig.py
import logging
import sys

#
format = logging.Formatter(%(levelname)-10s %(asctime)s %(message)s)

# , CRITICAL
# stderr
crit_hand = logging.StreamHandler(sys.stderr)
crit_hand.setLevel(logging.CRITICAL)
crit_hand.setFormatter(format)

# ,
applog_hand = logging.FileHandler(app.log)
applog_hand.setFormatter(format)

# app
app_log = logging.getLogger(app)

logging

461

app_log.setLevel(logging.INFO)
app_log.addHandler(applog_hand)
app_log.addHandler(crit_hand)

# app.net
logging.getLogger(app.net).setLevel(logging.ERROR)

- - , ,
. , .
, , :
import logging
app_log = logging.getLogger(app)
...
app_log.critical(An error occurred)

logging.config
Python
logging INI.
logging.config.
fileConfig(filename [, defaults [, disable_existing_loggers]])
filename. defaults
.
filename ConfigParser. disable_ex_exexisting_loggers , ,
. True.
,
, logging.
, , , applogconfig.py .
; applogconfig.ini
;
; logging
; Logger, Handler
; Formatter, .
[loggers]
keys=root,app,app_net

[handlers]
keys=crit,applog

[formatters]

462

19.
keys=format

[logger_root]
level=NOTSET
handlers=

[logger_app]
level=INFO
propagate=0
qualname=app
handlers=crit,applog

[logger_app_net]
level=ERROR
propagate=1
qualname=app.net
handlers=

[handler_crit]
class=StreamHandler
level=CRITICAL
formatter=format
args=(sys.stderr,)

[handler_applog]
class=FileHandler
level=NOTSET
formatter=format
args=(app.log,)

[formatter_format]
format=%(levelname)-10s %(asctime)s %(message)s
datefmt=

:
import logging.config
logging.config.fileConfig(applogconfig.ini)

, , . logging
Logger. :
import logging
app_log = logging.getLogger(app)
...
app_log.critical(An error occurred)


, . ,
.

mmap

463

-, (-O)
, ,
if __debug__: . logging, , .
Null
Logger, . None , , .
:
class Null(object):
def __init__(self, *args, **kwargs): pass
def __call__(self, *args, **kwargs): return self
def __getattribute__(self, name): return self
def __setattr__(self, name, value): pass
def __delattr__(self,name): pass

log = Null()
log.critical(An error occurred.) #


. , Python
, , , .
6
7 .

logging , . .

logging .
, , .

mmap
mmap . , ,

. , . , .

464

19.

,
.
mmap(), UNIX Windows.
mmap(fileno, length [, flags, [prot [,access [, offset]]]])
(UNIX). mmap length ,
fileno.
fileno -1, .
flags
:

MAP_PRIVATE

.
.

MAP_SHARED

,
,
.
.

flags MAP_SHARED. prot , :

PROT_READ

PROT_WRITE

PROT_EXEC

prot PROT_READ | PROT_WRITE. , prot,


fileno. ,
/ (, os.open(name, os.O_RDWR)).
access flags prot.
:

ACCESS_READ

ACCESS_WRITE

/ .
.

ACCESS_COPY

/ . , .

mmap

465

access , mmap(fileno, length, access=ACCESS_READ).


access flags . offset

0.
mmap.ALLOCATIONGRANULARITY.
mmap(fileno, length[, tagname [, access [, offset]]])
(Windows) mmap length ,
fileno.
fileno -1, . length ,
, length. length
0,
, ( ). tagname
, .
tagname , . . tagname None,
. access
.. , , mmap() UNIX.
access ACCESS_WRITE. offset
0.
mmap.ALLOCATIONGRANULARITY.
m .
m.close()
. -
.
m.find(string[, start])
string. start . -1, .
m.flush([offset, size])
, , . offset size ,
. .
m.move(dst,src,count)
count , src, dst.
memmove() C,
, .

466

19.

m.read(n)
n , , .
m.read_byte()
, , 1.
m.readline()
, .
m.resize(newsize)
newsize
.
m.seek(pos[, whence])
. pos
whence , seek() .
m.size()
. ,
.
m.tell()
.
m.write(string)
, .
m.write_byte(byte)
, .

UNIX Windows mmap(), , access,


. , mmap(fileno,length,access=ACCESS_WRITE)
UNIX, Windows.

,
,
mmap.PAGESIZE.

UNIX SVR4
4 mmap() /dev/zero, .

UNIX BSD mmap()


mmap.MAP_ANON.

467

msvcrt

msvcrt
msvcrt
Microsoft Visual C.
Windows.
getch()
.
.
, \000
\xe0, . - Ctrl+C.
getwch()
, getch(), , .
getche()
, getch(), , - (
).
getwche()
, getche(), ,, .
get_osfhandle(fd)
fd. IOError, fd
.
heapmin()
Python . Windows NT IOError.
kbhit()
True, .
locking(fd, mode, nbytes)
, fd,
C. nbytes
, . mode :

(LK_UNLCK)

(LK_LOCK)

468

19.

()

.
(LK_NBLCK)

(LK_RLCK)

. (LK_NBRLCK)

10 IOError.
open_osfhandle(handle, flags)

C handle. flags
, os.O_APPEND, os.O_RDONLY os.O_TEXT,
. , os.fdopen() .
putch(char)
char .
putwch(char)
, putch(), , char
.
setmode(fd, flags)
fd. flags os.O_TEXT
os.O_BINARY .
ungetch(char)
.
, getch() getche().
ungetwch(char)
, ungetch(), , char
.

Win32,
32, Microsoft Foundation Classes, COM,
.
,
(Mark Hammond)
(Andy
Andy Robinson)) Python
Python Programming on Win32
32 (OReilly
OReilly
Reilly
Reilly & Associates,, 2000). , http://www.python.org
Windows.

469

optparse

.
winreg (. 511).

optparse
optparse
UNIX,, sys.argv.
9. optparse OptionParser.
OptionParser([**args])

OptionParser. , .
:

add_help_option

,
(--help -h). True.

conflict_handler


. error (
) resolve. error,

optparse.OptionConflictError.
resolve, ,
, ,
.
,
, .

description

, . , .

formatter

optparse.HelpFormatter,
.
optparse.IndentedHelpFormatter ( )
optparse.TitledHelpFormatter.

option_class

Python,
. optparse.Option.

option_list

, . ,
add_option().
Option.

470

19.

()

prog

, %prog
.

usage

, --help .
%prog [options],
%prog ,
os.path.basename(sys.argv[0]),
prog ( ).
,
optparse.SUPPRESS_USAGE.

version

, --version. None, --version .


,
--version . %prog, .

, OptionParser
. :
p = optparse.OptionParser()

p OptionParser :
p.add_option(name1, ..., nameN [, **parms])
p. name1, name2
. ,
, -f file.
,
.
:

action

, .
:
store .
,
.
store_const ,
, , const.

471

optparse

store_true , store_const, True.


store_false , store_true, False.
append , , . ,

, .
count , , .
1 ,
.
callback ,
callback.
help
. ,
,
-h --help.
version , OptionParser(). ,
, -v --version.

callback

, .
Python, callback(option, opt_str, value, parser, *args,
**kwargs). option
optparse.Option, opt_str , , value ( ),
parser OptionParser, args ,
callback_args, kwargs , callback_kwargs.

callback_args

, , callback.

callback_kwargs

, , callback.

choices

,
. , (,
[small, medium, large]).

472

19.

()

const

, store_
const.

default

, .
None.

dest

,
. , .

help

. ,
. optparse.SUPPRESS_HELP.
%default,
default.

metavar

, .

nargs

,
.
1. 1, ,
.

type

.. : string ( ), int, long, choice, float


complex.

p.disable_interspersed_args()
. , -x -y , , (: prog -x -y arg1 arg2 arg3).
p.enable_interspersed_args()
. , -x -y , , ,
: prog -x arg1 arg2 -y arg3. .
p.parse_args([arglist])
(options,
args), options , , args
. options , . , --output
options.output. -

optparse

473

, None. dest add_option(),


. sys.argv[1:]. arglist .
p.set_defaults(dest=value, ... dest=value)
. , , .
, dest
add_option(), .
p.set_usage(usage)
, , --help.

# foo.py
import optparse
p = optparse.OptionParser()

#
p.add_option(-t, action=store_true, dest=tracing)

# ,
p.add_option(-o, --outfile, action=store, type=string, dest=outfile)

# ,
p.add_option(-d, --debuglevel, action=store, type=int, dest=debug)

#
p.add_option(--speed, action=store, type=choice, dest=speed,
choices=[slow,fast,ludicrous])

# ,
p.add_option(--coord, action=store, type=int, dest=coord, nargs=2)

# ,
p.add_option(--novice, action=store_const, const=novice, dest=mode)
p.add_option(--guru, action=store_const, const=guru, dest=mode)

#
p.set_defaults(tracing=False,
debug=0,
speed=fast,
coord=(0,0),
mode=novice)

#
opt, args = p.parse_args()

#
print tracing :, opt.tracing

474

19.
print
print
print
print
print

outfile
debug
speed
coord
mode

:,
:,
:,
:,
:,

opt.outfile
opt.debug
opt.speed
opt.coord
opt.mode

#
print args
:, args


UNIX, :
% python foo.py -h
usage: foo.py [options]

options:
-h, --help show this help message and exit
-t
-o OUTFILE, --outfile=OUTFILE
-d DEBUG, --debuglevel=DEBUG
--speed=SPEED
--coord=COORD
--novice
--guru

% python foo.py -t -o outfile.dat -d 3 --coord 3 4 --speed=ludicrous blah


tracing : True
outfile : outfile.dat
debug : 3
speed : ludicrous
coord : (3, 4)
mode
: novice
args
: [blah]

% python foo.py --speed=insane


usage: foo.py [options]

foo.py:error:option --speed:invalid choice:insane


(choose from slow, fast, ludicrous)

, -x,
, --exclude. , , : -exclude, OptionError.

Python getopt,
C .
optparse (
, ).

os

475

optparse . .

.

os
os
. , , nt posix,
, . ,, ,, Windows UNIX,, . UNIX Linux
Mac OS X.
os :
environ
, .
.
putenv(), .
linesep
, . , \n
POSIX, , : \r\n Windows.
name
, : posix,
nt, dos, mac, ce, java, os2 riscos.
path
,
, . import os.path.


, , . , , , .
chdir(path)
path .
chroot(path)
(UNIX).

476

19.

ctermid()
(UNIX).
fchdir(fd)
. fd
(UNIX).
getcwd()
.
getcwdu()
.
getegid()
(UNIX).
geteuid()
(UNIX).
getgid()
(UNIX).
getgroups()
, (UNIX).
getlogin()
, (UNIX).
getpgid(pid)
pid. pid 0,
(UNIX).
getpgrp()
.

.
, (UNIX).
getpid()
(UNIX
Windows).
getppid()
(UNIX).
getsid(pid)
pid.
pid 0,
(UNIX).

os

477

getuid()

(UNIX).
putenv(varname, value)
value varname.
,
os.system(), popen(), fork() execv(). os.environ putenv(). putenv() os.environ (UNIX Windows).
setegid(egid)
(UNIX).
seteuid(euid)
(UNIX).
setgid(gid)
(UNIX).
setgroups(groups)
. groups . root (UNIX).
setpgrp()
setpgrp()
setpgrp(0, 0), , (
). (UNIX).
setpgid(pid, pgrp)
pid pgrp.
pid pgrp, . pid
pgrp, .
pid 0, . pgrp 0,
pid (UNIX).
setreuid(ruid,euid)
(UNIX).
setregid(rgid,egid)

(UNIX).
setsid()
.
, (UNIX).

478

19.

setuid(uid)
.
, root (UNIX).
strerror(code)
, code (UNIX
UNIX Windows).
). errno.
umask(mask)
umask umask. umask , (UNIX Windows).
uname()
(sysname, nodename, release, version, machine),
(UNIX).
unsetenv(name)
name.


,
. , fd.
fileno() .
close(fd)
fd, open()
pipe().
closerange(low, high)
fd low <= fd < high. .
dup(fd)
fd. ,
. . , , (UNIX
UNIX Windows).
dup2(oldfd, newfd)
oldfd newfd. newfd
, (UNIX
UNIX Windows).

479

os

fchmod(fd, mode)
mode ,
fd. os.open() (UNIX).
fchown(fd, uid, gid)
uid gid , fd.
, -1 (UNIX).
fdatasync(fd)
, fd (UNIX).
fdopen(fd [, mode [, bufsize]])
, fd. mode bufsize ,
open(). mode , r, w
a. Python 3
, open(), , .
Python 2
,
mode bufsize, .
fpathconf(fd, name)
, ,
fd. name . , <limits.h> <unistd.h>.
POSIX , name:

PC_ASYNC_IO

, - fd.

PC_CHOWN_RESTRICTED

, chown().
fd ,

.

PC_FILESIZEBITS

PC_LINK_MAX

PC_MAX_CANON

.
fd .

480

19.

()

PC_MAX_INPUT

. fd .

PC_NAME_MAX

PC_NO_TRUNC

, ENAMETOOLONG
, PC_NAME_MAX.

PC_PATH_MAX

, fd .

PC_PIPE_BUF

, fd
FIFO.

PC_PRIO_IO

,
- fd.

PC_SYNC_IO

,
- fd.

PC_VDISABLE

, . fd
.

, . , ,
os.pathconf_names. , os.pathconf_names, name
. ,
OSError,
fd. UNIX.
fstat(fd)
fd. ,
os.stat() (UNIX Windows).
fstatvfs(fd)
, , fd. ,
os.statvfs() (UNIX).
fsync(fd)
,
fd. , , - (, file),
fsync() fdatasync().
UNIX Windows.

481

os

ftruncate(fd, length)
, fd,
length (UNIX).
isatty(fd)
True, fd TTY-
- , (UNIX).
lseek(fd, pos, how)
fd pos. how :
SEEK_SET ,
SEEK_CUR
SEEK_END .
Python : 0, 1 2 .
open(file [, flags [, mode]])
file. flags , , :

O_RDONLY

O_WRONLY

O_RDWR

( ).

O_APPEND

O_CREAT

, .

O_NONBLOCK


, (UNIX).

O_NDELAY

, O_NONBLOCK (UNIX).

O_DSYNC

(UNIX).

O_NOCTTY


(UNIX).

O_TRUNC

, .

O_RSYNC

(UNIX).

O_SYNC

(UNIX).

O_EXCL

,
O_CREAT.

O_EXLOCK

O_SHLOCK

O_ASYNC

,
SIGIO.

482

19.

()

O_DIRECT

-,

, .

O_DIRECTORY

, .

O_NOFOLLOW

O_NOATIME

O_TEXT

(Windows).

O_BINARY

(Windows).

O_NOINHERIT

O_SHORT_LIVED

,
(Windows).

O_TEMPORARY

(Windows).

O_RANDOM

, (Windows).

O_SEQUENTIAL

, (Windows).

(O_SYNC, O_DSYNC, O_RSYNC) - ,


(, ,
). mode , ( stat), :

0100

(stat.S_IXUSR).

0200

(stat.S_IWUSR).

0400

(stat.S_IRUSR).

0700

//
(stat.S_IRWXU).

0010

(stat.S_IXGRP).

0020

(stat.S_IWGRP).

0040

(stat.S_IRGRP).

0070

//
(stat.S_IRWXG).

0001

(stat.S_IXOTH).

483

os

0002

(stat.S_IWOTH).

0004

(stat.S_IROTH).

0007

//
(stat.S_IRWXO).

4000

UID (stat.S_ISUID).

2000

GID (stat.S_ISGID).

1000

(stat.S_ISVTX).

mode 0777 & umask, umask . ,


0022 umask . umask
os.umask(). umask
Windows.
openpty()
(master,slave)
PTY TTY. UNIX.
pipe()
, . (r, w), . fork(). fork() , , ,
.

write() read() (UNIX).
read(fd, n)
n fd.
.
tcgetpgrp(fd)
, fd (UNIX).
tcsetpgrp(fd, pg)
, fd (UNIX).
ttyname(fd)
,
fd. fd , OSError (UNIX).

484

19.

write(fd, str)
str fd. , .



. , ,
:

altsep

,
None,
-. DOS Windows
/, sep
.

curdir

, :
. UNIX Windows, : Macintosh.

devnul

(, /dev/null).

extsep

, (, . foo.txt).

pardir

, : ..
UNIX Windows, :: Macintosh.

pathsep

,
(, $PATH): : UNIX, ;
DOS Windows.

sep

,
: / UNIX Windows, : Macintosh.

:
access(path, accessmode)
////// path. accessmode R_OK, W_OK, X_OK
F_OK , , . 1,
, 0 .
chflags(path, flags)
path. flags ,
, ,
. , UF_,
, ,
SF_, (UNIX).

485

os

stat.UF_NODUMP

stat.UF_IMMUTABLE

stat.UF_APPEND

stat.UF_OPAQUE

stat.UF_NOUNLINK

stat.SF_ARCHIVED

stat.SF_IMMUTABLE

stat.SF_APPEND

stat.SF_NOUNLINK

stat.SF_SNAPSHOT

chmod(path, mode)
path. mode
, open(), (UNIX Windows).
chown(path, uid, gid)
path uid gid. , uid gid
-1 (UNIX).
lchflags(path, flags)
, chflags(), (UNIX).
lchmod(path, mode)
, chflags(), , path
, ,
, .
lchown(path, uid, gid)
, chown(), (UNIX).
link(src, dst)
dst, src (UNIX).
listdir(path)
path.
,
. ... path
, .
, -
, .
path ,
.

486

19.

lstat(path)
stat(),
(UNIX).
makedev(major, minor)
, major minor
(UNIX).
major(devicenum)
devicenum,
makedev().
minor(devicenum)
devicenum,
makedev().
makedirs(path [, mode])
. mkdir(),
, .
path , OSError.
mkdir(path [, mode])
path mode. mode
0777. , UNIX, mode
.
mkfifo(path [, mode])
FIFO ( ) path mode. mode 0666 (UNIX).
mknod(path [, mode, device])
. path , mode , device
, os.makedev(). mode
, open(),
. ,
mode stat.S_IFREG, stat.S_IFCHR, stat.S_IFBLK stat.S_IFIFO
(UNIX).
pathconf(path, name)
, path. name .
fpathconf() (UNIX).
readlink(path)
, , path (UNIX).

487

os

remove(path)
path. unlink().
removedirs(path)
. , rmdir(), , path,
,
( ,
, ).
path , OSError.
rename(src, dst)
src dst.
renames(old, new)
.
, rename(), , new.
old, , removedirs().
rmdir(path)
path.
stat(path)
stat() path, . ,
. :

st_mode

st_ino

(inode).

st_dev

, .

st_nlink

st_uid

, .

st_gid

, .

st_size

st_atime

st_mtime

st_ctime

. , stat(),

488

19.

10 (st_mode, st_ino,
st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime).
. stat , .
stat_float_times([newvalue])
True, , stat(),
, . ,
newvalue.
statvfs(path)
statvfs() path,
. ,
.
:

f_bsize

f_frsize

f_blocks

f_bfree

f_bavail

f_files

f_ffree

f_favail

f_flag

( )

f_namemax

,, ,, . statvfs , (UNIX).
symlink(src, dst)
dst, src.
unlink(path)
path. , remove().
utime(path, (atime, mtime))
. (
-

os

489

.)
, time.time().
walk(top [, topdown [, onerror [,followlinks]]])
-, . top . topdown ,
( ) .
(dirpath, dirnames, filenames),
dirpath , , dirnames dirpath filenames dirpath,
. onerror ,, .
, os.error. . , dirnames . ,
dirnames - , .
, followlinks True.


, :
abort()
SIGABRT, . ,
.
defpath
, exec*p*(), PATH.
execl(path, arg0, arg1, ...)
execv(path, (arg0, arg1, ...)).
execle(path, arg0, arg1, ..., env)
execve(path, (arg0, arg1, ...), env).
execlp(path, arg0, arg1, ...)
execvp(path, (arg0, arg1, ...)).
execv(path, args)
path args,
( Python).
.
execve(path, args, env)
, execv(), env, ,

490

19.

. env ,
.
execvp(path, args)
execv(path, args),
, .
environ[PATH].
execvpe(path, args, env)
execvp(), env,
, execve().
_exit(n)
n, . , fork().
sys.exit(), . , 0 ,
, - .
:

EX_OK

EX_USAGE

EX_DATAERR

EX_NOINPUT

EX_NOUSER

EX_NOHOST

EX_NOTFOUND

EX_UNAVAILABLE

EX_SOFTWARE

EX_OSERR

EX_OSFILE

EX_CANTCREAT

EX_IOERR

-.

EX_TEMPFAIL

EX_PROTOCOL

EX_NOPERM

EX_CONFIG

os

491

fork()
. 0 . ,
,
(UNIX).
forkpty()
, . (pid, fd), pid
0 , fd .
UNIX.
kill(pid, sig)
sig pid.
signal (UNIX).
killpg(pgid, sig)
sig pgid.
signal (UNIX).
nice(increment)
increment .
. ,
root.
, , (UNIX).
plock(op)
, . op , , .
op , UNLOCK, PROCLOCK, TXTLOCK DATLOCK. Python,
<sys/lock.h>.
0 (root) (UNIX).
popen(command [, mode [, bufsize]])
command .. , , , mode r ( ) w.
bufsize , open().
command close() ,
, None.

492

19.

spawnv(mode, path, args)


path args
. args . args
. mode :

P_WAIT

.
.

P_NOWAIT

P_NOWAITO

, P_NOWAIT.

P_OVERLAY


( exec).

P_DETACH

. ,
.

spawnv() Windows UNIX.


spawnve(mode, path, args, env)
path , args
env
. args .
mode , spawnv().
spawnl(mode, path, arg1, ... , argn)
, spawnv(), ,
.
spawnle(mode, path, arg1, ... , argn, env)
, spawnve(), ,
. .
spawnlp(mode, file, arg1, ... , argn)
, spawnl(), file PATH (UNIX).
spawnlpe(mode, file, arg1, ... , argn, env)
, spawnle(), file PATH (UNIX).
spawnvp(mode, file, args)
, spawnv(), file PATH (UNIX).
spawnvpe(mode, file, args, env)
, spawnve(), file PATH (UNIX).

os

493

startfile(path [, operation])
, path.
,
Windows Explorer. ,
. ,
. path . operation , ,
path.
open, print, edit,
explore find ( (Windows)).
system(command)
command () . UNIX
, wait(). Windows
0. subprocess .
times()
5 , . UNIX
, , ,
. Windows
, .
wait([pid])
. 16-
, , , ( ). core- ,
. pid, ,
, .
wait() ,
(UNIX).
waitpid(pid, options)
pid
,
, wait(). options 0 WNOHANG,
,
. ,
- . -

494

19.

options WCONTINUED,
, . options
WUNTRACED, , , .
wait3([options])
, wait() ( ), ,
3 (pid, status, rusage), pid
, status ,
rusage ,
resource.getrusage(). options ,
waitpid().
wait4(pid, options)
, waitpid(), , , wait3().
, waitpid(), wait3() wait4(), (UNIX).
WCOREDUMP(status)
True, core- .
WIFEXITED(status)
True,
exit().
WEXITSTATUS(status)
WIFEXITED() True, exit().
, .
WIFCONTINUED(status)
True, .
WIFSIGNALED(status)
True,
.
WIFSTOPPED(status)
True, .
WSTOPSIG(status)
, .
WTERMSIG(status)
, .

495

os


, :
confstr(name)
. name .
, os.confstr_names.
, . , ValueError.
, ,
OSError. ,
,
, (, 32- , 64- ) (UNIX).
getloadavg()
3
1, 5 15 (UNIX).
sysconf(name)
.
name .
os.sysconf_names.
-1, , . , , ValueError OSError. 100
.. , POSIX.1,
UNIX:

SC_ARG_MAX

exec().

SC_CHILD_MAX

SC_CLK_TCK

SC_NGROUPS_MAX

SC_STREAM_MAX

- .

SC_TZNAME_MAX

SC_OPEN_MAX

496

19.

()

SC_JOB_CONTROL

SC_SAVED_IDS

urandom(n)
n , (, /dev/urandom UNIX).
). .

os .
error
, .
OSError. :
errno strerr. ,
, errno. . , ,
,
filename , .

os.path
os.path
. os.
abspath(path)
path, . , abspath(../Python/foo) /home/beahome/bea/beabeazley/Python/foo.
basename(path)
path. , basename(/usr/local/
python) python.
commonprefix(list)
,
list. list , .
dirname(path)
path. ,
dirname(/usr/local/python) /usr/local.

os.path

497

exists(path)
True, path . False, path .
expanduser(path)
~user . path
~, path .
expandvars(path)
path $name ${name}
. , path .
getatime(path)
,
( time). os.stat_
float_times() True,
.
getctime(path)
UNIX , Windows . , ( time). (
getatime()).
getmtime(path)
,
( time). (
getatime()).
getsize(path)
.
isabs(path)
True, path (
).
isfile(path)
True, path .
,
path , islink() isfile(),
True.
isdir(path)
True, path . .

498

19.

islink(path)
True, path . ,
False.
ismount(path)
True, path .
join(path1 [, path2 [, ...]])
. ,
join(home, beazley, Python) home/beazley/Python.
lexists(path)
True, path . True, .
normcase(path)
path. ,
. , Windows .
normpath(path)
path. . , : A//B, A/./B A/
foo/../B A/B. Windows
.
realpath(path)
path, , (UNIX).
relpath(path [, start])
path .
start ,
.
samefile(path1, path2)
True, path1 path2
(UNIX).
sameopenfile(fp1, fp2)
True, fp1 fp2
(UNIX).
samestat(stat1, stat2)
True, stat1 stat2, fstat(), lstat() stat(),
(UNIX).

signal

499

split(path)
path (head, tail), tail , head , . , /home/user/foo (/home/ user,
foo). (dirname(),
basename()).
splitdrive(path)
path (drive, filename), drive
.. , , drive .
splitext(path)
path . ,
splitext(foo.txt) (foo, .txt).
splitunc(path)
path (unc,rest), unc
UNC (Universal Naming Convention
), rest
(Windows).
supports_unicode_filenames
, True,
.

Windows , (, C:spam.txt). , . , C:\Foo\,


C:spam.txt , C:\Foo\C:spam.
txt, C:\spam.txt.

.
fnmatch (. 398), glob (. 399), os (. 475).

signal
signal Python. , , ,
- . signal
, UNIX, ,
.

500

19.

alarm(time)
time , SIGALRM
time .
. time ,
.
, , , (UNIX).
getsignal(signalnum)
signalnum.
Python, .
SIG_IGN, , SIG_DFL, ,
None, Python.
getitimer(which)

which.
pause()
(UNIX).
set_wakeup_fd(fd)
, \0 .
,
, , select. ,
fd,
.
setitimer(which, seconds [, interval])
, seconds interval .
. which : ITIMER_REAL, ITIMER_VIRTUAL
ITIMER_PROF. , . ITIMER_REAL
SIGALRM, ITIMER_VIRTUAL SIGVTALRM ITIMER_PROF
SIGPROF. 0 seconds . (seconds, interval) .
siginterrupt(signalnum, flag)
signalnum. flag False, signalnum .
True, . , OSError IOError
errno.EINTR errno.EAGAIN.

501

signal

signal(signalnum, handler)
handler signalnum.
handler Python,, :
. , SIG_IGN SIG_DFL . , SIG_IGN SIG_DFL.

. ValueError.

SIG*. ,
. :

SIGABRT

SIGALRM

SIGBUS

SIGCHLD

SIGCLD

SIGCONT

SIGFPE

SIGHUP

SIGILL

SIGINT

SIGIO

SIGIOT

SIGKILL

SIGPIPE

SIGPOLL

SIGPROF

SIGPWR

SIGQUIT

SIGSEGV

SIGSTOP

502

19.

()

SIGTERM

SIGTRAP

SIGTSTP

SIGTTIN

SIGTTOU

SIGURG

SIGUSR1

SIGUSR2

SIGVTALRM

SIGWINCH

SIGXCPU

SIGXFSZ

, :

SIG_DFL

SIG_IGN

NSIG

, ( socket , ,
signal).
import signal, socket
def handler(signum, frame):
print Timeout!
raise IOError, Host not responding.
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)
# 5
sock.connect(www.python.org, 80) #
signal.alarm(0)
#

subprocess

503

,
. SIGCHLD (
).

, Python.
,
, C (, ).

-,
. errno
errno.EINTR, ,
.

, SIGSEGV, Python.

Python . SIGPIPE , SIGINT


KeyboardInterrupt,
SIGTERM sys.exitfunc.


.
.

Windows .
, , .

subprocess
subprocess , , , .
, , os, popen2 commands.
Popen(args, **parms)
, , Popen, .
args , ls -l, ,
[ls, -l]. parms , .
:

504

19.

bufsize

, 0
, 1 , , . 0.

close_fds

True,
, 0, 1 2, . False.

creation_flags

Windows.
CREATE_NEW_CONSOLE. 0.

cwd

, . cwd.
None,
.

env

.
None,
.

executable

. , args.
,
. None.

preexec_fn

,
.
.

shell

True,
UNIX, os.system().
/bin/sh,
executable. None.

startupinfo

Windows.
None. STARTF_USESHOWWINDOW STARTF_USESTDHANDLERS.

stderr

, stderr.
, open(), PIPE, . None.

stdin

, stdin.
, stderr. None.

505

subprocess

stdout

, stdout.
, stderr. None.

universal_newlines

True, , stdin,
stdout stderr,
. open().

call(args, **parms)
, Popen(), , ( Popen).
, - .
, Popen().
check_call(args, **parms)
, call(), , CalledProcessError. returncode .
p Popen, Popen(), , .
p.communicate([input])
input . , ,
. (stdout, stderr), stdout stderr
. , input None ( ).
p.kill()
, UNIX
SIGKILL, Windows p.terminate().
p.poll()
, . ,
. None.
p.send_signal(signal)
. signal
, signal. Windows SIGTERM.

506

19.

p.terminate()
, SIGTERM
UNIX Win32 API TerminateProcess Windows.
p.wait()
.
p.pid
.
p.returncode
. None ,
. , (UNIX).
p.stdin, p.stdout, p.stderr
, -, (, stdout Popen() PIPE).
. None, .

# os.system()
ret = subprocess.call(ls -l, shell=True)

# , ,
ret = subprocess.call(rm f *.java,shell=True,
stdout=open(/dev/null))

# ,
p = subprocess.Popen(ls -l, shell=True, stdout=subprocess.PIPE)
out = p.stdout.read()

# ,
p = subprocess.Popen(wc, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate(s) # s

#
p1 = subprocess.Popen(ls -l, shell=True, stdout=subprocess.PIPE)
p2 = subprocess.Popen(wc,shell=True, stdin=p1.stdout,
stdout=subprocess.PIPE)
out = p2.stdout.read()

, ,
(, [wc,filename] wc filename).

time

507

(, Documents and
Settings Windows). ,
, . , , , .
Windows . , , (\r\n
\n). , Popen()
True universal_newlines.
subprocess
, TTY.
, ( ssh, ftp, svn ). ,
Expect, UNIX.

time
time .
Python , . (, 0 ). UNIX
1 1970 , time.gmtime(0).
, :
accept2dyear
, . True, False,
$PYTHONY2K . , accept2dyear .
altzone
, (DST),
.
daylight
, , .
timezone
( ) .

508

19.

tzname
/ (
) .
time :
asctime([tuple])
, , gmtime() localtime(), Mon Jul 12 14:45:23 1999.
.
clock()
.
ctime([secs])
secs, , ,
. ctime(secs) asctime(localtime(secs)). None
.
gmtime([secs])
secs, (Coordinated Universal Time, UTC),
struct_time, :

tm_year

, 1998

tm_mon

1-12

tm_mday

1-31

tm_hour

0-23

tm_min

0-59

tm_sec

0-61

tm_wday

0-6 (0 = )

tm_yday

1-366

tm_isdst

-1, 0, 1

(DST), tm_isdst
1, 0 , -1 .
secs
None, . struct_time , , .
localtime([secs])
struct_time, gmtime(),
. -

509

time

secs None,
.
mktime(tuple)
struct_time ,
( , localtime()), , , . tuple ,
OverflowError.
sleep(secs)
secs .
secs .
strftime(format [, tm])
tm struct_time,
, localtime() gmtime(),
( strftime() tm , ). format
,
:

%a

%A

%b

%B

%c

%d

[01-31]

%H

( 24- ) [00-23]

%I

( 12- ) [01-12]

%j

[001-366]

%m

[01-12]

%M

[00-59]

%p

AM ( ) PM ( )

%S

[00-61]

%U

[00-53] (, ,
00)

%w

(0 = )

510

19.

()

%W

[00-53] (, ,
00)

%x

%X

%y

[00-99]

%Y

%Z

( ,
)

%%


,
% . -
ValueError.
.
strptime(string [, format])
string
struct_time , localtime() gmtime(). format
, strftime(). format %a %b %d %H:%M:%S %Y.
ctime().
string ValueError.
time()
(Coordinated Universal Time, UTC)
, .
tzset()
,
TZ UNIX. :
os.environ[TZ] = US/Mountain
time.tzset()

os.environ[TZ] = CST+06CDT,M4.1.0,M10.5.0
time.tzset()

,
, -

winreg

511

POSIX X/Open, 69-99 19691999, 0-68 2000-2068.


,
, , . , 50-100 .

.
datetime (. 421).

winreg
winreg ( Python 2 _winreg) Windows.. , . , ,
. , sys.path
:
\HKEY_LOCAL_MACHINE\Software\Python\PythonCore\2.6\PythonPath

Software HKEY_LOCAL_MACHINE, Python Software . PythonPath sys.path.


.
( , Windows).
CloseKey(key)
key.
ConnectRegistry(computer_name, key)

. computer_name , \\computername. computer_
name None, . key ,
HKEY_CURRENT_USER HKEY_ USERS.
EnvironmentError. HKEY_*, _winreg:

HKEY_CLASSES_ROOT

HKEY_CURRENT_CONFIG

HKEY_CURRENT_USER

HKEY_DYN_DATA

HKEY_LOCAL_MACHINE

512

19.

HKEY_PERFORMANCE_DATA

HKEY_USERS

CreateKey(key, sub_key)
.
key HKEY_*. sub_key ,
. key
, sub_key None;
key.
DeleteKey(key, sub_key)
sub_key. key
HKEY_*. sub_key , . sub_key
; EnvironmentError.
DeleteValue(key, value)
value. key HKEY_*.
value .
EnumKey(key, index)
index. key HKEY_*. index , .
index , EnvironmentError.
EnumValue(key, index)
. key
HKEY_*.
index , .
(name, data, type), name ,
data , , type , .
type, :

REG_BINARY

REG_DWORD

32-

REG_DWORD_LITTLE_ENDIAN

32-

REG_DWORD_BIG_ENDIAN

32-

513

winreg

REG_EXPAND_SZ

, \0

REG_LINK

REG_MULTI_SZ

, \0.

REG_NONE

REG_RESOURCE_LIST

REG_SZ

, \0.

ExpandEnvironmentStrings(s)
s, %name%, .
FlushKey(key)
key, . ,
, . , .
.
RegLoadKey(key, sub_key, filename)
.
key HKEY_*. sub_key ,
, . filename
, .
SaveKey(),
SE_RESTORE_PRIVILEGE. ConnectRegistry(), filename
.
OpenKey(key, sub_key[, res [, sam]])
. key
HKEY_*. sub_key
, . res , ( ).
sam ,
. KEY_READ.
sam:

KEY_ALL_ACCESS

KEY_CREATE_LINK

KEY_CREATE_SUB_KEY

KEY_ENUMERATE_SUB_KEYS

514

19.

KEY_EXECUTE

KEY_NOTIFY

KEY_QUERY_VALUE

KEY_READ

KEY_SET_VALUE

KEY_WRITE

OpenKeyEx()
, OpenKey().
QueryInfoKey(key)
(num_subkeys, num_values,
last_modified), num_subkeys ,
num_values last_modified
, .
, 1 1601 .
QueryValue(key,sub_key)
. key

HKEY_*. sub_key , .
sub_key , key.
. ( , QueryValueEx).
QueryValueEx(key, value_name)
(value, type)
. key
HKEY_*. value_name .. ,
EnumValue().
SaveKey(key, filename)
key . key

HKEY_*. filename
. ,
.
SetValue(key, sub_key, type, value)
. key HKEY_*. sub_
key , . type , REG_SZ. value
, . sub_key -

winreg

515

, . key
KEY_SET_VALUE.
SetValueEx(key, value_name, reserved, type, value)
. key HKEY_*. value_
name . type
, ,
EnumValue(). value , . (, REG_DWORD) value , .
struct. reserved
( ).

, HKEY Windows,, PyHKEY.


,
Win32 API, int(). ,
, , , :
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, spam) as key:

20


, Python.. : , ,
.
,
.


. , , , ,
, ,
, . , , . - .

, os subprocess (,
os.fork(), subprocess.Popen() ).
. ,
.
, . , , , .
,
, (Interprocess
Interprocess Communication,, IPC).
). .

517

. -,
,
, send() recv(). , ,
( mmap). . ,
.
, .
, . ,

. ,
, ,
, . ,
, ,
.
,
. ( ) , .
, 10 ,
1/10 . ,
,
,
.
, , .
. ,

( ). ,
, . , , , ,
, .
, :

518

20.
write_lock = Lock()
...
# ,
write_lock.acquire()
f.write(Heres some data.\n)
f.write(Heres more data.\n)
...
write_lock.release()

, (Jason
Whittington): ? . . , .
:
, ,
.

Python
Python . ,
, Python .
, Python
(Global Interpreter Lock, GIL), - .
Python
, . GIL Python,, , .
GIL , . , -, , ,
, ,
.
, , ,
,
(
, ). .
, ,
. , -

multiprocessing

519

100 ,
10 000. , , 10 000 ,
,
,
, ( , ).
. , -, select,
-.
, asyncore, ,
Twisted (http://twistedmatrix/com).
, , , Python,
. , . , . ,
. , ,
,

.
, Python,, . , , ,
. , , , , .
, .
.

multiprocessing
multiprocessing , , .
, threading. , .
, , .

520

20.

multiprocessing , .
, ,
.
.

, multiprocessing,
,
.
Process ([group [, target [, name [, args [, kwargs]]]]])
, , .
. target , , , args
target, kwargs
target. args kwargs, target
. name . group
None. threading.
p Process :
p.is_alive()
True, p .
p.join([timeout])
p. timeout . , , , ,
.
p.run()
, . target,
Process. ,
Process, run().
p.start()
.
p.run().
p.terminate()
. , - . p , -

multiprocessing

521

. .
p , .
p Process :
p.authkey
. ,
32- ,
os.urandom(). ,
, . ,
.
p.daemon
, . 1 , Python, . ,
. p.daemon
p.start().
p.exitcode
. , None.
N , N.
p.name
.
p.pid
.
,
( ) :
import multiprocessing
import time

def clock(interval):
while True:
print(The time is %s % time.ctime())
time.sleep(interval)

if __name__ == __main__:
p = multiprocessing.Process(target=clock, args=(15,))
p.start()


( ) UNIX. ,
. . .

522

20.

, , Process:
import multiprocessing
import time

class ClockProcess(multiprocessing.Process):
def __init__(self,interval):
multiprocessing.Process.__init__(self)
self.interval = interval
def run(self):
while True:
print(The time is %s % time.ctime())
time.sleep(self.interval)

if __name__ == __main__:
p = ClockProcess(15)
p.start()


15 . ,
, . UNIX , Windows . , , Windows (command.exe),
Python, IDLE.


multiprocessing : .
.
, .
Queue([maxsize])
.
maxsize , .
. . , .
q Queue :
q.cancel_join_thread()
. join_thread().
q.close()
, .

multiprocessing

523

, .
, q
. -
. , ()
get(), () get() .
q.empty()
True, q .
, ,
,
( ).
q.full()
True, q .
( q.empty()).
q.get([block [, timeout]])
q. q , . block
True. False,
Queue.Empty ( Queue). timeout , .
,
Queue.Empty.
q.get_nowait()
, q.get(False).
q.join_thread()
. , , q.close(). , q.
, q.cancel_join_thread().
q.put(item [, block [, timeout]])
item . , . block
True.
False, Queue.Full (
Queue). timeout
,

524

20.

. Queue.Full.
q.put_nowait(item)
, q.put(item, False).
q.qsize()
, . ,
. NotImplementedError.
JoinableQueue([maxsize])
, . Queue,
, , .

.
q JoinableQueue , Queue, :
q.task_done()
, , , q.get(), . ValueError,
,
.
q.join()
, , . ,
q.task_done().
, ,
. , .
import multiprocessing

def consumer(input_q):
while True:
item = input_q.get()
#
print(item)
#
#
input_q.task_done()

def producer(sequence, output_q):


for item in sequence:
#
output_q.put(item)

multiprocessing

525

#
if __name__ == __main__:
q = multiprocessing.JoinableQueue()
# -
cons_p = multiprocessing.Process(target=consumer,args=(q,))
cons_p.daemon=True
cons_p.start()

# .
# sequence ,
# .
# -
# .
sequence = [1,2,3,4]
producer(sequence, q)

# ,
q.join()

- , , ,
( ,
). -
, , JoinableQueue. join();
, -
, .
. , , , :
if __name__ == __main__:
q = multiprocessing.JoinableQueue()
# -
cons_p1 = multiprocessing.Process(target=consumer,args=(q,))
cons_p1.daemon=True
cons_p1.start()
cons_p2 = multiprocessing.Process(target=consumer,args=(q,))
cons_p2.daemon=True
cons_p2.start()

# .
# sequence ,
# .
# -
# .
sequence = [1,2,3,4]
producer(sequence, q)

# ,
q.join()

526

20.

,
, , . , , .
,
, .
, .
, None :
import multiprocessing

def consumer(input_q):
while True:
item = input_q.get()
if item is None:
break
#
print(item)
#
#
print( )

def producer(sequence, output_q):


for item in sequence:
#
output_q.put(item)

if __name__ == __main__:
q = multiprocessing.Queue()
# -
cons_p = multiprocessing.Process(target=consumer,args=(q,))
cons_p.start()

# .
sequence = [1,2,3,4]
producer(sequence, q)

# ,
q.put(None)

# , -
cons_p.join()

, , ,
. , , , - ,
.
.

multiprocessing

527

Pipe([duplex])
(conn1, conn2),
conn1 conn2 Connection, . .
duplex False, conn1 , conn2 . Pipe() Process, .
c Connection, Pipe(),
:
c.close()
. , c .
c.fileno()
, .
c.poll([timeout])
True . timeout . . timeout
None, .
c.recv()
, c.send().
, EOFError.
c.recv_bytes([maxlength])
, c.send_bytes().
maxlength ,
. ,
IOError, . , EOFError.
c.recv_bytes_into(buffer [, offset])
buffer, ,
( bytearray ). offset , .
.. , BufferTooShort.
c.send(obj)
. obj
, pickle.

528

20.

c.send_bytes(buffer [, offset [, size]])


. buffer
, . offset ,
size , . c.recv_bytes().
. ,
:
import multiprocessing

# .
def consumer(pipe):
output_p, input_p = pipe
input_p.close()
# ,
while True:
try:
item = output_p.recv()
except EOFError:
break
#
print(item)
#
#
print( )

# . sequence
# , .
def producer(sequence, input_p):
for item in sequence:
#
input_p.send(item)

if __name__ == __main__:
(output_p, input_p) = multiprocessing.Pipe()
# -
cons_p = multiprocessing.Process(target=consumer,args=((output_p,
input_p),))
cons_p.start()

# ,
output_p.close()

#
sequence = [1,2,3,4]
producer(sequence, input_p)

# , ,
input_p.close()

# , -
cons_p.join()

multiprocessing

529

. , . ,
- , ,
- , .
,
recv() -.
, , EOFError,
. ,
, ,
.

. ,
/, / . :
import multiprocessing
#
def adder(pipe):
server_p, client_p = pipe
client_p.close()
while True:
try:
x,y = server_p.recv()
except EOFError:
break
result = x + y
server_p.send(result)
#
print( )

if __name__ == __main__:
(server_p, client_p) = multiprocessing.Pipe()
#
adder_p = multiprocessing.Process(target=adder,args=((server_p,
client_p),))
adder_p.start()

#
server_p.close()

#
client_p.send((3,4))
print(client_p.recv())
client_p.send((Hello,World))
print(client_p.recv())

# .
client_p.close()

# ,
adder_p.join()

530

20.

adder() , . , .
, send() recv() pickle .
(x, y) x + y. ,
, .


, .
, .
Pool([numprocess [,initializer [, initargs]]])
. numprocess ,
. , , cpu_count(). initializer , . initargs ,
initializer. initializer None.
p Pool :
p.apply(func [, args [, kwargs]])
func(*args, **kwargs) . , func
, . func , , p.apply()
p.apply_async().
p.apply_async(func [, args [, kwargs [, callback]]])
func(*args, **kwargs) .
AsyncResult,
. callback , . func ,
callback. callback - ,
.
p.close()
. - , .

multiprocessing

531

p.join()
, . close() terminate().
p.imap(func, iterable [, chunksize])
map(),
.
p.imap_unordered(func, iterable [, chunksize]])
, imap(), ,
,
.
p.map(func, iterable [, chunksize])
func iterable
.
, iterable , . chunksize .
chunksize .
p.map_async(func, iterable [, chunksize [, callback]])
, map(), , . AsyncResult,
. callback , . ,
callback.
p.terminate()
,
- .
, p .
apply_async() map_async() a AsyncResult, :
a.get([timeout])
, , , .
timeout . , multiprocessing.TimeoutError. ,
.
a.ready()
True, .

532

20.

a.sucessful()
True, .
, , AssertionError.
a.wait([timeout])
, .
timeout .
,
SHA512 :
import os
import multiprocessing
import hashlib

#
BUFSIZE = 8192
#
POOLSIZE = 2
#

def compute_digest(filename):
try:
f = open(filename,rb)
except IOError:
return None
digest = hashlib.sha512()
while True:
chunk = f.read(BUFSIZE)
if not chunk: break
digest.update(chunk)
f.close()
return filename, digest.digest()

def build_digest_map(topdir):
digest_pool = multiprocessing.Pool(POOLSIZE)
allfiles = (os.path.join(path,name)
for path, dirs, files in os.walk(topdir)
for name in files)
digest_map = dict(digest_pool.imap_unordered(compute_digest, allfiles,20))
digest_pool.close()
return digest_map

# . .
if __name__ == __main__:
digest_map = build_digest_map(/Users/beazley/Software/Python-3.0)
print(len(digest_map))

-
.
imap_unordered().
SHA512
compute_digest() ,

multiprocessing

533

. ,
75-
, .
,
, ,
. ,
, .


. , .
( mmap),
.
Value(typecode, arg1, ... argN, lock)
ctypes . typecode
, array (,
i, d ), ctypes (, ctypes.c_int,
ctypes.c_double ). arg1, arg2,
..., argN . lock
.
True ( ), . , Lock RLock,
. , v , Value(),
v.value. , v.value , v.value .
RawValue(typecode, arg1, ..., argN)
, Value(), , .
Array(typecode, initializer, lock)
ctypes. typecode
,
Value(). initializer , , ,
. lock , Value(). , a , Array(),

, ,
. a

534

20.

a.value,
.
RawArray(typecode, initializer)
, Array(), , .
,
, ( ).
,
Value() Array(), multiprocessing
:

Lock

Rlock


(
, )

Semaphore

BoundedSemaphore

Event

Condition


, threading. threading.
, multiprocessing , , ,
. send() recv() put()
get() .

. ,
:
import multiprocessing

class FloatChannel(object):
def __init__(self, maxsize):
self.buffer
= multiprocessing.RawArray(d,maxsize)
self.buffer_len = multiprocessing.Value(i)
self.empty
= multiprocessing.Semaphore(1)
self.full
= multiprocessing.Semaphore(0)
def send(self,values):
self.empty.acquire()
# ,
nitems = len(values)

multiprocessing

535

self.buffer_len = nitems
#
self.buffer[:nitems] = values #
self.full.release()
# ,
def recv(self):
self.full.acquire()
# ,
values = self.buffer[:self.buffer_len.value] #
self.empty.release()
# ,
return values

# .
def consume_test(count, ch):
for i in xrange(count):
values = ch.recv()

# .
def produce_test(count, values, ch):
for i in xrange(count):
ch.send(values)

if __name__ == __main__:
ch = FloatChannel(100000)
p = multiprocessing.Process(target=consume_test,
args=(1000,ch))
p.start()
values = [float(x) for x in xrange(100000)]
produce_test(1000, values, ch)
print()
p.join()

, . ,
FloatChannel 80 , Pipe (
pickle).


, . , ,

Python,, , . multiprocessing , ,
. ,
. -,
.
,
Manager().

536

20.

Manager()
.
SyncManager, multiprocessing.managers.
m SyncManager, Manager(), ,
-, .
. :
m.Array(typecode, sequence)
Array - .
.
m.BoundedSemaphore([value])
threading.BoundedSema.BoundedSemaBoundedSemaphore - .
m.Condition([lock])
threading.Condition
- . lock -, m.Lock() m.Rlock().
m.dict([args])
dict
- . ,
dict().
m.Event()
threading.Event - .
m.list([sequence])
list
- . ,
list().
m.Lock()
threading.Lock - .
m.Namespace()
- .
,
Python. , , n -
, (.), : n.name = value value = n.name.

multiprocessing

537

, name . name ,
,
, .
name ,
- .
m.Queue()
Queue.Queue - .
m.RLock()
threading.Rlock - .
m.Semaphore([value])
threading.Semaphore
- .
m.Value(typecode, value)
Value - .
.
, .
import multiprocessing
import time

# d ,
#
def watch(d, evt):
while True:
evt.wait()
print(d)
evt.clear()

if __name__ == __main__:
m = multiprocessing.Manager()
d = m.dict()
#
evt = m.Event()
#

# ,
p = multiprocessing.Process(target=watch,args=(d,evt))
p.daemon=True
p.start()

#
d[foo] = 42
evt.set()
time.sleep(5)

#
d[bar] = 37

538

20.
evt.set()
time.sleep(5)

#
p.terminate()
m.shutdown()

, watch() d , .

. ,
.

, ,
. , BaseManager, multiprocessing.managers.
managers.BaseManager([address [, authkey]])
BaseManager -, . address (hostname, port), . , , .
authkey ,
.
, current_process().authkey.
mgrclass , BaseManager,
-, .
mgrclass.register(typeid [, callable [, proxytype [, exposed [, method_to_typeid
[, create_method]]]]])
. typeid
, . .. callable ,
. proxytype ,
-, . ,
None. exposed ,
-.
, proxytype._exposed_,
proxytype , ( ,
(_)). method_to_typeid
, , , -

multiprocessing

539

-.
, ,
. method_to_typeid None, proxytype._method_to_typeid_, . create_method ,
, typeid
mgrclass. True.
m ,
BaseManager, .
, :
m.address
(hostname, port) ,
.
m.connect()
, BaseManager.
m.serve_forever()
.
m.shutdown()
, m.start().
m.start()

.
, , :
import multiprocessing
from multiprocessing.managers import BaseManager

class A(object):
def __init__(self,value):
self.x = value
def __repr__(self):
return A(%s) % self.x
def getX(self):
return self.x
def setX(self,value):
self.x = value
def __iadd__(self,value):
self.x += value
return self

class MyManager(BaseManager): pass


MyManager.register(A,A)

if __name__ == __main__:
m = MyManager()
m.start()

540

20.
#
a = m.A(37)
...

A . a
- . (, ) , . , ,
. :
>>> a.x
Traceback (most recent call last):
File <stdin>, line 1, in <module>
AttributeError: AutoProxy[A] object has no attribute x
(:
( ):
<stdin>, 1, <>
AttributeError: AutoProxy[A] x
)
>>> a.getX()
37
>>> a.setX(42)
>>>

- repr()
-, str() __repr__() . :
>>> a
<AutoProxy[A] object, typeid A at 0xcef230>
>>> print(a)
A(37)
>>>

, ,
(_), -. , a.__iadd__() :
>>> a += 37
Traceback (most recent call last):
File <stdin>, line 1, in <module>
TypeError: unsupported operand type(s) for +=: AutoProxy[A] and int
(:
( ):
<stdin>, 1, <>
TypeError: +=: AutoProxy[A] int
)
>>> a.__iadd__(37)
Traceback (most recent call last):
File <stdin>, line 1, in <module>
AttributeError: AutoProxy[A] object has no attribute __iadd__
(:
( ):

multiprocessing

541

<stdin>, 1, <>
AttributeError: AutoProxy[A] __iadd__
)
>>>

-,
.
, BaseProxy, multiprocessing.managers. ,
- A
, __iadd__()
x:
from multiprocessing.managers import BaseProxy

class AProxy(BaseProxy):
# , -
_exposed_ = [__iadd__,getX,setX]
# -
def __iadd__(self,value):
self._callmethod(__iadd__,(value,))
return self
@property
def x(self):
return self._callmethod(getX,())
@x.setter
def x(self,value):
self._callmethod(setX,(value,))

class MyManager(BaseManager): pass


MyManager.register(A, A, proxytype=AProxy)

proxy , BaseProxy, :
proxy._callmethod(name [, args [, kwargs]])
name , . name , args kwargs . name
-. _exposed_ -.
proxy._getvalue()
, -.
, , .
, .

, multiprocessing, ,

542

20.

, . ,
,
, . multiprocessing.connection , :
connections.Client(address [, family [, authenticate [, authkey]]])
, address.
address (hostname, port), , UNIX
r\\servername\pipe\pipename,
Windows servername ( servername .).
family ,
address, AF_INET, AF_UNIX
AF_PIPE.
, address. authentication , , . authkey
.. , current_process().authkey. Connection,
, .
connections.Listener([address [, family [, backlog [, authenticate [, authkey]]]]])
Listener, , , Client(). address, family, authenticate authkey
, Client(). backlog , , listen() ,
address .
backlog 1. address ,
. , address family, .
s Listener :
s.accept()
Connection.
AuthenticationError.
s.address
, .
s.close()
, .

543

multiprocessing

s.last_accepted
, .
-, ():
from multiprocessing.connection import Listener

serv = Listener((,15000),authkey=12345)
while True:
conn = serv.accept()
while True:
try:
x,y = conn.recv()
except EOFError:
break
result = x + y
conn.send(result)
conn.close()

-, :
from multiprocessing.connection import Client
conn = Client((localhost,15000), authkey=12345)

conn.send((3,4))
r = conn.recv()
print(r)

# 7

conn.send((Hello,World))
r = conn.recv()
print(r)
# HelloWorld
conn.close()


:
active_children()
Process, .
cpu_count()
, .
current_process()
Process, .
freeze_support()
,

544

20.

,
py2exe.
, , .
get_logger()
multiprocessing; ,
.
, logging.NOTSET .
set_executable(executable)
Python,, .
Windows.


multiprocessing
multiprocessing Python.
,
:

, .
, , .

, , , pickle.



.
, . ,
.

, . .


, ( , ).

, .
, -

threading

545

,

, terminate().

- ( ). .

multiprocessing ,
pyprocessing. .


Windows,, , . , multiprocessing fork() UNIX Windows.
. ,
UNIX.

threading
threading Thread , .

Thread
Thread . :
Thread(group=None, target=None, name=None, args=(), kwargs={})
Thread. group
None .
target , run()
. None, , . name .
Thread-N.
args target,
kwargs target.
t Thread :
t.start()
run() .
.

546

20.

t.run()
. target, .
, Thread, run().
t.join([timeout])
. timeout , .
,
.
t.is_alive()
True, t , False
. start()
, run(). t.isAlive(),
.
t.name
.. ( ,
). t.getName() t.setName(name), .
t.ident
. ,
None.
t.daemon
, , . start().
, ,
. Python ,
, .
, , .
t.setDaemon(flag) t.isDaemon(), .
, ,
( ) :
import threading
import time

def clock(interval):
while True:

547

threading
print( : %s % time.ctime())
time.sleep(interval)

t = threading.Thread(target=clock, args=(15,))
t.daemon = True
t.start()

,
:
import threading
import time

class ClockThread(threading.Thread):
def __init__(self,interval):
threading.Thread.__init__(self)
self.daemon = True
self.interval = interval
def run(self):
while True:
print( : %s % time.ctime())
time.sleep(self.interval)

t = ClockProcess(15)
t.start()

,
__init__(), Thread.__init__(), . , . ,
- Thread,
run() __init__().
daemon , .
Python , . ,
. True
daemon
.
.

Timer
Timer .
Timer(interval, func [, args [, kwargs]])
, func interval . args kwargs
func. ,
start().

548

20.

t Timer :
t.start()
. func, Timer(),
, .
t.cancel()
, .

Lock
( ) , . acquire()
release(). , ,
. , ,
. , , .
Lock :
Lock()
, .
lock Lock :
lock.acquire([blocking ])
. , , . blocking False,
False, ,
True .
lock.release()
. ,
,
, acquire().

RLock
, ,
. , , acquire() release().
release() .
RLock :

threading

549

RLock()
.
rlock RLock :
rlock.acquire([blocking ])
. , .
, , 1. ,
.
rlock.release()
.
, .
. , .


, , acquire()
release().
, acquire() , -
release().
Semaphore([value])
. value
. 1.
s Semaphore :
s.acquire([blocking])
.
, 1 .
, , release(). blocking
, acquire() Lock RLock.
s.release()
1. , , .
,
acquire() . , , .
BoundedSemaphore([value])
. value
. 1. -

550

20.

BoundedSemaphore , Semaphore, , release() acquire().



, . ,
acquire() release() .
produced = threading.Semaphore(0)
consumed = threading.Semaphore(1)

def producer():
while True:
consumed.acquire()
produce_item()
produced.release()

def consumer():
while True:
produced.acquire()
item = get_item()
consumed.release()

, , , .

.
, .
Event ,
set() clear(). wait() , .
Event()
Event .
e Event :
e.is_set()
True, .
isSet().
e.set()
. , ,
, .
e.clear()
.
e.wait([timeout])
,
. , .

threading

551

,
set() timeout. timeout ,
.
Event
, , /
. , :
evt = Event()

def producer():
while True:
#
...
evt.signal()

def consumer():
while True:
#
evt.wait()
#
...
#
evt.clear()

,

evt.wait() evt.clear(). , ,
, .
, ,
, .
.


(condition variable) , ,
, .
-, ,
. Condition
:
Condition([lock])
. lock
Lock RLock.
RLock.
cv Condition :

552

20.

cv.acquire(*args)
, .
acquire(*args) .
cv.release()
, .
release() .
cv.wait([timeout])
,
. , .
, , notify() notifyAll() .
. timeout
, . , .
cv.notify([n])
,
. , , , , .
n ,
, 1. wait()
, .
cv.notify_all()
, . notifyAll().
,
:
cv = threading.Condition()
def producer():
while True:
cv.acquire()
produce_item()
cv.notify()
cv.release()

def consumer():
while True:
cv.acquire()
while not item_is_available():
cv.wait()
#

553

threading
cv.release()
consume_item()

,
,
, notify()
( ). ,
, .
, , consumer()
while. ,
, .


, Lock, RLock
Semaphore, .
. , ,
. :
try:
lock.acquire()
#

...
finally:
lock.release()

, ,
:
with lock:
#

...


with , .
,
. :
with lock_A:
# A

...
with lock_B:
# B

...

554

20.

. , (, ),
- .


. , ,
. , ,
. , , .

, .
,
. :
class StoppableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__()
self._terminate
= False
self._suspend_lock = threading.Lock()
def terminate(self):
self._terminate = True
def suspend(self):
self._suspend_lock.acquire()
def resume(self):
self._suspend_lock.release()
def run(self):
while True:
if self._terminate:
break
self._suspend_lock.acquire()
self._suspend_lock.release()

...

, ,
- -,
. ,
, ,
.
- , -
, ,
.

threading

555



:
active_count()
Thread.
current_thread()
Thread,
.
enumerate()
Thread.
local()
local, . .
setprofile(func)
, . func
sys.setprofile() .
settrace(func)
,
. func sys.
settrace() .
stack_size([size])
,
. size , . size
32 768 (32 ) , 4096
(4 ). ,
ThreadError.


Python , , .
,
,
.
, .
,

556

20.

-, . ,
,
C multiprocessing. C
, ,
. multiprocessing , .


Python ,
,
,
,
, .
( queue).

queue (Queue)
queue ( Python 2 Queue)
,

.
queue :
Queue([maxsize])
FIFO (first-in
first-in
-in
in first-out
-out
out , ). maxsize ,
.
maxsize 0, .
LifoQueue([maxsize])
LIFO (last-in, first-out ,
), , .
PriorityQueue([maxsize])
, , . (priority, data),
priority .
q :
q.qsize()
. ,
.

queue (Queue)

557

q.empty()
True, , False .
q.full()
True, , False
.
q.put(item [, block [, timeout]])
item . block
True ( ), . (
block False) Full.
timeout .
Full.
q.put_nowait(item)
q.put(item, False).
q.get([block [, timeout]])
.
block True ( ),
. ( block False)
Empty. timeout
. Empty.
q.get_nowait()
q.get(0).
q.task_done()
, , . ,
, .
q.join()
, . ,
q.task_done().


. , , ,
, . , ,
. ,
:

558

20.
import threading
from queue import Queue # Use from Queue on Python 2

class WorkerThread(threading.Thread):
def __init__(self,*args,**kwargs):
threading.Thread.__init__(self,*args,**kwargs)
self.input_queue = Queue()
def send(self,item):
self.input_queue.put(item)
def close(self):
self.input_queue.put(None)
self.input_queue.join()
def run(self):
while True:
item = self.input_queue.get()
if item is None:
break
#
# ( print - )
print(item)
self.input_queue.task_done()
# . , ,
self.input_queue.task_done()
return

#
w = WorkerThread()
w.start()
w.send(hello)
# ( )
w.send(world)
w.close()

. -, ,
Connection,
multiprocessing. . ,
,
.
-, . close() , .
, . , run()
.
, .

559


,
.
, (tasklets), 1 (green threads), (greenlets) .
,
.
, . , -
( select) ,
-.
, yield, - ,
, next() send().
- . :
def foo():
for n in xrange(5):
print( foo %d % n)
yield

def bar():
for n in xrange(10):
print( bar %d % n)
yield

def spam():
for n in xrange(7):
print( spam %d % n)
yield

#
from collections import deque
taskqueue = deque()
taskqueue.append(foo())
# ()
taskqueue.append(bar())
taskqueue.append(spam())

#
while taskqueue:
#
task = taskqueue.pop()
try:

.
. .

560

20.
# yield
next(task)
taskqueue.appendleft(task)
except StopIteration:
#
pass

, . , -, .

select, 21 .

21


,
.
Python ,

, HTTP.
(, )
.
, UNIX Network
Programming, Volume 1: Networking APIs: Sockets and XTI .
(W. Richard Stevens) ( Prentice Hall, 1997, ISBN 0-13490012-X).1 , , 22 -.


, Python
,
: TCP UDP. TCP
,
. UDP
, ,
, . TCP, UDP
, ,
. - TCP.

. UNIX. . . . .: , 2003.

562

21.

, . , ,
,
, .
,
.
, , () . 16-
0 65 535,
. 0 1023

. ( : http://www.iana.org/assignments/port-numbers):

FTP-Data

20

FTP-Control

21

SSH

22

Telnet

23

SMTP ( )

25

HTTP (WWW)

80

IMAP

143

HTTPS ( WWW)

443

TCP-
- , . 21.1.
, TCP,, , , ,
. , accept() ,
. .
UDP ,
,
, . 21.2.
TCP
, socket.
.
#
from socket import *
import time

563

socket()

socket()

bind()

listen()

accept()

read()

connect()

write()

write()

read()

. 21.1. TCP-

socket()

socket()

bind()

bind()

recvfrom()

sendto()

sendto()

recvfrom()

. 21.2. UDP

564

21.
s = socket(AF_INET, SOCK_STREAM) # TCP
s.bind((,8888))
# 8888
s.listen(5)
# ;
#
# 5 .
while True:
client,addr = s.accept()
#
print( %s % str(addr))
timestr = time.ctime(time.time()) + \r\n
client.send(timestr.encode(ascii))
client.close()

:
# ,
from socket import *
s = socket(AF_INET,SOCK_STREAM) # TCP
s.connect((localhost, 8888)) #
tm = s.recv(1024)
# 1024
s.close()
print( : %s % tm.decode(ascii))

UDP socket , .
. . Python 3 , , .

encode(ascii). , ,
. , , ,
, . , .
decode(ascii) .
,
. 22 , , .

asynchat
asynchat , , asyncore.
, asyncore, , , - (,
HTTP).

asynchat

565

,
async_chat. : collect_incoming_data() found_terminator().
, - .
. found_
terminator() , . ,
HTTP
.
async_chat
FIFO.. , . , .
async_chat([sock])
. .
async_chat asyncore.dispatcher . sock ,
.
a async_chat
, asyncore.dispatcher:
a.close_when_done()
,
None FIFO.. , .
a.collect_incoming_data(data)
. data , .
.
a.discard_buffers()
, -
FIFO.
a.found_terminator()
, , set_terminator(). . ,
collect_incoming_data().
a.get_terminator()
.
a.push(data)
FIFO. data
.

566

21.

a.push_with_producer(producer)
producer FIFO.
producer , more(). more()
. . async_chat
more(),
. FIFO ,
push_with_producer().
s.set_terminator(term)
. term
, None. term , , ,
found_terminator(). term , , . ,
,
found_terminator(). term None, .
, a.push_with_
producer() .
simple_producer(data [, buffer_size])
, data
. buffer_size 512.
asynchat asyncore. , asyncore , , asynchat
. ,
-, GET.
, .
, ,
asyncore.
# HTTP, asynchat
import asynchat, asyncore, socket
import os
import mimetypes
try:
from http.client import responses
# Python 3
except ImportError:
from httplib import responses
# Python 2

# asyncore
#
class async_http(asyncore.dispatcher):
def __init__(self,port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET,socket.SOCK_STREAM)

567

asynchat
self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.bind((,port))
self.listen(5)

def handle_accept(self):
client,addr = self.accept()
return async_http_handler(client)

# HTTP.
class async_http_handler(asynchat.async_chat):
def __init__(self,conn=None):
asynchat.async_chat.__init__(self,conn)
self.data = []
self.got_header = False
self.set_terminator(b\r\n\r\n)

#
def collect_incoming_data(self,data):
if not self.got_header:
self.data.append(data)

# ( )
def found_terminator(self):
self.got_header = True
header_data
= b.join(self.data)
# ()
#
header_text
= header_data.decode(latin-1)
header_lines
= header_text.splitlines()
request
= header_lines[0].split()
op
= request[0]
url
= request[1][1:]
self.process_request(op,url)

# ,
def push_text(self,text):
self.push(text.encode(latin-1))

#
def process_request(self, op, url):
if op == GET:
if not os.path.exists(url):
self.send_error(404,File %s not found\r\n)
else:
type, encoding = mimetypes.guess_type(url)
size = os.path.getsize(url)
self.push_text(HTTP/1.0 200 OK\r\n)
self.push_text(Content-length: %s\r\n % size)
self.push_text(Content-type: %s\r\n % type)
self.push_text(\r\n)
self.push_with_producer(file_producer(url))
else:
self.send_error(501,%s method not implemented % op)
self.close_when_done()

568

21.
#
def send_error(self,code,message):
self.push_text(HTTP/1.0 %s %s\r\n % (code, responses[code]))
self.push_text(Content-type: text/plain\r\n)
self.push_text(\r\n)
self.push_text(message)

class file_producer(object):
def __init__(self,filename,buffer_size=512):
self.f = open(filename,rb)
self.buffer_size = buffer_size
def more(self):
data = self.f.read(self.buffer_size)
if not data:
self.f.close()
return data

a = async_http(8080)
asyncore.loop()

, URL,, , ,
.

asyncore
asyncore , , , , , ,
select().
,
. .
dispatcher, .
dispatcher([sock])
, , . sock
. ,
create_socket() ( ).

-. , dispatcher , .

dispatcher. ,
dispatcher.
d.handle_accept()
, ,
.

asyncore

569

d.handle_close()
.
d.handle_connect()
, .
d.handle_error()
, .
d.handle_expt()
, .
d.handle_read()
, , .
d.handle_write()
, .
d.readable()
select(), , . True, , False
. , ,
handle_read(), .
d.writable()
select(), , . True, , False .
, ,
handle_write(), .
.
.
d.accept()
. (client, addr), client ,
, addr .
d.bind(address)
address. address
(host, port), .
d.close()
.
d.connect(address)
. address (host,
port).

570

21.

d.create_socket(family, type)
. ,
socket.socket().
d.listen([backlog])
. backlog
, socket.listen(), .
d.recv(size)
size .. , .
d.send(data)
data. data .

:
loop([timeout [, use_poll [, map [, count]]]])
. use_poll
False, select(), poll(). timeout 30 .
map .
count ,
, . count None ( ), loop() , .
count 1,
.

- asyncore. : asynhttp,
, asynclient, . , asynchat.
, , -
,
, , .
# HTTP
import asyncore, socket
import os
import mimetypes
import collections
try:
from http.client import responses

# Python 3

571

asyncore
except ImportError:
from httplib import responses

# Python 2

#
class async_http(asyncore.dispatcher):
def __init__(self,port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.bind((,port))
self.listen(5)

def handle_accept(self):
client,addr = self.accept()
return async_http_handler(client)

# ,
class async_http_handler(asyncore.dispatcher):
def __init__(self, sock = None):
asyncore.dispatcher.__init__(self,sock)
self.got_request
= False
# HTTP ?
self.request_data = b
self.write_queue
= collections.deque()
self.responding
= False

# ,
def readable(self):
return not self.got_request

#
def handle_read(self):
chunk = self.recv(8192)
self.request_data += chunk
if b\r\n\r\n in self.request_data:
self.handle_request()

#
def handle_request(self):
self.got_request = True
header_data = self.request_data[:self.request_data.find(b\r\n\r\n)]
header_text = header_data.decode(latin-1)
header_lines = header_text.splitlines()
request
= header_lines[0].split()
op
= request[0]
url
= request[1][1:]
self.process_request(op,url)

#
def process_request(self,op,url):
self.responding = True
if op == GET:
if not os.path.exists(url):
self.send_error(404,File %s not found\r\n % url)
else:
type, encoding = mimetypes.guess_type(url)

572

21.
size = os.path.getsize(url)
self.push_text(HTTP/1.0 200 OK\r\n)
self.push_text(Content-length: %d\r\n % size)
self.push_text(Content-type: %s\r\n % type)
self.push_text(\r\n)
self.push(open(url,rb).read())
else:
self.send_error(501,%s method not implemented % self.op)

#
def send_error(self,code,message):
self.push_text(HTTP/1.0 %s %s\r\n % (code, responses[code]))
self.push_text(Content-type: text/plain\r\n)
self.push_text(\r\n)
self.push_text(message)

#
def push(self,data):
self.write_queue.append(data)

#
def push_text(self,text):
self.push(text.encode(latin-1))

# ,
def writable(self):
return self.responding and self.write_queue

#
def handle_write(self):
chunk = self.write_queue.popleft()
bytes_sent = self.send(chunk)
if bytes_sent != len(chunk):
self.write_queue.appendleft(chunk[bytes_sent:])
if not self.write_queue:
self.close()

#
a = async_http(8080)
#
asyncore.loop()

.
socket (. 586), select (. 572), SocketServer (. 611),
http (. 623).

select
select select() poll().
select() ,
, -,
. UNIX , -

573

select

, . Windows
.
select(iwtd, owtd, ewtd [, timeout])
, .
, fileno(),
. iwtd ,
, owtd ,
, ewtd ,
.
. timeout , .
timeout , . 0, . ,
.
.
,
. select.er.ererror. ,
IOError OSError.
poll()
, poll().
,
poll().
p, poll(), :
p.register(fd [, eventmask])
fd. fd , fileno(), . eventmask ,
, :

POLLIN

, .

POLLPRI

, .

POLLOUT

POLLERR

POLLHUP

POLLNVAL

574

21.

eventmask POLLIN,
POLLPRI POLLOUT.
p.unregister(fd)
fd , . KeyError, .
p.poll([timeout])
. timeout . (fd, event),
fd , event , . POLLIN,
POLLOUT . , POLLIN,
event & POLLIN . , , .


select() poll(), , . , Linux
select
(epoll),
. BSD .

select, http://docs.python.org/library/select.


-
select ,
,
.
, , -. ,

, , . , A Curious Course
on Coroutines and Concurrency (http://www.dabeaz.com/coroutines),
.
import select
import types
import collections

# ,
class Task(object):
def __init__(self,target):
self.target = target #

575

select
self.sendval = None
self.stack = []

# ,
#

def run(self):
try:
result = self.target.send(self.sendval)
if isinstance(result,SystemCall):
return result
if isinstance(result,types.GeneratorType):
self.stack.append(self.target)
self.sendval = None
self.target = result
else:
if not self.stack: return
self.sendval = result
self.target = self.stack.pop()
except StopIteration:
if not self.stack: raise
self.sendval = None
self.target = self.stack.pop()

# ,
class SystemCall(object):
def handle(self,sched,task):
pass

#
class Scheduler(object):
def __init__(self):
self.task_queue
self.read_waiting
self.write_waiting
self.numtasks

=
=
=
=

collections.deque()
{}
{}
0

#
def new(self,target):
newtask = Task(target)
self.schedule(newtask)
self.numtasks += 1

#
def schedule(self,task):
self.task_queue.append(task)

# ,
#
def readwait(self,task,fd):
self.read_waiting[fd] = task

# ,
#
def writewait(self,task,fd):
self.write_waiting[fd] = task

#
def mainloop(self,count=-1,timeout=None):

576

21.
while self.numtasks:
# -
if self.read_waiting or self.write_waiting:
wait = 0 if self.task_queue else timeout
r,w,e = select.select(self.read_waiting, self.write_waiting,
[], wait)
for fileno in r:
self.schedule(self.read_waiting.pop(fileno))
for fileno in w:
self.schedule(self.write_waiting.pop(fileno))

# , ,
#
while self.task_queue:
task = self.task_queue.popleft()
try:
result = task.run()
if isinstance(result,SystemCall):
result.handle(self,task)
else:
self.schedule(task)
except StopIteration:
self.numtasks -= 1

# , ,
#
else:
if count > 0: count -= 1
if count == 0:
return

#
class ReadWait(SystemCall):
def __init__(self,f):
self.f = f
def handle(self,sched,task):
fileno = self.f.fileno()
sched.readwait(task,fileno)

class WriteWait(SystemCall):
def __init__(self,f):
self.f = f
def handle(self,sched,task):
fileno = self.f.fileno()
sched.writewait(task,fileno)

class NewTask(SystemCall):
def __init__(self,target):
self.target = target
def handle(self,sched,task):
sched.new(self.target)
sched.schedule(task)

. , :

select

577

. ,
, yield, , , ,
send(value).

Task
. task Task
task.run(). , ,
yield,
. task.sendval , yield . ,
yield. , , ,
:

(type.
GeneratorType), , . stack Task , . .

SystemCall, , ,
(,
, , -, ). .

- ,
: ,
, .
, , , ,
, ,
. , . ,
.

StopIteration ,
.. , , (
),
,
.

SystemCall . ,
, yield

578

21.

SystemCall. ,
, UNIX
Windows. ,
,
, .
SystemCall .

Scheduler Task,
. ( task_queue), , .
. new() , Task . schedule() Task . mainloop() ,
, .
readwait() writewait() Task , -. ,
, .

mainloop() .
, , . , select(), . - , ,
,
. mainloop() run().
- ( StopIteration), . ,
,
. ,
, -. mainloop() count, . ,
.

SystemCall mainloop(). SystemCall, handle(),


Scheduler Task. ,
,
. ReadWait(), WriteWait() NewTask() ,
- . ,

579

select

ReadWait() readwait()
.
. .
SystemCall , . SystemCall,
, ,
. ( ), .
, .
, :
from socket import socket, AF_INET, SOCK_STREAM
def time_server(address):
import time
s = socket(AF_INET,SOCK_STREAM)
s.bind(address)
s.listen(5)
while True:
yield ReadWait(s)
conn,addr = s.accept()
print( %s % str(addr))
yield WriteWait(conn)
resp = time.ctime() + \r\n
conn.send(resp.encode(latin-1))
conn.close()

sched = Scheduler()
sched.new(time_server((,10000)))
sched.new(time_server((,11000)))
sched.run()

# 10000
# 11000


(
telnet). yield ReadWait() yield WriteWait()
,
- .
, , accept() send().
ReadWait WriteWait . , , ,
. ,
:
class CoSocket(object):
def __init__(self,sock):

580

21.
self.sock = sock
def close(self):
yield self.sock.close()
def bind(self,addr):
yield self.sock.bind(addr)
def listen(self,backlog):
yield self.sock.listen(backlog)
def connect(self,addr):
yield WriteWait(self.sock)
yield self.sock.connect(addr)
def accept(self):
yield ReadWait(self.sock)
conn, addr = self.sock.accept()
yield CoSocket(conn), addr
def send(self,bytes):
while bytes:
evt = yield WriteWait(self.sock)
nsent = self.sock.send(bytes)
bytes = bytes[nsent:]
def recv(self,maxsize):
yield ReadWait(self.sock)
yield self.sock.recv(maxsize)

, CoSocket:
from socket import socket, AF_INET, SOCK_STREAM
def time_server(address):
import time
s = CoSocket(socket(AF_INET,SOCK_STREAM))
yield s.bind(address)
yield s.listen(5)
while True:
conn,addr = yield s.accept()
print(conn)
print( %s % str(addr))
resp = time.ctime()+\r\n
yield conn.send(resp.encode(latin-1))
yield conn.close()

sched = Scheduler()
sched.new(time_server((,10000)))
sched.new(time_server((,11000)))
sched.run()

# 10000
# 11000

CoSocket , . ,
yield (
, ). , :
- ? , ,
,
. -

581

select

, ,
yield.
-, ,
, . ,
asynchat asyncore.
import os
import mimetypes
try:
from http.client import responses
except ImportError:
from httplib import responses
from socket import *

# Python 3
# Python 2

def http_server(address):
s = CoSocket(socket(AF_INET,SOCK_STREAM))
yield s.bind(address)
yield s.listen(50)

while True:
conn,addr = yield s.accept()
yield NewTask(http_request(conn,addr))
del conn, addr

def http_request(conn,addr):
request = b
while True:
data = yield conn.recv(8192)
request += data
if b\r\n\r\n in request: break

header_data
= request[:request.find(b\r\n\r\n)]
header_text
= header_data.decode(latin-1)
header_lines = header_text.splitlines()
method, url, proto = header_lines[0].split()
if method == GET:
if os.path.exists(url[1:]):
yield serve_file(conn,url[1:])
else:
yield error_response(conn,404,File %s not found % url)
else:
yield error_response(conn,501,%s method not implemented % method)
yield conn.close()

def serve_file(conn,filename):
content,encoding = mimetypes.guess_type(filename)
yield conn.send(bHTTP/1.0 200 OK\r\n)
yield conn.send((Content-type: %s\r\n % content).encode(latin-1))
yield conn.send((Content-length: %d\r\n %
os.path.getsize(filename)).encode(latin-1))
yield conn.send(b\r\n)
f = open(filename,rb)

582

21.
while True:
data = f.read(8192)
if not data: break
yield conn.send(data)

def error_response(conn,code,message):
yield conn.send((HTTP/1.0 %d %s\r\n %
(code, responses[code])).encode(latin-1))
yield conn.send(bContent-type: text/plain\r\n)
yield conn.send(b\r\n)
yield conn.send(message.encode(latin-1))

sched = Scheduler()
sched.new(http_server((,8080)))
sched.mainloop()

,
.
, .



- ( asyncore
asynchat), ,
,
Python.
, .
- , ,
,
(. 20 ).
asyncore , -.
asynchat asyncore. ,
-. ,
-, .
- .
, ,
(, - -,
-, ).
Twisted (http://twistedmatrix.com),
, .

select

583

,
,
Python 2.5. , ,
. , ,
, , yield. Python, (http://www.stackless.
com), .
,
-. , ,
, , , . , select() , , . Linux ,
epoll(), .
, ,
(, )
, . - .
, , 10
:
bottles = 10000000

def drink_beer():
remaining = 12.0
while remaining > 0.0:
remaining -= 0.1

def drink_bottles():
global bottles
while bottles > 0:
drink_beer()
bottles -= 1

,
,
, . , ,
, :

584

21.
def server(port):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((,port))
s.listen(5)
while True:
client,addr = s.accept()
client.send((%d bottles\r\n % bottles).encode(latin-1)
client.close()

#
thr = threading.Thread(target=server,args=(10000,))
thr.daemon=True
thr.start()
drink_bottles()

,
- . ,
, :
def drink_bottles():
global bottles
while bottles > 0:
drink_beer()
bottles -= 1
scheduler.mainloop(count=1,timeout=0) #

# , .
def server(port):
s = CoSocket(socket.socket(socket.AF_INET,socket.SOCK_STREAM))
yield s.bind((,port))
yield s.listen(5)
while True:
client,addr = yield s.accept()
yield client.send((%d bottles\r\n % bottles).encode(latin-1)
yield client.close()

scheduler = Scheduler()
scheduler.new(server(10000))
drink_bottles()

, ,
, , , . ( ,
2 ) (
1000 ), ,
1 , 5 , . , , , ,
,
,

select

585

. , , ,
,
. 10 ,

.
,
. ,
, 50%. ,
,
1,2 ,
3% , . ,
, , .
, . , , . -
, .
,
. , . , , ,
, .

, . , - (,
aio_* UNIX).
Python ,
,
. ,
, , Python
.

586

21.

socket
socket
BSD.. UNIX,, socket .

(IP,
IP,, TIPC,, Bluetooth ). (Internet Protocol, IP),
, TCP UDP.. Python , IPv4 IPv6, IPv4
.
, ,
, .
, 22, SocketServer, .


socket . , .
, :

AF_BLUETOOTH

Bluetooth

AF_INET

IPv4 (TCP, UDP)

AF_INET6

IPv6 (TCP, UDP)

AF_NETLINK

Netlink

AF_PACKET

AF_TIPC


(Transparent Inter-Process Communication protocol, TIPC)

AF_UNIX

UNIX

AF_INET AF_INET6, .
AF_BLUETOOTH , (
). AF_NETLINK, AF_PACKET AF_TIPC Linux. AF_NETLINK
Linux. AF_PACKET
(,
ethernet). AF_TIPC ,
, Linux (http://tipc.sourceforge.net/).

587

socket


socket
. ( ) . , :

SOCK_STREAM

, (TCP)

SOCK_DGRAM

(UDP)

SOCK_RAW

SOCK_RDM

SOCK_SEQPACKET

SOCK_STREAM SOCK_
DGRAM, TCP UDP
(IP). SOCK_RDM UDP, ,
(
, ). SOCK_SEQPACKET
, .
SOCK_RDM SOCK_SEQPACKET ,
.
SOCK_RAW
,
(, ICMP).
SOCK_RAW ,
.
. , AF_PACKET ethernet Linux,, , SOCK_STREAM. SOCK_DGRAM
SOCK_RAW. AF_NETLINK SOCK_RAW.

,
. .

AF_INET (IPv4)
-, IPv4,
4, (host, port). :

588

21.
(www.python.org, 80)
(66.113.130.182, 25)

host ,
INADDR_ANY, .
, ,
. host <broadcast>, INADDR_BROADCAST API .
, , www.pywww.py.pypython.org, IP-
(DNS). DNS
IP-. ,
,
66.113.130.182.

AF_INET6 (IPv6)
IPv6 4
(host, port, flowinfo, scopeid). IPv6 host port
, IPv4, ,
IPv6
6 , , , : FEDC:BA98:7654:3210:FE
DC:BA98:7654:3210 080A::4:1 ( , , ,
).
flowinfo 32- , 24- ( 24 ) 4- ( 4 ), 4 .
,
.
flowinfo 0.
scopeid 32- ,
.
FE80:... ,
( ).
scopeid ,
. ,
ifconfig UNIX ipv6 if Windows.
FEC0:... , (, ). scopeid
.
flowinfo
scopeid IPv6 (host, port),
IPv4.

589

socket

AF_UNIX
UNIX ,
, : /tmp/myserver.

AF_PACKET
Linux ,
(device, protonum [, pkttype [, hatype [, addr]]]), device
, , eth0,
protonum , ethernet,
<linux/if_ether.h> (, 0x0800
IP ). packet_type , ,
:

PACKET_HOST

, .

PACKET_BROADCAST

PACKET_MULTICAST

PACKET_OTHERHOST

, ,
, (promiscuous).

PACKET_OUTGOING

, ,
.

hatype , , ARP,
<linux/if_arp.h>. addr , hatype. ethernet
addr , 6 .

AF_NETLINK
Netlink Linux ,
(pid, groups), pid groups
. pid ; , , 0,
. groups ,
.
Netlink.

AF_BLUETOOTH
Bluetooth . L2CAP
(addr, psm), addr , 01:23:45:67:
89:ab, psm . RFCOMM

590

21.

(addr, channel), addr


, channel . HCI (deviceno,), deviceno
. SCO host.
BDADDR_ANY 00:00:00:00:00:00. BDADDR_LOCAL
00:00:00:ff:ff:ff.

AF_TIPC
TIPC (addr_type, v1, v2, v3
[, scope]), . addr_
type ,
v1, v2 v3:

TIPC_ADDR_NAMESEQ

v1 , v2 v3 0.

TIPC_ADDR_NAME

v1 , v2 v3
.

TIPC_ADDR_ID

v1 , v2 v3 0.

scope :
TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE TIPC_NODE_SCOPE.

, socket:
create_connection(address [, timeout])
SOCK_STREAM address
. address
(host, port). timeout
. getaddrinfo()
.
fromfd(fd, family, socktype [, proto])
fd .
, , ,
socket().
. SocketType.
getaddrinfo(host, port [, family [, socktype [, proto [, flags]]]])
host port , , .
host IP-
- . port , (, http, ftp, smtp). -

socket

591

(family, socktype, proto, canonname,


sockaddr). family, socktype proto ,, socket(). canonname ,
. sockaddr
, , . :
>>> getaddrinfo(www.python.org,80)
[(2,2,17,,(194.109.137.226,80)), (2,1,6,,(194.109.137.226),80))]

getaddrinfo() .
UDP (proto=17), TCP
(proto=6). , flags getaddrinfo(). ,
IPv4 TCP:
>>> getaddrinfo(www.python.org,80,AF_INET,SOCK_STREAM)
[(2,1,6,,(194.109.137.226,80))]


, AF_UNSPEC. ,
TCP,
IPv4,
IPv6:
>>> getaddrinfo(www.python.org,http, AF_UNSPEC, SOCK_STREAM)
[(2,1,6,,(194.109.137.226,80))]

getaddrinfo()
(IPv4, IPv6
). , , , ,
IPv6.
getdefaulttimeout()
.
None , .
getfqdn([name])

name. , . , getfqdn(foo) foo.quasievil.org.
gethostbyname(hostname)
, www.python.org, IPv4. IP-
, 132.151.1.90. IPv6.
gethostbyname_ex(hostname)

592

21.

IPv4, (hostname,
aliaslist, ipaddrlist), hostname ,
aliaslist
, ipaddrlist IPv4
. , gethostbyname_ex(www.python.org)
: (fang.python.org, [www.python.
org], [194.109.137.226]). IPv6.
gethostname()
.
gethostbyaddr(ip_address)
, gethostbyname_ex(), IP-, 132.151.1.90. ip_address IPv6, FEDC:BA98:7654:3210:FEDC:BA98:7654:3210, , IPv6.
getnameinfo(address, flags)
address (host, port), flags. address , ,
: (www.python.org,80). flags ,
:

NI_NOFQDN

NI_NUMERICHOST

NI_NAMEREQD

. ,
address DNS.

NI_NUMERICSERV

port .

NI_DGRAM

, , UDP, TCP ( ).

, . :
>>> getnameinfo((194.109.137.226,80),0)
(fang.python.org, http)
>>> getnameinfo((194.109.137.226,80),NI_NUMERICSERV)
(fang.python.org,80)

getprotobyname(protocolname)
( icmp) (
IPPROTO_ICMP), socket()
. socket.error, .
(raw) .

socket

593

getservbyname(servicename [, protocolname])

. , getservbyname(ftp, tcp) 21. protocolname
tcp, udp. socket.error, servicename
.
getservbyport(port [, protocolname])
, getservbyname().
port ,
. , getservbyport(21, tcp) ftp. protocolname
tcp, udp. socket.error,
.
has_ipv6
, True,
IPv6.
htonl(x)
32-
32-
( , big-endian).
htons(x)
16-
16-
( , big-endian).
inet_aton(ip_string)
IPv4, (,
135.128.11.209), 32- 32- .
.
, C,, .
IPv6.
inet_ntoa(packedip)
IPv4 ,
c (, 135.128.11.209).
packedip ,
32- IP-. ,
, C,, . IPv6.
inet_ntop(address_family, packed_ip)
IP- packed_ip , 123.45.67.89. address_family
AF_INET AF_INET6.

594

21.

(, ).
inet_pton(address_family, ip_string)
IP-, 123.45.67.89, .
address_family , AF_INET AF_INET6.
.
ntohl(x)
32- ( , big-endian)
-endian)
endian)) 32- .
ntohs(x)
16- ( , big-endian)
-endian)
endian)) 16- .
setdefaulttimeout(timeout)
. timeout ,
. None, (
).
socket(family, type [, proto])
,, . family ,
type , . TCP
socket(AF_INET, SOCK_STREAM).
UDP socket(AF_INET, SOCK_DGRAM). SocketType ( ).
, , ( 0). (SOCK_RAW) ,
. , Python
AF_INET AF_INET6, :

IPPROTO_AH

IPv6

IPPROTO_BIP

Banyan VINES (Banyan


Banyan Virtual Network System,, )

IPPROTO_DSTOPTS IPv6

595

socket

IPPROTO_EGP

(EGP)

IPPROTO_EON

ISO CNLP (Connectionless Network Protocol,


)

IPPROTO_ESP

IPv6

IPPROTO_
FRAGMENT

IPv6

IPPROTO_GGP

(RFC 823)

IPPROTO_GRE

(RFC 1701)

IPPROTO_HELLO

HELLO FuzzBall

IPPROTO_HOPOPTS IPv6
IPPROTO_ICMP

IPv4 ICMP (Internet Control Message Protocol,


)

IPPROTO_ICMPV6

IPv6 ICMP

IPPROTO_IDP

XNS IDP (Xerox Network Services Internet Datagram Protocol,


Xerox )

IPPROTO_IGMP

IPPROTO_IP

IPv4

IPPROTO_IPCOMP

IP

IPPROTO_IPIP

IP IP

IPPROTO_IPV4

IPv4

IPPROTO_IPV6

IPv6

IPPROTO_MOBILE

IP

IPPROTO_ND

Netdisk

IPPROTO_NONE

IPv6,
6, ( )

IPPROTO_PIM

IPPROTO_PUP

Xerox
(PARC Universal Packet, PUP)

IPPROTO_RAW

IP

IPPROTO_ROUTING IPv6
IPPROTO_RSVP

IPPROTO_TCP

TCP

IPPROTO_TP

OSI (TP-4)

IPPROTO_UDP

UDP

IPPROTO_VRRP

IPPROTO_XTP

596

21.

, AF_BLUETOOTH:

BTPROTO_L2CAP

BTPROTO_HCI

BTPROTO_RFCOMM

BTPROTO_SCO

socketpair([family [, type [, proto ]]])


, family, type proto. ,
socket(). UNIX (family=AF_UNIX). type SOCK_DGRAM SOCK_STREAM. type
SOCK_STREAM, ,
. proto 0 ( ).
, os.fork(). , socketpair()
os.fork(). , .
SocketType.
s :
s.accept()
(conn, address), conn
,
, address
.
s.bind(address)
address. .
(hostname, port). IP- INADDR_ANY ( ), <broadcast>
INADDR_BROADCAST. INADDR_ANY ( ) hostname , ,
. ,
. INADDR_BROADCAST (<broadbroadcast>) hostname , .
s.close()
.. , .

socket

597

s.connect(address)
, address.
address , ,
(hostname, port).
socket.error. ,
, hostname
localhost.
s.connect_ex(address)
, connect(address),
0, errno.
s.fileno()
.
s.getpeername()
, .
(ipaddr, port), . .
s.getsockname()
.
(ipaddr, port).
s.getsockopt(level, optname [, buflen])
. level .
SOL_SOCKET, , IPPROTO_IP. optname .
buflen , ,
, .
buflen , , .
. struct .
,
Python.. API .
. , , C,
. .
SOL_SOCKET:

598

21.

SO_ACCEPTCONN

0, 1

SO_BROADCAST

0, 1

SO_DEBUG

0, 1

SO_DONTROUTE

0, 1

SO_ERROR

int

SO_EXCLUSIVEADDRUSE

0, 1

. SO_REUSEADDR.

SO_KEEPALIVE

0, 1


,
.

SO_LINGER

linger

close(), .
linger , 32-
(onoff, seconds).

SO_OOBINLINE

0, 1

SO_RCVBUF

int

( ).

SO_RCVLOWAT

int

,
,
select() , .

SO_RCVTIMEO

timeval


. timeval ,
32-
(seconds, microseconds).

SO_REUSEADDR

0, 1

SO_REUSEPORT

0, 1

,
,
1 .

SO_SNDBUF

int

( ).

SO_SNDLOWAT

int

,
,
select() , .

599

socket

SO_SNDTIMEO

timeval


.
timeval
SO_RCVTIMEO.

SO_TYPE

int

SO_USELOOPBACK

0, 1

IPPROTO_IP:

IP_ADD_MEMBERSHIP

ip_mreg


( ).
ip_mreg , 32-
IP- (multiaddr, localaddr),
multiaddr ,
localaddr IP-
.

IP_DROP_MEMBERSHIP

ip_mreg

( ).
ip_mreg .

IP_HDRINCL

int

IP-.

IP_MAX_MEMBERSHIPS

int

IP_MULTICAST_IF

in_addr

.
in_addr
, 32-
IP-.

IP_MULTICAST_LOOP

0, 1

, .

IP_MULTICAST_TTL

uint8

(time-to-live) .
int8
, 8-
.

IP_OPTIONS

ipopts

IP-. ipopts

44 .
RFC 791.

IP_RECVDSTADDR

0, 1

IP-
.

600

21.

()

IP_RECVOPTS

0, 1

IP .

IP_RECVRETOPTS

0, 1

IP .

IP_RETOPTS

0, 1

, IP_RECVOPTS, , .

IP_TOS

int

IP_TTL

int

TTL ( ).

IPPROTO_IPV6:

IPV6_CHECKSUM

0, 1

IPV6_DONTFRAG

0, 1


,
MTU.

IPV6_DSTOPTS

ip6_dest

. ip6_
dest (next, len, options),
next 8- , ;
len 8- , 8 ,
8 ; options .

IPV6_HOPLIMIT

int

IPV6_HOPOPTS

ip6_hbh

.
ip6_hbh , ip6_dest.

IPV6_JOIN_GROUP

ip6_mreg

.
ip6_mreg
(multiaddr,
index), multiaddr 128-
IPv6, index 32-
.

IPV6_LEAVE_GROUP

ip6_mreg

IPV6_MULTICAST_HOPS

int

601

socket

IPV6_MULTICAST_IF

int

IPV6_MULTICAST_LOOP

0, 1

IPV6_NEXTHOP

sockaddr_
in6


.
sockaddr_in6
,
sockaddr_in6 C,
<netinet/in.h>.

IPV6_PKTINFO

ip6_pktinfo .
ip6_pktinfo (addr, index),
addr 128- IPv6,
index 32- .

IPV6_RECVDSTOPTS

0, 1

IPV6_RECVHOPLIMIT

0, 1

IPV6_RECVHOPOPTS

0, 1

IPV6_RECVPKTINFO

0, 1

IPV6_RECVRTHDR

0, 1

IPV6_RECVTCLASS

0, 1

IPV6_RTHDR

ip6_rthdr

. ip6_
rthdr (next, len, type, segleft,
data), next, len, type segleft
8- ,
data .
RFC 2460.

IPV6_RTHDRDSTOPTS

ip6_dest

, .

IPV6_RECVPATHMTU

0, 1

IPV6_PATHMTU.

IPV6_TCLASS

int

IPV6_UNICAST_HOPS

int

602

21.

()

IPV6_USE_MIN_MTU

-1, 0, 1


MTU. 1
. -1
.

IPV6_V6ONLY

0, 1

,
IPv6.

SOL_TCP:

TCP_CORK

0, 1

1
.

TCP_DEFER_ACCEPT

0, 1

TCP_INFO

tcp_info

. tcp_info
.

TCP_KEEPCNT

int


, .

TCP_KEEPIDLE

int

,
,

, TCP_KEEPALIVE.

TCP_KEEPINTVL

int

, .

TCP_LINGER2

int

FIN_WAIT2.

TCP_MAXSEG

int

TCP.

TCP_NODELAY

0, 1

1 .

TCP_QUICKACK

0, 1

1 ACK . ACK.

TCP_SYNCNT

int


SYN , .

603

socket

TCP_WINDOW_CLAMP

int


TCP.

s.gettimeout()
,
. None,
.
s.ioctl(control, option)
WSAIoctl
Windows. control SIO_
RCVALL, IP-
- . . option
:

RCVALL_OFF

IPv4 IPv6.

RCVALL_ON

(promiscuous)
promiscuous)) IPv4
Pv4
v4 IPv6,
Pv6,
v6, .
, .
, ARP,
.

RCVALL_IPLEVEL

IP-, . ,
IP-, .

s.listen(backlog)
.
backlog ,
, ,
.
1, 5 .
s.makefile([mode [, bufsize]])
, . mode
bufsize , open().
, os.dup(),
. s .
s.recv(bufsize [, flags])
. . bufsize.

604

21.

flags , (
0). , , ,
(
):

MSG_DONTROUTE

( ).

MSG_DONTWAIT

MSG_EOR

.
SOCK_SEQPACKET.

MSG_PEEK

( ).

MSG_OOB

/ .

MSG_WAITALL

,
( ).

s.recv_into(buffer [, nbytes [, flags]])


, recv(), , buffer, . nbytes . ,
buffer. flags , recv().
s.recvfrom(bufsize [, flags])
recv(), (data,
address), data ,
address , . flags , recv(). UDP.
s.recvfrom_info(buffer [, nbytes [, flags]])
, recvfrom(),
buffer. nbytes . , buffer. flags ,
recv().
s.send(string [, flags])
.
flags , recv(). .
string. .
s.sendall(string [, flags])
; ,
, .

socket

605

None; . flags
, send().
s.sendto(string [, flags], address)
.
flags , recv(). address (host, port), .
. . UDP.
s.setblocking(flag)
flag , .
( ). ,
recv() send() , socket.er.ererror.
,
.
s.setsockopt(level, optname, value)
value option .
level optname , getsockopt(). value , . ,
. ,
getsockopt().
s.settimeout(timeout)
,
. timeout , . None
.
socket.timeout.
, , ,
( connect()).
s.shutdown(how)
. how 0, . how 1, . how 2,
, .
s , ,
socket().

606

21.

s.family

(, AF_INET)

s.proto

s.type

(, SOCK_STREAM)

, socket.
error
, . (errno,
mesg), , . IOError.
herror
, . (herrno, hmesg)
. error.
gaierror
, ,
getaddrinfo() getnameinfo().
(errno, mesg), errno ,
mesg . errno , socket:

EAI_ADDRFAMILY

EAI_AGAIN

EAI_BADFLAGS

EAI_BADHINTS

EAI_FAIL

EAI_FAMILY

EAI_MEMORY

EAI_NODATA

, .

EAI_NONAME

EAI_PROTOCOL

EAI_SERVICE

EAI_SOCKTYPE

EAI_SYSTEM

socket

607

timeout
.. setdefaulttimeout()
settimeout() .
timeout. error.


TCP.. -, , UDP:
# , UDP
#
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((,10000))
while True:
data, address = s.recvfrom(256)
print( %s % str(address))
s.sendto(becho: + data, address)

, ,
:
# , UDP
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(bHello World, (, 10000))
resp, addr = s.recvfrom(256)
print(resp)
s.sendto(bSpam, (, 10000))
resp, addr = s.recvfrom(256)
print(resp)
s.close()

.
,
, , . (W. Richard Stevens)
UNIX Network Programming,1 .

socket
recvmsg() sendmsg(), ,
,
. , -

. UNIX. . . . .: , 2003.

608

21.

, PyXAPI (http://pypi.python.
org/pypi/PyXAPI).

, ,
,
, . , ,
, .
,
,
.

ssl
ssl ,
(Secure Sockets Layer, SSL), .
OpenSSL (http://www.openssl.org).
SSL
.
; ,
SSL, ,
:
wrap_socket(sock [, **opts])
sock ( socket) SSL SSLSocket.
connect() accept(). opts , .

server_side

, , (True) (False).
False.

keyfile

,, .. PEM , certfile
, .

certfile

, . PEM.

cert_reqs

,
.
CERT_NONE , , CERT_
OPTIONAL , , CERT_REQUIRED ,
. , ca_certs.

609

ssl

ca_certs

, , .

ssl_version

SSL. :
PROTOCOL_TLSv1, PROTOCOL_SSLv2, PROTOCOL_SSLv23 PROTOCOL_SSLv3.
PROTOCOL_SSLv3.

do_handshake_
on_connect

, , SSL .
True.

suppress_
ragged_eofs

, read() .
True ( ), .
False, .

s SSLSocket, socket.socket, :
s.cipher()
(name, version, secretbits), name
, version SSL secretbits
.
s.do_handshake()
SSL.. , wrap_socket() do_handshake_on_connect False. s ,
SSLError . e.args[0] SSLError
SSL_ERROR_WANT_READ SSL_ERROR_WANT_WRITE, ,
. ,
, s.do_handshake().
s.getpeercert([binary_form])
, .
None. , , . ,
subject notAfter. binary_form
True,
DER.
s.read([nbytes])
nbytes .
nbytes 1024 .
s.write(data)
data. .

610

21.

s.unwrap()
SSL , .
, :
cert_time_to_seconds(timestring)
timestring , ,
, time.time().
DER_cert_to_PEM_cert(derbytes)
derbytes DER PEM.
PEM_cert_to_DER_cert(pemstring)
pemstring PEM
DER.
get_server_certificate(addr [, ssl_version [, ca_certs]])
SSL
PEM. addr (hostname, port).
ssl_version SSL,, ca_certs , wrap_socket().
RAND_status()
True, SSL .
RAND_egd(path)
256 . path .
RAND_add(bytes, entropy)
bytes . entropy
, .

, SSL- :
import socket, ssl

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_s = ssl.wrap_socket(s)
ssl_s.connect((gmail.google.com,443))
print(ssl_s.cipher())

#
ssl_s.write(bGET / HTTP/1.0\r\n\r\n)

SocketServer

611

#
while True:
data = ssl_s.read()
if not data: break
print(data)
ssl_s.close()

,
SSL:
import socket, ssl, time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
s.bind((,12345))
s.listen(5)

while True:
client, addr = s.accept() #
print , addr
client_ssl = ssl.wrap_socket(client,
server_side=True,
certfile=timecert.pem)
client_ssl.sendall(bHTTP/1.0 200 OK\r\n)
client_ssl.sendall(bConnection: Close\r\n)
client_ssl.sendall(bContent-type: text/plain\r\n\r\n)
resp = time.ctime() + \r\n
client_ssl.sendall(resp.encode(latin-1))
client_ssl.close()
client.close()

, timecert.pem. UNIX:
% openssl req new x509 days 30 nodes out timecert.pem keyout timecert.pem

,
-, URL https://localhost:1234.
, , . ,
, .

SocketServer
Python 3 socketserver. SocketServer , TCP,
UDP UNIX.

, BaseRequestHandler. h Base-

612

21.

RequestHandler ,
:
h.finish()
, handle() . .
, setup() handle() .
h.handle()
.
,
. h.request
, h.client_address h.server
, . , TCP,
h.request . .
h.setup()
handle()
. .. ,
SSL,, .
, , , :
try:
from socketserver import BaseRequestHandler # Python 3
except ImportError:
from SocketServer import BaseRequestHandler # Python 2
import socket
import time

class TimeServer(BaseRequestHandler):
def handle(self):
resp = time.ctime() + \r\n
if isinstance(self.request,socket.socket):
#
self.request.sendall(resp.encode(latin-1))
else:
#
self.server.socket.sendto(resp.encode(latin-1),
self.client_address)

,
, TCP,, StreamRequestHandler, BaseRequestHandler. : h.wfile , ,
, h.rfile , , . :

SocketServer

613

try:
from socketserver import StreamRequestHandler # Python 3
except ImportError:
from SocketServer import StreamRequestHandler # Python 2
import time

class TimeServer(StreamRequestHandler):
def handle(self):
resp = time.ctime() + \r\n
self.wfile.write(resp.encode(latin-1))


, DatagramRequestHandler BaseRequestHandler.
, StreamRequestHandler. :
try:
from socketserver import DatagramRequestHandler # Python 3
except ImportError:
from SocketServer import DatagramRequestHandler # Python 2
import time

class TimeServer(DatagramRequestHandler):
def handle(self):
resp = time.ctime() + \r\n
self.wfile.write(resp.encode(latin-1)

, self.wfile,
, handle().

,
. :
TCPServer(address, handler)
, TCP IPv4. address (host, port). handler
BaseRequestHandler, .
UDPServer(address, handler)
, UDP IPv4. address handler
, TCPServer().
UnixStreamServer(address, handler)
, UNIX. TCPServer.
UnixDatagramServer(address, handler)
,
UNIX. UDPServer.

614

21.

:
s.fileno()
.
, select().
s.serve_forever()
.
s.shutdown()
serve_forever().
:
s.RequestHandlerClass
,
.
s.server_address
, , : (127.0.0.1, 80).
s.socket
, .
TimeHandler
TCP:
from SocketServer import TCPServer

serv = TCPServer((,10000,TimeHandler)
serv.serve_forever()


UDP:
from SocketServer import UDPServer

serv = UDPServer((,10000,TimeHandler)
serv.serve_forever()

SocketServer .
, .


, , , , . , -

SocketServer

615

, .
, ,
:
Server.address_family
, . socket.AF_INET.
IPv6, socket.AF_INET6.
Server.allow_reuse_address
, . , ,
( ).
False.
Server.request_queue_size
, listen() .
5.
Server.socket_type
, , socket.SOCK_STREAM socket.SOCK_DGRAM.
Server.timeout
,
. handle_timeout() ( ),
. .
,
.
,
:
from SocketServer import TCPServer

class TimeServer(TCPServer):
allow_reuse_address = True

serv = TimeServer((,10000,TimeHandler)
serv.serve_forever()

, ,
. , .
Server.activate()
listen() .
self.socket.

616

21.

Server.bind()
bind() .
Server.handle_error(request, client_address)
, .
sys.exc_info() traceback.
Server.handle_timeout()
, . , .
Server.verify_request(request, client_address)
, . - .
,
-.
, . :
ForkingMixIn
-, UNIX , .
max_children ,
timeout , -. active_children .
ThreadingMixIn
-, ,
, . . ,
daemon_threads True.
, -
. , , :
from SocketServer import TCPServer, ForkingMixIn

class TimeServer(ForkingMixIn, TCPServer):


allow_reuse_address = True
max_children = 10

serv = TimeServer((,10000,TimeHandler)
serv.serve_forever()

SocketServer

617

, ,
, SocketServer .

ForkingUDPServer(address, handler)

ForkingTCPServer(address, handler)

ThreadingUDPServer(address, handler)

ThreadingTCPServer(address, handler)

,
- . ForkingTCPServer:
class ForkingTCPServer(ForkingMixIn, TCPServer): pass


SocketServer
, , HTTP XML-RPC.
, .
, XML-RPC,
, ,
(loopback) :
try:
from xmlrpc.server import SimpleXMLRPCServer
# Python 3
from socketserver import ForkingMixIn
except ImportError:
# Python 2
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SocketServer import ForkingMixIn

class MyXMLRPCServer(ForkingMixIn,SimpleXMLRPCServer):
def verify_request(self, request, client_address):
host, port = client_address
if host != 127.0.0.1:
return False
return SimpleXMLRPCServer.verify_request(self,request,client_address)

#
def add(x,y):
return x+y
server = MyXMLRPCServer((,45000))
server.register_function(add)
server.serve_forever()

, xmlrpclib. , ,
Python:
>>> import xmlrpclib
>>> s = xmlrpclib.ServerProxy(http://localhost:45000)

618

21.
>>> s.add(3,4)
7
>>>

, , .
localhost ,
.

22

-
, , HTTP, XML-RPC, FTP and SMTP.
, -, CGI, 23 -. ,
, , 24 .
,
Python 2 3 .
, Python 3, . Python .
Python 2.

ftplib
ftplib FTP.
,
urllib, . , FTP-.
-. FTP,, RFC 959.
, FTP:
FTP([host [, user [, passwd [, acct [, timeout]]]]])
, FTP-. host . user, passwd acct -

620

22. -

, .
, , connect() login().
host, connect(). user, passwd acct, login().
timeout .
f FTP :
f.abort()
.
, .
f.close()
FTP-.
f.
f.connect(host [, port [, timeout]])
FTP- .
host , port
FTP (
21). timeout . , FTP().
f.cwd(pathname)
, pathname.
f.delete(filename)
filename .
f.dir([dirname [, ... [, callback]]])
, LIST.
dirname , .1 , LIST.
callback , . ,
retrlines().
sys.stdout.
f.login([user, [passwd [, acct]]])
, , . user
; anonymous. 1

. . .

ftplib

621

passwd ;
( ). acct , . ,
FTP().
f.mkd(pathname)
.
f.ntransfercmd(command [, rest])
, transfercmd(), ,
(sock, size), sock , , , size
None, .
f.pwd()
.
f.quit()
FTP-, QUIT.
f.rename(oldname, newname)
.
f.retrbinary(command, callback [, blocksize [, rest]])
, . command , ,
RETR filename. callback
,
. , .
blocksize .
8192 . rest
. ,
, .
FTP , error_reply.
f.retrlines(command [, callback])
,, . command , , RETR filename.
callback , .
, . callback sys.stdout.
f.rmd(pathname)
pathname .

622

22. -

f.sendcmd(command)
.
command .
, .
f.set_pasv(pasv)
. pasv
, True ,
False . .
f.size(filename)
filename . None,
- .
f.storbinary(command, file [, blocksize])
, . command , STOR filename,
filename ,
. file , file.read(blocksize)
. blocksize . 8192 .
f.storlines(command, file)
, . command , STOR filename. file , file.
readline() .
f.transfercmd(command [, rest])
FTP-
- . , PORT EPRT . , EPSV PASV
. , , FTP- command.
, ,
. rest
. FTP ,
error_reply.

, FTP:
host
= ftp.foo.com
username = dave

623

http
password = 1235
filename = somefile.dat

import ftplib
ftp_serv = ftplib.FTP(host,username,password)
# ,
f = open(filename,rb)
# FTP
resp = ftp_serv.storbinary(STOR +filename, f)
#
ftp_serv.close

FTP urllib.
:
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen

# Python 3
# Python 2

u = urlopen(ftp://username:password@somehostname/somefile)
contents = u.read()

http
http ,
HTTP, (cookies). (Hypertext Transfer Protocol,
HTTP) ,
:
1. HTTP :
GET /document.html HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.61 [en] (X11; U; SunOS 5.6 sun4u)
Host: rustler.cs.uchicago.edu:8000
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8


...

, ()
. -
, , cookies,, . -
, . ,
-

624

22. -

.
(\r\n).
2. :
HTTP/1.0 200 OK
Content-type: text/html
Content-length: 72883 bytes
...
:

...

HTTP,
.
- ,
, -,
cookies . , , .
:

GET

POST

HEAD

PUT

, , . 22.1.
http.client, , .
22.1. ,


200

OK

201

CREATED

202

ACCEPTED

204

NO_CONTENT

625

http

(3xx)
300

MULTIPLE_CHOICES

301

MOVED_PERMANENTLY

302

MOVED_TEMPORARILY

303

NOT_MODIFIED

(4xx)
400

BAD_REQUEST

401

UNAUTHORIZED

403

FORBIDDEN

404

NOT_FOUND

(5xx)
500

INTERNAL_SERVER_ERROR

501

NOT_IMPLEMENTED

502

BAD_GATEWAY

503

SERVICE_UNAVAILABLE

, , ,
, RFC-822.
-822. : ;
RFC.. , .

http.client (httplib)
http.client HTTP
. Python 2 httplib.
urllib.
, HTTP/1.0
/1.0 HTTP/1.1,
/1.1, SSL-,
Python OpenSSL.
; urllib. ,
HTTP, ,
,
urllib,
, GET POST. HTTP RFC 2616 (HTTP/1.1)
RFC 1945 (HTTP/1.0).

626

22. -

,
HTTP- :
HTTPConnection(host [,port])
HTTP-. host , port . 80.
HTTPConnection.
HTTPSConnection(host [, port [, key_file=kfile [, cert_file=cfile]]])
HTTP-, .
443. key_file cert_file
PEM,, . . HTTPSConnection.
h HTTPConnection HTTPSConnection :
h.connect()
, HTTPConnection() HTTPSConnection().
, .
h.close()
.
h.send(bytes)
bytes .
, /. h.endheaders().
h.putrequest(method, selector [, skip_host [, skip_accept_encoding]])
. method HTTP,
GET POST. selector
, : /index.html. skip_host skip_accept_en_accept_enaccept_en_enencoding , HTTP Host:
Accept-Encoding:. False.
HTTP/1.1
, , ,
CannotSendRequest.
h.putheader(header, value, ...)
, RFC-822.
-822. , , , . . , ,
CannotSendRequest.

http

627

h.endheaders()
, .
h.request(method, url [, body [, headers]])
HTTP. method url
, h.putrequest(). body ,
. body , Contextlength: . headers , header:value h.putheader().
h.getresponse()
HTTPResponse, . h , , ResponseNotReady.
r HTTPResponse, getresponse(),
:
r.read([size])
size . size , .
r.getheader(name [,default])
. name ,
default , .
r.getheaders()
(header, value).
, r HTTPResponse :
r.version
HTTP, .
r.status
HTTP, .
r.reason
HTTP, .
r.length
, .

HTTP-
- :

628

22. -

HTTPException

,
HTTP.

NotConnected

InvalidURL

URL .

UnknownProtocol

HTTP.

UnknownTransferEncoding

UnimplementedFileMode

IncompleteRead

BadStatusLine

, HTTP/1.1.
/1.1. HTTP/1.1
/1.1 / , , , ,
.
.

ImproperConnectionState

,
HTTP-.

CannotSendRequest

CannotSendHeader

ResponseNotReady

HTTPConnection
POST ,
, urllib.
import os
try:
from httplib import HTTPConnection
# Python 2
except ImportError:
from http.client import HTTPConnection # Python 3

BOUNDARY = $Python-Essential-Reference$
CRLF
= \r\n

def upload(addr, url, formfields, filefields):


#
formsections = []
for name in formfields:

629

http
section = [
--+BOUNDARY,
Content-disposition: form-data; name=%s % name,
,
formfields[name]
]
formsections.append(CRLF.join(section)+CRLF)

#
fileinfo = [(os.path.getsize(filename), formname, filename)
for formname, filename in filefields.items()]

# HTTP-
filebytes = 0
fileheaders = []
for filesize, formname,filename in fileinfo:
headers = [
--+BOUNDARY,
Content-Disposition: form-data; name=%s; filename=%s % \
(formname, filename),
Content-length: %d % filesize,

]
fileheaders.append(CRLF.join(headers)+CRLF)
filebytes += filesize

#
closing = --+BOUNDARY+--\r\n

#
content_size = (sum(len(f) for f in formsections) +
sum(len(f) for f in fileheaders) +
filebytes+len(closing))

#
conn = HTTPConnection(*addr)
conn.putrequest(POST,url)
conn.putheader(Content-type,
multipart/form-data; boundary=%s % BOUNDARY)
conn.putheader(Content-length, str(content_size))
conn.endheaders()

#
for s in formsections:
conn.send(s.encode(latin-1))

#
for head,filename in zip(fileheaders,filefields.values()):
conn.send(head.encode(latin-1))
f = open(filename,rb)
while True:
chunk = f.read(16384)
if not chunk: break
conn.send(chunk)
f.close()
conn.send(closing.encode(latin-1))

630

22. -
r = conn.getresponse()
responsedata = r.read()
conn.close()
return responsedata

# : .
# name, email, file_1,file_2
# (, ).
server
= (localhost, 8080)
url
= /cgi-bin/upload.py
formfields = {
name : Dave,
email : dave@dabeaz.com
}
filefields = {
file_1 : IMG_1008.JPG,
file_2 : IMG_1757.JPG
}
resp = upload(server, url,formfields,filefields)
print(resp)

http.server
(BaseHTTPServer, CGIHTTPServer, SimpleHTTPServer)
http.server , HTTP. Python 2
: BaseHTTPServer, CGIHTTPServer SimpleHTTPServer.

HTTPServer
HTTP.. Python 2 BaseHTTPServer.
HTTPServer(server_address, request_handler)
HTTPServer. server_address (host, port), ,
. request_handler
-, BaseHTTPRequestHandler, .
HTTPServer TCPServer, socketserver. , HTTP,
, HTTPServer, . HTTP,
:
try:
from http.server import HTTPServer
from socketserver import ThreadingMixIn
except ImportError:
from BaseHTTPServer import HTTPServer
from SocketServer import ThreadingMixIn

# Python 3

# Python 2

631

http
class MyHTTPServer(ThreadingMixIn,HTTPServer):
def __init__(self,addr,handler,subnet):
HTTPServer.__init__(self,addr,handler)
self.subnet = subnet
def verify_request(self, request, client_address):
host, port = client_address
if not host.startswith(subnet):
return False
return HTTPServer.verify_request(self,request,client_address)

#
serv = MyHTTPServer((,8080), SomeHandler, 192.168.69.)
serv.serve_forever()

HTTPServer HTTP.
- , .
, . .

SimpleHTTPRequestHandler CGIHTTPRequestHandler
, -. -, Apache.
CGIHTTPRequestHandler(request, client_address, server)
. , , CGI,
CGI ( cgi_directories ,
[/cgi-bin, /htbin]). GET, HEAD
POST. HTTP ( 302),

CGI-.
-. CGI nobody. Python 2
CGIHTTPServer.
SimpleHTTPRequestHandler(request, client_address, server)
. HEAD GET .
IOError 404 File not
found (404 ). 403 Directory listing not supported
(403 ). Python 2 SimpleHTTPServer.
,
:
handler.server_version
, .
, SimpleHTTP/0.6.

632

22. -

handler.extensions_map
, MIME. application/octet-stream. -,
CGI-:
try:
from http.server import HTTPServer, CGIHTTPRequestHandler # Python 3
except ImportError:
from BaseHTTPServer import HTTPServer
# Python 2
from CGIHTTPServer import CGIHTTPRequestHandler
import os

#
os.chdir(/home/httpd/html)

# CGIHTTP 8080
serv = HTTPServer((,8080),CGIHTTPRequestHandler)
serv.serve_forever()

BaseHTTPRequestHandler
BaseHTTPRequestHandler
,
HTTP. , SimpleHTTPRequestHandler CGIHTTPRequestHandler, .
Python 2 BaseHTTPServer.
BaseHTTPRequestHandler(request, client_address, server)
, HTTP.
HTTP-
- ,
do_REQUEST, . , GET do_GET(),
POST do_POST().
, , .
BaseHTTPRequestHandler,
.
BaseHTTPRequestHandler.server_version
, , ServerName/1.2.
BaseHTTPRequestHandler.sys_version
Python, Python/2.6.
BaseHTTPRequestHandler.error_message_format
, ,
. code, message explain. :

633

http
<head>
<title>Error response</title>
</head>
<body>
<h1>!</h1>
<p>: %(code)d.
<p>: %(message)s.
<p>: %(code)s = %(explain)s.
</body>

BaseHTTPRequestHandler.protocol_version
HTTP,, . HTTP/1.0.
BaseHTTPRequestHandler.responses
HTTP (message, explain), . ,, 404 (Not Found, Nothing matches the given URI).
, error_mes_mesmessage_format, .
b BaseHTTPRequestHandler, , :

b.client_address

(host, port).

b.command

, GET, POST, HEAD .

b.path

, /index.html.

b.request_version

HTTP, ,
HTTP/1.0.

b.headers

HTTP.
- ,
, headername
in b.headers headerval = b.headers[headername].

b.rfile

. , (, POST).

b.wfile

, ,
.

,
:
b.send_error(code [, message])
.
code HTTP. message
.

634

22. -

log_error().
, error_message_format, . .
.
b.send_response(code [, message])
. HTTP, Server Date. code HTTP, message .
log_request().
b.send_header(keyword, value)
HTTP . keyword , value .
send_response().
b.end_headers()
,
HTTP.
b.log_request([code [, size]])
. code HTTP, size ( ). log_message().
b.log_error(format, ...)
. log_message().
b.log_message(format, ...)
sys.stderr. format , .
.
HTTP,, ,
.
try:
from http.server import BaseHTTPRequestHandler, HTTPServer
# Py 3
except ImportError:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer # Py 2

class DictRequestHandler(BaseHTTPRequestHandler):
def __init__(self,thedict,*args,**kwargs):
self.thedict = thedict
BaseHTTPRequestHandler.__init__(self,*args,**kwargs)

def do_GET(self):
key = self.path[1:]

# /

635

http
if not key in self.thedict:
self.send_error(404, No such key)
else:
self.send_response(200)
self.send_header(content-type,text/plain)
self.end_headers()
resp = Key : %s\n % key
resp += Value: %s\n % self.thedict[key]
self.wfile.write(resp.encode(latin-1))

#
d = {
name : Dave,
values : [1,2,3,4,5],
email : dave@dabeaz.com
}
from functools import partial
serv = HTTPServer((,9000), partial(DictRequestHandler,d))

import threading
d_mon = threading.Thread(target=serv.serve_forever)
d_mon.start()

,
URL, http://localhost:9000/name http://localhost:9000/values.
, .
,
, .
, ,
__init__().
functools.partial(), .
,
, , .

http.cookies (Cookie)
http.cookies
cookie . Python 2 Cookie.
cookie -,
, . cookie , HTTP
HTTP, :
Set-Cookie: session=8273612; expires=Sun, 18-Feb-2001 15:00:00 GMT; \
path=/; domain=foo.bar.com

cookie , JavaScript <head> HTML:


<SCRIPT LANGUAGE=JavaScript>
document.cookie = session=8273612; expires=Sun, 18-Feb-2001 15:00:00 GMT; \

636

22. -
Feb 17; Path=/; Domain=foo.bar.com;
</SCRIPT>

http.cookies cookie,, , ,
cookie, (morsels).
, , {expires, path, comment, domain,
max-age, secure, version, httponly}. , name,
, expires path.
. cookie,
, :
c = SimpleCookie()

(), :
c[session] = 8273612
c[user] = beazley

, :
c[session][path] = /
c[session][domain] = foo.bar.com
c[session][expires] = 18-Feb-2001 15:00:00 GMT

cookie HTTP c.output(). :


print(c.output())
#
# Set-Cookie: session=8273612; expires=...; path=/; domain=...
# Set-Cookie: user=beazley

cookie HTTP,, , key=value, : session=8273612;


user=beazley. , expires, path domain,
. cookie HTTP_COOKIE, CGI.. cookie :
c = SimpleCookie(os.environ[HTTP_COOKIE])
session = c[session].value
user
= c[user].value

SimpleCookie.
SimpleCookie([input])
cookie,
.
c SimpleCookie :

http

637

c.output([attrs [,header [,sep]]])


, HTTP.
attrs ,
(expires, path, domain ).
. header
HTTP ( Set-Cookie:). sep
, ;
.
c.js_output([attrs])
JavaScript, cookie , JavaScript.
attrs ,
.
c.load(rawdata)
c rawdata. rawdata , , ,
HTTP_COOKIE CGI. rawdata , key-value c[key]=value.
key/value Morsel. m Morsel
expires, path, comment,
domain, max-age, secure, version httponly. , m
:
m.value
, cookie.
m.coded_value
, cookie,
.
m.key
cookie.
m.set(key, value, coded_value)
m.key, m.value m.coded_value, .
m.isReservedKey(k)
, k , expires,
path, domain .
m.output([attrs [,header]])
HTTP m.
attrs ,
(expires, path ). header
HTTP ( Set-Cookie:).

638

22. -

m.js_output([attrs])
JavaScript,, cookie .
m.OutputString([attrs])
cookie HTTP
JavaScript.

cookie , CookieError.

http.cookiejar (cookielib)
http.cookiejar
cookie . Python 2
cookielib.
,
cookie ,
urllib,
. , http.cookiejar
cookie .
, , cookie,, , .
, :
CookieJar()
, cookie,
, HTTP,
cookie HTTP.. cookie , CookieJar .
FileCookieJar(filename [, delayload ])
FileCookieJar,
cookie . filename . delayload True,
. .
MozillaCookieJar(filename [, delayload ])
FileCookieJar, cookies.txt,
Mozilla.
LWPCookieJar(filename [, delayload ])
FileCookieJar, Set-Cookie3,
libwww-perl.

smtplib

639

.
, .
, cookie.
urllib.request .

smtplib
smtplib SMTP,,
SMTP,, RFC 821 RFC 1869. , .
:
SMTP([host [, port]])
, SMTP.
host, , SMTP.
port .
25. host,
connect(). connect() , .
s SMTP :
s.connect([host [, port]])
SMTP. host ,
(127.0.0.1).
port , 25. SMTP(), connect() .
s.login(user, password)
, .
user , password .
s.quit()
, QUIT.
s.sendmail(fromaddr, toaddrs, message)
. fromaddr
. toaddrs . message ,
RFC-822.
-822. email. , , ,
ASCII,, 0 127. -

640

22. -

.. , UTF-8,
,
message.

, smtplib
:
import smtplib
fromaddr = someone@some.com
toaddrs = [recipient@other.com]
msg = From: %s\r\nTo: %s\r\n\r\n % (fromaddr, ,.join(toaddrs))
msg +=
!

server = smtplib.SMTP(localhost)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

urllib
urllib
HTTP,, FTP . , , , -, , , - .
,
.
.
Python 2 urllib , urllib, urllib2, urlparse robotparser. Python 3
urllib.

urllib.request (urllib2)
urllib.request , URL.. Python 2 urllib2.
HTTP.. , -:
try:
from urllib.request import urlopen # Python 3
except ImportError:
from urllib2 import urlopen
# Python 2

641

urllib
u = urlopen(http://docs.python.org/3.0/library/urllib.request.html)
data = u.read()

, . ,
-, , cookie,, .
, ( ).

urlopen()
urlopen().
urlopen(url [, data [, timeout]])
URL url ,, ,, .
url URL Request,
. data
URL , .
, HTTP- GET ( ) POST. , urllib.parse.urlencode().
timeout
, .
u, , urlopen(), :

u.read([nbytes])

nbytes
.

u.readline()

u.readlines()

u.fileno()

u.close()

u.info()


URL. HTTP HTTP,
. FTP content-length.
content-length content-type.

u.getcode()

HTTP . ,
200,
404.

u.geturl()

URL ,
.

642

22. -

, u, , . ,
codecs -
.
, URLError. , HTTP,
.

.
, . :
try:
u = urlopen(http://www.python.org/perl.html)
resp = u.read()
except HTTPError as e:
resp = e.read()

,
urlopen(), - -.
, , . , , HTTP_PROXY
os.environ. , os.environ[HTTP_PROXY] = http://example.com:12345.
url urlopen() , http://www.python.org.
, HTTP-,
Request
url.
Request(url [, data [, headers [, origin_req_host [, unverifiable]]]])
Request. url URL (, http://www.foo.bar/spam.html). data
URL, HTTP. ,
HTTP- GET POST. headers
, key-value, HTTP. origin_req_host , . unverifiable
True, URL, .
URL, , ,
URL,, , , URL , .
unverifiable False.
r Request :
r.add_data(data)
. HTTP-,
POST. data URL,

urllib

643

Request(). ,
data.
r.add_header(key, val)
. key ,
val . .
r.add_unredirected_header(key, val)
, . key val , add_header().
r.get_data()
( ).
r.get_full_url()
URL .
r.get_host()
, .
r.get_method()
HTTP, GET POST.
r.get_origin_req_host()
, .
r.get_selector()
URL, (, /index.html).
r.get_type()
URL (, http).
r.has_data()
True, .
r.is_unverifiable()
True, .
r.has_header(header)
True, header.
r.set_proxy(host, type)
-. host, type. URL, , .
, Request User-Agent, urlopen().
, , Internet Explorer, Firefox .

644

22. -
headers = {
User-Agent:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)
}

r = Request(http://somedomain.com/,headers=headers)
u = urlopen(r)


urlopen() ,
cookie HTTP.. , build_opener() :
build_opener([handler1 [, handler2, ... ]])
URL.
handler1, handler2 . , . :

CacheFTPHandler

FTP

FileHandler

FTPHandler

URL FTP

HTTPBasicAuthHandler

HTTP

HTTPCookieProcessor

cookie

HTTPDefaultErrorHandler

HTTP,
HTTPError

HTTPDigestAuthHandler

- HTTP

HTTPHandler

URL HTTP

HTTPRedirectHandler

HTTP

HTTPSHandler

URL HTTP

ProxyHandler

ProxyBasicAuthHandler

ProxyDigestAuthHandler

UnknownHandler

URL

ProxyHandler, UnknownHandler, HTTPHandler, HTTPSHandler, HTTPDefaultErrorHandler, HTTPRedirectHandler, FTPHandler, FileHandler HTTPErrorProcessor. . -

urllib

645

, , .
, -
, , . , HTTPHandler ,
.
, build_opener(), open(url
[, data [, timeout]]), URL,, , .
open() , urlopen().
install_opener(opener)
opener URL, urlopen(). opener
, build_opener().
,
,
urlib.request.


, ,
, HTTPBasicAuthHandler, HTTPDigestAuthHandler, ProxyBasicAuthHandler ProxyDigestAuthHandler. , :
h.add_password(realm, uri, user, passwd)
realm URI
uri. . uri URI,, URI . realm , . . , -
. uri
URL,, . realm uri (Administrator,
http://www.somesite.com). user password .
:
auth = HTTPBasicAuthHandler()
auth.add_password(Administrator,
http://www.secretlair.com,drevil,12345)

#
opener = build_opener(auth)

# URL
u = opener.open(http://www.secretlair.com/evilplan.html)

646

22. -

HTTP cookie
cookie
HTTPCookieProcessor. :
cookiehand = HTTPCookieProcessor()
opener = build_opener(cookiehand)
u = opener.open(http://www.example.com/)

HTTPCookieProcessor
CookieJar, http.cookiejar.
HTTPCookieProcessor
CookieJar, cookie.. :
cookiehand = HTTPCookieProcessor(
http.cookiejar.MozillaCookieJar(cookies.txt)
)
opener = build_opener(cookiehand)
u = opener.open(http://www.example.com/)

-
-, ProxyHandler.
ProxyHandler([proxies])
, -. proxies ,
(, http, ftp ) URL .
, :
proxy = ProxyHandler({http: http://someproxy.com:8080/}
auth = HTTPBasicAuthHandler()
auth.add_password(realm,host, username, password)
opener = build_opener(proxy, auth)

u = opener.open(http://www.example.com/doc.html)

urllib.response
, , , urllib.request. .

urllib.parse
urllib.parse URL,
http://www.python.org.

URL ( urlparse Python 2)


URL : scheme://netloc/path;parameters?
query#fragment. , netloc

647

urllib

hostname:port
user:pass@hostname. URL:
urlparse(urlstring [, default_scheme [, allow_fragments]])
URL urlstring
ParseResult. default_scheme (http, ftp
) , , URL.
allow_fragments , .
r ParseResult (scheme,
netloc, path, parameters, query, fragment), :

r.scheme

(, http)

r.netloc

(, www.python.org)

r.path

(, /index.html)

r.params

r.query

(, name=Dave&id=42)

r.fragment

r.username

, username:password@hostname

r.password

r.hostname

r.port

,
hostname:port

ParseResult URL,, r.geturl().


urlunparse(parts)
URL ,
urlparse(). parts .
urlsplit(url [, default_scheme [, allow_fragments]])
, urlparse(), , URL parameters . URL,
, : scheme://netloc/path1;param1/path2;param2/
path3?query#fragment. SplitResult,
(scheme, netloc, path, query, fragment).
, r SplitResult :

648

22. -

r.scheme

(, http)

r.netloc

(, www.python.org)

r.path

(, /index.html)

r.query

(, name=Dave&id=42)

r.fragment

r.username

, username:password@hostname

r.password

r.hostname

r.port

,
hostname:port

SplitResult URL,
r.geturl().
urlunsplit(parts)
URL ,
urlsplit(). parts URL.
urldefrag(url)
(newurl, fragment), newurl url , fragment ( ). url , newurl url,
fragment .
urljoin(base, url [, allow_fragments])
URL, URL base
url. allow_fragments ,
urlparse(). URL , .
parse_qs(qs [, keep_blank_values [, strict_parsing]])
qs ( application/x-www-form-urlencod/x-www-form-urlencodx-www-form-urlencod-www-form-urlencodwww-form-urlencod-form-urlencodform-urlencod-urlencodurlencoded) ,
, , . keep_blank_values , . True,
. False ( ), . strict_
parsing . True,
, , ValueError. .

urllib

649

parse_qsl(qs [, keep_blank_values [, strict_parsing]])


, parse_qs(), , (name, value), name , value .

URL ( urllib Python 2)


, / , URL.
quote(string [, safe [, encoding [, errors]]])
string , URL.
, , (_), (,), (.)
(-) . %xx. safe , ; /. encoding
, ASCII.
utf-8. errors

strict. encoding errors Python 3.
quote_plus(string [, safe [, encoding [, errors]]])
quote() (+). string safe ,
quote(). encoding errors ,
quote().
quote_from_bytes(bytes [, safe])
, quote(), . . Python 3.
unquote(string [, encoding [, errors]])
%xx . encoding errors
%xx. utf-8 replace. encoding
errors Python 3.
unquote_plus(string [, encoding [, errors]])
unquote(),
(+) .
unquote_to_bytes(string)
, unquote(), .

650

22. -

urlencode(query [, doseq])
query ,
URL , POST. query (key, value).
key=value, &, , key value, quote_plus(). doseq
, True, -
query
key. v
key=v.

, URL, HTTP-
GET, URL:
try:
from urllib.parse import urlparse, urlencode, parse_qsl # Python 3
except ImportError:
from urlparse import urlparse, parse_qsl
# Python 2
from urllib import urlencode

# URL
form_fields = {
name : Dave,
email : dave@dabeaz.com,
uid : 12345
}
form_data = urlencode(form_fields)
url = http://www.somehost.com/cgi-bin/view.py?+form_data

#
r = urlparse(url)
print(r.scheme)
print(r.netloc)
print(r.path)
print(r.params)
print(r.query)
print(r.fragment)

URL
#
#
#
#
#
#

http
www.somehost.com
/cgi-bin/view.py

uid=12345&name=Dave&email=dave%40dabeaz.com

#
parsed_fields = dict(parse_qsl(r.query))
assert form_fields == parsed_fields

urllib.error
urllib.error , urllib.

xmlrpc

651

ContentTooShort
, ,
( Content-Length). Python 2 urllib.
HTTPError
, HTTP.. , , . , , .
URLError. Python 2 urllib2.
URLError
. IOError.
reason . Python 2 urllib2.

urllib.robotparser (robotparser)
urllib.robotparser ( Python 2 robotparser)
robots.txt,
-.
.

urllib
,
, , , .
, .

Python 2 , urllib.urlopen(),
,
Python 2.6 Python 3. urllib.urlopen() urllib2.urlopen(),
, urllib.
request.urlopen(), .

xmlrpc
xmlrpc ,
, XML-RPC. XML-RPC
, XML
HTTP .
XML-RPC
-RPC
RPC - , -

652

22. -

Python,
,
. XML-RPC http://
www.xmlrpc.com.

xmlrpc.client (xmlrpclib)
xmlrpc.client XML-RPC.
Python 2 xmlrpclib. , ServerProxy:
ServerProxy(uri [, transport [, encoding [, verbose [, allow_none
[, use_datetime]]]])
uri XML-RPC,
http://www.foo.com/RPC2. URI : http://user:pass@
host:port/path, user:pass . base-64
Authorization: . Python OpenSSL,
HTTPS. transport
, . , , HTTP HTTPS.
( ). encoding .. UTF-8. verbose True,

. allow_none True, None.
,
. use_datetime .
True, datetime. False.
s ServerProxy . s. ,
,
:
>>> s = ServerProxy(http://www.xmlrpc.com/RPC2)
>>> s.currentTime.getCurrentTime()
<DateTime u20051102T20:08:24 at 2c77d8>
>>>

RPC ,
Python.. XML-RPC
-RPC
RPC -

653

xmlrpc

:
XML-RPC

Python

True False

int

float

(
, XML)

, ,
XML-RPC

(xmlrpc.client.DateTime)

(xmlrpc.client.Binary)


, d xmlrpc.client.DateTime.
d.value ISO 8601. , time,
d.timetuple(). , b xmlrpc.client.Binary. b.data
. , , . Python 2
, ASCII.
,
.
RPC , TypeError xmlrpclib.Fault.
XML-RPC
-RPC
RPC , :
s.system.listMethods()
, XML-RPC.
s.methodSignatures(name)
name.
, (, string, int, int),
, . -

654

22. -

. XML-RPC, Python,
, .
s.methodHelp(name)
name, . HTML. , .
,
xmlrpclib:
boolean(value)
value XML-RPC.
-RPC.
RPC.. , Python
, .
Binary(data)
XML-RPC . data . Binary.
/
base-64 . b
Binary b.data.
DateTime(daytime)
XML-RPC, . daytime
ISO 8601, ,, time.localtime(), datetime, datetime.
dumps(params [, methodname [, methodresponse [, encoding [, allow_none]]]])
params XML-RPC.
params Fault. methodname .
methodresponse .
True, XML-RPC.
params . encoding
XML. UTF-8. allow_
none , ,
None . None XML-RPC,
-RPC,
RPC,, . allow_none False.
loads(data)
data, XML-RPC,
(params, methodname), params ,
methodname . , , Fault.

xmlrpc

655

MultiCall(server)
MultiCall, XML-RPC .
,
RPC . server ServerProxy,
. MultiCall
, ServerProxy. , ,
, MultiCall .
, RPC.
,
RPC. , MultiCall() , system.multicall().
, MultiCall:
multi = MultiCall(server)
multi.foo(4,6,7)
# foo
multi.bar(hello world)
# bar
multi.spam()
# spam
# XML-RPC
foo_result, bar_result, spam_result = multi()

xmlrpc.client :
Fault
XML-RPC. faultCode
. faultString .
ProtocolError
, ,
URL . url URI,
. errcode . errmsg . headers HTTP- , .

xmlrpc.server
(SimpleXMLRPCServer, DocXMLRPCServer)
xmlrpc.server ,
XML-RPC. Python 2
: SimpleXMLRPCServer DocXMLRPCServer.
SimpleXMLRPCServer(addr [, requestHandler [, logRequests]])
XML-RPC, addr (, (localhost,8080)). requestHandler -

656

22. -

, . SimpleXMLRPCRequestHandler(), . logRequests
, . True.
DocXMLRPCServer(addr [, requestHandler [, logRequest])
XML-RPC,
HTTP- GET ( ). , , ,
. , SimpleXMLRPCServer().
s SimpleXMLRPCServer DocXMLRPCServer :
s.register_function(func [, name])
func XML-RPC.
name . name,
. , Python,
(.). name ,, .
s.register_instance(instance [, allow_dotted_names])
, , register_function().
instance _dispatch(self, methodname, params), . methodname , params . , _dispatch(), .
_dispatch() ,
instance. ,, . allow_dotted_names ,
. ,
foo.bar.spam, , instance.foo.bar.spam.
False. True .
, Python.. ,
.
s.register_introspection_functions()
XML-RPC system.listMethods(), system.methodHelp() system.methodSignature().

xmlrpc

657

system.methodHelp()
( ). system.methodSignature() , ,
( Python
, ).
s.register_multicall_functions()
XML-RPC
-RPC
RPC system.multicall().
DocXMLRPCServer :
s.set_server_title(server_title)
HTML- . server_title
HTML- <title>.
s.set_server_name(server_name)
HTML-. server_name
<h1>.
s.set_server_documentation(server_documentation)
HTML- . server_
documentation , XML-RPC.
XML-RPC ,
CGI.
:
CGIXMLRPCRequestHandler([allow_none [, encoding]])
CGI, , SimpleXMLRPCServer. , SimpleXMLRPCServer().
DocCGIXMLRPCRequestHandler()
CGI, , DocXMLRPCServer. , , CGIXMLRPCRequestHandler(). , , .
c CGI
,
XML-RPC. :
c.handle_request([request_text])
XML-RPC.
. request_text, HTTP- POST.

658

22. -

. add. ,
math, ,
, .
try:
from xmlrpc.server import SimpleXMLRPCServer
# Python 3
except ImportError:
from SimpleXMLRPCServer import SimpleXMLRPCServer # Python 2
import math

def add(x,y):

return x+y

s = SimpleXMLRPCServer((,8080))
s.register_function(add)
s.register_instance(math)
s.register_introspection_functions()
s.serve_forever()


CGI:
try:
from xmlrpc.server import CGIXMLRPCRequestHandler
# Python 3
except ImportError:
from SimpleXMLRPCServer import CGIXMLRPCRequestHandler # Python 2
import math

def add(x,y):

return x+y

s = CGIXMLRPCRequestHandler()
s.register_function(add)
s.register_instance(math)
s.register_introspection_functions()
s.handle_request()

XML-RPC
-RPC
RPC Python xmlrpc.client xmlrpclib. , , :
>>> from xmlrpc.client import ServerProxy
>>> s = ServerProxy(http://localhost:8080)
>>> s.add(3,5)
8
>>> s.system.listMethods()
[acos, add, asin, atan, atan2, ceil, cos, cosh, degrees,
exp, fabs, floor, fmod, frexp, hypot, ldexp, log, log10,
modf, pow, radians, sin, sinh, sqrt, system.listMethods,
system.methodHelp, system.methodSignature, tan, tanh]

xmlrpc

659

>>> s.tan(4.5)
4.6373320545511847
>>>


, XML-RPC,
-RPC,
RPC,, . , XML-RPC

, XML-RPC. pickle .
XML-RPC
-RPC
RPC . XML-RPC , ,
, ,
( ). , XMLRPC , . , HTTP-,
, .
,
XML-RPC
.
TCPServer, socketserver.
XML-RPC ,
(, ,
). , , SimpleXMLRPCRequestHandler DocXMLRPCRequestHandler, do_POST(). , :
try:
from xmlrpc.server import (SimpleXMLRPCServer,
SimpleXMLRPCRequestHandler)
except ImportError:
from SimpleXMLRPCServer import (SimpleXMLRPCServer,
SimpleXMLRPCRequestHandler)
class MaxSizeXMLRPCHandler(SimpleXMLRPCRequestHandler):
MAXSIZE = 1024*1024 # 1 M
def do_POST(self):
size = int(self.headers.get(content-length,0))
if size >= self.MAXSIZE:
self.send_error(400,Bad request)
else:
SimpleXMLRPCRequestHandler.do_POST(self)

s = SimpleXMLRPCServer((,8080),MaxSizeXMLRPCHandler)

HTTP, .

23

-
Python
- . -,
Python
HTML-,
-, -. ,
, ( , , , ).
,
.
-, Python . , -
-, Apache,
Python
. Python .
, HTML , :
<FORM ACTION=/cgi-bin/subscribe.py METHOD=GET>
Your name : <INPUT type=Text name=name size=30>
Your email address: <INPUT type=Text name=email size=30>
<INPUT type=Submit name=submit-button value=Subscribe>
</FORM>

ACTION Python
subscribe.py, .
AJAX (Asynchronous
Asynchronous JavaScript and XML JavaScript
Script
cript XML).
). JavaScript HTML-.
, , JavaScript HTTP -, ( Python).

661
JavaScript . .
, , XML,
JSON .
HTML,, , .
<html>
<head>
<title>ACME Officials Quiet After Corruption Probe</title>
<style type=text/css>
.popup { border-bottom:1px dashed green; }
.popup:hover { background-color: #c0c0ff; }
</style>
</head>
<body>
<span id=popupbox
style=visibility:hidden; position:absolute; background-color:#ffffff;>
<span id=popupcontent></span>
</span>
<script>
/* */
var popup = document.getElementById(popupbox);
var popupcontent = document.getElementById(popupcontent);

/* */
function ShowPopup(hoveritem,name) {
var request = new XMLHttpRequest();
request.open(GET,cgi-bin/popupdata.py?name=+name, true);
request.onreadystatechange = function() {
var done = 4, ok = 200;
if (request.readyState == done && request.status == ok) {
if (request.responseText) {
popupcontent.innerHTML = request.responseText;
popup.style.left = hoveritem.offsetLeft+10;
popup.style.top = hoveritem.offsetTop+20;
popup.style.visibility = Visible;
}
}
};
request.send();
}
/* */
function HidePopup() {
popup.style.visibility = Hidden;
}
</script>
<h3>ACME Officials Quiet After Corruption Probe</h3>
<p>
Today, shares of ACME corporation
(<span class=popup onMouseOver=ShowPopup(this,ACME);

662

23. -
onMouseOut=HidePopup();>ACME</span>)
plummetted by more than 75% after federal investigators revealed that
the board of directors is the target of a corruption probe involving
the Governor, state lottery officials, and the archbishop.
</p>
</body>
</html>

ShowPopup() JavaScript
Python popupdata.py, .
HTML,, . .. 23.1 , .
, - Python, , Python.
: , Python, . , , ,
http://wiki.python.org/moin/WebFrameworks.

. 23.1. HTML
,
popupdata.py

, -
. : CGI, Python WSGI,, , -
Python.

cgi
cgi CGI, ,
-
.

663

cgi

CGI, -
CGI- . CGI
: sys.stdin
, .
,
-:

AUTH_TYPE

CONTENT_LENGTH

sys.stdin

CONTENT_TYPE

DOCUMENT_ROOT

GATEWAY_INTERFACE

CGI

HTTP_ACCEPT

MIME,

HTTP_COOKIE

cookie

HTTP_FROM

(
)

HTTP_REFERER

URL

HTTP_USER_AGENT

PATH_INFO

PATH_TRANSLATED

PATH_INFO

QUERY_STRING

REMOTE_ADDR

IP-

REMOTE_HOST

REMOTE_IDENT

REMOTE_USER

REQUEST_METHOD

(GET POST)

SCRIPT_NAME

SERVER_NAME

SERVER_PORT

SERVER_PROTOCOL

SERVER_SOFTWARE

CGI sys.stdout. , CGI-,


, , CGI Programming with Perl, 2nd Edition,1
1

. CGI- Perl, 2- . .
.: -, 2001.

664

23. -

(Shishir Gundavaram) (OReilly, 2000). . -, HTML


CGI- ,
. Python
FieldStorage. :
import cgi
form = cgi.FieldStorage()
name = form.getvalue(name) # name
email = form.getvalue(email) # email

-, CGI- : HTTP
( HTML).
). . ,
HTTP:
print Content-type: text/html\r
print \r

# HTML
# (!)

. :
print <TITLE>My CGI Script</TITLE>
print <H1>Hello World!</H1>
print You are %s (%s) % (name, email)

HTTP \r\n Windows.


\r.
, Status:. :
print
print
print
print

Status: 401 Forbidden\r


# HTTP
Content-type: text/plain\r
\r
# ()
Youre not worthy of accessing this page!

, :
print Status: 302 Moved\r
print Location: http://www.foo.com/orderconfirm.html\r
print \r

cgi FieldStorage.
FieldStorage([input [, headers [, outerboundary [, environ [, keep_blank_values
[, strict_parsing]]]]]])
, ,
. input , ,
, POST.
sys.stdin. headers outerboundary -

665

cgi

. environ , CGI-. keep_blank_values , .


False. strict_parsing ,
. False.
form FieldStorage . , f = form[key] key.
f, ,
FieldStorage, MiniFieldStorage.
, f:

f.name

f.filename

f.value

f.file

, ,

f.type

f.type_options

, contenttype HTTP-

f.disposition

content-disposition; None

f.disposition_options

, contentdisposition

f.headers

, ,
HTTP

:
form.getvalue(fieldname [, default])
fieldname. , . default
, . : ,
, .
, form.getfirst(),
.
form.getfirst(fieldname [, default])
fieldname. default
, .

666

23. -

form.getlist(fieldname)
fieldname.
, .
.
cgi MiniFieldStorage,
name value.
, ,
FieldStorage .
FieldStorage , Python,, .
, FieldStorage (
multipart/form-data) .
( application/x-www-form-urlencodapplication/x-www-form-urlencod/x-www-form-urlencodx-www-form-urlencod-www-form-urlencodwww-form-urlencod-form-urlencodform-urlencod-urlencodurlencoded) MiniFieldStorage ,
. :
form = cgi.FieldStorage()
if name not in form:
error(Name is missing)
return
name = form[name].value # name
email = form[email].value # email

, value
.
,
, file. ,

:
fileitem = form[userfile]
if fileitem.file:
# ;
linecount = 0
while True:
line = fileitem.file.readline()
if not line: break
linecount = linecount + 1

, CGI-:
escape(s [, quote])
&, < >, s, HTML, &amp;, &lt; &gt;.

cgi

667

quote , () &quot;.
parse_header(string)
, HTTP-,
content-type. , . ,
parse_header(text/html; a=hello; b=world)

:
(text/html, {a:hello, b:world}).

parse_multipart(fp, pdict)
multipart/form-data, . fp
, pdict contenttype. , .
multipart/*.
FieldStorage.
print_directory()
HTML
. .
.
print_environ()
HTML.. .
print_environ_usage()

HTML. .
print_form(form)
, . form FieldStorage.
.
test()
HTTP-
- HTML.. , , CGI .

CGI-
- CGI- . , .
-, print HTML.. -

668

23. -

Python
HTML,, , . .
string.Template. , :
import cgi
from string import Template

def error(message):
temp = Template(open(errormsg.html).read())
print Content-type: text/html\r
print \r
print temp.substitute({message : message})

form = cgi.FieldStorage()
name = form.getfirst(name)
email = form.getfirst(email)
if not name:
error(name not specified)
raise SystemExit
elif not email:
error(email not specified)
raise SystemExit

#
confirmation = subscribe(name, email)

#
values = {
name : name,
email : email,
confirmation: : confirmation,
# ...
}
temp = Template(open(success.html).read()
print temp.substitute(values)

error.html success.html, HTML,


$variable, ,
CGI-. , success.html :
<HTML>
<HEAD>
<TITLE>Success</TITLE>
</HEAD>
<BODY>
Welcome $name. You have successfully subscribed to our
newsletter. Your confirmation code is $confirmation.
</BODY>
</HTML>

669

cgi

temp.substitute() . ,
, CGI-. Python

; , , -.
. http://wiki.python.org/moin/
Templating.
-, CGI-, . ,
, ,
- , , .
Python
. , , sqlite3, , MySQL.
, , CGI,
HTTP,, cookie,, , , -.
, , , . .

CGI- ,
-. CGI- cgi-bin. , . .

UNIX CGI-
- Python , , :
#!/usr/bin/env python
import cgi
...

, cgitb. , import cgitb; cgitb.enable().


,
-.

CGI-
- , os.system() os.popen(), -

670

23. -

, .
,
(
, , UNIX). ,
- URL , , - , ,
.

UNIX setuid CGI. ,


, .

from cgi import *.


cgi , .

cgitb
, .
, .
CGI-,
.
enable([display [, logdir [, context [, format]]]])
. display , . 1. logdir , , . logdir ,
,
tempfile.mkstemp(). context , ,
, . format
. html ( ) format HTML. ,
.
handle([info])
,
enable(). info (exctype, excvalue, tb),
exctype , excvalue , tb . sys.exc_info(). info
, .

671

WSGI

CGI-,
-, import cgitb; enable().

WSGI
WSGI (Web Server Gateway Interface -) - -, - .
PEP 333 (http://www.python.org/dev/
peps/pep-0333).
http://www.wsgi.org. wsgiref
,
, .

WSGI
WSGI,, - , , webapp(environ, start_response), , environ , , , ,
, CGI-:

CONTENT_LENGTH

CONTENT_TYPE

HTTP_ACCEPT

MIME,

HTTP_COOKIE

cookie

HTTP_REFERER

URL

HTTP_USER_AGENT

PATH_INFO

QUERY_STRING

REQUEST_METHOD

(GET POST)

SCRIPT_NAME

SERVER_NAME

SERVER_PORT

SERVER_PROTOCOL

, environ , WSGI:

672

23. -

wsgi.version

, WSGI (, (1,0)
WSGI 1.0).

wsgi.url_scheme

, URL . , http https.

wsgi.input

, , .
, .

wsgi.errors

, ,
.

wsgi.multithread

, True
.

wsgi.multiprocess

, True .

wsgi.run_once

; True ,

.

start_response start_
response(status, headers),
. status ,
200 OK 404 Not Found, headers (name, value), HTTP-,
, (Content-type,text/html).
, , -
,
, ,
(,
ISO-8859-1 Latin-1).
-, .
, ,
UTF-8, .
WSGI,, , ,
cgi:
import cgi
def subscribe_app(environ, start_response):
fields = cgi.FieldStorage(environ[wsgi.input],
environ=environ)

name = fields.getvalue(name)
email = fields.getvalue(email)

673

wsgiref
status = 200 OK
headers = [(Content-type,text/plain)]
start_response(status, headers)

response = [
Hi %s. Thank you for subscribing. % name,
You should expect a response soon.
]
return (line.encode(utf-8) for line in response)

. -, WSGI- -
, - . cgi, ,
, . , start_response(), .
.
-,
. Python 3 WSGI-
, .
WSGI-
- , .
.

wsgiref
wsgiref WSGI,, , CGI-.

wsgiref.simple_server
wsgiref.simple_server HTTP-,
WSGI-.
-. , :
make_server(host, port, app)
HTTP-, .
host , port . app , ,
WSGI-. ,
s.serve_forever(), s ,
make_server().
demo_app(environ, start_response)
WSGI-,
Hello World. app
make_server() .

674

23. -

WSGI-:
def my_app(environ, start_response):
#
start_response(200 OK, [(Content-type,text/plain)])
return [Hello World]

if __name__ == __main__:
from wsgiref.simple_server import make_server
serv = make_server(,8080, my_app)
serv.serve_forever()

wsgiref.handlers
wsgiref.handlers ,
WSGI,, - (, CGI- - Apache).
.
CGIHandler()
- WSGI,
CGI.
-, cgi.
BaseCGIHandler(stdin, stdout, stderr, environ [, multithread [, multiprocess]])
- WSGI,
CGI,, - . stdin, stdout stderr , ,
-. environ , CGI. multithread multiprocess
,
wsgi.multithread wsgi.multiprocess.
multithread True, multiprocess
False.
SimpleHandler(stdin, stdout, stderr, environ [, multithread [, multiprocess]])
- WSGI, BaseCGIHandler, stdin, stdout, stderr environ. BaseCGIHandler,
,
(, BaseCGIHandler
Status:).
run(app),
WSGI-
- . WSGI-, CGI:

wsgiref

675

#!/usr/bin/env python
def my_app(environ, start_response):
#
start_response(200 OK,[(Content-type,text/plain)])
return [Hello World]

from wgiref.handlers import CGIHandler


hand = CGIHandler()
hand.run(my_app)

wsgiref.validate
wsgiref.validate ,
WSGI- , .
validator(app)
WSGI-, WSGI- app.
, app, ,
, , WSGI.. AssertionError.
validator():
def my_app(environ, start_response):
#
start_response(200 OK,[(Content-type,text/plain)])
return [Hello World]

if __name__ == __main__:
from wsgiref.simple_server import make_server
from wsgiref.validate import validator
serv = make_server(,8080, validator(my_app))
serv.serve_forever()

, WSGI-.
-. Python,
PEP 333, ,
, WSGI..
WSGI. , WSGI,

.

676

23. -

webbrowser
webbrowser ,
- .
. , , HTML,, , , .
open(url [, new [, autoraise]])
url . new 0,
, . new 1, . new
2, . autoraise True,
.
open_new(url)
url .
open(url, 1).
open_new_tab(url)
url . open(url, 2).
get([name])
. name
, , netscape, mozilmozilla, kfm, grail, windows-default, internet-config command-line. open() open_new(), ,
. name
.
register(name, constructor [, controller])

get(). name . constructor
, ,
. controller
. , constructor None.
c, get(),
:
c.open(url [, new])
, open().
c.open_new(url)
, open_new().

24



, ,
base 64, HTML, XML JSON, .

base64
base64 , base 64, base
32 base 16. base 64
, , ,
HTTP.
RFC-3548 RFC-1421.
base 64
24 (3 ). 24- 6- . 6-
ASCII :

025

ABCDEFGHIJKLMNOPQRSTUVWXYZ

2651

abcdefghijklmnopqrstuvwxyz

5261

0123456789

62

63

3 (24 ), 24- .
=,
. , 16--

678

24.

3-
.
3-
.. base 64 ( 12 8 ), ==, . base 64 ,
(=) (==) .
base 32
40 (5 ). 40-
5- . 5-
:

025

ABCDEFGHIJKLMNOPQRSTUVWXYZ

2631

2-7

base 64,
40- , 40 ,
=.
(======), .
base 16
. 4- 09 AF. base 16 .
b64encode(s [, altchars])
s base 64. altchars
,
+ /, base 64. ,
base 64 URL.
b64decode(s [, altchars])
s, base 64,
. altchars
,
+ /, base 64. s
, TypeError.
standard_b64encode(s)
s base 64.
standard_b64decode(s)
s, base 64.

base64

679

urlsafe_b64encode(s)
s base 64, - _
+ / . b64encode(s, -_).
urlsafe_b64decode(s)
s, base 64,
+ / - _ .
b32encode(s)
s base 32.
b32decode(s [, casefold [, map01]])
s, base 32. casefold True, . ( ). map01, , ,
1 (, I L).
, 0 O.

, TypeError.
b16encode(s)
s base 16 ( ).
b16decode(s [,casefold])
s, base 16. casefold True,
.
AF ( ).
- , TypeError.
,
base64,
Python:
decode(input, output)
base 64. input
, . output , .
decodestring(s)
s, base 64.
.
encode(input, output)
base 64. input
, . output , .

680

24.

encodestring(s)
s base 64.

binascii
binascii ,
ASCII ,
base 64, BinHex UUencoding.
a2b_uu(s)
s uuencode
. 45 , , . .
b2a_uu(data)

uuencode, ASCII. data 45 ,
Error.
a2b_base64(string)
string base 64 .
b2a_base64(data)

base 64, ASCII. data 57 , ( ).
a2b_hex(string)
string .
unhexlify(string).
b2a_hex(data)

. hexlify(data).
a2b_hqx(string)
BinHex 4 RLE (Run-Length
Run-Length
-Length
Length Encoding ).
rledecode_hqx(data)
RLE- data. , , Incomplete.

csv

681

rlecode_hqx(data)
BinHex 4 RLE- data.
b2a_hqx(data)
BinHex 4,
ASCII. data
RLE. , ,
data 3.
crc_hqx(data, crc)
BinHex 4 CRC data. crc .
crc32(data [, crc])
CRC-32 data. crc . , crc 0.

csv
csv , , (Comma-Separated
Comma-Separated
-Separated
Separated Values,, CSV).
). CSV , , -, (,) . :
Blues,Elwood,1060 W Addison,Chicago, IL 60613,B263-1655-2187,116,56

. , CSV,
. ,
-. ,
, . , split(,).
reader(csvfile [, dialect [, **fmtparams])
,
csvfile. csvfile , . ,
. dialect , Dialect. dialect ,
CSV.
, , excel ( ) excel-tab,
,

682

24.

. fmtparams ,
. :

delimiter

, (
,).

doublequote

, , (quotechar),
. True,
. False,
(escapechar).
True.

escapechar

, , quoting
QUOTE_NONE. None.

lineterminator

, ( \r\n).

quotechar

,
, - ( ).

skipinitialspace

True, , -, ( False).

writer(csvfile [, dialect [, **fmtparam]])


,
CSV. csvfile
, , write().
dialect , reader(),
CSV.. fmtparams , reader().
:

quoting

.
: QUOTE_ALL (
), QUOTE_MINIMAL (
, - ), QUOTE_NONNUMERIC ( ) QUOTE_NONE (
).
QUOTE_MINIMAL.

csv

683

w :
w.writerow(row)
. row .
w.writerows(rows)
. rows , writerow().
DictReader(csvfile [, fieldnames [, restkey [, restval [, dialect [, **fmtparams]]]]])
, , . fieldnames , .
,
. restkey , , ,
fieldnames. restval ,
,
,
fieldnames. restkey restval None. dialect fmtparams ,
reader().
DictWriter(csvfile, fieldnames [, restval [, extrasaction [, dialect [, **fmtparams]]]])
, , , . fieldnames . restval ,
, fieldnames, . extrasaction , , , ,
fieldnames. extrasaction
raise, ValueError.
ignore, . dialect fmtparams
, writer().
w DictWriter :
w.writerow(row)
. row
, .
w.writerows(rows)
. rows
, writerow().

684

24.

Sniffer()
Sniffer,
CSV.
s Sniffer :
s.sniff(sample [, delimiters])
, sample, Dialect, .. sample CSV,
. delimiters
-.
s.has_header(sample)
, sample, True,
, .

csv ,
. ,

CSV ( ), ,
,

.
, Dialect
,
reader() writer() (delimiter, doublequote, escapechar,
lineterminator, quotechar, quoting, skipinitialspace).

:
register_dialect(name, dialect)
dialect Dialect name.
unregister_dialect(name)
Dialect name.
get_dialect(name)
Dialect name.
list_dialects()
.
: excel excel-tab.

import csv
# CSV
f = open(scmods.csv,r)

email

685

for r in csv.reader(f):
lastname, firstname, street, city, zip = r
print({0} {1} {2} {3} {4}.format(*r))

# DictReader
f = open(address.csv)
r = csv.DictReader(f,[lastname,firstname,street,city,zip])
for a in r:
print({firstname} {lastname} {street} {city} {zip}.format(**a))

# SV
data = [
[Blues,Elwood,1060 W Addison,Chicago,IL,60613 ],
[McGurn,Jack,4802 N Broadway,Chicago,IL,60640 ],
]
f = open(address.csv,w)
w = csv.writer(f)
w.writerows(data)
f.close()

email
email
, ,
, MIME.
email , .

:
, smtplib.


email , :
message_from_file(f)
, f, , . MIME, , .
Message.
message_from_string(str)
, str. Message.
m Message, ,
:

686

24.

m[name]

name.

m.keys()

m.values()

m.items()

,
.

m.get(name [,def])

name. def
,
.

len(m)

str(m)

. ,
as_string().

name in m

True,
name.

m , :
m.get_all(name [, default])
name.
, default.
m.get_boundary([default])
boundary, Content-type . ,
===============0995017162==, . boundary ,
default.
m.get_charset()
, (, iso-8859-1).
m.get_charsets([default])
,
. , ,
. Contenttype, . -
Content-type,
default (
None).
m.get_content_charset([default])
Content-type
. , default.

email

687

m.get_content_maintype()
(, text multipart).
m.get_content_subtype()
(, plain mixed).
m.get_content_type()
,
(, multipart/mixed text/plain).
m.get_default_type()
(, text/plain
).
m.get_filename([default])
filename Content-Disposition,
. default, filename.
m.get_param(param [, default [, header [, unquote]]])
, charset format
Content-Type: text/plain; charset=utf-8; format=flowed. . param , default , , header
unquote , ,
. header , Content-type. unquote
True. ,
(charset, language, value), , , RFC-2231.
charset , iso-8859-1,
language , en, value
.
m.get_params([default [, header [, unquote]]])
header . default , .
header , Content-type. unquote , , (True ).
(name, value), name ,
value , get_param().
m.get_payload([i [, decode]])
. ,
.
, , . , , -

688

24.

i . ,
.
decode True,
Content-Transfer-Encoding, (, quoted-printable, base64 ). ,
, i None,
decode True decode
. ,
. UTF8 - ,
decode(), .
m.get_unixfrom()
From ... UNIX, .
m.is_multipart()
True, m , .
m.walk()
, ,
Message. , .
.
, Message ,
.
m.preamble
, , , , , , .
m.epilogue
, ,
, .
m.defects
, . email.errors.
, Message
. , , .

689

email
import email
import sys

f = open(sys.argv[1],r)
#
m = email.message_from_file(f) #

#
print(From
: %s
print(To
: %s
print(Subject : %s
print()

/
% m[from])
% m[to])
% m[subject])

if not m.is_multipart():
# .
payload = m.get_payload(decode=True)
charset = m.get_content_charset(iso-8859-1)
print(payload.decode(charset))
else:
# .
#
1. text/plain
#
2.
for s in m.walk():
filename = s.get_filename()
if filename:
print( : %s % filename)
data = s.get_payload(decode=True)
open(filename,wb).write(data)
else:
if s.get_content_type() == text/plain:
payload = s.get_payload(decode=True)
charset = s.get_content_charset(iso-8859-1)
print(payload.decode(charset))

, ,
, . ,
. m.get_content_charset() payload.decode().


,
Message, email.
message, Message,
( ).
Message()
.
m Message , ,
.

690

24.

m.add_header(name, value, **params)


. name
, value params ,
. , add_header(Foo,Bar,spam=major)
Foo: Bar; spam=major.
m.as_string([unixfrom])
. unixfrom . True,
From ... UNIX. unixfrom
False.
m.attach(payload)
, . payload Message (, email.mime.text.MIMEText). payload
, .
, set_payload(),
.
m.del_param(param [, header [, requote]])
param header. ,
Foo: Bar; spam=major, del_param(spam,Foo)
spam=major . requote True ( ), .
header, Content-type.
m.replace_header(name, value)
name value.
, KeyError.
m.set_boundary(boundary)
boundary boundary. Content-type
boundary. Content-type, HeaderParseError.
m.set_charset(charset)
. charset , iso-8859-1 euc-jp.
Content-type (, Content-type: text/html; charset=iso-8859-1).
m.set_default_type(ctype)
ctype. ctype , MIME,
text/plain message/rfc822.
Content-type .

email

691

m.set_param(param, value [, header [, requote [, charset [, language]]]])


. param , value . header , Contenttype. requote
, , .
True. charset
language . , RFC-2231.
param*=iso-8859-1en-ussome%20value.
m.set_payload(payload [, charset])
payload .
payload . , , payload Message.
charset , ( set_charset()).
m.set_type(type [, header [, requote]])
, Content-type. type , text/plain
multipart/mixed, . header , Content-type.
requote
, , .
True.
m.set_unixfrom(unixfrom)
From ... UNIX. unixfrom , , From. , m.as_string()
unixfrom True.
, Message
,
, . , . , ,
attach() Message. , ,
.
MIMEApplication(data [, subtype [, encoder [, **params]]])
email.mime.application. ,, . data
. subtype

692

24.

octet-stream. encoder email.encoders.


base 64. params
, Content-type .
MIMEAudio(data [, subtype [, encoder [, **params]]])
email.mime.audio. ,
. data
. subtype
, mpeg wav. subtype , ,
sndhdr. encoder params ,
MIMEApplication().
MIMEImage(data [, subtype [, encoder [, **params]]])
email.mime.image. ,
. data . subtype
, jpg png. subtype , , imghdr. encoder params
, MIMEApplication().
MIMEMessage(msg [, subtype])
email.mime.message. MIME,
. msg
. subtype
rfc822.
MIMEMultipart([subtype [, boundary [, subparts [, **params]]]])
email.mime.multipart. MIME,
. subtype , Content-type:
Content-type:
-type:
type:: multipart/subtype. mixed.
boundary , , .
None, . subparts Message, . params
, Content-type .
, ,
Message.attach().
MIMEText(data [, subtype [, charset]])
email.mime.text.
. data .. subtype , plain

693

email

( ) html. charset us-ascii. .


, , ,
:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.audio import MIMEAudio

sender
receiver
subject
body
audio

=
=
=
=
=

jon@nogodiggydie.net
dave@dabeaz.com
Faders up!
I never should have moved out of Texas. -J.\n
TexasFuneral.mp3

m = MIMEMultipart()
m[to]
= receiver
m[from]
= sender
m[subject] = subject

m.attach(MIMEText(body))
apart = MIMEAudio(open(audio,rb).read(),mpeg)
apart.add_header(Content-Disposition,attachment,filename=audio)
m.attach(apart)

#
s = smtplib.SMTP()
s.connect()
s.sendmail(sender, [receiver],m.as_string())
s.close()

. , ,
.

email , ( ,
).
4.0, Python 2.6 Python 3.0.
,
, .

694

24.

hashlib
hashlib
, MD5
5 SHA1.
1. , ,
:

md5()

MD5 (128 )

sha1()

SHA1 (160 )

sha224()

SHA224 (224 )

sha256()

SHA256 (256 )

sha384()

SHA384 (384 )

sha512()

SHA512 (512 )

d ,
, :

d.update(data)


. data .
.

d.digest()

d.hexdigest()


, .

d.copy()

. .

d.digest_size

d.block_size

, .

, :
new(hashname)
. hashname
, md5 sha256.
, , ,
OpenSSL ( ).

hmac

695

hmac
hmac HMAC (Keyed-Hashing for
Message Authentication - ),
RFC-2104.
-2104. HMAC , , , MD5 SHA-1.
new(key [, msg [, digest]])
HMAC, key , msg digest , -.
hashlib.md5().
,
, .
h HMAC :
h.update(msg)
msg HMAC.
h.digest()
, , ,
.
. MD5 16 ; SHA-1 20
.
h.hexdigest()

.
h.copy()
HMAC.

hmac ,
. key new() ,
. ,
HMAC ,
HMAC.. , HMAC (
)
, . :
import hmac
secret_key = bpeekaboo # , .
#

696

24.
# , os.urandom()
# .

data = bHello World

#
# out - -,
# .
h = hmac.new(secret_key)
h.update(data)
out.send(data)
#
out.send(h.digest())
#

#
# in - -,
# .
h = hmac.new(secret_key)
data = in.receive()
#
h.update(data)
digest = in.receive()
# ,
if digest != h.digest():
raise AuthenticationError(Message not authenticated)

HTMLParser
Python 3 html.parser. HTMLParser HTMLParser,
HTML XHTML. , ,
HTMLParser, .
HTMLParser()
, HTML.
.
h HTMLParser :
h.close()

. , HTML
.
h.feed(data)
.. . (,
HTML),
), feed(), .
h.getpos()

(line, offset).

HTMLParser

697

h.get_starttag_text()
, .
h.handle_charref(name)
- ,
, &#ref;. name . , &#229; name
229.
h.handle_comment(data)
- , . data . , <!--comment--> data comment.
h.handle_data(data)
- ,
. data .
h.handle_decl(decl)
- ,
<!DOCTYPE HTML ...>. decl , <! >.
h.handle_endtag(tag)
- , .
tag , . , </BODY>
tag body.
h.handle_entityref(name)
- ,
&name;. name . , &lt; name
lt.
h.handle_pi(data)
- ,
<?processing instruction>. data , <? >. XHTML, <?...?>,
? data.
h.handle_startendtag(tag, attrs)
-
XHTML, <tag name=value.../>. tag -

698

24.

. attrs
(name, value), name ,, , value .
. ,
<a href=http://www.foo.com/> tag
a, attrs [(href,http://www.foo.com)].
, handle_starttag() handle_endtag().
h.handle_starttag(tag, attrs)
- ,
<tag name=value ...>. tag attrs ,
handle_startendtag().
h.reset()
,
.
HTMLParser :
HTMLParserError
. HTMLParserError
. msg , lineno , , offset
.

HTML,
urllib, <a href=...>:
# printlinks.py
try:
from HTMLParser import HTMLParser
from urllib2 import urlopen
except ImportError:
from html.parser import HTMLParser
from urllib.request import urlopen
import sys

class PrintLinks(HTMLParser):
def handle_starttag(self,tag,attrs):
if tag == a:
for name,value in attrs:
if name == href: print(value)

p
= PrintLinks()
u
= urlopen(sys.argv[1])
data = u.read()
charset = u.info().getparam(charset)
# Python 2
#charset = u.info().get_content_charset() # Python 3
p.feed(data.decode(charset))
p.close()

699

json

, HTML,, urllib, .
, ,
, .
, Python 2 Python 3.

HTMLParser . HTML . , , . ,
HTML,
Beautiful Soup (http://pypi.python.org/pypi/BeautifulSoup).

json
json
JavaScript (JavaScript Object Notation, JSON). JSON http://json.org.1
JavaScript. Python, . , JSON [value1, value2, ... ], {name:value, name:value, .... }.
JSON
Python. Python,
, JSON, ( ).
JSON

Python

object

dict

array

list (tuple)

string

unicode (str, bytes)

number

int, float

true

True

false

False

null

None

: http://www.json.org/json-ru.html. . .

700

24.

,
. , ,
utf-8 (,
). JSON .
JSON
:
dump(obj, f, **opts)
obj JSON f,
. opts , :

skipkeys

, , ,
( ) , .
True, .
False ( ),
TypeError.

ensure_ascii

, , f.
False. True,
f ,
, , codecs .

check_circular

, ,
. True.
False ,
OverflowError.

allow_nan

, , , , (, NaN, inf, -inf).


True.

cls

JSONEncoder, .
,
, JSONEncoder, . ,
dump(),
.

indent

, . . None,
.

701

json

separators

(item_separator, dict_separator), item_sepa_sepaseparator -,


, dict_
separator -, .
(, , : ).

encoding

, .
utf-8.

default

, ,
.
,
(, ),
TypeError. TypeError.

dumps(obj, **opts)
, dump(), ,
.
load(f, **opts)
f, , JSON
Python. opts
, , . , f.read(), f .
,
, JSON .

encoding

,
. utf-8.

strict

, ,
JSON () . True,
.

cls

JSONDecoder, .
,
, JSONDecoder, . ,
load(),
.

702

24.

()

object_hook

,
JSON.
dict().

parse_float

,
JSON.. float().

parse_int

, JSON.
int().

parse_constant

,
JSON, NaN, true, false .

loads(s, **opts)
, load(), , JSON s.
, pickle marshal, ,
. , dump()
JSON
. load() JSON ( ,
). JSON , HTML XML.
XML
.
JSON
, , ,
:
JSONDecoder(**opts)
, JSON. opts
, , load().
d JSONDecoder :
d.decode(s)
JSON Python. s
JSON.
d.raw_decode(s)
(pyobj, index), pyobj JSON Python, index s,
JSON.

mimetypes

703

, , JSON.
JSONEncoder(**opts)
, Python
JSON. opts ,
,
dump().
e JSONEncoder :
e.default(obj)
, obj Python
, . ,
JSON (, , ).
e.encode(obj)
obj Python JSON.
e.iterencode(obj)
, obj Python JSON .
JSON . ,

, . , , ,
.
, JSONDecoder JSONEncoder, __init__(), . ,
:
class MyJSONDecoder(JSONDecoder):
def __init__(self, **kwargs):
#
foo = kwargs.pop(foo,None)
bar = kwargs.pop(bar,None)
# ,
JSONDecoder.__init__(self,**kwargs)

mimetypes
mimetypes MIME ,
.
MIME.. MIME /, : text/html, image/png audio/mpeg.
guess_type(filename [, strict])
MIME , URL.. (type, encoding), type /,

704

24.

encoding (, compress gzip). (None, None), .


strict True ( ), MIME,
IANA (http://www.iana.org/assignments/media-types).
, MIME.
guess_extension(type [, strict])
MIME.. , (.). None. strict True ( ), MIME
guess_all_extensions(type [, strict])
, guess_extension(), .
init([files])
. files
. , MIME , :
image/jpeg: jpe jpeg jpg
text/html: htm html
...

read_mime_types(filename)
filename. ,

MIME. ,
None.
add_type(type, ext [, strict])
MIME. type
MIME, text/plain, ext
, .txt, strict ,
, MIME . True.

quopri
quopri quotedprintable .. 8- ,
ASCII,, (,
128-255). , quoted-printable:

quopri

705

, ASCII, =,
.

= . , (, =0C). , =3D. = ,
. , .

,
.

,
ASCII.. , Copyright 2009, Python
bCopyright \xa9 2009.
quoted-printable bCopyright =A9 2009,
\xa9 =A9.
decode(input, output [, header])
quoted-printable. input output , , .
header True, (_) .
. MIME. False.
decodestring(s [, header])
s quoted-printable.
s ,
. header , decode().
encode(input, output, quotetabs [, header])
quoted-printable. input output ,, ,, . quotetabs True,
.
. quotetabs
False. header , decode().
encodestring(s [, quotetabs [, header]])
s quoted-printable. . quotetabs header
, encode().

quoted-printable 8- .

706

24.
, ASCII ASCII,
. ,
,
.

xml
Python ,
XML.
XML- ,
. , , XML.
XML , Inside
Inside XML
(Steve
Steve Holzner)) (New
New Riders)) XML
XML in a Nutshell
(Elliotte
Elliotte Harold)) . (W.
W.. Scott Means)) (OReilly
OReilly
Reilly
Reilly and Associates). , XML
Python, Python & XML (Christopher Jones)
(OReilly and Associates) XML Processing with Python
(Sean McGrath) (Prentice Hall).
Python XML.
XML, SAX
DOM. SAX (Simple
Simple API for XML XML) ,
XML-,
-, XML, -,
. DOM (Document
Document Object Model ) XML-
. DOM . SAX,, DOM , Python. Python
,
Java JavaScript.
XML- ,
SAX DOM,, ElementTree. Python XML-, Python,
SAX DOM.
XML-,
-, ElementTree .
XML.. -

707

xml

Python ,
, XML .
, , , XSLT
XPATH.
http://wiki.python.org/moin/PythonXml.

XML
XML,
.
<?xml version=1.0 encoding=iso-8859-1?>
<recipe>
<title>
Famous Guacamole
</title>
<description>
A southwest favorite!
</description>
<ingredients>
<item num=4> Large avocados, chopped </item>
<item num=1> Tomato, chopped </item>
<item num=1/2 units=C> White onion, chopped </item>
<item num=2 units=tbl> Fresh squeezed lemon juice </item>
<item num=1> Jalapeno pepper, diced </item>
<item num=1 units=tbl> Fresh cilantro, minced </item>
<item num=1 units=tbl> Garlic, minced </item>
<item num=3 units=tsp> Salt </item>
<item num=12 units=bottles> Ice-cold beer </item>
</ingredients>
<directions>
Combine all ingredients and hand whisk to desired consistency.
Serve and enjoy with ice-cold beers.
</directions>
</recipe>

, , : <title>...</title>.
, <item> <ingredients>. XML-
, . <recipe>. , , , item : <item
num=4>Large avocados, chopped</item>.
XML-
. , .
, , .

708

24.

xml.dom.minidom
xml.dom.minidom XML-
, , DOM.. , :
parse(file [, parser])
file ,
. file
, . parser
SAX2-
2- , . ,
.
parseString(string [, parser])
, parse(), ,
, .

, , , . n, , , :

n.attributes

, (
).

n.childNodes

n.

n.firstChild

n.

n.lastChild

n.

n.localName

.
(, <foo:bar ...>), , .

n.namespaceURI

, n ( ).

n.nextSibling

, n
.
,
None.

n.nodeName

. .

n.nodeType

, . , Node:
ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_
FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_TYPE_NODE, ELEMENT_NODE,
ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE TEXT_NODE.

709

xml

n.nodeValue

. .

n.parentNode

n.prefix

, . ,
<foo:bar ...> foo.

n.previousSibling

, n .

, .
.
n.appendChild(child)
child n. .
n.cloneNode(deep)
n. deep True,
.
n.hasAttributes()
True, - .
n.hasChildNodes()
True, - .
n.insertBefore(newchild, ichild)
newchild ichild. ichild n.
n.isSameNode(other)
True, other DOM, n.
n.normalize()
.
n.removeChild(child)
child n.
n.replaceChild(newchild,oldchild)
oldchild newchild. oldchild
n.
,
, -
Document, Element Text.
.

710

24.

Document
d Document . :
d.documentElement
.
d.getElementsByTagName(tagname)

tagname.
d.getElementsByTagNameNS(namespaceuri, localname)
, namespaceuri localname. NodeList.

Element
e Element XML,
<foo>...</foo>. ,
Text.
:
e.tagName
. , <foo ...>, foo.
e.getElementsByTagName(tagname)
.
e.getElementsByTagNameNS(namespaceuri, localname)
. namespaceuri localname , . , <foo xmlns:foo=http://
www.spam.com/foo>, namespaceuri
http://www.spam.com/foo. <foo:bar>
localname bar.
NodeList.
e.hasAttribute(name)
True, name.
e.hasAttributeNS(namespaceuri, localname)
True, , namespaceuri localname.
, getElementsByTagNameNS().
e.getAttribute(name)
name. . , .

711

xml

e.getAttributeNS(namespaceuri, localname)
,
namespaceuri localname.
.. , . , getElementsByTagNameNS().

Text
Text . t.data t Text. ,
,
Text.


. DOM Python
.
n.toprettyxml([indent [, newl]])
XML, n
. indent , .
\t. newl
\n.
n.toxml([encoding])
XML, n . encoding (, utf-8).
, .
n.writexml(writer [, indent [, addindent [, newl]]])
XML writer. writer , write(), . indent , . n. addindent ,

n. newl .

DOM
xml.dom.minidom
XML-:
from xml.dom import minidom
doc = minidom.parse(recipe.xml)

ingredients = doc.getElementsByTagName(ingredients)[0]
items
= ingredients.getElementsByTagName(item)

for item in items:


num
= item.getAttribute(num)

712

24.
units
= item.getAttribute(units)
text
= item.firstChild.data.strip()
quantity = %s %s % (num,units)
print(%-10s %s % (quantity,text))

xml.dom.minidom ,
XML.. .

xml.etree.ElementTree
xml.etree.ElementTree
ElementTree,
.
XML,,
.

ElementTree
ElementTree , .
ElementTree([element [, file]])
ElementTree. element
, . , . file , XML .
tree ElementTree :
tree._setroot(element)
element .
tree.find(path)
, path. path
,
. path :
path

tag


tag, <tag>...</tag>.
. tag,
, <foo><tag>...</tag></foo>,
path.

713

xml
path

parent/tag

tag,
parent. path .

. ,
*/tag tag.

//

,
. , .//tag
tag,
.

XML,
, tag path {uri}tag, uri
, http://www.w3.org/TR/html4/.
tree.findall(path)
, path, , , .
tree.findtext(path [, default])
, path. default
, ,
, .
tree.getiterator([tag])
, , ,
tag. tag , , .
tree.getroot()
.
tree.parse(source [, parser])
XML-
- . source
, , XML-.
-. parser TreeBuilder,
.
tree.write(file [, encoding])
. file
, , .
encoding ,

714

24.

. , , (
utf-8 ascii).


, ElementTree, , ,
:
Comment([text])
. text
. XML.
Element(tag [, attrib [, **extra]])
. tag .
, <foo>....</foo>, tag
foo. attrib , . extra
.
fromstring(text)
XML text; ,
XML(), .
ProcessingInstruction(target [, text])
, . target text .. XML <?target text?>.
SubElement(parent, tag [, attrib [, **extra]])
, Element(), , , parent.
XML(text)
XML text. ,
text <foo>....</foo>,
foo.
XMLID(text)
, XML(text), , id
, id . (elem, idmap), elem ,
idmap , ID. ,
XMLID(<foo id=123><bar id=456>Hello</bar></foo>) (<Element
foo>, {123: <Element foo>, 456: <Element bar>}).

715

xml


, , ElementTree, ,
. elem Python:

elem[n]

n- elem.

elem[n] = newelem

n- elem newelem.

del elem[n]

n- elem.

len(elem)

elem.

elem.tag

, . , <foo>...</foo> tag foo.

elem.text

, . ,
,
XML.

elem.tail

, .
XML ,
,
.

elem.attrib

,
:
elem.append(subelement)
subelement .
elem.clear()
, , ,
.
elem.find(path)
, path.
elem.findall(path)
, path. , , .

716

24.

elem.findtext(path [, default])
, path. default ,
, .
elem.get(key [, default])
key. default ,
, .
XML, key {uri}key, uri , http://www.w3.org/TR/html4/.
elem.getchildren()
.
elem.getiterator([tag])
,
tag.
elem.insert(index, subelement)
,
index.
elem.items()
(name, value).
elem.keys()
.
elem.remove(subelement)
subelement .
elem.set(key, value)
value key.


ElementTree . .
TreeBuilder([element_factory])
, ElementTree start(), end() data() .. element_factory ,
.
t TreeBuilder :
t.close()
ElementTree
.

xml

717

t.data(data)
.
t.end(tag)
.
t.start(tag, attrs)
. tag , attrs .


, xml.
etree.ElementTree:
dump(elem)
elem sys.stdout .
XML.
iselement(elem)
, elem .
iterparse(source [, events])
XML ,
source. source ,, , XML. events .
: start, end, start-ns end-ns. , end. , (event, elem), event
, start end, elem .
start
, , .
end , .
parse(source)
XML ElementTree. source , , XML.
tostring(elem)
XML, elem
.

XML-
ElementTree
XML-
- . DOM.

718

24.
from xml.etree.ElementTree import ElementTree

doc = ElementTree(file=recipe.xml)
ingredients = doc.find(ingredients)

for item in ingredients.findall(item):


num
= item.get(num)
units
= item.get(units,)
text
= item.text.strip()
quantity = %s %s % (num, units)
print(%-10s %s % (quantity, text))

, ElementTree,
. ,
, <item>...</item>.
from xml.etree.ElementTree import ElementTree

doc = ElementTree(file=recipe.xml)
for item in doc.findall(.//item):
num
= item.get(num)
units
= item.get(units,)
text
= item.text.strip()
quantity = %s %s % (num, units)
print(%-10s %s % (quantity, text))

XML- recipens.xml, :
<?xml version=1.0 encoding=iso-8859-1?>
<recipe xmlns:r=http://www.dabeaz.com/namespaces/recipe>
<r:title>
Famous Guacamole
</r:title>
<r:description>
A southwest favorite!
</r:description>
<r:ingredients>
<r:item num=4> Large avocados, chopped </r:item>
...
</r:ingredients>
<r:directions>
Combine all ingredients and hand whisk to desired consistency.
Serve and enjoy with ice-cold beers.
</r:directions>
</recipe>

,

URI.
URI, :
from xml.etree.ElementTree import ElementTree
doc = ElementTree(file=recipens.xml)
ns = {

719

xml
r : http://www.dabeaz.com/namespaces/recipe
}
ingredients = doc.find({%(r)s}ingredients % ns)
for item in ingredients.findall({%(r)s}item % ns):
num
= item.get(num)
units
= item.get(units,)
text
= item.text.strip()
quantity = %s %s % (num, units)
print(%-10s %s % (quantity, text))

ElementTree
XML, .
, XML
:
<?xml version=1.0 encoding=utf-8?>
<music>
<album>
<title>A Texas Funeral</title>
<artist>Jon Wayne</artist>
...
</album>
<album>
<title>Metaphysical Graffiti</title>
<artist>The Dead Milkmen</artist>
...
</album>
... 100 000 ...
</music>

, . , XML 10
100 . , ElementTree.
iterparse().
<album> :
from xml.etree.ElementTree import iterparse

iparse = iterparse(music.xml, [start,end])


# music
for event, elem in iparse:
if event == start and elem.tag == music:
musicNode = elem
break

#
albums = (elem for event, elem in iparse
if event == end and elem.tag == album)

for album in albums:


#
...
musicNode.remove(album) #

720

24.

iterparse()
, .
musicNode.remove(album) <album>
( ). , , , ,
.

ElementTree XML- Python.


, . ,
XML XML, DTD.. . , lxml.
etree (http://codespeak.net/lxml/),
ElementTree libxml2 libxslt
XPATH, XSLT .

ElementTree , (Fredrik Lundh) (


: http://effbot.org/zone/element-index.htm).
,
, .

xml.sax
xml.sax XML-,
-, SAX2.
parse(file, handler [, error_handler])
XML- file. file
. handler ,
.. error_handler , ,
.
parseString(string, handler [, error_handler])
, parse(), XML- string.

-
- XML-, parse()
parseString() -.
, , ContentHandler. c ContentHandler , :

xml

721

c.characters(content)
, .
content .
c.endDocument()
.
c.endElement(name)
name. , </foo>
foo name.
c.endElementNS(name, qname)
,
XML. name (uri,
localname), qname .
qname None, SAX . ,
<foo:bar xmlns:foo=http://spam.com>, name
(uhttp://spam.com, ubar).
c.endPrefixMapping(prefix)
XML. prefix
.
c.ignorableWhitespace(whitespace)
, . whitespace , .
c.processingInstruction(target, data)
, XML,, <? ... ?>. target
, data . , <?xml-stylesheet href=mystyle.css type=text/css?>
target xml-stylesheet, data
href=mystyle.css type=text/css.
c.setDocumentLocator(locator)
, , ,
.
, - ,
. ,
locator, : getColumnNumber(), getLineNumber(), getPublicId() getSystemId(),
.

722

24.

c.skippedEntity(name)
, . name .
c.startDocument()
.
c.startElement(name, attrs)
, XML. name
, attrs
. , XML- <foo bar=whatever spam=yes>,
name foo, attrs
bar spam. attrs
,
:

attrs.getLength()

attrs.getNames()

attrs.getType(name)

name

attrs.getValue(name)

name

c.startElementNS(name, qname, attrs)


, XML . name
(uri, localname), qname
( qname None, SAX2 ). attrs . , <foo:bar xmlns:foo=http://
spam.com blah=whatever> name (uhttp://
spam.com, ubar), qname None attrs blah. attrs ,
, startElement(), .
, ,
:

attrs.getValueByQName(qname)

qname.

attrs.getNameByQName(qname)

(namespace, localname) qname

attrs.getQNameByName(name)


name

attrs.getQNames()

723

xml

c.startPrefixMapping(prefix, uri)
XML. ,
<foo:bar xmlns:foo=http://spam.com>, prefix foo, uri http://
spam.com.

SAX, ,
.
xml.dom.minidom.
from xml.sax import ContentHandler, parse

class RecipeHandler(ContentHandler):
def startDocument(self):
self.initem = False
def startElement(self,name,attrs):
if name == item:
self.num
= attrs.get(num,1)
self.units = attrs.get(units,none)
self.text = []
self.initem = True
def endElement(self,name):
if name == item:
text = .join(self.text)
if self.units == none: self.units =
unitstr = %s %s % (self.num, self.units)
print(%-10s %s % (unitstr,text.strip()))
self.initem = False
def characters(self,data):
if self.initem:
self.text.append(data)

parse(recipe.xml,RecipeHandler())

xml.sax ,
XML-
- . , DTD .
.

xml.sax.saxutils
xml.sax.saxutils
, SAX-,
.
escape(data [, entities])
data . , < &lt;. entities

724

24.

, . , entities { u\xf1 : &ntilde; },


&ntilde;.
unescape(data [, entities])
data
. , &lt; <. entities
, . entities ,
escape(), { &ntilde; : u\xf1 }.
quoteattr(data [, entities])
data
,
XML.. , , print <element
attr=%s> % quoteattr(somevalue). entities ,
escape().
XMLGenerator([out [, encoding]])
ContentHandler,
XML XML-.
-. XML-. out , sys.stdout. encoding
iso-8859-1.
, .

25


, ,
, Python. ,
,
,
, , , .
, , http://docs.
python.org/library/modname.
http://docs.python.org/library/modindex.html.
, , ,
Python 2 Python 3. -
, , ,
. Python 3
. , ,
.

Python
, , Python Python.

Python.

bdb

code

codeop

Python

726

25.

()

compileall


Python,

copy_reg (copyreg)


pickle

dis

distutils

Python

fpectl

imp

import

keyword

,
Python

linecache

modulefinder

parser


Python

pickletools

pkgutil

pprint

pyclbr

py_compile

Python
-

repr (reprlib)

repr()

symbol

tabnanny

test

token

tokenize

Python

user

zipimport

zip-


,
,
.

difflib

fpformat

stringprep

textwrap

727



. ,
, , 19 .

crypt

crypt UNIX

curses

curses

grp

pty

pipes

nis

Sun NIS

platform

pwd

readline

GNU readline

rlcompleter

GNU readline

resource

sched

spwd

stat

,
os.stat()

syslog

syslog UNIX

termios

TTY UNIX

tty

728

25.

imaplib

IMAP

nntplib

NNTP

poplib

POP3

smtpd

SMTP

telnetlib

Telnet



, 24 .

binhex

BinHex4

formatter

mailcap

mailcap

mailbox

netrc

netrc

plistlib

Macintosh plist

uu

uuencode

xdrlib

Sun XDR

gettext

locale

729

audioop

aifc

AIFF AIFC

sunau

Sun AU

wave

WAV

chunk

IFF

colorsys

imghdr

sndhdr

ossaudiodev

OSS-


, - :

cmd

calendar

shlex

sched

Tkinter (tkinter)

Tcl/Tk Python

winsound

Windows

III


26. Python
A. Python 3

26


Python
Python
C. Python , . : Python
import.
, , Python.
Python-C,
-C,
C,, Python
. Python-C , C
Python,, . ,
Python
C,
.

Python-C.
, Python,, C.. , ,
,
Extending
Extending and Embedding the Python Interpreter
( Python), http://docs.python.org/extending,
Python/C API Reference Manual
(
Python/C), http://docs.python.org/c-api.
ctypes. ,
-

734

26. Python

, C, -
C C.
, , Python,

. SWIG (http://
www.swig.org) , Python
C.
http://
wiki.python.org/moin/IntegratingPythonWithOtherLanguages.


C Python..
Python , C.. , C
, , :
/* : example.h */
#include <stdio.h>
#include <string.h>
#include <math.h>

typedef struct Point {


double x;
double y;
} Point;

/* x y */
extern int gcd(int x, int y);

/* och nch s */
extern int replace(char *s, char och, char nch);

/* */
extern double distance(Point *a, Point *b);

/* */
#define MAGIC 0x31337

. :
/* example.c */
#include example.h
/* x y */
int gcd(int x, int y) {
int g;
g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}

735

return g;
}

/* */
int replace(char *s, char oldch, char newch) {
int nrep = 0;
while (s = strchr(s,oldch)) {
*(s++) = newch;
nrep++;
}
return nrep;
}

/* */
double distance(Point *a, Point *b) {
double dx,dy;
dx = a->x - b->x;
dy = a->y - b->y;
return sqrt(dx*dx + dy*dy);
}

C main(), :
/* main.c */
#include example.h
int main() {
/* gcd() */
{
printf(%d\n, gcd(128,72));
printf(%d\n, gcd(37,42));
}
/* replace() */
{
char s[] = Skipping along unaware of the unspeakable peril.;
int nrep;
nrep = replace(s, ,-);
printf(%d\n, nrep);
printf(%s\n,s);
}
/* distance() */
{
Point a = { 10.0, 15.0 };
Point b = { 13.0, 11.0 };
printf(%0.2f\n, distance(&a,&b));
}
}

:
% a.out
8
1
6
Skipping-along-unaware-of-the-unspeakable-peril.
5.00

736

26. Python


, C, , Python C.
_example:
/* pyexample.c */
#include Python.h
#include example.h

static char py_gcd_doc[] = ;


static PyObject *
py_gcd(PyObject *self, PyObject *args) {
int x,y,r;
if (!PyArg_ParseTuple(args,ii:gcd,&x,&y)) {
return NULL;
}
r = gcd(x,y);
return Py_BuildValue(i,r);
}

static char py_replace_doc[] = ;


static PyObject *
py_replace(PyObject *self, PyObject *args, PyObject *kwargs) {
static char *argnames[] = {s,och,nch,NULL};
char
*s,*sdup;
char
och, nch;
int
nrep;
PyObject *result;
if (!PyArg_ParseTupleAndKeywords(args,kwargs, scc:replace,
argnames, &s, &och, &nch)) {
return NULL;
}
sdup = (char *) malloc(strlen(s)+1);
strcpy(sdup,s);
nrep = replace(sdup,och,nch);
result = Py_BuildValue((is),nrep,sdup);
free(sdup);
return result;
}

static char py_distance_doc[] = ;


static PyObject *
py_distance(PyObject *self, PyObject *args) {
PyErr_SetString(PyExc_NotImplementedError,distance() not implemented.);
return NULL;
}

static PyMethodDef _examplemethods[] = {


{gcd, py_gcd, METH_VARARGS, py_gcd_doc},
{replace, py_replace, METH_VARARGS | METH_KEYWORDS, py_replace_doc},
{distance,py_distance,METH_VARARGS, py_distance_doc},
{NULL, NULL, 0, NULL}

737

};

#if PY_MAJOR_VERSION < 3


/* Python 2 */
void init_example(void) {
PyObject *mod;
mod = Py_InitModule(_example, _examplemethods);
PyModule_AddIntMacro(mod,MAGIC);
}
#else
/* Python 3 */
static struct PyModuleDef _examplemodule = {
PyModuleDef_HEAD_INIT,
_example,
/* */
NULL,
/* , NULL */
-1,
_examplemethods
};
PyMODINIT_FUNC
PyInit__example(void) {
PyObject *mod;
mod = PyModule_Create(&_examplemodule);
PyModule_AddIntMacro(mod, MAGIC);
return mod;
}
#endif

Python.h.
C,
Python, -. -
(self args, PyObject *)
(self, args kwargs, PyObject *). self ,
- .
self. self NULL. args
, .
kwargs .
Python C PyArg_ParseTuple() PyArg_Par_ParParseTupleAndKeywords().
Py_BuildValue(). .
, py_gcd_doc py_replace_
doc, . ( ).
- ,
, . - py_replace() ,
C (

738

26. Python

). , -
, Python.
, PyExc_SetString(), - py_distance().
NULL .
_examplemethods Python - C.. . METH_VARARGS ,
-.
- .
METH_VARARGS | METH_KEYWORDS, , - .
-.
, Python 2 Python 3. Python 2
init_example. Py_InitModule(_ex_InitModule(_exInitModule(_ex(_exexample,_examplemethods) _example
, , . Python 3 _examplemodexamplemodule PyModuleDef, .
PyInit__example(), ,
. , ,
. , PyModule_AddIntMacro() .
, . modname, Python 2
initmodname(), Python 3 PyInit_modname(). ,
.


C , , _example.
Python.. , _socket, _thread, _sre _fileio, C socket, threading, re
io. C
.
Python, , :
# example.py
from _example import *

739

#
...

- Python ,
.
Python, C.
. , , C Python.


distutils. ,
setup.py, , :
# setup.py
from distutils.core import setup, Extension

setup(name=example,
version=1.0,
py_modules = [example.py],
ext_modules = [
Extension(_example,
[pyexample.c,example.c])
]
)


Python (example.py) , (pyexample.c, example.c). ,
:
% python setup.py build_ext --inplace

.
_examplemodule.so, _examplemodule.pyd .
. :
% python3.0
Python 3.0 (r30:67503, Dec 4 2008, 09:40:15)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type help, copyright, credits or license for more information.
>>> import example
>>> example.gcd(78,120)
6
>>> example.replace(Hello World, ,-)
(1, Hello-World)
>>> example.distance()
Traceback (most recent call last):
File <stdin>, line 1, in <module>

740

26. Python
NotImplementedError: distance() not implemented.
(:
( ):
<stdin>, 1, <module>
NotImplementedError: distance()
)
>>>


,
.
setup.py, :
# setup.py
from distutils.core import setup, Extension

setup(name=example,
version=1.0,
py_modules = [example.py],
ext_modules = [
Extension(_example,
[pyexample.c,example.c],
include_dirs = [/usr/include/X11,/opt/include],
define_macros = [(DEBUG,1),
(MONDO_FLAG,1)],
undef_macros = [HAVE_FOO,HAVE_NOT],
library_dirs= [/usr/lib/X11, /opt/lib],
libraries = [ X11, Xt, blah ])
]
)

, python setup.py install.


8 , .

. . Linux:
linux % gcc -c -fpic -I/usr/local/include/python2.6 example.c pyexample.c
linux % gcc -shared example.o pyexample.o -o _examplemodule.so

Python
C
, Python C.
Python.h.
int PyArg_ParseTuple(PyObject *args, char *format, ...);
args
C. format ,

741

, .
26.126.3, args. C,
. format. , .
int PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kwargs, char *format,
char **kwlist, ...);
args kwargs
. format ,
PyArg_ParseTuple(). , kwlist , . 1,
0.
.. 26.1 , format . C ,
PyArg_Parse*().
, .
26.1.
PyArg_Parse*

Python

signed char *r

unsigned char *r

short *r

unsigned short *r

int *r

unsigned int *r

long int *r

unsigned long *r

long long *r

unsigned long long *r

Py_ssize_t *r

float *r

double *r

Py_complex *r

,
Python, ,
C,

742

26. Python

OverflowError.
( I, H, K ) . int float Python.
.
, , , __int__() __float__(). , , __int__(),
( __int__()
).
. 26.2 ,
. .
26.2.
PyArg_Parse*

Python

char *r
1

char **r

s#

char **r, int *len

s*

Py_buffer *r

None

char **r

z#

, None

char **r, int *len

z*

, ,
None

Py_buffer *r

(
)

char **r

y#

char **r, int *len

y*

Py_buffer *r

()

Py_UNICODE **r

u#

()

Py_UNICODE **r, int *len

es

const char *enc, char **r

es#

const char *enc, char **r, int *len

et

(- const char *enc, char **r, int *len


)

et#

const char *enc, char **r, int *len

t#

char **r, int *len

743

Python

, /

char **r

w#

, /

char **r, int *len

w*

, /

Py_buffer *r

C , char * .
, ,
. , C
NULL (\x00).
s, z, u,
es et, . 26.2. , NULL; TypeError.
C ,
NULL. Python 2 8-
, , Python 3 , et, str Python
.
C,, , (
UTF-8). u, , Python. Py_UNICODE,
wchar_t C.
es et . , utf-8 iso-8859-1, . et
es , ,
.
, es et
PyMem_Free().
, , :
PyObject *py_wrapper(PyObject *self, PyObject *args) {
char *buffer;
if (!PyArg_ParseTuple(args,es,utf-8,&buffer)) {
return NULL;
}
/* - . */
...

744

26. Python
/* */
PyMem_Free(buffer);
return result;
}

s#, z#, u#, es# et#.


, , ,
. NULL. ,
,
.
Python
. , (, , array, ).
, , , . ,
es# et#
, , , , .
,
PyMem_Free().
s* z* s# z#, Py_buf_bufbuffer .
PEP-3118,
-3118, , char *buf, int len int itemsize, , ( )
, . , ,
,
.
, , ,
, . PyBuffer_Release() .
t#, w, w# w* ,
s, , , . t# ,
. w ,
. Python,, , ,
.
C.
y, y# y* s, , .

745


. y
, NULL.
. 26.3 , Python PyObject *.
C,, Python, ,
, .
26.3. Python
PyArg_Parse*

Python

PyObject **r

O!

PyTypeObject *type, PyObject **r

O&

int (*converter)(PyObject *, void *), void *r

PyObject **r

PyObject **r

O, S U Python PyObject *.
S U .
O! :
Python PyObject * ,
.
, TypeError. :
/* */
PyObject *listobj;
PyArg_ParseTuple(args,O!, &PyList_Type, &listobj);

C,
Python,
.
C

Python

PyList_Type

list

PyDict_Type

dict

PySet_Type

set

PyFrozenSet_Type

frozen_set

PyTuple_Type

tuple

PySlice_Type

slice

PyByteArray_Type

bytearray

746

26. Python

O& (converter, addr)


PyObject * C. converter
int converter(PyObject *obj, void *addr), obj
Python, addr ,
PyArg_ParseTuple().
converter() 1, 0 . , converter() .
Python, , C.
, py_distance() :
/* Point. */
int convert_point(PyObject *obj, void *addr) {
Point *p = (Point *) addr;
return PyArg_ParseTuple(obj,ii, &p->x, &p->y);
}
PyObject *py_distance(PyObject *self, PyObject *args) {
Point p1, p2;
double result;
if (!PyArg_ParseTuple(args,O&O&,
convert_point, &p1, convert_point, &p2)) {
return NULL;
}
result = distance(&p1,&p2);
return Py_BuildValue(d,result);
}

, format , , , . :

(items)

. items .

. .

. .

(items) , Python.
C. ,
- py_distance():
PyObject *py_distance(PyObject *self, PyObject *args) {
Point p1, p2;
double result;
if (!PyArg_ParseTuple(args,(dd)(dd),
&p1.x, &p1.y, &p2.x, &p2.y)) {

747


return NULL;
}
result = distance(&p1,&p2);
return Py_BuildValue(d,result);
}

| ,
.
. :
. , , , .
; . ,
, ,
. , -
: ;. :
PyArg_ParseTuple(args,ii:gcd, &x, &y);
PyArg_ParseTuple(args,ii; gcd requires 2 integers, &x, &y);

/* */
PyArg_ParseTuple(args,s|s, &buffer, &delimiter);

C
Python
C Python :
PyObject *Py_BuildValue(char *format, ...)
Python
C. format , . C,
.
, format,
, PyArg_ParseTuple*(),
. 26.4.
26.4. Py_BuildValue()

Python

None

void

char *

, NULL.
NULL,
None.

s#

char *, int

. \x00.
NULL,
None.

748

26. Python

26.4 ()

Python

char *

, s,
.

y#

char *, int

, s#,
.

None

char *

, s.

z#

None

char *, int

, s#.

Py_UNICODE *

, NULL.

NULL, None.

u#

Py_UNICODE *

char *

C,
NULL,
.

U#

char *, int

C
.

char

8- .

unsigned char

8- .

short

16- .

unsigned short

16-
.

int

unsigned int

long

unsigned long

long long

long long.

unsigned long
long

long long
.

Py_ssize_t

size Python.

char

.
Python
.

float

double

Py_complex

749

Python

PyObject *

Python.
, ,
1.
,
. ,
-
, .

O&

, C,
.

PyObject *

, O.

PyObject *

, O,
, .

(items)

vars

items.
items .
vars
C,, items.

[items]

vars

items.
items . vars
C,
items.

{items}

vars

items.

:
Py_BuildValue()
Py_BuildValue(i,37)
Py_BuildValue(ids,37,3.4,hello)
Py_BuildValue(s#,hello,4)
Py_BuildValue(())
Py_BuildValue((i),37)
Py_BuildValue([ii],1,2)
Py_BuildValue([i,i],1,2)
Py_BuildValue({s:i,s:i},x,1,y,2)

None
37
(37, 3.5, hello)
hell
()
(37,)
[1,2]
[1,2]
{x:1, y:2}

char *; , (
UTF-8). -

750

26. Python

Python.

y y#, .


.
:
int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
. name , value Python, .
Py_BuildValue().
int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
.
void PyModule_AddStringConstant(PyObject *module, const char *name, const char
*value)
. value , NULL.
void PyModule_AddIntMacro(PyObject *module, macro)
.
macro .
void PyModule_AddStringMacro(PyObject *module, macro)
.


,
NULL. NULL, :
void PyErr_NoMemory()
MemoryError.
void PyErr_SetFromErrno(PyObject *exc)
exc. exc . errno C.
void PyErr_SetFromErrnoWithFilename(PyObject *exc, char *filename)
, PyErr_SetFromErrno(), .
void PyErr_SetObject(PyObject *exc, PyObject *val)
exc. exc , val .

751

void PyErr_SetString(PyObject *exc, char *msg)


exc. exc , msg , .
exc :
C

Python

PyExc_ArithmeticError

ArithmeticError

PyExc_AssertionError

AssertionError

PyExc_AttributeError

AttributeError

PyExc_EnvironmentError

EnvironmentError

PyExc_EOFError

EOFError

PyExc_Exception

Exception

PyExc_FloatingPointError

FloatingPointError

PyExc_ImportError

ImportError

PyExc_IndexError

IndexError

PyExc_IOError

IOError

PyExc_KeyError

KeyError

PyExc_KeyboardInterrupt

KeyboardInterrupt

PyExc_LookupError

LookupError

PyExc_MemoryError

MemoryError

PyExc_NameError

NameError

PyExc_NotImplementedError

NotImplementedError

PyExc_OSError

OSError

PyExc_OverflowError

OverflowError

PyExc_ReferenceError

ReferenceError

PyExc_RuntimeError

RuntimeError

PyExc_StandardError

StandardError

PyExc_StopIteration

StopIteration

PyExc_SyntaxError

SyntaxError

PyExc_SystemError

SystemError

PyExc_SystemExit

SystemExit

PyExc_TypeError

TypeError

PyExc_UnicodeError

UnicodeError

PyExc_UnicodeEncodeError

UnicodeEncodeError

PyExc_UnicodeDecodeError

UnicodeDecodeError

PyExc_UnicodeTranslateError

UnicodeTranslateError

752

26. Python

()
C

Python

PyExc_ValueError

ValueError

PyExc_WindowsError

WindowsError

PyExc_ZeroDivisionError

ZeroDivisionError

,
:
void PyErr_Clear()
.
PyObject *PyErr_Occurred()
, . , .
NULL.
int PyErr_ExceptionMatches(PyObject *exc)
, exc.
, 1, 0.
, Python. exc .
try-except C:
/* Python */
if (PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_ValueError)) {
/* */
...
PyErr_Clear();
return result; /* PyObject * */
} else {
return NULL; /* */
}
}


, Python,, C Python. ,
PyObject *.

Py_INCREF(obj)

, obj. .

753

Py_DECREF(obj)

,
obj. .

Py_XINCREF(obj)

, obj. .

Py_XDECREF(obj)

,
obj. .

Python C , Extending
Extending and Embedding the Python Interpreter
( Python), http://docs.python.org/extending, -. , C
,
:

Python C, .

, ,
.

Python (,
) C . , , , .

(
) ( ).


. ,
, ,
. ,
. , ,
:
Py_BEGIN_ALLOW_THREADS
. C Python C API
.

754

26. Python

Py_END_ALLOW_THREADS
. , .
:
PyObject *py_wrapper(PyObject *self, PyObject *args) {
...
PyArg_ParseTuple(args, ...)
Py_BEGIN_ALLOW_THREADS
result = run_long_calculation(args);
Py_END_ALLOW_THREADS
...
return Py_BuildValue(fmt,result);
}

Python
Python C. Python , ,
C , , , , Python.


C. C,
,
:
#include <Python.h>
int main(int argc, char **argv) {
Py_Initialize();
PyRun_SimpleString(print(Hello World));
Py_Finalize();
return 0;
}

, .
, .


C,, , UNIX,
Python.h ,
libpython2.6.a. /usr/
local/include/python2.6, /usr/local/lib/python2.6/

Python

755

config. Windows Python.. , , . , , Python, ,


, .


, :
int PyRun_AnyFile(FILE *fp, char *filename)
fp ,
UNIX, PyRun_InteractiveLoop(). PyRun_SimpleFile(). filename
.. . filename NULL,
???.
int PyRun_SimpleFile(FILE *fp, char *filename)
PyRun_SimpleString(), , Python fp.
int PyRun_SimpleString(char *command)
command __main__ .
0 -1 .
int PyRun_InteractiveOne(FILE *fp, char *filename)
.
int PyRun_InterativeLoop(FILE *fp, char *filename)
.
void Py_Initialize()
Python.
C API ,, Py_SetProgramName(), PyEval_InitThreads(), PyEval_ReleaseLock()
PyEval_AcquireLock().
int Py_IsInitialized()
1, , 0
.
void Py_Finalize()
,
, Py_Initialize().
, .
, .

756

26. Python

void Py_SetProgramName(char *name)


,
argv[0] sys.
Py_Initialize().
char *Py_GetPrefix()
. , sys.prefix.
char *Py_GetExecPrefix()
.
, sys.exec_prefix.
char *Py_GetProgramFullPath()
Python.
char *Py_GetPath()
. , -,
(: UNIX, ; DOS/Windows).
int PySys_SetArgv(int argc, char **argv)
sys.argv. Py_
Initialize().

Python
C
C,, :

Python ( import).

, .

Python, .

( )

C API Python:
PyObject *PyImport_ImportModule(const char *modname)
modname .
PyObject *PyObject_GetAttrString(PyObject *obj, const char *name)
. obj.name Python.

Python

757

int PyObject_SetAttrString(PyObject *obj, const char *name, PyObject *value)


. obj.name = value Python.
PyObject *PyEval_CallObject(PyObject *func, PyObject *args)
func args. func
Python (, , ). args .
PyObject *PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
func args kwargs. func , args
kwargs .
re C.
c, stdin ,
.
#include Python.h
int main(int argc, char **argv) {
PyObject *re;
PyObject *re_compile;
PyObject *pat;
PyObject *pat_search;
PyObject *args;
char buffer[256];

if (argc != 2) {
fprintf(stderr, : %s \n,argv[0]);
exit(1);
}

Py_Initialize();

/* import re */
re = PyImport_ImportModule(re);

/* pat = re.compile(pat,flags) */
re_compile = PyObject_GetAttrString(re,compile);
args = Py_BuildValue((s), argv[1]);
pat = PyEval_CallObject(re_compile, args);
Py_DECREF(args);

/* pat_search = pat.search */
pat_search = PyObject_GetAttrString(pat,search);

/* */
while (fgets(buffer,255,stdin)) {
PyObject *match;
args = Py_BuildValue((s), buffer);

758

26. Python
/* match = pat.search(buffer) */
match = PyEval_CallObject(pat_search,args);
Py_DECREF(args);
if (match != Py_None) {
printf(%s,buffer);
}
Py_XDECREF(match);
}
Py_DECREF(pat);
Py_DECREF(re_compile);
Py_DECREF(re);
Py_Finalize();
return 0;
}

Python
. ,
,
C C .

Python
C

Python C. ,
,
. ,
, PyArg_ParseTuple(), . , ,
Python C, , ,
Python :
Python C
long

PyInt_AsLong(PyObject *)

long

PyLong_AsLong(PyObject *)

double PyFloat_AsDouble(PyObject *)
char

*PyString_AsString(PyObject *) ( Python 2)

char

*PyBytes_AsString(PyObject *) ( Python 3)

, C API (http://docs.python.org/c-api).

ctypes

759

ctypes
ctypes DLL , C,
Python.. , (, , ), ctypes
, C,, -
C. ctypes
, . , , .


, , C,, , :
CDLL(name [, mode [, handle [, use_errno [, use_last_error]]]])
, C.
name , libc.so.6 msvcrt.dll. mode , ,
dlopen()
UNIX. ,
RTLD_LOCAL, RTLD_GLOBAL RTLD_DEFAULT ( ), . Windows mode .. handle (
). None. use_er_ererrno ,
errno C . , errno

. use_
errno False. use_last_error , get_last_
error() set_last_error() .
Windows. use_
last_error False.
WinDLL(name [, mode [, handle [, use_errno [, use_last_error]]]])
, CDLL(), , ,
Windows stdcall (Windows).
, , name .
ctypes.util:

760

26. Python

find_library(name)
ctypes.util.
name. name , libc, libm . , ,
, /usr/lib/libc.so.6.
,
(, LD_LIBRARY_PATH
). None, .


,
CDLL(), -, C. ,
. :
>>> import ctypes
>>> libc = ctypes.CDLL(/usr/lib/libc.dylib)
>>> libc.rand()
16807
>>> libc.atoi(12345)
12345
>>>

libc.rand() libc.atoi(),
C.
ctypes ,
int char * int. , ,
,
C , .
:
>>> libc.atof(34.5)
-1073746168
>>>

,
func
:
func.argtypes
, ctypes (
) func.
func.restype
, ctypes,
func. , void,
None.

761

ctypes

func.errcheck
Python, (result,
func, args), result , ,
func args .
.
atof(),
:
>>> libc.atof.restype=ctypes.c_double
>>> libc.atof(34.5)
34.5
>>>

ctypes.d_double . .


. 26.5 , ctypes, argtypes restype
. Python
Python, .
26.5. ctypes

ctypes

Python

c_bool

bool

True False

c_bytes

signed char

c_char

char

c_char_p

char *

, NULL

c_double

double

c_longdouble

long double

c_float

float

c_int

int

c_int8

signed char

8-

c_int16

short

16-

c_int32

int

32-

c_int64

long long

64-

c_long

long

c_longlong

long long

c_short

short

762

26. Python

26.5 ()

ctypes

Python

c_size_t

size_t

c_ubyte

unsigned char

c_uint

unsigned int

c_uint8

unsigned char

8-

c_uint16

unsigned short

16-

c_uint32

unsigned int

32-

c_uint64

unsigned long long

64-

c_ulong

unsigned long

c_ulonglong

unsigned long long

c_ushort

unsigned short

c_void_p

void *

c_wchar

wchar_t

c_wchar_p

wchar_t *

, NULL

, C,, :
POINTER(type)
, type. , POINTER(c_int) C int *.
, ,
. , c_int*4 int[4] C.
, C,, , Structure Union.
_fields_, . _fields_ 2 3 (name, ctype)
(name, ctype, width), name ,
ctype , , width ,
. ,
C:
struct Point {
double x, y;
};

ctypes :

763

ctypes
class Point(Structure):
_fields_ = [ (x, c_double),
(y, c_double) ]


,
, . , c_int, c_double
,
Python ( , ). c_int, c_double .
Python .
,
, ,
, :
byref(cvalue [, offset])
cvalue. cvalue
, ctypes.
offset ,
. , , .
pointer(cvalue)
, cvalue.
cvalue ,
ctypes. POINTER, .
, , C
double *:
dval = c_double(0.0)
r = foo(byref(dval))

# double
# foo(&dval)

p_dval = pointer(dval) # -
r = foo(p_dval)
# foo(p_dval)

# dval
print (dval.value)

, , int float.
,
C .
cobj.value cobj,
ctypes, . ,
dval.value , dval c_double.

764

26. Python

C ,
. StructureType:
StructureType(*args, **kwargs)
StructureType, StructureType
, Structure Union.
*args ;
, _fields_.
**kwargs
.


ctypes, c_int, POINTER
, ,
ctypes , .
ty.from_buffer(source [,offset])
ty, ,
source. source , , (,
bytearray, array, array, mmap
). offset .
ty.from_buffer_copy(source [, offset])
, ty.from_buffer(), , , source .
ty.from_address(address)
ty,
address, , .
ty.from_param(obj)
ty obj Python.. obj, . , Python
c_int.
ty.in_dll(library, name)
ty . library ,
, CDLL. name
.. , .
, int status, libexample.so.

ctypes

765

libexample = ctypes.CDLL(libexample.so)
status = ctypes.c_int.in_dll(libexample,status)


,
ctypes:
addressof(cobj)
cobj . cobj
ctypes.
alignment(ctype_or_obj)
, ctypes, . ctype_or_obj
ctypes .
cast(cobj, ctype)
cobj , ctypes,
ctype. , cobj
, ctype .
create_string_buffer(init [, size])
, , c_char. init , , ,
. size ,
, init .
size , init.
.
create_unicode_buffer(init [, size])
, create_string_buffer(), ,
c_wchar.
get_errno()
, ctypes, errno.
get_last_error()
, ctypes, LastError Windows.
memmove(dst, src, count)
count src dst. src dst , , ctypes, . , memmove() C.

766

26. Python

memset(dst, c, count)
count c ,
dst. dst
, ctypes. c
0-255, .
resize(cobj, size)
,
cobj ctypes. size .
set_conversion_mode(encoding, errors)
,
8- . encoding , utf-8, errors , : strict ignore.
(encoding, errors) .
set_errno(value)
, ctypes, errno. .
set_last_error(value)
, ctypes, LastError Windows .
sizeof(type_or_cobj)
, ctypes, .
string_at(address [, size])
, size , address. size , , NULL.
wstring_at(address [, size])
, size , address. size , , NULL.

ctypes C,
,
Python .
# example.py

import ctypes
_example = ctypes.CDLL(./libexample.so)

ctypes

767

# int gcd(int, int)


gcd = _example.gcd
gcd.argtypes = (ctypes.c_int,
ctypes.c_int)
gcd.restype = ctypes.c_int

# int replace(char *s, char olcdh, char newch)


_example.replace.argtypes = (ctypes.c_char_p,
ctypes.c_char,
ctypes.c_char)
_example.replace.restype = ctypes.c_int

def replace(s, oldch, newch):


sbuffer = ctypes.create_string_buffer(s)
nrep = _example.replace(sbuffer,oldch,newch)
return (nrep,sbuffer.value)

# double distance(Point *p1, Point *p2)


class Point(ctypes.Structure):
_fields_ = [ (x, ctypes.c_double),
(y, ctypes.c_double) ]

_example.distance.argtypes = (ctypes.POINTER(Point),
ctypes.POINTER(Point))
_example.distance.restype = ctypes.c_double

def distance(a,b):
p1 = Point(*a)
p2 = Point(*b)
return _example.distance(byref(p1),byref(p2))

ctypes
. , C .
,
C. ,
, replace(), , ,
C .
distance()
, Point
.

ctypes ,
. , Windows
, . , .

768

26. Python



ctypes
,
C .
.
. , ,
.
http://wiki.python.org/moin/Integr
atingPythonWithOtherLanguages. SWIG (http://www.swig.org), . ,
SWIG .
, . , SWIG , , :
/* example.i : Swig */
%module example
%{
/* . */
#include example.h
%}
/* . C */
typedef struct Point {
double x;
double y;
} Point;
extern int
gcd(int, int);
extern int
replace(char *s, char oldch, char newch);
extern double distance(Point *a, Point *b);

, SWIG ,
Python.. SWIG, :
% swig python example.i
%

.c .py.
. distutils,
.i setup.py;
SWIG . , setup.py, ,
example.i, SWIG.
# setup.py
from distutils.core import setup, Extension

Jython IronPython

769

setup(name=example,
version=1.0,
py_modules = [example.py],
ext_modules = [
Extension(_example,
[example.i,example.c])
]
)

, example.i setup.py , . python setup.py build_ext --inplace, .

Jython IronPython
C. , Java,
Jython (http://www.jython.org) Python Java. jython Java import. :
bash-3.2$ jython
Jython 2.2.1 on java1.5.0_16
Type copyright, credits or license for more information.
>>> from java.lang import System
>>> System.out.println(Hello World)
Hello World
>>>

, .NET
NET Windows,, IronPython (http://www.codeplex.com/Wiki/View.
aspx?ProjectName=IronPython) Python C#. IronPython
.NET Python.
:
% ipy
IronPython 1.1.2 (1.1.2) on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import System.Math
>>> dir(System.Math)
[Abs, Acos, Asin, Atan, Atan2, BigMul, Ceiling, Cos, Cosh,
...]
>>> System.Math.Cos(3)
-0.9899924966
>>>

Jython IronPython
. , Python,
.

Python 3
2008 Python 3.0. Python
, Python 2 .
Python 3 Whats
Whats
ss New in Python 3.0 ( Python 3.0), http://docs.python.
org/3.0/whatsnew/3.0.html1. , 26
,
Whats New. ,
Python 2 Python 3, , . , print() , Python 3
.
,
Python,, 3, ,
.
2to3, .

Python 3?
, : Python 3.0?. Python
, Python 3
Python 2 ( ).

, Python 2. , -

( )
http://www.ibm.com/developerworks/
ru/library/l-python3-1/. . .

771

, ,
Python 2.
, Python 3.0,
. Python
. Python 3. ,
, . (2009 )
Python,
Python 2.4,
2.6 3.0. , , Python 2. 2012 , ,
.
Python 3 , , Python 3
, . , , Python 2 ,
Python 3. ,
, . Python 3;

, .
, Python 3.0 ,
, , . , - .
. - -
. , , , . , ,
Python 3.0 Python.
, Python 2,
Python 3 .


Python 3,
Python 2.

772

A. Python 3


Python 3 , UTF-8. ,
, . ,

U+0080 . :
= 3.141592654
r = 4.0
print(2**r)


, .
,
. ,
, , ( , APL).
).
.


, ,
{ }. :
days = { Mon, Tue, Wed, Thu, Fri, Sat, Sun }

set():
days = set([Mon, Tue, Wed, Thu, Fri, Sat, Sun])


{ expr for x in s if condition } . expr s . :
>>> values = { 1, 2, 3, 4 }
>>> squares = {x*x for x in values}
>>> squares
{16, 1, 4, 9}
>>>

{ kexpr:vexpr for k,v in s if condition }


.. s (key, value) .
kexpr, vexpr. , dict().
, , prices.dat :

773


GOOG 509.71
YHOO 28.34
IBM 106.11
MSFT 30.47
AAPL 122.13

, , , :
fields = (line.split() for line in open(prices.dat))
prices = {sym:float(val) for sym,val in fields}

:
d = {sym.lower():price for sym,price in prices.items()}

, , $100.00:
d = {sym:price for sym,price in prices.items() if price >= 100.0}



Python 2 , :
items = [1,2,3,4]
a,b,c,d = items #

,
.
Python 3, , ,
. :
a,*rest = items
a,*rest,d = items
*rest, d = items

# a = 1, rest = [2,3,4]
# a = 1, rest = [2,3], d = 4
# rest = [1,2,3], d = 4

, *, . , .
( ), . :
points = [ (1,2), (3,4,red), (4,5,blue), (6,7) ]
for x,y, *opt in points:
if opt:
#

774

A. Python 3


, nonlocal. :
def countdown(n):
def decrement():
nonlocal n
n -= 1
while n > 0:
print(T-minus, n)
decrement()

Python 2 , , . nonlocal .



. :
def foo(x:1,y:2) -> 3:
pass

__annotations__, , .
return . :
>>> foo.__annotations__
{y: 4, x: 3, return: 5}
>>>

. ,
. , , . ,
:
def foo(x:int, y:int) -> str:

. ,
Python. ,
, . :
def bar(x, *args:additional, **kwargs:options):

, Python . ,
, -

775

.
, , , , , , ,
. -, :
def ensure(func):
#
return_check = func.__annotations__.get(return,None)
arg_checks = [(name,func.__annotations__.get(name))
for name in func.__code__.co_varnames]

# ,
# ,
def assert_call(*args,**kwargs):
for (name,check),value in zip(arg_checks,args):
if check: assert check(value), %s %s % (name, check.__doc__)
for name,check in arg_checks[len(args):]:
if check: assert check(kwargs[name]), %s %s % (name,
check.__doc__)
result = func(*args,**kwargs)
assert return_check(result), return %s % return_check.__doc__
return result

return assert_call

:
def positive(x):
must be positive
return x > 0

def negative(x):
must be negative
return x < 0

@ensure
def foo(a:positive, b:negative) -> positive:
return a b

:
>>> foo(3,-2)
5
>>> foo(-5,2)
Traceback (most recent call last):
File <stdin>, line 1, in <module>
File meta.py, line 19, in call
def assert_call(*args,**kwargs):
AssertionError: a must be positive
(:
( ):
<stdin>, 1, <module>
meta.py, 19,
def assert_call(*args,**kwargs):

776

A. Python 3
AssertionError: a
)
>>>


, .
. :
def foo(x, *args, strict=False):

strict
. :
a = foo(1, strict=True)
args
strict. , , ,
, , * . :
def foo(x, *, strict=False):

:
foo(1,True)

# . TypeError: foo() takes 1 positional argument


#
TypeError: foo() 1
foo(1,strict=True) # Ok.

Ellipsis
Ellipsis object (...)
. . :
>>> x = ...
# Ellipsis
>>> x
Ellipsis
>>> a = [1,2,...]
>>> a
[1, 2, Ellipsis]
>>> ... in a
True
>>> x is ...
True
>>>

. (...) -

777

(, ,
).


.
. raise from. :
try:

except ValueError as e:
raise SyntaxError(Couldnt parse configuration) from e

SyntaxError
, :
Traceback (most recent call last):
File <stdin>, line 2, in <module>
ValueError: invalid literal for int() with base 10: nine

The above exception was the direct cause of the following exception:

Traceback (most recent call last):


File <stdin>, line 4, in <module>
SyntaxError: Couldnt parse configuration
(:
( ):
<stdin>, 2, <module>
ValueError: int() 10: nine

, , :

( ):
<stdin>, 4, <module>
SyntaxError:
)

__cause__, . from raise .



. :
def error(msg):
print(m) # : ( m )

try:

except ValueError as e:
error(Couldnt parse configuration)

Python 2,
NameError,

778

A. Python 3

error(). Python 3 . , :
Traceback (most recent call last):
File <stdin>, line 2, in <module>
ValueError: invalid literal for int() with base 10: nine

During handling of the above exception, another exception occurred:

Traceback (most recent call last):


File <stdin>, line 4, in <module>
File <stdin>, line 2, in error
NameError: global name m is not defined
(:
( ):
<stdin>, 2, <module>
ValueError: int() 10: nine

, , :

( ):
<stdin>, 4, <module>
<stdin>, 2, error
NameError: m
)

__context__ .

super()
super() . Python 3
. :
class C(A,B):
def bar(self):
return super().bar() # bar()

Python 2 super(C,self).bar().
- ,
.


Python 2 , . , ,
. , . , ( ,
).
Python 3
, . __prepre-

779

pare__(cls, name, bases, **kwargs). .


. ,
:
class MyMeta(type):
@classmethod
def __prepare__(cls,name,bases,**kwargs):
print(,name,bases,kwargs)
return {}
def __new__(cls,name,bases,classdict):
print(,name,bases,classdict)
return type.__new__(cls,name,bases,classdict)

Python 3 . , , MyMeta:
class Foo(metaclass=MyMeta):
print( )
def __init__(self):
pass
def bar(self):
pass
print( )

, , :
Foo () {}


Foo () {__module__: __main__,
bar: <function bar at 0x3845d0>,
__init__: <function __init__ at 0x384588>}

__prepare__() , class. , class Foo(metaclass=


MyMeta,spam=42,blah=Hello) MyMeta.__prepare__() spam blah. .
__prepare__() , , . ,
,
, ,
dict, __setitem__(), . , ,
, -
.

780

A. Python 3
class MultipleDef(dict):
def __init__(self):
self.multiple= set()
def __setitem__(self,name,value):
if name in self:
self.multiple.add(name)
dict.__setitem__(self,name,value)

class MultiMeta(type):
@classmethod
def __prepare__(cls,name,bases,**kwargs):
return MultipleDef()
def __new__(cls,name,bases,classdict):
for name in classdict.multiple:
print(name, )
if classdict.multiple:
raise TypeError( )
return type.__new__(cls,name,bases,classdict)

,

. :
class Foo(metaclass=MultiMeta):
def __init__(self):
pass
def __init__(self,x): # . __init__ .
pass


Python 2 Python 3,
, Python 3 . , , . Python 3 ,
, Python 2. , Python 2, .
, , Python 2.


Python 3
() (). ,
hello, ,
, bhello, ( , ASCII).

781

Python 3 str bytes. , , TypeError. Python 2,


.
s , s.encode(encoding). , s.encode(utf-8) s UTF-8. t
, t.decode(encoding).
encode() decode() .

Python 2 .
, Python 3
. ,
split() replace(),
, Python 2. , print(), repr()
b. (%, .format()) . :
x = bHello World
print(x)
# bHello World
print(bYou said %s % x) # TypeError: % operator not supported
# TypeError: %

, , .
, , , ASCII. ,
, bytes.
. , :
>>> # , ()
>>> status = 200
>>> msg = OK
>>> proto = HTTP/1.0
>>> response = %s %d %s % (proto, status, msg)
>>> print(response)
HTTP/1.0 200 OK

>>> # , (ASCII)
>>> status = 200
>>> msg = bOK
>>> proto = bHTTP/1.0
>>> response = b%s %d %s % (proto, status, msg)
Traceback (most recent call last):
File <stdin>, line 1, in <module>

782

A. Python 3
TypeError: unsupported operand type(s) for %: bytes and tuple
(:
( ):
<stdin>, 1, <module>
TypeError: %: bytes tuple
)

>>> response = proto + b + str(status) + b + msg


Traceback (most recent call last):
File <stdin>, line 1, in <module>
TypeError: cant concat bytes to str
(:
( ):
<stdin>, 1, <module>
TypeError: bytes str
)

>>> bytes(status)
b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00....

>>> bytes(str(status))
Traceback (most recent call last):
File <stdin>, line 1, in <module>
TypeError: string argument without an encoding
(:
( ):
<stdin>, 1, <module>
TypeError:
)

>>> bytes(str(status),ascii)
b200

>>> response = proto + b + bytes(str(status),ascii) + b + msg


>>> print(response)
bHTTP/1.0 200 OK

>>> print(response.decode(ascii))
HTTP/1.0 200 OK
>>>

, Python 3
. , ,
ASCII,
.
, - ,
. ,
s.encode(latin-1).

.

783

,
, .
. , os.listdir(dirname) ,
, dirname . dirname ,
.

-
Python 3 -,
19 , io. -
,
.
- - , Python 3
, (
UTF-8) - .
- , .

-, . :
>>> f = open(foo.txt,wb)
>>> f.write(Hello World\n)
Traceback (most recent call last):
File <stdin>, line 1, in <module>
File /tmp/lib/python3.0/io.py, line 1035, in write
raise TypeError(cant write str to binary stream)
TypeError: cant write str to binary stream
(:
( ):
<stdin>, 1, <module>
/tmp/lib/python3.0/io.py, 1035, write
raise TypeError( str )
TypeError: str
)
>>>

, , . , , ,
( HTTP, SMTP, FTP )
/ . , , -
, , . .

784

A. Python 3

print() exec()
print exec Python 2 .
print(), print Python 2:
print(x,y,z)
# , : print x, y, z
print(x,y,z,end= ) # , : print x, y, z,
print(a,file=f)
# , : print >>f, a

, print print(), ,
.
exec exec(),
Python 3 Python 2.
:
def foo():
exec(a = 42)
print(a)

Python 2 foo() 42. Python 3


NameError , a . , exec(),
, , globals() locals(). , locals(), .
, exec(),
, .
:
def foo():
_locals = locals()
exec(a = 42,globals(),_locals)
a = _locals[a]
#
print(a)

, , Python 3
, exec(), eval()
execfile() Python 2. , execfile()
( , exec()).


Python 3 , Python 2. , zip(), map() range(), , .
, , list().
Python 3
. Python 2, , -

785

/, ,
d.keys(), d.values() d.items() . Python 3 . :
>>> s = { GOOG: 490.10, AAPL: 123.45, IBM: 91.10 }
>>> k = s.keys()
>>> k
<dict_keys object at 0x33d950>
>>> v = s.values()
>>> v
<dict_values object at 0x33d960>
>>>

, for. :
>>> for x in k:
...
print(x)
...
GOOG
AAPL
IBM
>>>

,
.
, ,
. :
>>> s[ACME] = 5612.25
>>> for x in k:
...
print(x)
...
GOOG
AAPL
IBM
ACME
>>>

,
list(), list(s.keys()).


Python 3 int, 32- , , .
int (
).
,
. , 3/5 0.6, 0. ,
. ,
8/2 4.0, 4.

786

A. Python 3

Python 3 .
Python 2 ,
. :
>>> 3 < Hello
True
>>>

Python 3 TypeError. :
>>> 3 < Hello
Traceback (most recent call last):
File <stdin>, line 1, in <module>
TypeError: unorderable types: int() < str()
(:
( ):
<stdin>, 1, <module>
TypeError: : int() < str()
)
>>>

, , Python 3 .
, sort() , <,
. Python 2 , .


Python 3 . , , __iter__()
next(). next()
__next__().
, , , ,
. next() .
next(),
next() __next__().

,
Python 3 ,
sys.argv os.environ
, .
, .
, ,
, .

787

,
Python . , , . ,
os.listdir(b/foo).


Python 3 . , , . ,
(, gopherlib,
rfc822 ).
. , ConfigParser,
Queue SocketServer, configparser, queue socketserver
.
.
, , . ,
http, , HTTP; , HTML,
html; , XML-RPC,
xmlrpc, .
, ,
Python 2.6 Python 3.0. ,
Python 2, , , -
. , Python 3 popen2,
Python 2 . subprocess.


import, , . 8 ,
, , , ,
:
foo/
__init__.py
spam.py
bar.py

spam.py import bar, ImportError, , bar.py


. , spam.py

788

A. Python 3

import foo.bar , from . import bar.


Python 2 import
, , sys.path.

2to3
Python 2
Python 3 .
, , , , ,
Python 3 ,
Python 2. ,
,
.

Python 2.6
Python 3 Python 2.6. , Python 2.6
Python 2.5,
, Python 3.
, , , - .
, Python 2, Python 3,
Python 3.
Python 2.6 ,
-3 Python 2.6 , . :
bash-3.2$ python -3
Python 2.6 (trunk:66714:66715M, Oct 1 2008, 18:36:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type help, copyright, credits or license for more information.
>>> a = { }
>>> a.has_key(foo)
__main__:1: DeprecationWarning: dict.has_key() not supported in 3.x; use the in
operator
(:
__main__:1: DeprecationWarning: dict.has_key() 3.x;
in
)
False
>>

789

2to3

,
Python 2.6,
Python 3.


Python , , doctest unittest. ,
, Python 3.
, .
,
Python 2.6
.

2to3
Python 3 2to3, Python 2.6 Python 3.
Tools/scripts, , , python3.0. ,
UNIX Windows.
,
.
# example.py
import ConfigParser

for i in xrange(10):
print i, 2*i

def spam(d):
if not d.has_key(spam):
d[spam] = load_spam()
return d[spam]

2to3, 2to3
example.py. :
% 2to3 example.py
RefactoringTool: Skipping implicit
RefactoringTool: Skipping implicit
RefactoringTool: Skipping implicit
RefactoringTool: Skipping implicit
--- example.py (original)
+++ example.py (refactored)
@@ -1,10 +1,10 @@
# example.py
-import ConfigParser
+import configparser

fixer:
fixer:
fixer:
fixer:

buffer
idioms
set_literal
ws_comma

790

A. Python 3
-for
+for
+

i in xrange(10):
print i, 2*i
i in range(10):
print(i, 2*i)

def spam(d):
if not d.has_key(spam):
if spam not in d:
d[spam] = load_spam()
return d[spam]
RefactoringTool: Files that need to be modified:
RefactoringTool: example.py
+

, 2to3 , , ,
. diff.
2to3 ,
,
Python, ,
.
2to3 , ,
. ,
2to3, . , spam().
d.has_key(). has_key()
in. 2to3 ,
,
spam() . , d
- (, ), has_key(), in
. , 2to3, . , Python 2
, ,
. , 2to3 .
,
. ,
.
2to3 . ,
2to3 -l. :
% 2to3 -l
Available transformations for the -f/--fix option:
( -f/--fix)
apply
basestring
buffer

2to3

791

callable
...
...
xrange
xreadlines
zip

, ,
, 2to3
-f _. ,
-f, .
, -w: 2to3
-f -w _. :
% 2to3 -f xrange -w example.py
--- example.py (original)
+++ example.py (refactored)
@@ -1,7 +1,7 @@
# example.py
import ConfigParser
-for i in xrange(10):
+for i in range(10):
print i, 2*i

def spam(d):
RefactoringTool: Files that were modified:
RefactoringTool: example.py

example.py ,
, xrange()
range() .
example.py example.
py.bak.
-f -x. 2to3 -x _,
, , -x.
2to3 , .
, , 2to3
.
, ,
.
2to3 ,
. -v , , . -p 2to3,
print ( from __future__ import print_statement).

792

A. Python 3


Python 2 Python 3. ,
.
1. ,
Python 2.
2. Python 2.6 , .
3. -3 Python 2.6.
,
. , , -
Python 2.5 , ,
. .
4. (
).
5. Python 3 ,
.
(
). , -
.
6. 2to3 Python 3.
. . ,
2to3 , , , . ,
2to3 (
print except, xrange(),
), .

, .
, Python 2.6, Python 3, - .

Python; ,
,
Python 2.6 .
2to3 (, -

2to3

793

), ,
,
2to3.

Python 2 Python 3
, Python 3, , Python 2
Python 3? ,
, . , print , except (
sys.exc_info()). Python
. , - ,
Python 2 Python 3.
, ,
Python 2 Python 3, , ,
Python 2.6,
2to3,
.
, Python, .
,
Python 2.6 Python 3, 2to3.


, Python
. Python 3 , .
http://bugs.python.org.
Python .



''' ( ), 30, 51
""" ( ), 30, 51
' (), 30, 51
" (), 30, 51
- (),
, 227
- (), ,
36
- , , 96
- , 96
() () , 33, 54
() , 110
_ () ,
50
_ (), , 24
, 230
_ _( ) , 50
. (),
, 159
. (), ,
58, 77, 110
... (), 55, 84
... , 230
; ( ), , 26, 49
: (), , 106
@ , 55, 139
#!
UNIX, 231
, 202
# , 25, 49
\\ , 48
\\
, 51, 52
% , 27,
103
% , 96
// , 96

*
from module import,
191
, 47
, 131
, 132
, 131
* , 96
* , 99
**
, , 132
** , 96
**= , 109
*= , 109
>>> , 23, 229
>> , 212
& , 97
& , 36,
109
&= , 110
| , 97
~ , 97
> ( ), ,
29, 98
< ( ), ,
29, 98
< ,
, 106
> ,
, 106
>>,
print, 29
<< , 97
>> , 97
<<= , 110
>>= , 110
-= , 109


//= , 109
/= , 109
%= , 109
+= , 109
|= , 110
>= , 98
<= , 98
!= , 98
== , 98, 113
| , 36,
109
/ , 96
+ , 99
, 32
, 31
+ , , 96
+ , 96
^ ,
97
^ , 36, 109
^ , , 106
^= , 110
{} , 106
{} , 36, 54
, 772
[] , 32, 54
[] , 65, 99
, 72
, 90
,
100
[:] , 31, 32, 65, 99
[::] , 65, 99
,
101
[!] , pdb, 243
0b, , 50
0o, , 50
0x, , 50
2to3, , 788
, 790
-3, , 226, 788

A
'a', , open(), 207
a2b_base64(), , binascii,
680
a2b_hex(), , binascii,
680

795
a2b_hqx(), , binascii,
680
a2b_uu(), , binascii, 680
ABCMeta, , 182, 326
abc, , 182, 326
abort(), FTP, 620
abort(), , os, 489
abspath(), , os.path, 496
__abstractmethods__,
types , 79
@abstractmethod, , 182, 326
@abstractproperty, , 182, 326
__abs__(), , 93
abs(), , 97, 259
operator, 346
accept2dyear, , time,
507
accept(),
dispatcher, 569
, 596
accept(), Listener,
542
access(), , os, 484
acosh(), , math, 319
acos(), , math, 319
acquire(),
Condition, 552
Lock, 548
RLock, 549
Semaphore, 549
activate(), SocketServer,
615
active_children(), ,
multiprocessing, 543
active_count(), ,
threading, 555
__add__(), , 92, 179
add(),
, 36, 74
TarFile, 403
add(), , operator, 346
add_data(),
Request, 642
addfile(), ,
TarFile, 403
addFilter(),
Handler, 456
Logger, 450
addHandler(),
Logger, 453
add_header(),
Message, 690
Request, 643

796
addLevelName(), ,
logging, 460
add_option(), , optparse,
206
add_option(),
PtionParser, 470
add_password(),
AuthHandler, 645
address_family,
SocketServer, 615
addressof(cobj), , ctypes,
765
address,
BaseManager, 539
Listener , 542
add_section(),
ConfigParser, 417
add_type(), , mimetypes,
704
add_unredirected_header(), Request, 643
adler32(), , zlib, 413
AF_*, , socket, 586
aifc, , 729
AJAX, , , 661
alarm(), , signal, 500
alias, , pdb,
243
alignment(), , ctypes,
765
__all__,
, 198
, 192
all(), , 66, 99, 259
allow_reuse_address,
SocketServer, 615
altsep, , os, 484
altzone, , time, 507
__and__(), , 92
and, , 113
and_(), , operator, 346
__annotations__, ,
anydbm, , 392
any(), , 66, 99, 259
api_version, , sys,
293
%APPDATA%,
Windows, 232
append(),
deque, 333
Element, 715
array, 329
, 32, 66


appendChild(),
Node, 709
appendleft(), , deque,
333
apply_async(),
Pool, 530
apply(), Pool, 530
args,
, 125
Exception, 274
args, , partial, 340
a(rgs) , , pdb,
243
argtypes,
ctypes, 760
argv, , sys, 33, 205,
228, 293
ArithmeticError, , 123, 273
Array(), Manager,
536
array, , 328
array(),
array, 329
multiprocessing, 533
arraysize, Cursor,
378
as,
from-import, 191
import, 46, 190
except, 45
as,
except, 121
with, 127
ASCII,
UTF-8, 221
, 220
ascii(), , 259
Python 3, 260
ascii_letters, , string,
362
ascii_lowercase, ,
string, 362
ascii_uppercase, ,
string, 362
asctime(), , time, 508
asin(), , math, 319
asinh(), , math, 319
as_integer_ratio(), , 64
assert, , 128, 275
-O, 195
assert_(),
TestCase, 241
assertAlmostEqual(),


TestCase, 241
assertEqual(),
TestCase, 241
AssertionError, , 123, 128,
275
assertNotAlmostEqual(),
TestCase, 241
assertNotEqual(),
TestCase, 241
assertRaises(),
TestCase, 241
as_string(),
Message, 690
astimezone(),
datetime, 426
async_chat, , asyncchat, 565
asynchat, , 564
, 582
asyncore, , 519, 564, 568
, 582
AsyncResult, ,
multiprocessing, 531
atan2(), , math, 319
atanh(), , math, 319
atan(), , math, 319
atexit, , 234
atexit, , 280
attach(), Message,
690
attrgetter(), , operator,
348
attrib, Element,
715
AttributeError, , 123, 275
, 177
attributes,
Node, 708
audioop, , 729
authkey,
Process, 521
awk, UNIX, , 152

B
-b, , 226
b, , 54
b2a_base64(), , binascii,
680
b2a_hex(), , binascii,
680
b2a_hqx(), , binascii,
681
b2a_uu(), , binascii, 680

797
b16decode(), , base64,
679
b16encode(), , base64,
679
b32decode(), , base64,
679
b32encode(), , base64,
679
b64decode(), , base64,
678
b64encode(), , base64,
678
'backslashreplace',
, 217
BadStatusLine, ,
http.client, 628
base64, , 677
base64, , , 677
BaseCGIHandler(), ,
wsgiref.handlers, 674
BaseException, , 123, 273
BaseHTTPRequestHandler, , http.server, 632
BaseHTTPServer, , 630
BaseManager, ,
multiprocessing, 538
BaseManager(), ,
multiprocessing, 538
basename(), , os.path,
496, 499
BaseProxy, ,
multiprocessing, 541
BaseRequestHandler, ,
SocketServer, 611
__bases__,
, 176
types, 79
basestring, , 260
basicConfig(), , logging,
446
BasicContext, ,
decimal, 315
.bat, , Windows, 231
bdb, , 725
Beautiful Soup, , 699
betavariate(), , random,
324
bin(), , 112, 260
Binary(),
,
381
xmlrpc.client, 654

798
binascii, , 680
bind(),
SocketServer, 616
dispatcher, 569
, 596
binhex, , 728
bisect_left(), , bisect,
332
bisect_right(), , bisect,
332
bisect, , 331
bisect(), , bisect, 332
block_size, , 694
Bluetooth, , 586
, 589
BOM_*, , codecs, 352
__bool__(), , 87, 90
bool, , 63
bool(), , 260
boolean(), , xmlrpc.
client, 654
BoundedSemaphore,
multiprocessing, 534
threading, 549
BoundedSemaphore() ,
Manager, 536
BoundedSemaphore (), ,
threading, 549
break, , 119
, 142
b(reak), , pdb,
244
BSD, kqueue, 574
BTPROTO_*, , socket,
596
BufferedIOBase,
, 444
BufferedIOBase, , io, 440
BufferedRandom, , io, 441
BufferedReader, , io, 440
BufferedRWPair, , io, 441
BufferedWriter, , io, 441
buffer_info(), ,
array, 329
bufsize, , open(), 208
build_opener(), , urllib.
request, 644
__builtin__, , 259
BuiltinFunctionType, , 302
BuiltinFunctionType, , 75, 78
builtin_module_names, , sys, 293


builtins, , Python 3, 259
byref(), , ctypes, 763
bytearray(), , 260, 261
byteorder, , sys, 293
bytes, , Python 3, 54
bytes(), , 261
BytesIO, , io, 442
byteswap(), , array,
329
bz2, , 395
BZ2Compressor(), , bz2,
396
BZ2Decompressor(), ,
bz2, 396
BZ2File(), , bz2, 395

C
C++, ,
, 160
c_*, ctypes, 761
C#, , 769
-, , 227
C3-, , 164
CacheFTPHandler, , urllib.
request, 644
calcsize(), , struct, 366
calendar, , 729
__call__(), , 94
, 78
, 78
call(), , subprocess, 505
Callable, ,
337
_callmethod(),
BaseProxy, 541
callproc(), Cursor,
376
cancel(),
Timer, 548
cancel_join_thread(),
Queue, 522
CannotSendHeader, , http.
client, 628
CannotSendRequest, , http.
client, 628
capitalize(), , 68
capitals, ,
Context, 314
capwords(), , string, 365
cast(), , ctypes, 765
category(), ,
unicodedata, 222, 370


__cause__,
Exception, 274, 777
CDLL(), , ctypes, 759
ceil(), , math, 319
center(), , 69
cert_time_to_seconds(), ,
ssl, 610
cgi, , 662
CGI-, 662
WSGI-, 674
, 669
, 669
, 670
, 663
, 667
CGIHandler(), , wsgiref.
handlers, 674
CGIHTTPRequestHandler, ,
http.server, 631
CGIHTTPRequestHandler(), ,
http.server, 631
CGIHTTPServer, , 630
cgitb, , 670
CGIXMLRPCRequestHandler, , xmlrpc.server, 657
CGIXMLRPCRequestHandler(), , xmlrpc.server, 657
chain(), , itertools, 342
characters(),
ContentHandler, 721
chdir(), , os, 475
check_call(), ,
subprocess, 505
check_unused_args(),
Formatter, 364
chflags(), , os, 484
childNodes,
Node, 708
chmod(), , os, 485
choice(), , random, 323
chown(), , os, 485
chr(), , 112, 261
chroot(), , os, 475
chunk, , 729
cipher(),
SSLSocket, 609
__class__,
, 176
, 78
, 79
class, , 43, 158
, 185
, 43, 161
classmethod, , 76, 166, 261

799
classmethod(), , 261
ClassType, , ,
186
cleandoc(), , inspect, 284
cl(ear), , pdb,
244
clear(),
, 74
deque, 333
Element, 715
Event, 550
, 72
clear_flags(), ,
Context, 314
clear_memo(), , Pickler, 291
_clear_type_cache(), ,
sys, 297
Client(), ,
multiprocessing, 542
client_address,
BaseRequestHandler,
612
client_address,
BaseHTTPRequestHandler, 633
clock(), , time, 248, 508
cloneNode(),
Node, 709
close(),
, 234
, 42, 83, 142, 143
urlopen, 641
Connection, 527
dispatcher, 569
FTP, 620
Handler, 457
HTMLParser, 696
HTTPConnection,
626
IOBase, 437
Listener, 542
mmap, 465
Pool, 530
Queue, 522
TarFile, 403
TreeBuilder, 716
ZipFile, 410
, 596
Connection, 376
Cursor, 377
, 208
close(),
os, 478

800
closed,
IOBase, 437
, 210
closefd, , open(), 208
closefd, FileIO,
439
CloseKey(), , winreg, 511
closerange(), , os, 478
close_when_done(), async_chat, 565
closing(), , contextlib,
339
__closure__, , 76, 138
cmath, , 319
cmd, , 729
cmp(), , 261
filecmp, 396
cmpfiles(), filecmp, 397
co_*,
, 81
__coerce__(), , , 180
__code__, , 76
code, , 725
CodecInfo, , codecs, 349
codecs, , 218, 349, 642
coded_value,
Morsel, 637
codeop, , 725
CodeType, , 80, 302
coding:, , 56
collect(), , gc, 282
collect_incoming_data(),
async_chat, 565
collection, , 252
collections, , 184, 332
colorsys, , 729
combinations(), ,
itertools, 343
combine(),
datetime, 425
combining(), ,
unicodedata, 371
command,
BaseHTTPRequestHandler, 633
commands, ,
pdb, 244
commands, , 416
comment, ,
ZipInfo, 412
Comment(), , xml.etree.
ElementTree, 714
commit(),
Connection, 376


common, ,
dircmp, 397
common_dirs, ,
dircmp, 398
common_files, ,
dircmp, 398
common_funny, , dircmp, 398
commonprefix(), ,
os.path, 496
communicate(),
Popen, 505
compileall, , 726
compile(), , 156, 261
re, 357
complete_statement(), ,
sqlite3, 385
__complex__(), , 93
, 180
Complex, ,
numbers, 321
complex, , 63
complex(), , 111, 262
compress(),
compressobj, 413
BZ2Compressor, 396
compress(),
zlib, 413
bz, 396
CompressionError, ,
tarfile, 406
compressobj(), , zlib, 413
compress_size, ,
ZipInfo, 412
compress_type, ,
ZipInfo, 412
concat(), , operator, 347
Condition,
multiprocessing, 534
threading, 551
condition, ,
pdb, 244
Condition(),
Manager, 536
Condition(), , threading,
551
ConfigParser, ,
configparser, 417
configparser, , 416
ConfigParser, , 416
confstr(), , os, 495
conjugate(),
, 64
, 64


connect(),
BaseManager, 539
dispatcher, 569
FTP, 620
SMTP, 639
, 597
connect(),
sqlite3, 384
,
376
connect_ex(), ,
597
Connection,
,
376
sqlite3, 385
Connection, ,
multiprocessing, 527
ConnectRegistry(), ,
winreg, 511
Container, ,
336
__contains__(), , 89
contains(), , operator,
347
__context__,
Context, , decimal, 311
ContentHandler, , xml.sax,
720
ContentTooShort, ,
urllib.error, 651
contextlib, , 127, 339
@contextmanager, , 127
contextmanager(), ,
contextlib, 339
continue, , 119
c(ont(inue)), ,
pdb, 245
convert_field(),
Formatter, 364
Cookie, , 635
CookieError, , http.
cookies, 638
CookieJar, , http.cookiejar,
638
CookieJar(), , http.
cookiejar, 638
cookielib, , 638
__copy__(), , 281
copy(),
, 74
Context, 314
HMAC, 695
, 694

801
, 72
copy, , 61, 100, 280
, 281
copy(),
copy, 280
shutil, 401
copy2(), , shutil, 401
copyfileobj(), , shutil,
401
copyfile(), , shutil, 401
copymode(), , shutil, 401
copy_reg, , 726
copyreg, , 726
copyright, , sys, 293
copysign(), , math, 319
copystat(), , shutil, 401
copytree(), , shutil, 401
@coroutine, , 144
cos(), , math, 319
cosh(), , math, 319
countOf(), , operator,
347
count(),
array, 329
, 67
, 69
count(), , itertools, 343
'cp437', , , 220
'cp1252', , , 220
cPickle, , 292
cProfile, , 247
cpu_count(), ,
multiprocessing, 543
CRC, , ZipInfo,
412
crc32(),
binascii, 681
crc32(), , zlib, 413
crc_hqx(), , binascii, 681
create_aggregate(), Connection, 386
create_collation(),
Connection, 386
create_connection(), ,
socket, 590
create_decimal(), ,
Context, 314
created,
Record, 451
create_function(),
Connection, 386
create_socket(),
dispatcher, 570

802
create_string_buffer(), ,
ctypes, 765
create_system, ,
ZipInfo, 412
create_unicode_buffer(), , ctypes, 765
create_version, ,
ZipInfo, 412
critical(), Logger,
448
crypt, , 727
csv, , 681
csv,
, 681
, 62
ctermid(), , os, 476
ctime(), date, 422
ctime(), , time, 508
ctype,
, 763
ctypes, , 533, 759
, 761
,
759
, 765
, 762
, 760
, 759
, 765
, 766
, 765
, 764
, 762
cunifvariate(), , random,
324
curdir, , os, 484
currentframe(), ,
inspect, 284
_current_frames(), , sys,
297
current_process(), ,
multiprocessing, 543
current_thread(), ,
threading, 555
curses, , 727
Cursor, , , 376
cursor(),
Connection, 376
cwd(), FTP, 620
cycle(), , itertools, 343

D
daemon,
Process, 521
Thread, 546
data, Text, 711
data(),
TreeBuilder, 717
DatabaseError, ,
, 381
DataError, , , 381
DatagramHandler, ,
logging, 454
DatagramRequestHandler, ,
SocketServer, 613
date, , datetime, 421
date(), datetime,
426
Date(),
, 380
DateFromTicks(),
, 381
date_time, ,
ZipInfo, 412
datetime, , datetime, 424
datetime, , 421
DateTime(), , xmlrpc.
client, 654
daylight, , time, 507
dbhash, , 391
dbm, , 391
__debug__, , 128, 463
debug,
TarFile, 403
ZipFile, 410
sys.flags, 294
debug(), Logger,
448
Decimal, , decimal, 310
decimal, , 309
, 316
sum(), 101
, 311
Decimal, , , 318
decimal(), , unicodedata,
372
decode(),
Python 3, 781
CodecInfo, 350
IncrementalDecoder,
351
JSONDecoder, 702


, 54, 68, 69
decode(), , 216
decode(),
base64, 679
quopri, 705
decodestring(),
base64, 679
quopri, 705
decomposition(), ,
unicodedata, 372
decompress(),
decompressobj, 414
BZ2Decompressor,
396
decompress(),
bz, 396
zlib, 413
decompressobj(), , zlib,
413
__deepcopy__(), , 281
deepcopy(), , copy, 61,
281
def, , 39, 76, 130
default(),
JSONEncoder, 703
DefaultContext, ,
decimal, 315
defaultdict, , 332
defaultdict(), ,
collections, 334
default_factory, ,
defaultdict, 334
__defaults__, , 76
defaults(),
ConfigParser, 417
defects,
Message, 688
defpath, , os , 489
degrees(), , math, 319
__del__(), , 85, 173, 234
, 282
, 173
del, , 59, 102
__del__(), 173
, 66
, 108
, 72
del, ,
37
__delattr__(), , 88, 177
delattr(), , 262
__delete__(), , 89,
delete(), FTP, 620

803
DeleteKey(), , winreg,
512
DeleteValue(), , winreg,
512
__delitem__(), , 89
, 91
delitem(), , operator,
347
del_param(),
Message, 690
delslice(), , operator, 347
170
demo_app(), , wsgiref.
simple_server, 673
denominator, ,
Fraction, 318
denominator, , 64
DeprecationWarning, ,
278, 303
deque, , collection, 252
, 252
deque, , 332
deque(), , collections,
332
DER_cert_to_PEM_cert(), ,
ssl, 610
dereference, ,
TarFile, 403
description,
Cursor, 378
devnul, , os, 484
Dialect, , csv, 684
dict, , 63
dict(), , 112, 262
dict(), Manager,
536
dict(), , 37
__dict__, , 254, 263
, 176
types, 79
, 80, 191
, 76, 155
, 79, 176
DictReader(), , csv, 683
DictWriter(), , csv, 683
difference_update(), , 74
difference(), , 74
diff_files, ,
dircmp, 398
difflib, , 727
dig, sys.float_info,
294

804
digest(),
HMAC, 695
, 694
digest_size, , 694
digit(), , unicodedata,
373
digits, , string, 362
__dir__(), , 95, 263
dir(), FTP, 620
dir(), , 43, 47, 262
, 95
dircmp(), filecmp, 397
dirname(), , os.path,
496, 499
dis, , 250, 726
dis(), , dis, 250
disable, , pdb,
245
disable(),
gc, 282
logging, 459
disable_interspersed_args(), PtionParser, 472
discard(), , 74
discard_buffers(),
async_chat, 565
dispatcher, , asyncore, 568
__displayhook__, ,
sys, 230, 293
displayhook(), , sys, 297
disposition,
FieldStorage, 665
disposition_options,
FieldStorage, 665
distutils, , 200, 726, 739
, 739
,
202
Windows,
202

, 201

SWIG, 768
__div__(), , 92
div(), , operator, 346
division_new, sys.
flags, 294
division_warning,
sys.flags, 294
__divmod__(), , 92
divmod(), , 97, 263
dllhandle, , sys, 293


__doc__, , 55
, 78
, 78
, 80
types, 79
, 47, 76, 154
DocCGIXMLRPCRequestHandler, ,
xmlrpc.server, 657
DocCGIXMLRPCRequestHandler(),
, xmlrpc.server, 657
doctest, , 236, 237
verbose, 237
Document, , xml.dom.
minidom, 710
documentElement,
Document, 710
DocXMLRPCServer, ,
xmlrpc.server, 656
DocXMLRPCServer, , 655
DocXMLRPCServer(), ,
xmlrpc.server, 656
do_handshake(),
SSLSocket, 609
DOM,
XML, 706
, 711
dont_write_bytecode, sys.flags, 294
dont_write_bytecode, , sys, 293
d(own) , , pdb,
245
dropwhile(), , itertools,
343
dst(), tzinfo, 428
dumbdbm, , 391
dump(), , Pickler, 291
dump(),
json, 700
marshal, 288
pickle, 223, 289
xml.etree.ElementTree, 717
dumps(),
json, 701
marshal, 288
pickle, 289
xmlrpc.client, 654
dup(), , os, 478
dup2(), , os, 478

E
e, , math, 320
-e, , 226


EAI_*, , socket, 606
east_asian_width(), ,
unicodedata, 373
.egg, , 203
, 194
Element, , xml.dom.
minidom, 710
Element(), , xml.etree.
ElementTree, 714
ElementTree, , XML,
706
ElementTree, , xml.etree.
ElementTree, 712
elif, , 117
Ellipsis, , 84
, 84
Python 3, 776
, 80
else, , 117
try, 122
for while, 120
email, , 685
email.message, , 689
Emax, , Context,
314
Emin, , Context,
314
Empty, , Queue, 523,
557
empty(), Queue,
523, 557
enable, , pdb,
245
enable(),
cgitb, 670
gc, 282
enable_callback_tracebacks(), ,
sqlite3, 385
enable_interspersed_args(), PtionParser, 472
encode(),
Python 3, 781
CodecInfo, 349
IncrementalEncoder,
351
JSONEncoder, 703
, 68
encode(), , 216
encode(),
base64, 679
quopri, 705
EncodedFile, , codecs, 352
EncodedFile, , codecs, 218

805
encodestring(),
base64, 680
quopri, 705
encoding,
open(), 208
, 216
encoding,
TextIOWrapper, 443
, 211
end,
print(), 213
end(),
MatchObject, 360
TreeBuilder, 717
endDocument(),
ContentHandler, 721
endElementNS(),
ContentHandler, 721
endElement(),
ContentHandler, 721
end_headers(),
BaseHTTPRequestHandler, 634
endheaders(),
HTTPConnection, 627
endpos,
MatchObject, 361
endPrefixMapping(),
ContentHandler, 721
endswith(), , 69
__enter__(), , , 94, 126
enumerate(), , 118, 263
threading, 555
EnumKey(), , winreg,
512
EnumValue(), , winreg,
512
environ, , os, 207, 475
EnvironmentError, , 123,
274
EOFError, , 123, 275
epilogue,
Message, 688
epoll, Linux, 574
epsilon,
sys.float_info, 294
__eq__(), , 87
eq(), , operator, 346
errcheck,
ctypes, 761
errno, , 430
error,
os.path, 496
socket, 606

806
error(), Logger,
448
errorcode, , errno,
430
errorlevel, ,
TarFile, 404
error_message_format,
BaseHTTPRequestHandler, 632
errors,
open(), 208
, 217
errors,
TextIOWrapper, 443
escape(),
cgi, 666
re, 358
xml.sax.saxutils, 723
eval(), , 86, 111, 156, 263
repr(), 86
Event,
multiprocessing, 534
threading, 550
Event(), Manager,
536
Event(), , threading, 550
EX_*, , 490
exc_clear(), , sys, 297
except, , 45, 121
, 121
excepthook(), , sys, 121,
297
Exception, , 123, 273, 274,
778
exception(),
Logger, 450
__excepthook__, ,
sys, 293
exc_info,
Record, 451
exc_info(), , sys, 82, 126,
298
exec(), , 156, 263
Python 3, 784
execl(), , os, 489
execle(), , os, 489
execlp(), , os, 489
exec_prefix, , sys,
231, 293
execute(),
Connection, 387
Cursor, 377
executable, , sys, 293


executemany(),
Connection, 387
Cursor, 377
executescript(),
Connection, 387
execv(), , os, 489
execve(), , os, 489
execvp(), , os, 490
execvpe(), , os, 490
exists(), , os.path, 497
__exit__(), , 94
, 126
exit(), , 235
exit(),
os, 235, 277, 490
sys, 277, 298
exitcode,
Process , 521
exp(), Decimal,
311
exp(), , math, 319
expand(),
MatchObject, 360
ExpandEnvironmentStrings(), ,
winreg, 513
expandtabs(), , 68
expanduser(), , os.path,
497
expandvars(), , os.path,
497
expovariate(), , random,
324
extend(),
deque, 333
array, 330
, 66
ExtendedContext, ,
decimal, 315
extendleft(), , deque,
333
extensions_map,
HTTPRequestHandler, 632
Extension(), , distutils,
739
external_attr, ,
ZipInfo, 412
extra, , ZipInfo,
412
extract(), ,
TarFile, 404
ZipFile, 410
extractall(), ,
ZipFile, 410


ExtractError, ,
tarfile, 406
extractfile(), ,
TarFile, 404
extract_stack(), ,
traceback, 301
extract_tb(), , traceback,
301
extract_version, , ZipInfo, 412
extsep, , os, 484

F
f_*,
, 82
statvfs, 488
F_*, , fcntl, 434
fabs(), , math, 319
factorial(), , math, 319
failIfAlmostEqual(),
TestCase, 241
failIfEqual(),
TestCase, 241
failIf(),
TestCase, 241
failUnless(),
TestCase, 241
failUnlessAlmostEqual(),
TestCase, 241
failUnlessEqual(),
TestCase, 241
failUnlessRaises(),
TestCase, 241
failureException,
TestCase, 241
False, , 50, 64
family, , 606
Fault, , xmlrpc.
client, 655
fchdir(), , os, 476
fchmod(), , os, 479
fchown(), , os, 479
fcntl, , 210, 434
fcntl(), , fcntl, 434
fdatasync(), , os, 479
fdopen(), , os, 479
feed(),
HTMLParser, 696
fetchall(), Cursor,
377
fetchmany(),
Cursor, 377

807
fetchone(), Cursor,
377
FieldStorage(), , cgi, 664
__file__, , 80
file,
FieldStorage, 665
file,
print(), 29, 213
filecmp, , 396
fileConfig(), , logging,
461
FileCookieJar(), , http.
cookiejar, 638
FileHandler,
logging, 454
urllib.request, 644
FileIO, , io, 438
filename,
FieldStorage, 665
Record, 451
ZipInfo, 412
fileno(),
SocketServer, 614
urlopen, 641
Connection, 527
IOBase, 437
, 597
, 573
, 209, 210
file_offset, ,
ZipInfo, 412
file_size, ,
ZipInfo, 412
Filter, , logging, 450
filter(), , 264
Python 3, 264
fnmatch, 399
filterwarnings(), ,
warnings, 304
finally, , 122
, 553
find(),
Element, 715
ElementTree, 712
mmap, 465
, 68, 69
findall(),
Element, 715
ElementTree, 713
Regex, 359
findall(), , re, 358
findCaller(),
Logger, 450

808
finditer(), Regex,
359
finditer(), , re, 358
find_library(), , ctypes,
760
findtext(),
Element, 716
ElementTree, 713
finish(),
BaseRequestHandler, 612
firstChild,
Node, 708
flag_bits, ,
ZipInfo, 412
flags,
Context, 314
Regex, 359
flags, , sys, 293
__float__(), , 93
, 180
float, , 63
float(), , 31, 33, 111, 264
float_info, , sys, 294
FloatingPointError, , 123,
275
flock(), , fcntl, 436
__floordiv__(), , 92
floor(), , math, 319
floordiv(), , operator,
346
FlushKey(), , winreg,
513
flush(),
compressobj, 414
decompressobj, 414
BufferedWriter, 441
BZ2Compressor, 396
Handler, 457
IOBase, 437
mmap, 465
, 209
fma(), Decimal,
311
fmod(), , math, 319
fnmatch, , 398
fnmatch(), , fnmatch,
399
fnmatchcase(), ,
fnmatch, 399
for, , 29, 37, 91, 101, 117
, 40
, 29, 209
fork(), , os, 491


ForkingMixIn, , ,
SocketServer, 616
ForkingTCPServer, ,
SocketServer, 617
ForkingUDPServer, ,
SocketServer, 617
forkpty(), , os, 491
__format__(), , 86, 108
format, Struct,
367
format(),
Formatter, 363
, 107
, 68, 105, 107, 214
format(), , 27, 31, 32, 86, 111,
264
formatargspec(), ,
inspect, 284
formatargvalues(), ,
inspect, 284
format_exception_only(), , traceback, 301
format_exception(), ,
traceback, 301
format_exc(), ,
traceback, 301
format_list(), ,
traceback, 301
format_stack(), ,
traceback, 301
format_tb(), , traceback,
301
Formatter,
logging, 458
string, 363
formatter, , 728
format_value(),
Formatter, 364
formatwarning(), ,
warnings, 304
found_terminator(),
async_chat, 565
fpathconf(), , os, 479
fpectl, , 726
fpformat, , 727
Fraction, , fractions, 317
fractions, , 64, 317
fragment,
SplitResult, 648
FrameType, , 80, 302
freeze_support(), ,
multiprocessing, 543
frexp(), , math, 319

809


from,
, 191
from_address(),
ctype, 764
from_buffer_copy(), ctype, 764
from_buffer(),
ctype, 764
from_decimal(), Fraction,
318
fromfd(), , socket, 590
fromfile(), , array,
330
from_float(), Fraction, 318
from __future__ import, ,
232
fromhex(),
, 65
from_iterable(), , 343
fromkeys(), , 72
fromlist(), , array,
330
from module import *, , 47,
192
,
, 50
__all__, 192
fromordinal(),
date, 422
datetime, 425
from_param(),
ctype, 764
fromstring(), ,
array, 330
fromstring(), , xml.
etree.ElementTree, 714
fromtimestamp(),
date, 422
datetime, 425
fromutc(), tzinfo,
428
frozenset, , 63, 73, 109
frozenset(), , 112, 264
fstat(), , os, 480
fstatvfs(), , os, 480
fsum(), , math, 320
fsync(), , os, 480
FTP , , 622
FTP(), , ftplib, 619
FTPHandler, , urllib.
request, 644
ftplib, , 619
ftruncate(), , os, 481

Full, , Queue, 523,


557
full(), Queue, 523,
557
__func__, , 78
func, , partial, 340
funcName,
Record, 451
FunctionType, , 75, 302
functools, , 111, 156, 339
funny_files, ,
dircmp, 398
__future__, , 233, 262
, 233
future_builtins, , Python
2, 279
FutureWarning, , 278,
303

G
gaierror, , socket,
606
gammavariate(), ,
random, 324
garbage, , gc, 282
gauss(), , random, 324
gc, , 60, 281
gcd(), , fractions, 319
gdbm, , 391
__ge__(), , 87
ge(), , operator, 347
GeneratorExit, , 123, 275
, 143
, 144
GeneratorType, , 80, 302
get(),
AsyncResult, 531
ConfigParser, 417
Element, 716
Message, 686
Queue, 523, 557
, 37, 72
get(),
webbrowser, 676
getaddrinfo(), , socket,
590
get_all(),
Message, 686
getargspec(), , inspect,
284
getargvalues(), , inspect,
284

810
getatime(), , os.path,
497
getattr(), , 264
__getattr__(), , 88, 176
__slots__, 178
__getattribute__(), , 88, 176
__slots__, 178
getttribute(),
Element, 710, 711
getboolean(),
ConfigParser, 417
get_boundary(),
Message, 686
get_charsets(),
Message, 686
get_charset(),
Message, 686
getch(), , msvcrt, 467
getche(), , msvcrt, 467
getcheckinterval(), , sys,
298
getchildren(),
Element, 716
getclasstree(), , inspect,
284
getcode(), urlopen, 641
getcomments(), ,
inspect, 284
get_content_charset(),
Message, 686
get_content_maintype(),
Message, 687
get_content_subtype(),
Message, 687
get_content_type(), Message, 687
getcontext(), , decimal,
314
get_count(), , gc, 282
getctime(path) , ,
os.path, 497
getcwd(), , os, 476
getcwdu(), , os, 476
get_data(),
Request, 643
get_debug(), , gc, 282
getdefaultencoding(), ,
sys, 216, 272, 298
getdefaulttimeout(), ,
socket, 591
get_default_type(), Message, 687
get_dialect(), , csv, 684


getdlopenflags(), , sys,
298
getdoc(), , inspect, 285
getEffectiveLevel(),
Logger, 452
getegid(), , os, 476
getElementsByTagNameNS(),
Document, 710
Element, 710
getElementsByTagName(),
Document, 710
Element, 710
get_errno(), , ctypes,
765
geteuid(), , os, 476
get_field(),
Formatter, 363
getfile(), , inspect, 285
get_filename(),
Message, 687
getfilesystemencoding(), ,
sys, 298
getfirst(),
FieldStorage, 665
getfloat(),
ConfigParser, 418
getfqdn(), , socket, 591
_getframe(), , sys, 298
getframeinfo(), ,
inspect, 285
get_full_url(),
Request, 643
getgid(), , os, 476
getgroups(), , os, 476
getheader(),
HTTPResponse, 627
getheaders(),
HTTPResponse, 627
get_host(),
Request, 643
gethostbyaddr(), ,
socket, 592
gethostbyname(), ,
socket, 591
gethostbyname_ex(), ,
socket, 592
gethostname(), , socket,
592
getinfo(), ,
ZipFile, 410
getinnerframes(), ,
inspect, 285


getint(),
ConfigParser, 418
__getitem__(), , 89, 266, 270
, 91
getitem(), , operator,
347
getiterator(),
Element, 716
ElementTree, 713
getitimer(), , signal, 500
get_last_error(), ,
ctypes, 765
getLength(), ,
SAX, 722
getLevelName(), ,
logging, 460
getlist(),
FieldStorage, 666
getloadavg(), , os, 495
getlogger(),
logging, 447
multiprocessing, 544
getlogin(), , os, 476
getmember(), ,
TarFile, 404
getmembers(), ,
TarFile, 404
getmembers(), , inspect,
285
get_method(),
Request, 643
getmodule(), , inspect,
285
getmoduleinfo(), ,
inspect, 285
getmodulename(), ,
inspect, 286
getmro(), , inspect, 286
getmtime(), , os.path,
497
getName(),
Thread, 546
getNameByQName(), ,
SAX, 722
getnameinfo(), , socket,
592
getNames(), ,
SAX, 722
getnames(),
TarFile, 404
get_nowait(),
Queue, 523, 557

811
get_objects(), , gc, 282
getopt, , 474
get_origin_req_host(),
Request, 643
get_osfhandle(), ,
msvcrt, 467
getouterframes(), ,
inspect, 286
getoutput(), ,
commands, 416
get_param(),
Message, 687
get_params(),
Message, 687
get_payload(),
Message, 687
getpeercert(),
SSLSocket, 609
getpeername(), ,
597
getpgid(), , os, 476
getpgrp(), , os, 476
getpid(), , os, 476
getpos(),
HTMLParser, 696
getppid(), , os, 476
getprofile(), , sys, 298
getprotobyname(), ,
socket, 592
getQNameByName(), ,
SAX, 722
getQNames(), , SAX, 722
getrandbits(), , random,
323
getrecursionlimit(), ,
sys, 153, 299
getrefcount(), , sys, 59
get_referents(), , gc, 282
get_referrers(), , gc, 282
getresponse(),
HTTPConnection, 627
getroot(),
ElementTree, 713
get_selector(),
Request, 643
getservbyname(), ,
socket, 593
getservbyport(), ,
socket, 593
get_server_certificate(), , ssl, 610

812
GetSetDescriptorType, , 302
getsid(), , os, 476
getsignal(), , signal, 500
getsize(), , os.path, 497
getsizeof(), , sys, 249,
299
getslice(), , operator,
347
getsockname(), ,
597
getsockopt(), ,
597
getsource(), , inspect,
286
getsourcefile(), , inspect,
286
getsourcelines(), ,
inspect, 286
get_starttag_text(),
HTMLParser, 697
__getstate__(), , 292
, 281
pickle, 224
getstate(), , random, 323
getstatusoutput(), ,
commands, 416
gettarinfo(), ,
TarFile, 404
gettempdir(), , tempfile,
407
gettempprefix(), ,
tempfile, 407
get_terminator(),
async_chat, 565
gettext, , 728
get_threshold(), , gc,
282
gettimeout(), ,
603
gettrace(), , sys, 299
get_type(),
Request, 643
getType(), ,
SAX, 722
getuid(), , os, 477
get_unixfrom(),
Message, 688
geturl(),
SplitResult, 647, 648
urlopen, 641
getvalue(),
FieldStorage, 665
StringIO, 443


_getvalue(),
BaseProxy, 541
get_value(),
Formatter, 364
getValue(), ,
SAX, 722
getValueByQName(), ,
SAX, 722
getwch(), , msvcrt, 467
getwche(), , msvcrt, 467
getweakrefcount(), ,
weakref, 306
getweakrefs(), , weakref,
306
getwindowsversion(), ,
sys, 299
__get__(), , 89, 170
gi_*, , 83
gid, , TarInfo,
405
glob, , 399
glob(), , glob, 399
global, , 39, 134, 189

, 39
__globals__, , 76, 137
globals(), , 265
gmtime(), , time, 508
gname, ,
TarInfo, 405
group(),
MatchObject, 360
groupby(), , itertools,
343
groupdict(),
MatchObject, 360
groupindex,
Regex, 359
groups(),
MatchObject, 360
grp, , 727
__gt__(), , 87
gt(), , operator, 347
guess_all_extensions(), ,
mimetypes, 704
guess_extension(), ,
mimetypes, 704
guess_type(), ,
mimetypes, 703
gzip, , 400
GzipFile, , gzip, 400
GzipFile(), , gzip, 400

H
-h, , 226
handle(),
BaseRequestHandler, 612
handle(), , cgitb, 670
handle_accept(),
dispatcher, 568
handle_charref(),
HTMLParser, 697
handle_close(),
dispatcher, 569
handle_comment(), HTMLParser, 697
handle_connect(),
dispatcher, 569
handle_data(),
HTMLParser, 697
handle_decl(),
HTMLParser, 697
handle_endtag(),
HTMLParser, 697
handle_entityref(), HTMLParser, 697
handle_error(),
dispatcher, 569
SocketServer, 616
handle_expt(),
dispatcher, 569
handle_pi(),
HTMLParser, 697
handle_read(),
dispatcher, 569
Handler, , logging, 454
handle_startendtag(),
HTMLParser, 697
handle_starttag(),
HTMLParser, 698
handle_timeout(),
SocketServer, 616
handle_write(),
dispatcher, 569
hasAttribute(),
Element, 710
hasAttributeNS(),
Element, 710
hasAttributes(),
Node, 709
hasattr(), , 265
hasChildNodes(),
Node, 709
has_data(),
Request, 643
__hash__(), , 87, 265

813
hash(), , 265
Hashable, ,
337
has_header(),
Request, 643
Sniffer, 684
hashlib, , 694
has_ipv6, , socket,
593
has_key(), , 72
has_option(),
ConfigParser, 418
has_section(),
ConfigParser, 418
header_offset, ,
ZipInfo, 412
headers,
BaseHTTPRequestHandler, 633
FieldStorage, 665
heapify(), , heapq, 341
heapmin(), , msvcrt, 467
heappop(), , heapq, 341
heappushpop(), , heapq,
342
heappush(), , heapq, 341
heapq, , 341
heapreplace(), , heapq,
342
h(elp), , pdb,
245
help(), , 47, 265
, 154
herror, , socket, 606
hex(), ,
65
hex(), , 112, 265
hexdigest(),
HMAC, 695
, 694
hexdigits, , string,
362
hexversion, , sys, 295
HIGHEST_PROTOCOL, , pickle, 224
HKEY_*, , winreg,
511
HMAC, , 695
hmac, , 695
hostname,
ParseResult, 647
SplitResult, 648

814
html, , 696
HTML-, , 660
HTMLParser, , html.parser,
696
html.parser, , 696
HTMLParser, , 696
HTMLParserError, ,
html.parser, 698
htonl(), , socket, 593
htons(), , socket, 593
http, , 623
HTTP,
, 624
, 624
, 623
HTTP-
POST, 628
,
634

, 581
, 630
asynchat, 566
asyncore, 570
HTTPBasicAuthHandler, ,
urllib.request, 644
http.client, , 625
HTTPConnection(), ,
http.client, 626
http.cookiejar, , 638
HTTPCookieProcessor,
urllib.cookies, 646
urllib.request, 644
http.cookies, , 635, 646
HTTPDefaultErrorHandler, , urllib.request, 644
HTTPDigestAuthHandler, ,
urllib.request, 644
HTTPError, , urllib.
error, 651
HTTPException, ,
http.client, 628
HTTPHandler,
logging, 454
urllib.request, 644
httplib, , 625
HTTPRedirectHandler, ,
urllib.request, 644
HTTPResponse, , http.
client, 627
HTTPSConnection, , http.
client, 626
HTTPSConnection(), ,
http.client, 626


HTTPServer, , http.server,
630
http.server, , 630
HTTPServer(), , http.
server, 630
HTTPSHandler, , urllib.
request, 644
hypot(), , math, 320

I
-i, , 226
__iadd__(), , 92
__iand__(), , 93
IBM General Decimal Arithmetic
Standard, , 309
id(), , 58, 265
ident, Thread,
546
__idiv__(), , 92
IEEE 754, , 309
if, , 117
__debug__, 129
ifilter(), , itertools, 344
ifilterfalse(), , itertools,
344
__ifloordiv__(), , 93
iglob(), , glob, 399
ignorableWhitespace(),
ContentHandler, 721
ignore, , pdb,
245
'ignore',
, 217
ignore_environment, sys.flags, 294
ignore_patterns(), ,
shutil, 401
ignore_zeros, ,
TarFile, 404
__ilshift__(), , 93
imag,
, 64
, 64
, 64
imap(), Pool, 531
imap(), , itertools, 344
imaplib, , 728
imap_unordered(), Pool, 531
imghdr, , 729
__imod__(), , 93
imp, , 286, 726


import, , 33, 46, 80, 189
Python 3, 199
, 189
, 190

, 198
sys.modules, 191
sys.path, 194
as, 190
.pyc, 195
, 191
, 191
, 197
, 192
, 194
, 196
ImportError, , 124, 195, 232,
275
ImproperConnectionState, ,
http.client, 628
__imul__(), , 92
in, , 29
__contains__(), 90
, 72
, 99
, 108
,
101
, 37
INADDR_*, , socket,
596
IncompleteRead, ,
http.client, 628
IncrementalDecoder, ,
codecs, 351
IncrementalEncoder, ,
codecs, 351
incrementalencoder(),
CodecInfo, 351
IndentationError, , 124, 275
index(),
array, 330
, 66
, 68, 69
IndexError, , 100, 124, 275
indexOf(), , operator,
347
indices(), , 84
in_dll(),
ctype, 764
inet_aton(), , socket, 593
inet_ntoa(), , socket, 593

815
inet_ntop(), , socket,
593
inet_pton(), , socket,
594
Inf, , decimal, 315
info(),
urlopen, 641
Logger, 448
infolist(), ,
ZipFile, 410
.ini-
, 461

Python, 416
init(), , mimetypes, 704
__init__(), , 78, 85, 159
, 186
, 125
, 173
, 44
, 161
__init__.py, , 197
input(), , 30, 265
Python 3, 30, 211
sys, 296
insert(),
Element, 716
array, 330
, 32, 67
insertBefore(),
Node, 709
insort(), , bisect, 332
insort_left(), , bisect,
332
insort_right(), , bisect,
332
inspect, sys.flags,
294
inspect, , 283
install, , setup.py, 203
install_opener(), , urllib.
request, 645
__instancecheck__(), , 88, 182
__int__(), , 93
, 180
int, , 63
int(), , 31, 111, 265
Integral, ,
numbers, 321
IntegrityError, ,
, 382

816
interactive, sys.
flags, 294
InterfaceError, ,
, 381
internal_attr, ,
ZipInfo, 412
InternalError, ,
, 382
interrupt(),
Connection, 387
intersection(), , 74
intersection_update(), ,
74
inv(), , operator, 346
InvalidURL, , http.
client, 628
__invert__(), , 93
invert(), , operator, 346
io, , 437
Python 3, 783
IOBase, , 444
IOBase, , io, 437
ioctl(), , 603
ioctl(), , fcntl, 435
IOError, , 45, 123, 275
__ior__(), , 93
__ipow__(), , 93
IP_*, , socket, 599
IPPROTO_*, , socket,
594
IPv4, , 586
, 587
IPv6, , 586
, 588
IPV6_*, , socket, 600
IronPython,
, 769
__irshift__(), , 93
is, , 58,
113
is_(), , operator, 347
isabs(), , os.path, 497
isabstract(), , inspect,
287
is_alive(),
Process, 520
Thread, 546
isAlive(), Thread,
546
isalnum(), , 68, 69
isalpha(), , 69
isatty(),
IOBase, 437


, 209
isatty(), , os, 481
isblk(), , TarInfo,
405
isbuiltin(), , inspect, 287
ischr(), , TarInfo,
405
isclass(), , inspect, 287
iscode(), , inspect, 287
isDaemon(),
Thread, 546
isdatadescriptor(), ,
inspect, 287
isdev(), , TarInfo,
405
isdigit(), , 69
isdir(), , TarInfo,
405
isdir(), , os.path, 497
isdisjoint() , , 74
iselement(), , xml.etree.
ElementTree, 717
isenabled(), , gc, 283
isEnabledFor(),
Logger, 450
isfifo(), , TarInfo,
405
isfile(), , TarInfo,
405
isframe(), , inspect, 287
isfunction(), , inspect,
287
isgenerator(), , inspect,
287
isgeneratorfunction(), ,
inspect, 287
isinf(), , math, 320
isinstance(), , 59, 63, 266
, 180
, 181
, 182
islice(), , itertools, 344
islink(), , os.path, 498
islinkname, ,
TarInfo, 405
islnk(), , TarInfo,
405
islower(), , 69
ismethod(), , inspect,
287
ismethoddescriptor(), ,
inspect, 287
ismodule(), , inspect,
287

817


ismount(), , os.path, 498
is_multipart(),
Message, 688
isnan(), , math, 320
is_not(), , operator, 347
'iso-8859-1', , , 220
isocalendar(),
date, 422
isoformat(),
date, 422
time, 424
isoweekday(),
date, 422
isreg(), , TarInfo,
405
isReservedKey(),
Morsel, 637
isroutine(), , inspect,
287
isSameNode(),
Node, 709
is_set(), Event,
550
isspace(), , 69
issubclass(), , 181, 266
, 180
, 182
issubset(), , 74
issuperset(), , 74
issym(), , TarInfo,
405
is_tarfile(), , tarfile, 402
istitle(), , 70
istraceback(), , inspect,
287
__isub__(), , 92
is_unverifiable(),
Request, 643
isupper(), , 68, 70
is_zipfile(), , zipfile, 409
itemgetter(), , operator,
348
items(), , 72
ConfigParser, 418
Element, 716
Message, 686
itemsize, , array,
329
ItemsView, ,
338
__iter__(), , 91, 117, 266
Iterable, ,
337

Iterator, ,
337
iterdecode(), , codecs,
352
iterdump(),
Connection, 387
iterencode(),
JSONEncoder, 703
iterencode(), , codecs,
352
iterkeyrefs(),
WeakKeyDictionary, 307
iterparse(), , xml.etree.
ElementTree, 717
itertools, , 119, 342
itertools.izip(), , 273
itervaluerefs(),
WeakValueDictionary, 307
iter(), , 266
__itruediv__(), , 92
__ixor__(), , 93
izip(), , itertools, 119,
273, 344
izip_longest(), ,
itertools, 344

J
J, ,
, 51
Java, , 769
join(),
JoinableQueue, 524
Pool, 531
Process, 520
Queue, 557
Thread, 546
, 70
join(), , os.path, 498
JoinableQueue(), ,
multiprocessing, 524
join_thread(),
Queue, 523
json, , 699
pickle marshal,
702
JSON (JavaScript Object Notation
JavaScript), ,
699
JSONDecoder, , json, 702
JSONEncoder, , json, 703
js_output(),
SimpleCookie, 637

818
jumpahead(), , random,
323
j(ump), , pdb,
245
Jython,
, 769

K
kbhit(), , msvcrt, 467
key, Morsel,
637
KEY_*, , winreg , 513
KeyboardInterrupt, , 123,
211, 276
KeyError, , 72, 124, 276
keyrefs(),
WeakKeyDictionary, 307
keys(),
Element, 716
Message, 686
keys(), , 72
KeysView, ,
338
keyword, , 726
keywords, ,
partial, 340
kill(), Popen, 505
kill(), , os, 491
killpg(), , os, 491

L
L, ,
, 51
lambda, , 76, 152
LambdaType, , 302
last_accepted,
Listener, 543
lastChild, Node,
708
lastgroup,
MatchObject, 361
lastindex,
MatchObject, 361
last_traceback, , sys,
295
last_type, , sys, 295
last_value, , sys, 295
'latin-1', , , 220
lchflags(), , os, 485
lchmod(), , os, 485
lchown(), , os, 485
ldexp(), , math, 320


__le__(), , 87
le(), , operator, 346
left_list, ,
dircmp , 397
left_only, ,
dircmp, 397
__len__(), , 87, 89, 90, 270
, 87
len(), , 90, 266
, 72
,
65
, 74, 109
, 108
,
99
length,
HTTPResponse, 627
letters, , string, 362
levelname,
Record, 451
levelno, Record,
451
lexists(), , os.path, 498
libwww-perl, , 638
LifoQueue(), , Queue,
556
limit_denominator(),
Fraction, 318
line_buffering,
TextIOWrapper, 443
linecache, , 726
lineno, Record,
451
linesep, , os, 475
link(), , os, 485
Linux, , 415
l(ist), , pdb,
245
list(),
Manager, 536
TarFile, 405
list, , 63
list(), , 32, 66, 111, 266
, 37
list_dialects(), , csv, 684
listdir(),
Python 3, 783, 787
os, 485
listen(),
dispatcher, 570
, 603

819


Listener, , multiprocessing,
542
ljust(), , 70
ln(), Decimal, 311
load(),
SimpleCookie, 637
Unpickler, 291
load(),
json, 701
marshal, 288
pickle , 223, 290
loads(),
marshal, 288
pickle, 290
xmlrpc.client, 654
json, 702
local(), , threading, 555
localcontext(), , decimal,
315
locale, , 728
localName,
Node, 708
locals(), , 266
localtime(), , time, 508
Lock,
multiprocessing, 534
threading, 548
Lock(), Manager,
536
Lock(), , threading, 548
LOCK_*, , flock(),
436
lockf(), , fcntl, 436
locking(), , msvcrt, 467
log(), Logger, 450
log(), , math, 320
log1p(), , math, 320
log10(), Decimal,
311
log10(), , math, 320
LogAdapter(), , logging,
459
log_error(),
BaseHTTPRequestHandler, 634
Logger, , logging, 447
logging, , 445
, 445
, 462
, 454
, 448
, 458

, 448
, 451
multiprocessing, 544
, 460
, 453
, 451

, 450

, 457
login(),
FTP, 620
SMTP, 639
log_message(),
BaseHTTPRequestHandler, 634
lognormvariate(), ,
random, 324
log_request(),
BaseHTTPRequestHandler, 634
__long__(), , 93
long, , 63
long(), , 267
lookup(),
codecs, 349
unicodedata, 373
LookupError, , 124, 273
loop(), , asyncore, 570
lower(), , 70
lowercase, , string,
362
lseek(), , os, 481
__lshift__(), , 92
lstat(), , os, 486
lstrip(), , 70
__lt__(), , 87
lt(), , operator, 346
LWPCookieJar(), ,
http.cookiejar, 638

M
-m, , 226
mailbox, , 728
mailcap, , 728
__main__, , 193, 227
major(), , os, 486
makedev(), , os, 486
makedirs(), , os, 486
makefile(), , 603
make_server(), ,
wsgiref.simple_server, 673
maketrans(), , string,
365

820
Manager(), ,
multiprocessing, 536
mant_dig, sys.
float_info, 294
map(), Pool, 531
map(), , 267
map_async(),
Pool, 531
Mapping, ,
337
MappingView,
, 338
marshal, , 288
match(), Regex,
359
match(), , re, 358
MatchObject, , re, 360
math, , 319
max,
date, 422
datetime, 425
timedelta, 427
max, sys.float_info,
294
max(), , 33, 65, 99, 267

, 88
, 109
max_10_exp, sys.
float_info, 294
max_exp, sys.float_
info, 294
maxint, , sys, 295
maxsize, , sys, 295
maxunicode, , sys,
295
md5(), , hashlib, 694
MemberDescriptorType, , 302
memmove(), , ctypes,
765
MemoryError, , 124, 276
MemoryHandler, , logging,
454
memset(), , ctypes, 766
merge(),
heapq, 342
message,
Exception, 274
Message, , email, 685
Message(), , email.
message, 689
message_from_file(), ,
email, 685


message_from_string(), ,
email, 685
__metaclass__, , 185
__metaclass__, ,
185
metaclass, , 185
methodcaller(), ,
operator, 348
methodHelp(),
ServerProxy, 654
methodSignatures(),
ServerProxy, 653
MethodType, , 75, 302
MIMEApplication, , email,
691
MIMEAudio, , email, 692
MIMEImage, , email, 692
MIMEMessage, , email, 692
MIMEMultipart, , email, 692
MIMEText, , email, 692
mimetypes, , 703
min,
date, 422
datetime, 425
time, 423
timedelta, 426
sys.float_info, 294
min(), , 33, 65, 99, 267

, 88
, 109
min_10_exp, sys.
float_info, 295
min_exp, sys.float_
info, 294
minor(), , os, 486
mirrored(), ,
unicodedata, 373
mkd(), FTP, 621
mkdir(), , os, 486
mkdtemp(), , tempfile,
407
mkfifo(), , os, 486
mknod(), , os, 486
mkstemp(), , tempfile,
407
mktemp(), , tempfile,
407
mktime(), , time, 509
mmap, , 463
mmap(), , mmap, 464
__mod__(), , 92
mod(), , operator, 346


mode,
FileIO, 439
TarInfo, 405
, 210
modf(), , math, 320
modulefinder, , 726
__module__, types, 79
module, Record,
451
modules, , sys, 196,
295
ModuleType, , 75, 302
Morsel, , http.cookies, 637
move(), mmap,
465
move(),
shutil, 402
MozillaCookieJar(), ,
http.cookiejar, 638
-m pdb, , 246
__mro__, , 163
MSG_*, , socket, 604
msvcrt, , 467
mtime, ,
TarInfo, 405
__mul__(), , 92
mul(), , operator, 346
MultiCall(), , xmlrpc.
client, 655
multiprocessing, , 519
c , 541
, 529
, 545
, 526
, 522
, 530
, 533
, 535
MutableMapping,
, 338
MutableSequence,
, 337
MutableSet, ,
337

N
\\N, , 53
__name__,
, 78
, 78
, 80
types, 79
, 76

821
__name__, , 193
name,
FieldStorage, 665
FileIO, 439
Record, 451
Process, 521
TarInfo, 405
Thread, 546
, 210
name, , os, 475
name(), , unicodedata,
373
NamedTemporaryFile(), , tempfile, 408
namedtuple(), ,
collections, 335
NameError, , 124, 276
, 134
, 234
namelist(), ,
ZipFile, 410
Namespace(),
Manager, 536
namespaceURI,
Node, 708
NaN, , decimal, 315
__ne__(), , 87
ne(), , operator, 347
__neg__(), , 93
neg(), , operator, 346
negInf, , decimal, 315
nested(), , contextlib,
339
Netlink, , 586
, 589
netloc,
ParseResult, 647
SplitResult, 648
netrc, , 728
__new__(), , 85
, 186
, 173
, 85
new(), ,
hashlib, 694
hmac, 695
newline, , open(),
208
newlines,
TextIOWrapper, 443
, 211
n(ext) , , pdb,
245
__next()__, , 786

822
next(), , 91
Python 3, 786
, 40, 83, 142
, 144
, 117, 267, 268
TarFile, 405
, 209
next(), , 267
nextset(), Cursor,
377
nextSibling,
Node, 708
NI_*, , socket , 592
nice(), , os, 491
nis, , 727
nlargest(), , heapq, 342
nntplib, , 728
nodeName,
Node, 708
nodeType,
Node, 708
nodeValue,
Node, 709
None,
, 133
, 131
None, , 63
nonlocal, Python 3, 135,
774
normalize(),
Node, 709
normalize(), ,
unicodedata, 223, 373
normalvariate(), ,
random, 324
normcase(), , os.path,
498
normpath(), , os.path,
498
no_site, sys.flags,
294
not, , 113
not_(), , operator, 346
NotConnected, , http.
client, 628
notify(),
Condition, 552
notify_all(),
Condition, 552
notifyAll(),
Condition, 552
NotImplementedError, , 124,
276


NotSupportedError, , , 382
now(),
datetime, 425
nsmallest(), , heapq, 342
NTEventLogHandler, ,
logging, 455
ntohl(), , socket, 594
ntohs(), , socket, 594
ntransfercmd(),
FTP, 621
Number, ,
numbers, 321
numbers.Integral, , 267
numbers, , 184, 321
numerator, ,
Fraction, 318
numerator, , 64
numpy, , 64, 331

O
-O, , 128,
195, 226
-OO, , 195,
227
object, , 43, 161
object, , 75
object(), , 268
oct(), , 112, 268
octdigits, , string,
362
open(),
ZipFile, 410
,
676
open(), , 29, 33, 207, 210, 268
Python 3, 208
codecs, 352
dbm, 391
gzip, 400
io, 443
os, 481
shelve, 393
tarfile, 402
webbrowser, 676
OpenKey(), , winreg, 513
open_new_tab(),
webbrowser, 676
open_new(),
, 676
open_new(),
webbrowser, 676


open_osfhandle(), ,
msvcrt, 468
openpty(), , os, 483
OpenSSL, , 608
, 611
OperationalError, , , 381
operator, , 346
optimize, sys.flags,
294
OptionParser(), ,
optparse, 469
options(),
ConfigParser, 418
optionxform(),
ConfigParser, 418
optparse, , 205, 469
__or__(), , 92
or, , 113
or_(), , operator, 346
ord(), , 112, 269
os, , 207, 475
os.environ, , 207
OSError, , 123, 276
os._exit(), , 235, 277
os.path, , 496
ossaudiodev, , 729
OS X, , 415
output(),
Morsel, 637
SimpleCookie, 637
OutputString(),
Morsel, 638
OverflowError, , 276

P
p, , pdb, 245
P_*, spawnv(),
492
pack(), Struct,
367
pack(), , struct, 366
PACKET_*,, socket,
589
pack_into(),
Struct, 367
pack_into(), , struct,
366
params,
ParseResult, 647
paramstyle, , , 380

823
pardir, , os, 484
parentNode,
Node, 709
paretovariate(), ,
random, 324
parse(),
ElementTree, 713
Formatter, 363
parse(),
xml.dom.minicom, 708
xml.etree.ElementTree, 717
xml.sax, 720
parse_args(), , optparse,
206
parse_args(),
PtionParser, 472
parse_header(), , cgi,
667
parse_multipart(), , cgi,
667
parse_qs(), , urllib.
parse, 648
parse_qsl(), , urllib.
parse, 649
parser, , 726
parseString(),
xml.dom.minicom, 708
xml.sax, 720
partial, , 340
partial(),
, 635
functools, 111
partition(), , 68, 70
pass, , 28, 49, 117
password,
ParseResult, 647
SplitResult, 648
__path__, , 80
__path__, , , 200
path,

BaseHTTPRequestHandler, 633
ParseResult, 647
SplitResult, 648
path,
os, 475
sys, 231, 295
pathconf(), , os, 486
pathname,
Record, 451
pathsep, , os, 484
pattern, Regex,
359

824
pause(), , signal, 500
pdb, , 242
.pdbrc, , 247
, 243
peek(),
BufferedReader, 440
PEM_cert_to_DER_cert(), , ssl, 610
PEP 333, (WSGI), 671
Perl,
,
136
permutations(), ,
itertools, 345
pi, , math, 320
pickle, , 223, 288, 289
copy, 281
, 224
, 224
, 225
Pickler, , pickle, 291
pickletools, , 726
pid,
Popen, 506
Process, 521
pipe(), ,
multiprocessing, 527
os, 483
pipes, , 727
pkgutil, , 726
platform, , 727
platform, , sys, 296
plistlib, , 728
plock(), , os, 491
pm(), , pdb, 243
pointer(), , ctypes, 762,
763
POLL*, , select, 573
poll(),
Connection, 527
Poll, 574
Popen, 505
poll(), , select, 573
Pool, , multiprocessing, 530
Pool(), ,
multiprocessing, 530
pop(),
, 74
deque, 333
array, 330
, 72
, 67


Popen, , subprocess , 505
popen(),
os, 491
subprocess, 503
popitem(), , 72
popleft(), , deque, 333
poplib, , 728
port,
ParseResult, 647
SplitResult, 648
__pos__(), , 93
pos,
MatchObject, 360
pos(), , operator, 346
posix, , TarFile,
405
POSIX, , 415
post_mortem(), , pdb,
243
__pow__(), , 92
pow(), , 97, 269
math, 320
pp, , pdb, 245
pprint, , 726
preamble,
Message, 688
prec, , Context,
314
prefix, Node,
709
prefix, , setup.py, 203
prefix, , sys, 231, 296
__prepare__(),
Python 3, 779
previousSibling,
Node, 709
print, , 24, 212
sys.stdout, 211
, 29,
212

, 212
print(), , 213
Python 3, 269, 784
Python 2.6, 213
, 213

, 213
-, 213
printable, , string,
362
print_directory(), , cgi,
667


printdir(), ,
ZipFile, 411
print_environ_usage(), ,
cgi, 667
print_environ(), , cgi,
667
print_exception(), ,
traceback, 300
print_exc(), , traceback,
300
print_form(), , cgi, 667
print_last(), , traceback,
301
print_stack(), ,
traceback, 301
print_tb(), , traceback,
300
PriorityQueue(), ,
Queue, 556
process,
Record, 451
Process, , multiprocessing,
520
Process (), ,
multiprocessing, 520
processingInstruction(),
ContentHandler, 721
ProcessingInstruction(), , xml.etree.ElementTree, 714
product(), , itertools,
345
profile, , 247
ProgrammingError, , , 382
propagate,
Logger, 452
@property, , 168
property(), , 269
proto, , 606
protocol, ,
pickle, 224
ProtocolError, ,
xmlrpc.client, 655
protocol_version,
BaseHTTPRequestHandler, 633
proxy(), , weakref, 306
ProxyBasicAuthHandler, ,
urllib.request, 644
ProxyDigestAuthHandler, ,
urllib.request, 644
ProxyHandler, , urllib.
request, 644
ProxyTypes, , weakref, 307
ps1, , sys, 230, 296

825
ps2, , sys, 230, 296
pstats, , 248
pty, , 727
punctuation, , string,
362
push(), async_
chat, 565
push_with_producer(),
async_chat, 566
put(), Queue, 523,
557
putch(), , msvcrt, 468
putenv(), , os, 477
putheader(),
HTTPConnection, 626
put_nowait(),
Queue, 524, 557
putrequest(),
HTTPConnection, 626
putwch(), , msvcrt, 468
pwd(), FTP, 621
pwd, , 727
.py, , 46, 194
, 46
py2app, , 203
py2exe, , 203
py3k_warning, sys.
flags, 294
py3kwarning, , sys,
296
PyArg_ParseTupleAndKeywords(), , 741
PyArg_ParseTuple(), , 740
Py_BEGIN_ALLOW_THREADS, , 753
Py_BuildValue(), , 747
PyBytes_AsString(), , 758
.pyc, , 194
, 195
import, 195
pyclbr, , 726
py_compile, , 726
Py_DECREF(), , 753
pydoc, , 47
Py_END_ALLOW_THREADS, , 754
PyErr_Clear(), , 752
PyErr_ExceptionMatches(), ,
752
PyErr_NoMemory(), , 750
PyErr_Occurred(), , 752
PyErr_SetFromErrnoWithFilename(),
, 750
PyErr_SetFromErrno(), , 750

826
PyErr_SetObject(), , 750
PyErr_SetString(), , 751
PyEval_CallObjectWithKeywords(),
, 757
PyEval_CallObject(), , 757
Py_Finalize(), , 755
PyFloat_AsDouble(), , 758
Py_GetExecPrefix(), , 756
Py_GetPath(), , 756
Py_GetPrefix(), , 756
Py_GetProgramFullPath(), , 756
PyImport_ImportModule(), ,
756
Py_INCREF(), , 752
Py_Initialize(), , 755
PyInt_AsLong(), , 758
Py_IsInitialized(), , 755
PyLong_AsLong(), , 758
PyModule_AddIntConstant(), ,
750
PyModule_AddIntMacro(), , 750
PyModule_AddObject(), , 750
PyModule_AddStringConstant(), , 750
PyModule_AddStringMacro(), ,
750
.pyo, , 194
, 195
import, 195
PyObject_GetAttrString(), , 756
PyObject_SetAttrString(), , 757
pyprocessing, , 545
PyRun_AnyFile(), , 755
PyRun_InteractiveOne(), , 755
PyRun_InterativeLoop(), , 755
PyRun_SimpleFile(), , 755
PyRun_SimpleString(), , 755
Py_SetProgramName(), , 756
PyString_AsString(), , 758
PySys_SetArgv(), , 756
Python
,
23
, 24
Python 3, 774
2to3, , 788
ascii(), , 260
commands, , 416
Ellipsis, , , 776
encode() decode(), , 781
exec(), , 784
next(), , 83, 786
nonlocal, , 135, 774
open(), , 208, 352


print(), , 784
super(), , 162, 778
xrange() range(), , 38, 71
, 772
, 772
WSGI, 673
, 786

, 787
, 770
, 772
, 778
, 774
, 78
,
784
Python 2
Python 3, 793
, 96

, 619
, 73
, 786
, 786
, 98

, 792
, 135
, 786
, 773
, 787
, 772
-, 783
Python 2, 771
, 786
, 771
, 783
, 780
, 785
, 777
Python.h, , 737
PYTHON*, , 228
.pyw, , 194
Py_XDECREF(), , 753
Py_XINCREF(), , 753
PyZipFile, , zipfile, 410
PyZipFile(), , zipfile,
410

Q
-q, , 227
qsize(), Queue,
524, 556
query,
ParseResult, 647
SplitResult, 648
QueryValueEx(), ,
winreg, 514
QueryValue(), , winreg,
514
Queue(), Manager,
537
queue, , 556
Queue(),
multiprocessing, 522
Queue, 556
q(uit), , pdb,
246
quit(),
FTP, 621
SMTP, 639
quopri, , 704
quote(), , urllib.parse,
649
quoteattr(), , xml.sax.
saxutils, 724
quoted-printable, , 704, 705
quote_from_bytes(), ,
urllib.parse, 649
quote_plus(), , urllib.
parse, 649

R
!r, , ,
108
r, ,
53
'r', , open(), 207
__radd__(), , 92
radians(), , math, 319
radix, sys.float_
info, 295
raise, , 45, 120, 125
__rand__(), , 92
RAND_egd(), , ssl, 610
randint(), , random, 323
random, , 322
random(), , random, 324
randrange(), , random,
323

827
RAND_status(), , ssl,
610
range(), , 38, 269
Python 3, 38
Rational, ,
numbers, 321
RawArray(), ,
multiprocessing, 534
RawConfigParser, ,
configparser, 421
raw_decode(),
JSONDecoder, 702
raw_input(), , 30, 211, 265, 270
Python 3, 30
sys, 296
RawIOBase, ,
444
'raw-unicode-escape', , , 222
RawValue(), ,
multiprocessing, 533
RCVALL_*, , socket,
603
__rdiv__(), , 92 read1(), BufferedReader, 440
__rdivmod__(), , 92
re,
MatchObject, 361
re, , 68, 101, 354
read(),
urlopen, 641
BufferedReader, 440
ConfigParser, 418
FileIO, 439
HTTPResponse, 627
mmap, 466
SSLSocket, 609
StreamReader, 350
TextIOWrapper, 443
ZipFile, 411
, 208, 209
read(),
os, 483
readable(),
dispatcher, 569
IOBase, 437
readall(), FileIO,
439
read_byte(),
mmap, 466
ReadError, , tarfile,
406
reader(), , csv, 681

828
readfp(),
ConfigParser, 418
readinto(),
BufferedReader, 440
readlines(),
urlopen, 641
IOBase, 438
StreamReader, 350
, 33, 208, 209
readline, , 230
readline(),
urlopen, 641
IOBase, 438
mmap, 466
StreamReader, 350
TextIOWrapper, 443
, 29, 208, 209
readline, , 270, 727
readlink(), , os, 486
read_mime_types(), ,
mimetypes, 704
ready(),
AsyncResult, 531
Real, , numbers, 321
real,
, 64
, 64
, 64
realpath(), , os.path, 498
reason,
HTTPResponse, 627
Record, , logging, 451
recv(),
Connection, 527
dispatcher, 570
, 603
recv_bytes_into(),
Connection, 527
recv_bytes(),
Connection, 527
recvfrom_info(), ,
604
recvfrom(), , 604
recv_into(), , 604
recvmsg(), ,
, 607
__reduce_ex__(), , 292
__reduce__(), , 292
reduce(), , functools,
340
ref(), , weakref, 306
ReferenceError, , 124, 276
ReferenceType, , 306


REG_*, , winreg, 512
register(),
, 183
BaseManager, 538
Poll, 573
register(),
atexit, 280
webbrowser, 676
register_adapter(), ,
sqlite, 385
register_converter(), ,
sqlite3, 385
register_dialect(), , csv,
684
register_function(),
XMLRPCServer, 656
register_instance(),
XMLRPCServer, 656
register_introspection_functions(),
XMLRPCServer, 656
register_multicall_functions(),
XMLRPCServer, 657
RegLoadKey(), , winreg,
513
release(),
Condition, 552
Lock, 548
RLock, 549
Semaphore, 549
reload(), , 196
relpath(), , os.path, 498
remove(),
, 36, 75
deque, 333
Element, 716
array, 330
, 66
remove(), , os, 487
removeChild(),
Node, 709
removedirs(), , os, 487
removeFilter(),
Handler, 457
Logger, 450
removeHandler(),
Logger, 453
remove_option(),
ConfigParser, 418
remove_section(),
ConfigParser, 419
rename(), FTP,
621
rename(), , os, 487
renames(), , os, 487


repeat(),
itertools, 345
operator, 347
timeit, 249
replace(),
date, 423
datetime, 426
time, 424
, 68, 70
'replace',
, 217
replaceChild(),
Node, 709
replace_header(),
Message, 690
report(), , dircmp,
397
report_full_closure(), ,
dircmp, 397
report_partial_closure(), ,
dircmp, 397
repr, , 726
repr(), , 31, 86, 108, 111, 230,
270
str(), 31
request,
BaseRequestHandler, 612
Request(), , urllib.
request, 642
RequestHandlerClass,
SocketServer, 614
request_queue_size,
SocketServer, 615
request_version, BaseHTTPRequestHandler, 633
reserved, ,
ZipInfo, 412
reset(),
HTMLParser, 698
IncrementalDecoder,
351
IncrementalEncoder,
351
StreamReader, 350
StreamWriter, 351
resetwarnings(), ,
warnings, 305
resize(), mmap,
466
resize(), , ctypes, 766
resolution,
date, 422
time, 424
timedelta, 427

829
resource, , 727
ResponseNotReady, , http.
client, 628
responses,
BaseHTTPRequestHandler, 633
restype, ctypes, 760
retrbinary(), FTP,
621
retrlines(), FTP,
621
return, , 133
r(eturn), ,
pdb, 246
returncode,
Popen, 506
reverse(),
array, 330
, 66
reversed(), , 270
rfile,
BaseHTTPRequestHandler, 633
StreamRequestHandler, 612
rfind(), , 68, 70
__rfloordiv__(), , 92
right_list, ,
dircmp, 397
right_only, ,
dircmp, 397
rindex(), , 68, 70
rjust(), , 70
rlcompleter, , 727
rlecode_hqx(), , binascii,
681
rledecode_hqx(), ,
binascii, 680
RLock,
multiprocessing, 534
threading, 548
RLock(), ,
Manager, 537
RLock(), , threading,
549
__rlshift__(), , 92
rmd(), FTP, 621
rmdir(), , os, 487
__rmod__(), , 92
__rmul__(), , 92
rmtree(), , shutil, 402
robotparser, , 651
robots.txt, , 651

830
rollback(),
Connection, 376
rollover(), ,
SpooledTemporaryFile, 408
__ror__(), , 92
rotate(),
deque, 333
RotatingFileHandler, ,
logging, 455
round(), , 97, 270
rounding, ,
Context, 314
rounds, sys.float_
info, 295
rowcount, Cursor,
378
row_factory,
Connection, 389
rpartition(), , 68, 70
__rpow__(), , 92
rshift(), , operator, 346
__rshift__(), , 92
__rrshift__(), , 92
rsplit(), , 68, 70
rstrip(), , 70
__rsub__(), , 92
__rtruediv__(), , 92
run, , pdb,
246
run(),
Process, 520
Thread, 546
run(),
pdb, 242
cProfile, 247
profile, 247
runcall(), , pdb, 242
runeval(), , pdb, 242
RuntimeError, , 124, 276
, 153
RuntimeWarning, , 278,
303
__rxor__(), , 92

S
-s, , 227
!s, , ,
108
SafeConfigParser, ,
configparser, 421
safe_substitute(),
Template, 365
samefile(), , os.path, 498


same_files, ,
dircmp, 398
sameopenfile(), ,
os.path, 498
samestat(), , os.path,
498
sample(), , random, 323
SaveKey(), , winreg, 514
SAX,
XML, 706
, 723
sched, , 727, 729
scheme,
ParseResult, 647
SplitResult, 648
search(), Regex,
359
search(), , re, 358
sections(),
ConfigParser, 419
seed(), , random, 322
seek(),
IOBase, 438
mmap, 466
, 208, 210
seekable(),
IOBase, 438
select, , 519, 572
, 500
select(),
asyncore, 568
select, 573
, 583
__self__,
, 78
, 78
self, , 44, 160
Semaphore,
multiprocessing, 534, 537
threading, 549
, 550
Semaphore(),
Manager, 537
Semaphore(), ,
threading, 549
send(),
, 42, 83, 144
Connection, 527
dispatcher, 570
HTTPConnection,
626
, 604
sendall(), , 604


send_bytes(),
Connection, 528
sendcmd(), FTP,
622
send_error(),
BaseHTTPRequestHandler, 633
send_header(),
BaseHTTPRequestHandler, 634
sendmail(), SMTP,
639
sendmsg(), ,
, 607
send_response(),
BaseHTTPRequestHandler, 634
send_signal(),
Popen, 505
sendto(), , 605
sep,
print(), 213
sep, , os, 484
Sequence, ,
337
serve_forever(),
BaseManager, 539
SocketServer, 614
server,
BaseRequestHandler, 612
server_address,
SocketServer, 614
ServerProxy, ,
xmlrpc.client, 652
ServerProxy(), ,
xmlrpc.client, 652
server_version,
BaseHTTPRequestHandler,
632
HTTPRequestHandler, 631
__set__(), , 89, 170
Set, , 337
set(),
ConfigParser, 419
Element, 716
Event, 550
Morsel, 637
set(), , 35, 111, 270
set, , 63, 73, 109
__setattr__(), , 88, 176
__slots__, 178
setattr(), , 270
set_authorizer(),
Connection, 387
setblocking(), ,
605

831
set_boundary(),
Message, 690
set_charset(),
Message, 690
setcheckinterval(), , sys,
299
setcontext(), , decimal,
315
set_conversion_mode(), , ctypes, 766
setDaemon(),
Thread, 546
set_debug(), , gc, 283
setdefaultencoding(), ,
sys, 299
setdefault(),
, 72
defaultdict,
334
set_defaults(),
optparse, 206
PtionParser, 473
setdefaulttimeout(), ,
socket, 594
set_default_type(), Message, 690
setdlopenflags(), , sys,
299
setDocumentLocator(),
ContentHandler, 721
setegid(), , os, 477
set_errno(), , ctypes, 766
seteuid(), , os, 477
set_executable(), ,
multiprocessing, 544
setFormatter(),
Handler, 458
setgid(), , os, 477
setgroups(), , os, 477
setinputsize(),
Cursor, 377
__setitem__(), , 89
, 91
setitem(),
operator, 347
setitimer(), , signal, 500
set_last_error(), ,
ctypes, 766
setLevel(),
Handler, 456
Logger, 450
setmode(), , msvcrt, 468

832
setName(),
Thread, 546
setoutputsize(),
Cursor, 378
set_param(),
Message, 691
setpassword(), ,
ZipFile, 411
set_pasv(), FTP,
622
set_payload(),
Message, 691
setpgid(), , os, 477
setpgrp(), , os, 477
setprofile(),
sys, 300
threading, 555
set_progress_handler(),
Connection, 389
set_proxy(),
Request, 643
setrecursionlimit(), ,
sys, 153, 300
setregid(), , os, 477
setreuid(), , os, 477
_setroot(),
ElementTree, 712
set_server_documentation(), XMLRPCServer, 657
set_server_name(),
XMLRPCServer, 657
set_server_title(),
XMLRPCServer, 657
setsid(), , os, 477
setslice(), , operator, 347
setsockopt(), ,
605
__setstate__(), , 292
, 281
pickle, 224
setstate(), , random, 323
set_terminator(),
async_chat, 566
set_threshold(), , gc, 283
settimeout(),
, 605
settrace(),
pdb, 242
sys , 300
threading, 555
set_type(),
Message, 691
setuid(), , os, 478


set_unixfrom(),
Message, 691
setUp(),
TestCase, 240
setup(),
BaseRequestHandler, 612
setup(), , distutils, 201,
739
setup.py,
setuptools, 203
install, 203
C, 739
, 201
setuptools, , 194, 203
set_usage(),
PtionParser, 473
SetValueEx(), , winreg,
515
SetValue(), , winreg, 514
set_wakeup_fd(), ,
signal, 500
sha1(), , hashlib, 694
sha224(), , hashlib, 694
sha256(), , hashlib, 694
sha384(), , hashlib, 694
sha512(), , hashlib, 694
Shelf, , shelve, 394
shelve, , 223, 393
dbhash, 394
shlex, , 729
showwarning(), ,
warnings, 304
shuffle(), , random, 323
shutdown(),
SocketServer, 614
BaseManager, 539
, 605
shutdown(), , logging,
460
shutil, , 400
SIG*, , 501
SIGHUP, , UNIX, 234
siginterrupt(), , signal,
500
signal, , 499
signal(), , signal, 501
SIGTERM, , UNIX, 234
SimpleCookie, , http.
cookies, 636
SimpleCookie(), , http.
cookies, 636
SimpleHandler(), ,
wsgiref.handlers, 674


SimpleHTTPRequestHandler, , http.server, 631
SimpleHTTPRequestHandler(), ,
http.server , 631
SimpleHTTPServer, , 630
simple_producer(), ,
asynchat, 566
SimpleXMLRPCServer, ,
xmlrpc.server, 655
SimpleXMLRPCServer, , 655
SimpleXMLRPCServer(), , xmlrpc.server, 655
sin(), , math, 320
sinh(), , math, 320
site, , 231, 298
sitecustomize, , 232
size,
Struct , 367
TarInfo, 405
size(),
FTP, 622
mmap, 466
Sized, , 337
sizeof(), , ctypes, 766
skippedEntity(),
ContentHandler, 722
sleep(), , time, 509
slice, , 80
slice(), , 84, 271
__slots__, , 254
, 177
__dict__ , 79
SMTP,
, 640
SMTP(), , smtplib, 639
smtpd, , 728
SMTPHandler, , logging,
455
smtplib, , 639
sndhdr, , 729
sniff(), Sniffer,
684
Sniffer(), , csv, 684
SO_*, , socket, 598
SOCK_*, , socket, 587
socket, SocketServer,
614
socket, , 586
socket(), , socket, 594
SocketHandler, , logging,
455
socketpair(), , socket,
596

833
SocketServer, , 611
Python 3, 611
socket_type,
SocketServer, 615
SocketType, , socket , 596
softspace, , 210
sort(), , 66
sorted(), , 271
span(),
MatchObject, 360
spawnl(), , os, 492
spawnle(), , os, 492
spawnlp(), , os, 492
spawnlpe(), , os, 492
spawnv(), , os, 492
spawnve(), , os, 492
spawnvp(), , os, 492
spawnvpe(), , os, 492
split(),
Regex, 359
, 35, 70
, 68
split(),
os.path, 499
re, 358
splitdrive(), , os.path,
499
splitext(), , os.path, 499
splitlines(), , 70
splitunc(), , os.path, 499
SpooledTemporaryFile(), , tempfile, 408
sprintf(), , , 103
spwd, , 727
SQL-
SQL, 379
, 389
,
152
, 379
sqlite3, , 383
SQLite, , 383
sqrt(), Decimal,
311
sqrt(), , math, 320
SSL, , , 611
ssl, , 608
st_*, , stat, 487
stack_size(), , threading,
555
stack(), , inspect, 288
standard_b64decode(), ,
base64, 678
standard_b64encode(), ,
base64, 678

834
StandardError, , 123
starmap(), , itertools,
345
start, , 84
start(),
BaseManager, 539
MatchObject, 360
Process, 520
Thread, 545
Timer, 548
TreeBuilder, 717
startDocument(),
ContentHandler, 722
startElementNS(),
ContentHandler, 722
startElement(),
ContentHandler, 722
startfile(), , os, 493
startswith(), , 71
stat, , 727
stat(),
os, 487
stat_float_times(), , os,
488
@staticmethod, , 44, 76, 165,
271
staticmethod(), , 271
status,
HTTPResponse, 627
statvfs(), , os, 488
__stderr__, , sys,
212, 296
stderr, Popen, 506
stderr, , sys, 205,
211, 296
stdin, Popen,
506
__stdin__, , sys, 212,
296
stdin, , sys, 30, 211,
296
__stdout__, , sys,
212, 296
stdout, , sys, 30, 211,
296
stdout, Popen,
506
step, , 84
s(tep) , , pdb,
246
stop, , 84
StopIteration, , 91, 123, 267,
276
, 142


, 144
storbinary(), FTP,
622
storlines(), FTP,
622
__str__(), , 86
str, , 63
str(), , 31, 86, 108, 111, 271
repr(), 31
print, 212
StreamError, ,
tarfile, 406
StreamHandler, , logging,
456
StreamReader, , codecs, 350
streamreader(),
CodecInfo, 350
StreamRequestHandler, ,
SocketServer, 612
StreamWriter, , codecs, 350
streamwriter(),
CodecInfo, 350
strerror(), , os, 478
strftime(),
date, 423
time, 424
strftime(), , time, 509
'strict',
, 217
string,
MatchObject, 361
string, , 214, 362
string_at(), , ctypes, 766
StringIO, , io, 443
stringprep, , 727
strip(), , 71
strptime(),
datetime, 425
strptime(), , time, 429,
510
Struct, , struct, 366
struct, , 366
Structure, , ctypes, 762
__sub__(), , 92, 179
sub(), Regex, 359
sub(),
operator, 346
re, 358
__subclasscheck__(), , 88, 182
subdirs, ,
dircmp, 398
SubElement(), , xml.
etree.ElementTree, 714

835


subn(), Regex,
359
subn(), , re, 359
subprocess, , 503
substitute(), , 214
Template, 365
sucessful(),
AsyncResult, 532
sum(), , 65, 99, 271
decimal, 101
, 65
sunau, , 729
super(), , 271
Python 3, 162, 778
, 162
supports_unicode_filenames, , os.path, 499
swapcase(), , 71
SWIG, , 734
, 768
, 768
symbol, , 726
symlink(), , os, 488
symmetric_difference(), ,
74
symmetric_difference_update(),
, 75
SyncManager, ,
multiprocessing, 536
SyntaxError, , 124, 276
, 131
SyntaxWarning, , 278,
303
sys.argv, , 33, 205, 228
sysconf(), , os, 495
sys.displayhook, , 230
sys.exec_prefix, , 231
sys.exit(), , 277
sys.getdefaultencoding(), , 272
syslog, , 727
SysLogHandler, , logging,
456
sys.modules, , 191, 196
sys.path, , 194, 231
sys.prefix, , 231
sys.ps1, , 230
sys.ps2, , 230
sys.__stderr__, , 212
sys.stderr, , 205, 211
sys.__stdin__, , 212
sys.stdin, , 211
sys.__stdout__, , 212

sys.stdout, , 211
system(command), , os,
493
SystemError, , 124, 277
SystemExit, , 25, 123, 205,
234, 277
system.listMethods(),
ServerProxy, 653
SystemRandom, , random,
325
sys_version,
BaseHTTPRequestHandler, 632
sys, , 33, 59, 292

T
-t, , 49, 227
-tt, , 49, 227
tabcheck, sys.flags,
294
TabError, , 49, 124, 277
tabnanny, , 726
tag, Element,
715
tagName,
Element, 710
tail, Element,
715
tail,
, 40
takewhile(), , itertools,
345
tan(), , math, 320
tanh(), , math, 320
TarError, , tarfile,
406
TarFile, , tarfile, 403
tarfile, , 402
TarInfo, , tarfile, 405
task_done(),
JoinableQueue, 524
Queue, 557
tb_*, , 82
tb_lineno(), , traceback,
301
tbreak, , pdb,
246
tcgetpgrp(), , os, 483
TCP_*, , socket, 602
TCP, , 561
, 562
TCP , , 563

836
TCPServer, , SocketServer,
613
tcsetpgrp(), , os, 483
tearDown(), TestCase, 240
tee(), , itertools, 345
tell(),
IOBase, 438
mmap, 466
, 208, 210
telnetlib, , 728
tempdir, , tempfile,
408
tempfile, , 407
Template, , string, 364
Template() , string, 214
template, , tempfile,
408
TemporaryFile(), ,
tempfile, 408
terminate(),
Pool, 531
Popen, 506
Process, 520
termios, , 727
test, , 726
test(), , cgi, 667
TestCase, , unittest, 240
testmod(), , doctest, 237
testzip(), , ZipFile,
411
text, Element,
715
Text, , xml.dom.minidom,
711
text_factory,
Connection, 389
TextIOBase,
, 444
TextIOBase, , io, 442
TextIOWrapper, , io, 442
textwrap, , 727
t.fail(),
TestCase, 241
this, , self ,
160
thread, Record,
451
Thread, , threading, 545
threading, , 534, 545
, 548
ThreadingMixIn, ,
SocketServer, 616
ThreadingTCPServer, ,
SocketServer, 617


ThreadingUDPServer, ,
SocketServer, 617
threadName,
Record, 451
threadsafety, , , 382
throw(), , 83, 145
time, , datetime, 423
time(), datetime,
426
time, , 248, 507
, 508

, 511
time(),
,
380
time, 248, 510
timedelta, , datetime, 426
TimedRotatingFileHandler, , logging, 456
TimeFromTicks(),
, 381
timeit, , 249
timeit(), , timeit, 249
timeout, SocketServer,
615
timeout, , socket, 607
Timer, , threading, 547
Timer(), , threading, 547
times(), , os, 493
TimestampFromTicks(), , 381
Timestamp(), , 381
timetuple(), date,
423
timetz(),
datetime, 426
timezone, , time, 507
TIPC_*, , socket, 590
TIPC, , 586
, 590
title(), , 71
Tkinter, , 729
today(), date, 422
tofile(), , array, 330
token, , 726
tokenize, , 726
tolist(), , array, 330
toordinal(), date,
423
toprettyxml(),
Node, 711

837


tostring(), , array,
330
tostring(), , xml.etree.
ElementTree, 717
total_changes,
Connection, 389
tounicode(), , array,
330
toxml(), Node,
711
trace(), , inspect, 288
__traceback__,
Exception, Python 3, 274
traceback, , 300
tracebacklimit, , sys,
296
TracebackType, , 80, 302
transfercmd(),
FTP , 622
translate(), , 68, 71
traps, , Context,
314
TreeBuilder, , xml.etree.
ElementTree, 716
TreeBuilder(), , xml.
etree.ElementTree, 716
triangular(), , random,
325
True, , 50, 64
__truediv__(), , 92
truediv(), , operator,
346
trunc(), , math, 320
truncate(),
IOBase, 438
, 209
truth(), , operator, 347
try, , 45, 120
ttyname(), , os, 483
tty, , 727
tuple, , 63
tuple(), , 111, 272
Twisted, , 519, 582
type,
FieldStorage, 665
TarInfo , 406
, 606
type(), , 184
type, , 75
type(), , 58, 272
, 126
typecode, , array,
329
TypeError, , 77, 124, 277

, 132
,
164
type_options,
FieldStorage, 665
types, , 301
tzinfo, , datetime, 428
tzname(),
time, 424
tzinfo, 429
tzname, , time, 508
tzset(), , time, 510

U
-u, , 227
u, , 52
'U', , open(), 207
UDP, , 561
, , 563
UDPServer, , SocketServer,
613
uid, , TarInfo,
406
umask(), , os, 478
unalias, , pdb,
246
uname, ,
TarInfo, 406
uname(), , os, 478
UnboundLocalError, , 124,
136, 277
unconsumed_tail, ,
decompressobj, 414
unescape(), , xml.sax.
saxutils, 724
ungetch(), , msvcrt, 468
ungetwch(), , msvcrt,
468
unichr(), , 112, 272
unicode, sys.flags,
294
unicode, , 63
unicode(), , 272
unicodedata, , 222, 369
UnicodeDecodeError, , 124,
277
UnicodeEncodeError, , 124,
277
UnicodeError, , 124, 217,
221, 277
'unicode-escape', , ,
222

838
UnicodeTranslateError, ,
124, 277
uniform(), , random, 324
UnimplementedFileMode, ,
http.client, 628
Union, , ctypes, 762
union(), , 74
unittest, , 239, 240
, 242
, 239
UNIX,
, 507
UnixDatagramServer, ,
SocketServer, 613
UnixStreamServer, ,
SocketServer, 613
UnknownHandler, , urllib.
request, 644
UnknownProtocol, ,
http.client, 628
UnknownTransferEncoding, , http.client, 628
unlink(), , os, 488
unpack(), Struct,
367
unpack(), , struct, 366
unpack_from(), , struct,
366
Unpickler, , pickle, 291
unquote(), , urllib.parse,
649
unquote_plus(), , urllib.
parse, 649
unquote_to_bytes(), ,
urllib.parse, 649
unregister(), Poll,
574
unregister_dialect(), ,
csv, 684
unsetenv(), , os, 478
until, , pdb,
246
unused_data, ,
decompressobj, 414
unwrap(),
SSLSocket, 610
u(p) , , pdb,
246
update(),
, 36, 75
HMAC, 695
, 694
, 72


update_wrapper(), ,
functools, 341
upper(), , 71
uppercase, , string,
362
urandom(), , os, 496
urldefrag(), , urllib.
parse, 648
urlencode(), , urllib.
parse, 650
URLError, , 642
urllib.error, 651
urljoin(), , urllib.parse,
648
urllib2, , 640
urllib.error, , 650
urllib.parse, , 646
urllib.request, , 640
urllib.response, , 646
urllib.robotparser, , 651
urllib, , 640
urlopen(), , urllib.
request, 641
urlparse, , 646
urlsafe_b64decode(), ,
base64, 679
urlsafe_b64encode(), ,
base64, 679
urlsplit(), , urllib.parse,
647
urlunparse(), ,
urllib.parse, 647
urlunsplit(), ,
urllib.parse, 648
user, , 726
User-Agent, HTTP-,
, 643
username,
SplitResult, 648
UserWarning, , 278, 303
utcfromtimestamp(),
datetime, 425
utcnow(),
datetime, 425
utcoffset(),
time, 424
tzinfo, 429
utctimetuple(),
datetime, 426
'utf-8', , , 221
'utf-16', , , 222
'utf-16-be', , , 222
'utf-16-le', , , 222


utime(), , os, 488
uu, , 728

V
-v, , 227
validator(), , wsgiref.
validate, 675
value,
FieldStorage, 665
Morsel, 637
value(), Manager,
537
value(), ,
multiprocessing, 533
ValueError, , 66, 124, 277
valuerefs(),
WeakValueDictionary, 307
values(),
Message, 686
, 72
ValuesView,
, 338
vars(), , 105, 272
verbose, sys.flags,
294
verify_request(),
SocketServer, 616
version,
HTTPResponse, 627
version, , sys, 296
version_info, , sys,
296
vformat(),
Formatter, 363
volume, ,
ZipInfo, 412
vonmisesvariate(), ,
random, 325

W
W, , 305
'w', , open(), 207
wait(),
AsyncResult, 532
Condition, 552
Event, 550
Popen, 506
wait(), , os, 493
wait3(), , os, 494
wait4(), , os, 494
waitpid(), , os, 493
walk(), Message,
688

839
walk(), , os, 489
warn(), , warnings, 278,
304
warn_explicit(), ,
warnings, 304
warning(),
Logger, 448
Warning, , 278, 303
warnings, , 278, 303
warnoptions, , sys,
296
WatchedFileHandler, ,
logging, 456
wave, , 729
WCOREDUMP(), , os,
494
WeakKeyDictionary, ,
weakref, 307
weakref, , 175, 305
WeakValueDictionary, ,
weakref, 307
webbrowser, , 676
weekday(), date,
423
weibullvariate(), ,
random, 325
WEXITSTATUS(), , os,
494
wfile,
BaseHTTPRequestHandler, 633

StreamRequestHandler, 612
w(here) , ,
pdb, 246
whichdb, , 392
whichdb(), , whichdb,
392
while, , 26, 117
whitespace, , string,
362
WichmannHill, , random,
325
WIFCONTINUED(), ,
os, 494
WIFEXITED(), , os, 494
WIFSIGNALED(), , os,
494
WIFSTOPPED(), , os,
494
WinDLL(), , ctypes, 759
Windows, , 415
, 511
, 433

840

multiprocessing, 545
WindowsError, , 277
winreg, , 511
winsound, , 729
winver, , sys, 297
with, , 45, 94
, 126, 553
, 45
@wraps(), , functools,
156
wraps(), , functools, 341
wrap_socket(), , ssl, 608
writable(),
dispatcher, 569
IOBase, 438
write(),
BufferedWriter, 441
ConfigParser, 419
ElementTree, 713
FileIO, 439
mmap, 466
SSLSocket, 609
StreamWriter, 351
TextIOWrapper, 443
ZipFile, 411
, 30, 208, 210
write(), , os, 484
write_byte(),
mmap, 466
writelines(),
IOBase, 438
StreamWriter, 351
, 208, 210
writepy(), ,
ZipFile, 411
writer(), , csv, 682
writerow(),
csv, 683
DictWriter, 683
writerows(),
csv, 683
DictWriter, 683
writestr(), ,
ZipFile, 412
writexml(), Node,
711
WSGI
, 673
,
675
, 672
, 672
, 675


wsgiref, , 673
wsgiref.handlers, , 674
wsgiref.simple_server, , 673
wsgiref.validate, , 675
WSGI (Web Server Gateway Interface
-), 671
wsgi.*, , 672
WSTOPSIG(), , os, 494
wstring_at(), , ctypes,
766
WTERMSIG(), , os, 494

X
-x, , 227
xdrlib, , 728
XML, , 707
xml, , 706
XML(), , xml.etree.
ElementTree, 714
'xmlcharrefreplace',
, 217
xml.dom.minidom, , 708
xml.etree.ElementTree, , 712
XMLGenerator(), , xml.
sax.saxutils, 724
XMLID(), , xml.etree.
ElementTree, 714
xmlrpc.client, , 652
xmlrpclib, , 652
xmlrpc.server, , 655
xmlrpc, , 651
XML-RPC, , 651

, 659
, 658
XML-RPC-

, 617
xml.sax.saxutils, , 723
xml.sax, , 720
__xor__(), , 92
xor(), , operator, 346
xrange() , 71
xrange, , 63
xrange(), , 38, 273

Y
yield, , 41, 143
yield, , 83, 141
, 40
, 128
, 41

841

Z
ZeroDivisionError, , 123, 277
zfill(), , 71
.zip,
, 194
zip(), , 119, 273
, 62
zipfile, , 409
ZipFile(), , zipfile, 409
zipimport, , 409, 726
ZipInfo, , zipfile, 410, 412
ZipInfo(), , zipfile, 410
zlib, , 413

, 97
, 59, 182,
326
-, 444
, 183
, 336
, 183
, 327
, 183
,
183
, 321
, , 587

URL, 646
, 510
, 469
, 685
, 429
, 152
, , 51
,
110
, 33
, 130
, 131
, 131
, 39
, 25

, 582
, 499
-, 519

, 36, 71
, , 251

, , 158, 167
, 89, 170
, 161
dircmp, 397

__slots__, 177
, 106
__init__(), 159
, 155
, 141
, 171
, 645


, 222
, 369
, 43, 160
, 273

CGI-, 669
DBM, 391
, 54
, 67
,
216
, 210
,
103
, ,
53

, 379
marshal, 289
pickle, 225
, 97

, 436, 467
, 46
, 126
, 517
sqlite3, 383
,
Python, 676
, , 215

842

-
, 572
, 442
, 23
-, 660
-
,
634
, 632
-, 669
, 553
, 516
, 548
, 232
, 32
, 135, 138
, 138
, 297
, , 52
, 39
, 50
, 407
, 407
,
, 584
,
JavaScript, , 661

Python
C, 756

Python C, 758
Python
C, 733, 754
, 273
, 45
, 78
, 278
, 63
, 78
, 259

, 509
, 30

CGI-, 666
FTP, 622
HTTP, 628


, 196
, 23
, 651
multiprocessing, 529
, 39, 130
Python C, 756
, , 75

, 189
, 156
, 416
popen(), , os, 491
system(), , os, 493
subprocess, 503
, 158, 185
__init__.py, 198
, 96
-, 150
, 150
, 150
, 151
,
150

, 111
, , 532
, 113
, 113
, 555

, 40, 83, 141



-, 83
break , 142
, 559
, 214
, 40
, 146
, 559
, 153
, 40
,
147
, Python 3, 772
, Python 3, 772

843


, 33, 148
,
151
, 149
-,
150
, 149
, 149
SQL, 152
awk, 152
, 516
,
518, 555
, 753
, 134
eval(), 156
, 82
, 61
, 251, 517
(greenlets), 559

CSV, ,
34
, 50
, distutils, 202
, , 366

-, 440
, 207
, 50
, 332
, 587
, 151
, 44, 55, 139, 256
, 155
, 188
,
341
, 140
, 141
, 140
, 154
, 154

, 93
, 93
, 96

, 93
, 96
, 521
, 89, 170
, 89
, 187
, 434
, 478
,
64
, 250
, , 190
, , 136
, 26, 165
,
, 165
,
select(), 574
, 64
, 458
, 36

, 97
, 77, 88
, 88
, , 516
, 309

, 445

, , 52
, 554
, 298
,
280
, 194, 195
, 68
, 136
, 137
, 139
, 139
,
, 379

844
, 503
, 506
, 230
Python, 25
, 50
(green threads), 559

, 122
, ,
476
, 49
, 50
, 61
Python 3,
772

, 50
, 50
, 58
, 554

, logging, 451
, 184

, 39

, 190
, 194
,
475
,
210
, 36

, 71
, 57
, 249
, , 248
, 61

Python 3, 786

, 499
, 398
, 132
, 132
sort(), 66
, 39


, 334
Python, 336

, 191
, 787
, 205
, 30
, 65
, 171
, 95
, 229
, 229
, 49
, 86, 230
, 230
, 227
, 500
,
213
, 375
, 382
, ctypes, 760
, ,
171
, 44, 120
finally, , 122
, 273
, 273
,
255
,
255
, 46
, 122
, 124
, 45
Python 3,
777
, 125
,
122
, 121
, 120
, 275
, 274
, 146

array, 330

, Python 3,
786
Python 3, 784
, 29, 37, 91, 117, 256
, 118
, 38
, 73
,
118
, 38
, 38

, 37
, 101
, 38
, 38
, 119
, 91, 117
, 65

, , 52
, 81

, 111

, 407
, 401
, 489
, 396, 397
, 402
Python (Python Package
Index, PyPI), 203
, 43
__mro__, , 163
super(), , 162
, 182
, 326
self, 160
__slots__, 177
, 89
, 160
, 253
, 188
, 170
,
, 190
, 184
, 171
, 275

845
, 78
, 158, 160
__del__() , 282
__init__(), 159
, 165, 261
, 43, 160
__init__(), 161
, 162
, 43
, 253
C++
Java, 160
, 84
, 160
, 141
, 164
, 161
, 167
, 169
, 44, 85, 159
, 84
, 186
, 44, 165, 271
, 75
,
88
, 172
, 171
, 50

, 36
, 71

, Python 3, 772
, 216
, 56, 353
, , 53
,
, 430
, , 543
, , 57
, 333
, 243
, 93, 109
, 49
, , 236
, 195
, 51, 64
, 98
, 40
, 146

846

, 34
, 51
, 32
, 31
, 60
, 54
, 57
, 694

, 61
, 60
, 60
, 401
, 61
, 61, 99
, 99
, 72
, logging,
445
, 33
, 34
, 34
, 103
, 36, 109
, 65
, 34
, 34
Python 3, 773
, 118
, 34
, 34
, 335
, 73
, 102
, 694
, , 517
, 34
, 376
, 341

, 135
, Python 3, 772
, 112
, 113
, 50, 63
, 98
, 555


, 134
eval(), 156
, 82


(Byte-Order Markers, BOM)
, 219
, 91
, 98
, 91
, 94, 126, 339
, 339
, 553
,
339

, 140
, 232
, 231
, 184, 256
, 187
__prepare__(), 779
, 186
, 76, 158
@classmethod, , 76
@staticmethod, , 76
, 78
super(), 162
, 168
, 76, 165
, 166
, 77
, 43
, 159
, 77
, 77, 169
, 76, 165
, 76
, 76, 159

multiprocessing, 534
threading, 548
, 559
, 516
Python, 518
, 518
, 559
, 133
, 42, 148, 559
, 516

847



, 518
, 518
, 518

multiprocessing, 544
,
61
, 32
, 518
, 35, 73
, 36
, 109
, 36
, 36
, 36
, 36
, 36
, 162
, 116
, 27
, 46, 80, 189
, 80
, 196

, 134
, 190
, 80
,
, 190
, 195
.pyc, 196
, 190
, 195
, 196
, 194
, 75
, 733
, 753
, 752
, 753

distutils, 739
ctypes, 759
, 750

C Python, 747

Python C, 740
, 736
, 737
-, 736


unittest, 239
, 239

Python 3, 789
-,
572

, , 174
, 587
, 43, 160
__init__(), , 161
isinstance(), , 59
issubclass(), , 180
__mro__, , 163
__slots__, , 178
, 182
, 161
, 125
, 162
, 162
, 184
, 162
,
164
, 248
, 247
,

-, 229
,
460
, 51

, 103
, 34

, 57
, 71
, Python 3, 774
,
39, 131

None, 63
, 51
, 77
Python 3, 78
,
49

848
-, 438
, 223

, 39
, 519
, 519
, 516
, 527
, , 750
, , 147
, 501
close() , 143
, logging,
453
,
218
,
368
, , 51
, |, 36
, 98
, 43, 57
, 57
, 83
, 83
, 58
, 184
, 81
, 82
, , 57
, 61
, 87
, 61
, 249, 299
, 463
, 463
, 61
, 117
, 59
, 282
, 176
, 306

marshal, 288
pickle, 289
, 305
, 223
, 81
, 81
, 58, 786
, 57
, 57
, 83
, 84
c , 82
, 82

, , 153
, 177
,
, 300
Python 2
Python 3, 793
, 97
Python 3, 98
, 96

, 80

, 34
[], 72
, 36
, 32
, 30
, 87

, 99
, 96
, 209
, 125
, 39

__slots__, , 177, 254


, 252
, 252
, 250
, 256
, 249
, 255
, 256
, 256
, 248

849


, 255
, 533
, 256
, 252
, 249
, 253
, 251
Python 3, 619

range(), 38
,
282

, 207

CGI-, 670
, 246
, 247
, 283
, 244
, 128
, 242
, 243
, 247
, 138
, threading, 547

multiprocessing, 530
, , 230
, 71
,
72
, 89
, 100
, 48
, 49

, 333
, 525

, 557
, 537
, 519
multiprocessing, 522
, 341, 342

, 31

, 197

, 34
, 57
, 118
, 205
Python 3, 786
optparse,
469
, 226

XML-, , 717
URL, 646
HTML, 696
XML, 706

ElementTree, 719
, 205
, 685
robots.txt, 651
CSV, 681
, , 73
, , 52
, , 52
, 178
, 179
, 180
, 178
, 133
, 25
, 135
, 134
, 61
, 158

, 159
, 134
, 205, 207, 475
CGI-, 663
Python 3, 786
WSGI, 671
,
228

, 497

850
, 49
, 551
, 192
, 118
, 496

Python 2 Python 3, 788
, 792
, 97
, &, 36

, 122
, 121
Python 2
Python 3, 24
, 133
, 61
, 99
, 72
, 196
, 120

, 212
,
200
, 161
, 105,
213
, , 68
, ,
118
, 59, 173
, 752
, 250
del, 60
, 60
, 191
, 69
, 165
, help(), 47
,
476

, 562

, 562
, 88
, decimal,
311


, TypeError, 164
, , 179
, 65
,
106
, 101
, 99
, 99
in, 99
, 99
, 100
, 99
, 65
, 99
, 101
, 89
, 102
, 550
decimal,
316

, 516
, 546
, 616
, 555
, 555
close() , 143
, 753
, 548
, 503
, 517
, 551
, 555
, 550
, 554
, 550
, 582
, 555
, 134
, 160

, 149
, 134
self , 160
import, 192
, 135
, 118


,
275

-, 293
, 421
, Python 3, 784
, 278
, 278

Python
C, 758
, 35
, 70
, 37
, 31
, 111
, 62
C
Python, 747
Python
C, 740
, , 180
, WSGI, 671

, 502
, 282

(Uniform Access Principle), 168
, 554
, 509
, 500
, 249

, 176
, 59
, 135
, 32

, 564

, 193
, 99

, 108
, 497

,
59
, 186

851
-, 180
-, 562
TCP, , 564
UDP, 607
-, 562
TCP, , 562
UDP, 607
, 579
SocketServer, 612

-, 150
logging, 462
, 59
-
, 442
-,
, 42
, 161
- multiprocessing, 535, 538
-, 646
, 587

import, 46, 189
, 158, 160
, 134

(Secure Sockets Layer, SSL), 608
, 94
, 247
, 248
, , 248,
508

,
526
, 521
, 506
, 520
, 516
, 491
, 520
, 530
, 218
,
368
, 530
, , 52
, 32

852
, 63
, 463
, 37
, 49
, 194
, 228
, 194
site, 231
.zip, 194

, 421
, 58
, 35, 68, 70
, 475

, 537
multiprocessing, 533

ctypes,
759
,
multiprocessing, 533
,
multiprocessing, 533
, , 555
, -, 36

, 561
, 564
, 592
, , 568
, 583

__mro__, , 163
, 164

, 366
, 34
, 99

multiprocessing, 545
, 200
C, 733
, 556

distutils, 739

ctypes, 766

SWIG, 768


.egg, 194

, 102
, Python 3, 773
, 102
, 66

, 68
, 65
, 317
,
, 103

, 354
re, 354
, 354
, 548
, , 228

open(), 207
, , 230
, , 382
,
489
, 153
, 153
, 141, 154
, 153
,
Python, 375
, Python 3,
787
, 160
, 455

, , 193
, 59, 281
__del__(), , , 282
, 60
, 281
, 174
, 234
, 137
, 158, 167
, 167



(Uniform Access Principle), 168
, 222
, 77, 169
, 176
, , 586
, 395, 400
c, 499
, 501

, 526
-
, 106
, , 497
, 53
, UNIX Windows, 207
, 54
- print(),
213

, 52
, 52
, 69
, 49
, ^, 36
,
503
, , 354
,
518
-, Python 3, 783
, , 248
, 430
, 165
, 175, 305
, 36, 71

, 253
, 36
, 72
, 36
, 36
, 72, 108
, 36
__hash__(), 87

, 37

853
, 38
, 105
, 72
, 109
Python 3,
784
, 108
,
37
, 72
, 72
, 37
, 108
, 37
, 536
, 37
dict(),
262
, 113
, 37, 72, 108
, ,
62
c
multiprocessing, 541

, 202
Windows, 202
, 201
, 363
, 159
, 562
, 596
, 587
select(),
573
, 586
, 587
, 113
, 205

, 685
, 693
, 689
, 41, 143
, 148, 559

, 583
next(), 144
, 519
, 574

854
, 144
, 148
, 148
, 577
, 146
, 145
, 41
, 559

select(), 574
, 153
, 574
, , 88
, , 51

, 689
, 693
c , 223
, 43, 84
, 91
, 27
, 509
%, 104
-, 106
, 106
format(), 107
, 32, 66
, 32
, 34
array,
330
deque, 252
, 32, 67
, 148
, 32, 66
, 67
, 32
, 38
, 65
, 32
insert(), 252
, 32
,
67
, 67
,
102
, 32
, 102
, 32


, 536
, 32
, 67
,
332
, 102
, 102
, 67, 102
UNIX, 501
, 58
, 98
Python 3, 786
, 113
, 102
, 65
, 65, 83
xrange, 71
, 91
, 90
, 102
, 102

, 198
-, 30,
211
, 44, 76, 165
, 166
Python 3, 771
, 251
, 30, 67
, 30
, 68
, 31
,
103
, 38
, 36
, 65
format(), 105
, 68
, 70
, 68
,
70
, 70
, 35
, 68
, 354
, 69
, 103

855


, 363
, 103

, 70
, 68


XML, 723

HTML, 666

WSGI, 672
Python 3,
783
,
, 442
Python 3, 780

, 213
, 47, 55, 76,
154
__doc__, 55
, 737
, 141, 154
XML-RPC, 654
, 55
doctest, 236
, 55
, 237
-OO, 195
,
NULL, UTF-8, 221

CGI-, 668
, 67, 215
, 372
, 564
, 216
, 373
,
103
, 51
, , 53
, 54
, 54

, 51
, 52
, 53
, 48, 116


, 334
, 37
, 34
, 160
, 53, 67, 221
,
, 228
, 53
, 354
, 53
, 53

, 36
, , 52
(tasklets), 559
, Python 3, 780
,
207
, , 508
, , 151

doctest, , 237
, 238
, 239
, 237
, Python 3, 780
, 111

bool, 63
complex, 63
dict, 63
float, 63
frozenset, 63, 73
int, 63
list, 63
long, 63
None, 63
set, 63, 73
str, 63
tuple, 63
type, 75
unicode, 63
xrange, 63
, 63, 259
, 73
, 75
, 57
, 71

856
, 587

, 244
, 64
, 44, 82

assert
-O, 195
, 297
, 402
, 66, 102

-OO, 195
, 487
, 66
, 37, 72
, 366
, 172
, 173
, 283
, 60, 281
, 172
, 489
,
multiprocessing, 535
, 114
, 117
, 28
, 202
,
203

-, 29
, 210
, 29
, 210
, 467
, 407
CSV, , 681
, 30
, 38
, 400
, 208
,
478
-, 438
, 209


, 207
, 463
, 29
, 208
, 207
bzip2, 395
gzip, 400
, 396
, 441
, 434
, ,
41
, ,
, 517
, 416
logging, 461

Python, 419
,
420
, , 36
, 450
, 449, 457
, 68, 103
!r, , 108
!s, , 108
, 106
, 105
%, 103
, 104
,
106
-, 106
, 106
, 213
, 39
__doc__, , 47
Python 3, 774
, 152
, 39, 131
, 76, 155
, 141
, 135, 138
, 134
, 39
, 78, 259
, 39, 130
,
111
, 139, 155
, 40

857


,
39

, 153
, 300

func_*, 76
, 39, 132
, 41
, 136
, 62
, 136
, 341
math, 319
, 39, 131
None, 63
, 139
, 139
, 130
lambda, 152
, 39

threading, 547
, 133
, 131
, 132
, 131
, 133
, 76
, 134
,
, 133
, 153
, 137
, 76, 154
, 280
,
111, 339
-, , 127
-
, 736

lambda, 153
, 111
,
256

, 71
,
555

, Python 3, 785
, 50
, 50
,
, 50
, 64
, 97
, 36
, 97
, 64
, Python 3, 777
, 60

, 282
, 175
, 305

__del__(), 174

asyncore, 568
, 148
, 37, 117
, 118
, 119
, 518

, , 171
, 50
, ,
178
, 51
, 65
, 309
, 64
, 36
, 31
, 309
, 64

, 318
, 64

858
, 31
, 50
, 64
, 30
CSV, ,
34
, , 29
, 416

, 305
, 50
, 562

, 158
, 274
, 78
, 57
, 85, 159
, 172

URL, 649

HTML, 666

, 51

, 640
, 172

-, 150
, 147

, 222
, 369
-, 218
XML, 217
, 219
, 222

WSGI, 673

You might also like