在16章中,作者建議讀者寫一個地址簿程序,還記得寫它都是在大一學c語言的年代了,不過下午還是抽出了一點時間來寫這個程序,寫了兩個版本,一個是使用python 字典來存儲名字和地址,另一個版本則使用python的面向對象特性,來鏈式存儲聯系人對象。
版本一代碼:
1
import cPickle as p,sys,re
2
3
# Author:nicky(nicky.jcoder@gmail.com)
4
# July 24th,2008
5
6
def readAdd():
7
f = file(ftxt)
8
map = p.load(f)
9
f.close()
10
return map
11
12
def autoSave():
13
f=file(ftxt,'w')
14
p.dump(book,f)
15
f.close()
16
17
def run():
18
while True:
19
try:
20
com = int(raw_input(welcome+features))
21
except:
22
print "\ninput error"
23
run()
24
if com == 1:
25
info = raw_input("input the name and address:")
26
infoList = re.split(' ',info)
27
add(infoList[0],infoList[1])
28
run()
29
autoSave()
30
book = readAdd()
31
elif com == 2:
32
info = raw_input("input the name:")
33
search(info)
34
run()
35
autoSave()
36
book = readAdd()
37
elif com == 3:
38
info = raw_input("input the name and address:")
39
infoList = re.split(' ',info)
40
modify(infoList[0],infoList[1])
41
run()
42
autoSave()
43
book = readAdd()
44
elif com == 4:
45
info = raw_input("input the name:")
46
delete(info)
47
run()
48
autoSave()
49
book = readAdd()
50
elif com ==5:
51
print 'All the people and their addresses are listed:'
52
browseAll()
53
elif com == 6:
54
autoSave()
55
sys.exit()
56
else:
57
print 'your input is invalid'
58
run()
59
60
def add(people,address):
61
if people not in book:
62
book[people]=address
63
print '\nadd successfully'
64
else:
65
print '\nthe '+people+' exists in the address book'
66
67
def search(people):
68
if people in book:
69
print "\n"+book[people]
70
else:
71
print '\nthe'+people+' is not in the address book'
72
73
def modify(people,address):
74
if people in book:
75
book[people]=address
76
print '\nmodify successfully'
77
else:
78
print '\nthe'+people+' is not in the address book'
79
80
def delete(people):
81
if people in book:
82
del book[people]
83
print '\ndelete successfully'
84
else:
85
print '\nsome errors has been arised'
86
87
def browseAll():
88
for people,address in book.items():
89
print people+':'+address
90
91
def judgeAddressbook():
92
f = file(ftxt,'w')
93
count =0
94
while True:
95
line=f.readline()
96
if len(line)==0:
97
break
98
count=count+1
99
if count == 0:
100
book={'test':'test'}
101
autoSave()
102
103
if __name__ == '__main__':
104
welcome = '\nWelcome to use AddressBook made by nicky.jcoder@gmail.com'
105
features="\nyou can use the features listed:\n1:add people and their address information\n2:search\n3:modify\n4:delete\n5:Browse all the info in the addressbook\n6:exit the address book application\nInput your choice:"
106
ftxt = 'addressbook.txt'
107
book={}
108
judgeAddressbook()
109
book=readAdd()
110
run()
版本二代碼:
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

1
import cPickle as p,sys,re
2
3
# Author:nicky(nicky.jcoder@gmail.com)
4
# July 24th,2008
5
6
def readAll():
7
f = file(ftxt)
8
list = p.load(f)
9
f.close()
10
return list
11
12
def autoSave():
13
f=file(ftxt,'w')
14
p.dump(book,f)
15
f.close()
16
17
class People:
18
def __init__(self,name,address):
19
self.name=name
20
self.address=address
21
def getName(self):
22
return self.name
23
def getAddress(self):
24
return self.address
25
def setAddress(address):
26
self.address=address
27
28
def run():
29
while True:
30
try:
31
com = int(raw_input(welcome+features))
32
except:
33
print "\ninput error"
34
run()
35
if com == 1:
36
info = raw_input("input the name and address:")
37
infoList = re.split(' ',info)
38
add(infoList[0],infoList[1])
39
run()
40
autoSave()
41
book = readAdd()
42
elif com == 2:
43
info = raw_input("input the name:")
44
search(info)
45
run()
46
autoSave()
47
book = readAdd()
48
elif com == 3:
49
info = raw_input("input the name and address:")
50
infoList = re.split(' ',info)
51
modify(infoList[0],infoList[1])
52
run()
53
autoSave()
54
book = readAdd()
55
elif com == 4:
56
info = raw_input("input the name:")
57
delete(info)
58
run()
59
autoSave()
60
book = readAdd()
61
elif com ==5:
62
print 'All the people and their addresses are listed:'
63
browseAll()
64
elif com == 6:
65
autoSave()
66
sys.exit()
67
else:
68
print 'your input is invalid'
69
run()
70
71
def add(people,address):
72
flag = -1
73
for item in book:
74
if people != item.getName():
75
instance = People(people,address)
76
book.append(instance)
77
print '\nadd successfully'
78
flag =0
79
if flag == -1:
80
print '\nthe '+people+' exists in the address book'
81
82
def search(people):
83
flag=-1
84
for item in book:
85
if people == item.getName():
86
print "\n"+item.getAddress()
87
flag=0
88
if flag ==-1:
89
print '\nthe'+people+' is not in the address book'
90
91
def modify(people,address):
92
flag=-1
93
for item in book:
94
if people == item.getName:
95
item.setAddress(address)
96
flag=0
97
print '\nmodify successfully'
98
if flag==-1:
99
print '\nthe'+people+' is not in the address book'
100
101
102
def delete(people):
103
flag =-1
104
for item in book:
105
if people == item.getName:
106
del item
107
flag=0
108
print '\ndelete successfully'
109
if flag ==-1:
110
print '\nsome errors has been arised'
111
112
113
def browseAll():
114
for item in book:
115
print item.getName()+":"+item.getAddress()
116
117
def judgeAddressbook():
118
f = file(ftxt,'w')
119
count =0
120
while True:
121
line=f.readline()
122
if len(line)==0:
123
break
124
count=count+1
125
if count == 0:
126
instance=People("test1","test1")
127
book.append(instance)
128
autoSave()
129
130
if __name__ == '__main__':
131
welcome = '\nWelcome to use AddressBook made by nicky.jcoder@gmail.com'
132
features="\nyou can use the features listed:\n1:add people and their address information\n2:search\n3:modify\n4:delete\n5:Browse all the info in the addressbook\n6:exit the address book application\nInput your choice:"
133
ftxt = 'addressbook_object.txt'
134
book=[]
135
judgeAddressbook()
136
book=readAll()
137
run()

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

運行方法應該不用說了
python 模塊
效果圖為