0%

【Life on Python】一个简易的通讯录

提供了 add, remove, find, view 和 exit 几个命令行接口,意义很明确了,所以 help 选项没空写内容也无所谓了。第一次自己思考结构写的 Python 程序,有点小爽。XD

1
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
import os
import cPickle as pickle
class Persion:
'''\
Describe a nature persion, his/her name, sex, and \
age.'''
def __init__(self, info):
'''Initializes the person's data.'''
self.name = info[0]
self.sex = info[1]
self.age = info[2]
print '(Initializing %s)' % self.name
class Contact(Persion):
'''\
Describe a contact that is a nature persion, and \
has his/her Tel., Addr., and Email.'''
def __init__(self, info):
Persion.__init__(self, info)
self.tel = info[3]
self.addr = info[4]
self.email = info[5]
self.info = {self.name : (self.sex, self.age, self.tel, self.addr,\
self.email)}
def tell(self):
print self.info

def command_line_interface():
prompt = '--> '
workingDict = {}
if os.path.exists(r'.\Contact.data'):
workingDict = pickle.load(open(r'.\Contact.data', 'r'))
else:
pass
while True:
getInput = raw_input(prompt)
if getInput == 'exit':
pickle.dump(workingDict, open(r'.\Contact.data', 'w'))
break
elif getInput == 'add':
newContact = generate_new_contact(get_info_from_raw_input())
workingDict[newContact.name] = newContact.info[newContact.name]
pass
elif getInput == 'find':
pattern = raw_input('Enter the name:')
if pattern in workingDict:
print '-' * 45
print 'Name: %s\nSex: %s\nAge: %s\nTel.: %s\nAddr.: %s\nEmail: %s' % (pattern, workingDict[pattern][0], workingDict[pattern][1], workingDict[pattern][2], workingDict[pattern][3], workingDict[pattern][4])
print '-' * 45
else:
print 'Sorry, but "%s" is not in your contacts list.' % pattern
elif getInput == 'remove':
pattern = raw_input('Enter the name:')
if pattern in workingDict:
workingDict.pop(pattern)
print 'Contact "%s" has been removed.' % pattern
else:
print 'Sorry, but "%s" is not in your contacts list.' % pattern
elif getInput == 'view':
population = len(workingDict)
if population == 1:
print 'There is only one contact:'
elif population == 0:
print 'There is no contact.'
continue
else:
print 'There are %d contacts:' % population
for name, info in workingDict.items():
print '-' * 45
print 'Name: %s\nSex: %s\nAge: %s\nTel.: %s\nAddr.: %s\nEmail: %s' % (name, info[0], info[1], info[2], info[3], info[4])
pass
elif getInput == 'help':
print getInput
else:
exec(getInput)
pass

def get_info_from_raw_input():
info = []
info.append(raw_input('Name: '))
info.append(raw_input('Sex: '))
info.append(raw_input('Age: '))
info.append(raw_input('Tel.: '))
info.append(raw_input('Addr.: '))
info.append(raw_input('Email: '))
return info
def generate_new_contact(info):
return Contact(info)

if __name__ == '__main__':
command_line_interface()
俗话说,投资效率是最好的投资。 如果您感觉我的文章质量不错,读后收获很大,预计能为您提高 10% 的工作效率,不妨小额捐助我一下,让我有动力继续写出更多好文章。