File tree

156 files changed

+6916
-0
lines changed

Some content is hidden

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

156 files changed

+6916
-0
lines changed
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!'
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!'
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+
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!'
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!'
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()
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')
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()
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'
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()
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()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python
2+
'''
3+
$Id$
4+
5+
queue.py -- simulate queue data structures using lists
6+
7+
NOTE: as of the time of publication, there is a bug in JPython1.1
8+
that does not recognize arguments for the list pop() method:
9+
TypeError: pop(): expected 0 args; got 1
10+
11+
Exercises:
12+
13+
13-9) create a Queue class
14+
15+
13-10) create a class similar to arrays in Perl which have
16+
both queue- and stack-like qualities and features
17+
'''
18+
19+
# create our data structure
20+
queue = []
21+
22+
#
23+
# enQ() -- add string to end of queue
24+
#
25+
def enQ():
26+
queue.append(raw_input('Enter new queue element: '))
27+
28+
#
29+
# deQ() -- remove string from front of queue
30+
#
31+
def deQ():
32+
if len(queue) == 0:
33+
print 'Cannot dequeue from empty queue!'
34+
else:
35+
print 'Removed [', queue.pop(0), ']'
36+
37+
#
38+
# viewQ() -- display queue contents
39+
#
40+
def viewQ():
41+
print str(queue)
42+
43+
#
44+
# showmenu() -- interactive portion of application
45+
# displays menu to prompt user and takes
46+
# action based on user response
47+
#
48+
def showmenu():
49+
prompt = """
50+
(E)nqueue
51+
(D)equeue
52+
(V)iew
53+
(Q)uit
54+
55+
Enter choice: """
56+
57+
# loop until user quits
58+
done = 0
59+
while not done:
60+
61+
# loop until user choses valid option
62+
chosen = 0
63+
while not chosen:
64+
65+
# if user hits ^C or ^D (EOF),
66+
# pretend they typed 'q' to quit
67+
try:
68+
choice = raw_input(prompt)[0]
69+
except (IndexError, EOFError, KeyboardInterrupt):
70+
choice = 'q'
71+
print '\nYou picked: [%s]' % choice
72+
73+
# validate option chosen
74+
if choice not in 'devq':
75+
print 'invalid option, try again'
76+
else:
77+
chosen = 1
78+
79+
# take appropriate action
80+
if choice == 'q':
81+
done = 1
82+
if choice == 'e':
83+
enQ()
84+
if choice == 'd':
85+
deQ()
86+
if choice == 'v':
87+
viewQ()
88+
89+
# run showmenu() as the application
90+
if __name__ == '__main__':
91+
showmenu()

0 commit comments

Comments
 (0)