-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddress_book.py
More file actions
150 lines (123 loc) · 4.17 KB
/
address_book.py
File metadata and controls
150 lines (123 loc) · 4.17 KB
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
#!/usr/bin/env python3.5
# -*- coding: utf-8 -*-
import os
import sys
import pickle
import re
from functools import wraps
class PersonInfor:
"""Person information,including name, email, phone number."""
def __init__(self, name, email, phone_num):
self.name = name
self.email = email
self.phone_num = phone_num
def print_infor(self):
print(self.name, ' ', self.email, ' ', self.phone_num)
def is_addrbook_opened(operation_fn):
"""Check whether the address book is opened or not."""
@wraps(operation_fn)
def wrap_fn(self_obj):
if self_obj.opened:
return operation_fn(self_obj)
else:
print("Invalid operation, due to the address book hasn't been opened!")
return
return wrap_fn
def is_addrbook_empty(operation_fn):
"""Check whether the address book is empty or not."""
def wrap_fn(self_obj):
if self_obj.dictdata:
return operation_fn(self_obj)
else:
print("This is a empty address book!")
return
return wrap_fn
class AddressBook:
"""Operations for address book, including add, brows, search person information."""
def __init__(self, book_file_path):
self.file_path = book_file_path
self.dictdata = {}
self.opened = False
def open(self):
if self.opened:
return
if os.path.exists(self.file_path): # check whether the specified address book file exist or not
with open(self.file_path, 'rb') as f:
self.dictdata = dict(pickle.load(f))
else:
createbook = input("Can't find the address book, create new one?(y/n) ")
if re.match(r'[yY]', createbook):
with open(self.file_path, 'wb') as f:
print("A new address book is created!")
elif re.match(r'[nN]', createbook):
sys.exit(0)
else:
print("Unknown command, exit!")
sys.exit(1)
self.opened = True
@is_addrbook_opened
def save(self):
"""Save the changed dict data to the disk."""
with open(self.file_path, 'wb') as f:
pickle.dump(self.dictdata, f)
self.opened = False
@is_addrbook_opened
@is_addrbook_empty
def browse(self):
for key in self.dictdata.keys():
self.dictdata[key].print_infor()
@is_addrbook_opened
def add(self):
p_name = input("input name:")
p_email = input("input email:")
p_phonenum = input("input phone num:")
p = PersonInfor(p_name, p_email, p_phonenum)
self.dictdata[p.name] = p
print("Added new person:")
p.print_infor()
@is_addrbook_opened
@is_addrbook_empty
def search(self):
p_name = input("input the person's name you wanna search:")
if p_name in self.dictdata.keys():
self.dictdata[p_name].print_infor()
else:
print("Can't find the %s on the address book!" % p_name)
@is_addrbook_opened
@is_addrbook_empty
def delete(self):
p_name = input("input the person's name you wanna delete:")
if p_name in self.dictdata.keys():
self.dictdata.__delitem__(p_name)
print("%s is deleted!" % p_name)
else:
print("Can't find the %s on the address book!" % p_name)
def main():
addr_book_file = 'address_book.data'
addr_book_file_path = os.path.join(os.getcwd(), addr_book_file)
addr_book = AddressBook(addr_book_file_path)
addr_book.open()
addr_book_operation = True
while addr_book_operation:
operation = input("""Operation list:
1. Browse address book
2. Add person
3. Delete person
4. Search person
5. Exit
""")
if operation == '1':
addr_book.browse()
elif operation == '2':
addr_book.add()
elif operation == '3':
addr_book.delete()
elif operation == '4':
addr_book.search()
elif operation == '5':
addr_book_operation = False
else:
print("Unknown operation!\n")
addr_book.save()
sys.exit(0)
main()