Skip to content

Commit 142aa24

Browse files
author
罗晟
committed
git
1 parent f11a58e commit 142aa24

File tree

165 files changed

+7084
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+7084
-3
lines changed

ch03/makeTextFile.diff

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--- makeTextFile.py 2006-01-29 21:29:44.000000000 -0700
2+
+++ makeTextFile-fix.py 2006-10-11 21:49:35.000000000 -0700
3+
@@ -3,11 +3,10 @@
4+
'makeTextFile.py -- create text file'
5+
6+
import os
7+
-ls = os.linesep
8+
9+
# get filename
10+
while True:
11+
-
12+
+ fname = raw_input('Enter file name: ')
13+
if os.path.exists(fname):
14+
print"*** ERROR: '%s' already exists" % fname
15+
else:
16+
@@ -25,8 +24,8 @@
17+
else:
18+
all.append(entry)
19+
20+
-# write lines to file with proper line-ending
21+
+# write lines to file with NEWLINE line terminator
22+
fobj = open(fname, 'w')
23+
-fobj.writelines(['%s%s' % (x, ls) for x in all])
24+
+fobj.write('\n'.join(all))
25+
fobj.close()
26+
print 'DONE!'

ch03/makeTextFile.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
'makeTextFile.py -- create text file'
4+
5+
import os
6+
7+
# get filename
8+
while True:
9+
fname = raw_input('Enter file name: ')
10+
if os.path.exists(fname):
11+
print"*** ERROR: '%s' already exists" % fname
12+
else:
13+
break
14+
15+
# get file content (text) lines
16+
all = []
17+
print "\nEnter lines ('.' by itself to quit).\n"
18+
19+
# loop until user terminates input
20+
while True:
21+
entry = raw_input('> ')
22+
if entry == '.':
23+
break
24+
else:
25+
all.append(entry)
26+
27+
# write lines to file with NEWLINE line terminator
28+
fobj = open(fname, 'w')
29+
fobj.write('\n'.join(all))
30+
fobj.close()
31+
print 'DONE!'

ch03/makeTextFile0.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
'makeTextFile.py -- create text file'
4+
5+
import os
6+
ls = os.linesep
7+
8+
# get filename
9+
while True:
10+
fname = raw_input('Enter file name: ')
11+
if os.path.exists(fname):
12+
print"*** ERROR: '%s' already exists" % fname
13+
else:
14+
break
15+
16+
# get file content (text) lines
17+
all = []
18+
print "\nEnter lines ('.' by itself to quit).\n"
19+
20+
# loop until user terminates input
21+
while True:
22+
entry = raw_input('> ')
23+
if entry == '.':
24+
break
25+
else:
26+
all.append(entry)
27+
28+
# write lines to file with proper line-ending
29+
fobj = open(fname, 'w')
30+
fobj.writelines(['%s%s' % (x, ls) for x in all])
31+
fobj.close()
32+
print 'DONE!'

ch03/makeTextFile1.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
'makeTextFile.py -- create text file'
4+
5+
import os
6+
ls = os.linesep
7+
8+
# get filename
9+
while True:
10+
fname = raw_input('Enter file name: ')
11+
if os.path.exists(fname):
12+
print"*** ERROR: '%s' already exists" % fname
13+
else:
14+
break
15+
16+
# get file content (text) lines
17+
all = []
18+
print "\nEnter lines ('.' by itself to quit).\n"
19+
20+
# loop until user terminates input
21+
while True:
22+
entry = raw_input('> ')
23+
if entry == '.':
24+
break
25+
else:
26+
all.append(entry)
27+
28+
# write lines to file with proper line-ending
29+
fobj = open(fname, 'w')
30+
fobj.writelines(['%s%s' % (x, ls) for x in all])
31+
fobj.close()
32+
print 'DONE!'

ch03/readTextFile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
3+
'readTextFile.py -- read and display text file'
4+
5+
# get filename
6+
fname = raw_input('Enter file name: ')
7+
print
8+
9+
# attempt to open file for reading
10+
try:
11+
fobj = open(fname, 'r')
12+
except IOError, e:
13+
print"*** file open error:", e
14+
else:
15+
# display contents to the screen
16+
for eachLine in fobj:
17+
print eachLine,
18+
fobj.close()

ch04/typechk.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
3+
def displayNumType(num):
4+
print num, 'is',
5+
if isinstance(num, (int, long, float, complex)):
6+
print 'a number of type:', type(num).__name__
7+
else:
8+
print 'not a number at all!!'
9+
10+
displayNumType(-69)
11+
displayNumType(9999999999999999999999L)
12+
displayNumType(98.6)
13+
displayNumType(-5.2+1.9j)
14+
displayNumType('xxx')

ch06/alt/NoneIndex.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/python
2+
3+
s = 'abcde'
4+
5+
#for x,y in enumerate(s):
6+
# print s[x:], s[:x]
7+
#for i in range(len(s), -1, -1):
8+
#print s[i:], s[:i], s[i::-1], s[:i:-1]
9+
10+
s = 'abcde'
11+
for i in [None] + range(-1, -len(s), -1):
12+
print s[:i]
13+
14+
raw_input()

ch06/alt/idcheck.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
'''
3+
$Id$
4+
5+
idcheck.py -- checks identifiers for validity
6+
7+
This application is limited in that it currently only supports
8+
checking identifiers with length > 1 (does not process identifiers
9+
of length greater than 1. This application also does not recognize
10+
do keywords.
11+
12+
Exercise:
13+
14+
6-2) update this script to process identifiers of length 1
15+
as well as recognizing keywords as invalid identifiers
16+
(for use by the programmer; they are valid Python
17+
identifiers otherwise).
18+
'''
19+
20+
import string # string utility module
21+
22+
# create alphabet and number sets
23+
alphas = string.letters + '_'
24+
nums = string.digits
25+
26+
# salutation message and input prompt
27+
print 'Welcome to the Identifier Checker v1.0'
28+
print 'Testees must be at least 2 chars long.'
29+
inp = raw_input('Identifier to test? ')
30+
31+
# only take action for identifiers with length > 1
32+
if len(inp) > 1:
33+
34+
# first character must be alphabetic
35+
if inp[0] not in alphas:
36+
print 'invalid: first symbol must be alphabetic'
37+
38+
# remaning characters can be alphanumeric
39+
else:
40+
for otherChar in inp[1:]:
41+
if otherChar not in alphas + nums:
42+
print 'invalid: remaining symbols must be alphanumeric'
43+
break
44+
else:
45+
print "okay as an identifier"
46+
else:
47+
print 'invalid: length must be > 1'

ch06/alt/idcheck2.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
3+
from keyword import kwlist
4+
import string
5+
6+
ALPHAS = string.ascii_letters + '_'
7+
NUMS = string.digits
8+
9+
def main():
10+
print 'Welcome to the Identifier Checker v2.0'
11+
myInput = raw_input('Identifier to test? ').strip()
12+
13+
if len(myInput) == 0:
14+
print "ERROR: no identifier candidate entered"
15+
return
16+
17+
if myInput in kwlist:
18+
print "ERROR: %r is a keyword" % myInput
19+
return
20+
21+
alnums = ALPHAS + NUMS
22+
for i, c in enumerate(myInput):
23+
if i == 0 and c not in ALPHAS:
24+
print 'ERROR: first symbol must be alphabetic'
25+
break
26+
if c not in alnums:
27+
print 'ERROR: remaining symbols must be alphanumeric'
28+
break
29+
else:
30+
print "okay as an identifier"
31+
32+
if __name__ == '__main__':
33+
main()

ch06/alt/insertVsColonZero.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/python
2+
3+
from time import time
4+
REPS = 17500
5+
6+
def insert():
7+
m = [None]
8+
i = 0
9+
now = time()
10+
while i < REPS:
11+
m.insert(0, i)
12+
i += 1
13+
print 'Elapsed (insert):', time() - now
14+
15+
def colonZero():
16+
m = [None]
17+
i = 0
18+
now = time()
19+
while i < REPS:
20+
m[:0] = [i]
21+
i += 1
22+
print 'Elapsed (colon-0):', time() - now
23+
24+
def main():
25+
insert()
26+
colonZero()
27+
28+
if __name__ == '__main__':
29+
main()
30+
raw_input()

0 commit comments

Comments
 (0)