This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddressBook.py
306 lines (271 loc) · 10.6 KB
/
AddressBook.py
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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import os
import re
'''
The function of this program
1.Function menu
2.Add data
3.Modify data
4.Check data
5.CheckDataByID
6.Remove data
'''
def ChineseNum(n):
# 判断中文字符数
return len(re.findall('([\u4e00-\u9fa5])', n))
class NkAddressBookManagement():
Name = '南开大学学生管理系统'
QuitTip = '输入 [q] 离开'
DataTitle = '详细数据'
# TableTab = ['No.', 'ID', 'Name', 'QQ', 'Tel', 'Email']
TableTab = ['序号', '学号', '姓名', 'QQ', '电话', '邮箱']
addData = '添加信息\t[a]'
Modifydata = '更新数据\t[m]'
checkData = '检查数据\t[c]'
removeData = '移除数据\t[r]'
def DisplayMenu(self):
clearTerminal()
tl = 40 # 总宽度
# 中文字符在控制台占据两个英文字符的文字
# 因此将总宽度减去中文字符数
nl = tl - ChineseNum(self.Name)
ql = tl - ChineseNum(self.QuitTip)
al = tl - ChineseNum(self.addData)
ml = tl - ChineseNum(self.Modifydata)
cl = tl - ChineseNum(self.checkData)
rl = tl - ChineseNum(self.Modifydata)
MenuFormat = '{0:{0}^%s}\n{2:{1}^%s}\n\n{3:{1}^%s}\n{4:{1}^%s}\n{5:{1}^%s}\n{6:{1}^%s}\n\n{7:{1}>%s}\n{0:{0}^%s}' % (
tl, nl, al, ml, cl, rl, ql, tl)
print(
MenuFormat.format('#', ' ', self.Name, self.addData,
self.Modifydata, self.checkData, self.removeData,
self.QuitTip))
def AddData(self, StudentList):
while True:
try:
clearTerminal()
StudentItem = dict()
# obetain student Info by user input
for index, item in enumerate(self.TableTab):
if item == '学号':
ID = int(input('%d.学号:' % index))
if ID in StudentList.keys():
raise Exception('学号已存在,输入 [q] -> [m] 来更新')
else:
StudentItem[item] = ID
elif item == 'QQ':
while True:
e = input('%d.%s:' % (index, item))
if checkQQnum(e):
StudentItem[item] = e
break
else:
print('QQ格式错误!')
elif item == '电话':
while True:
e = input('%d.%s:' % (index, item))
if checkTelnum(e):
StudentItem[item] = e
break
else:
print('电话错误!')
elif item == '邮箱':
while True:
e = input('%d.%s:' % (index, item))
if checkEmail(e):
StudentItem[item] = e
break
else:
print('邮箱错误!')
elif index != 0:
StudentItem[item] = input('%d.%s:' % (index, item))
StudentList.update({StudentItem['学号']: StudentItem})
print('😛 添加成功!\n')
self.DisplayTable({StudentItem['学号']: StudentItem})
except ValueError:
print('😥 学号必须是数字类型!!')
except Exception as ex:
print(ex)
YourInput = input('输入 [q] 返回菜单/输入任意字符继续:')
if YourInput == 'q':
self.DisplayMenu()
break
def ModifyData(self, StudentList):
clearTerminal()
while True:
Key = int(input('输入想要更改信息的学生学号:'))
# try other method to judge Key not found
if StudentList.get(Key) is not None:
print('你可以更新以下信息:', end='')
for index, item in enumerate(self.TableTab):
if index != 0 and index != 1:
print('[%s]' % item, end=' ')
print("(使用 ',' 分割)")
ModifiedKey = input('输入想要更新的信息:')
for item in ModifiedKey.split(','):
if item not in self.TableTab:
print('😥 输入错误,请确保输入正确!\n')
elif item == '邮箱':
while True:
ModifyData = input('%s改成:' % item)
if checkEmail(ModifyData):
StudentList[Key][item] = ModifyData
break
elif ModifyData == '':
break
else:
print('%s格式错误' % item)
elif item == '电话':
while True:
ModifyData = input('%s改成:' % item)
if checkTelnum(ModifyData):
StudentList[Key][item] = ModifyData
break
elif ModifyData == '':
break
else:
print('%s格式错误' % item)
elif item == 'QQ':
while True:
ModifyData = input('%s改成:' % item)
if checkQQnum(ModifyData):
StudentList[Key][item] = ModifyData
break
elif ModifyData == '':
break
else:
print('%s格式错误' % item)
else:
ModifyData = input('%s改成:' % item)
if ModifyData != '':
StudentList[Key][item] = ModifyData
print('😛 更新结束!\n')
else:
print('😨 学号不存在\n')
YourInput = input('输入 [q] 返回菜单/输入任意键继续:')
if YourInput == 'q':
self.DisplayMenu()
break
def CheckData(self, StudentList):
while True:
try:
clearTerminal()
key = input('输入 [all] 查看全部学生信息/也可以输入学号:')
if key == '':
continue
elif key == 'all':
self.DisplayTable(StudentList)
else:
key = int(key)
self.DisplayTable({key: StudentList[key]})
except KeyError:
print('😨 学号不存在\n')
except ValueError:
print('😥 学号必须是数字类型!!')
except Exception as ex:
print(ex)
YourInput = input('输入 [q] 返回菜单/输入任意键继续:')
if YourInput == 'q':
self.DisplayMenu()
break
def DisplayTable(self, list):
# The gap of the value of table
GAP = 17
length = len(self.TableTab)
# Some formatting for table
InfoFormat1 = ''
for item in self.TableTab:
ll = GAP - ChineseNum(item)
InfoFormat1 = InfoFormat1 + '|{:^%d}' % ll
InfoFormat1 = InfoFormat1 + '|'
InfoFormat2 = ('+{0:{0}^%d}' % GAP) * length + '+' # +---+---+
TitleFormat = '|{:^%d}|' % (length * GAP + length - 1 - ChineseNum(
self.DataTitle)) # | xxx |
print('-' * (length * GAP + length + 1))
print(TitleFormat.format(self.DataTitle))
print(InfoFormat2.format('-'))
print(InfoFormat1.format(*self.TableTab))
print(InfoFormat2.format('-'))
for index, item in enumerate(list.values()):
a = []
InfoFormat3 = '|{:^%d}' % GAP
for i in item.values():
a.append(i)
if isinstance(i, str):
info_l = GAP - ChineseNum(i)
InfoFormat3 = InfoFormat3 + '|{:^%d}' % info_l
else:
InfoFormat3 = InfoFormat3 + '|{:^%d}' % GAP
InfoFormat3 = InfoFormat3 + '|'
print(InfoFormat3.format(index + 1, *a))
print(InfoFormat2.format('-'))
print()
def RemoveData(self, StudentList):
while True:
clearTerminal()
try:
Key = int(input('输入想要删除学生的学号:'))
StudentList.pop(Key)
print('😛 删除成功!\n')
except KeyError:
print('😨 学号不存在\n')
except ValueError:
print('😥 学号必须是数值类型!!\n')
YourInput = input('输入 [q] 返回菜单/输入任意键继续:')
if YourInput == 'q':
self.DisplayMenu()
break
def start():
M = NkAddressBookManagement()
StudentList = dict()
M.DisplayMenu()
while True:
try:
SelectMenu = input('😋 请输入你的选项:')
if SelectMenu == 'a':
M.AddData(StudentList)
elif SelectMenu == 'm':
M.ModifyData(StudentList)
elif SelectMenu == 'c':
M.CheckData(StudentList)
elif SelectMenu == 'r':
M.RemoveData(StudentList)
elif SelectMenu == 'q':
clearTerminal()
break
else:
raise Exception('😥 输入错误!')
except Exception as ex:
print(ex)
# 检测 QQ 号
def checkQQnum(n):
rex = '[1-9]([0-9]{4,10})'
r = re.findall(rex, n)
if len(r) == 1:
return True
else:
return False
# 检测电话号码
def checkTelnum(n):
rex = '0?(13|14|15|18|17)[0-9]{9}'
r = re.findall(rex, n)
if len(r) == 1:
return True
else:
return False
# 检测邮箱
def checkEmail(n):
rex = r'\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}'
r = re.findall(rex, n)
if len(r) == 1:
return True
else:
return False
# 根据平台清空控制台
def clearTerminal():
platform = os.name
if platform == 'nt':
os.system('cls')
elif platform == 'posix':
os.system('clear')
if __name__ == '__main__':
start()