Repository navigation
Expand file tree
/
Copy pathaddress_book_model.py
More file actions
266 lines (208 loc) · 7.09 KB
/
Copy pathaddress_book_model.py
File metadata and controls
266 lines (208 loc) · 7.09 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
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
#!/usr/bin/env python
"""
sample data for NOSQL examples
This version has a not completely-trival data model
"""
class Person(object):
"""
class to represent an individual person
"""
def __init__(self,
last_name,
first_name='',
middle_name='',
cell_phone='',
email='',
):
"""
initialize a Person object:
"""
self.first_name = first_name.strip()
self.last_name = last_name.strip()
self.middle_name = middle_name.strip()
self.cell_phone = cell_phone.strip()
self.email = email.strip()
@property
def name(self):
return " ".join([self.first_name, self.middle_name, self.last_name])
def __str__(self):
msg = '{first_name} {middle_name} {last_name}'.format(**self.__dict__)
return msg
def __repr__(self):
"""
not a good ___repr__, but want to have something here
"""
return self.__str__()
class Address(object):
"""
class that represents an address
"""
def __init__(self,
line_1='',
line_2='',
city='',
state='',
zip_code='',
):
"""
initialize an address
"""
self.line_1 = line_1.strip()
self.line_2 = line_2.strip()
self.city = city.strip()
self.state = state.strip()
self.zip_code = str(zip_code).strip()
def __str__(self):
msg = "{line_1}\n{line_2}\n{city} {state} {zip_code}\n".format(**self.__dict__)
return msg
class Household(object):
"""
Class that represents a Household.
A household has one or more people, and a Location
"""
def __init__(self,
name='',
people=(),
address=None,
phone=''
):
self.name = name.strip()
self.people = list(people)
self.address = address
self.phone = phone.strip()
def __str__(self):
msg = [self.name + ":"]
msg += [str(self.address)]
return "\n".join(msg)
def __repr__(self):
return self.__str__()
class Business(Household):
"""
Class that represents a Business
A business has one or more people,
and address and a phone number
"""
# Same as household now, but you never know.
pass
class AddressBook(object):
"""
And address book -- has people, households, businesses.
All fully cross-referenced
"""
def __init__(self,
people=(),
businesses=(),
households=(),
):
self.people = list(people)
self.businesses = list(businesses)
self.households = list(households)
def add_person(self, person):
self.people.append(person)
def add_household(self, household):
self.households.append(household)
def add_business(self, business):
self.businesses.append(business)
def __str__(self):
msg = ["An Address Book:"]
msg += ["People:"]
msg += [" " + person.name for person in self.find_people()]
msg += ["Households:"]
msg += [" " + house.name for house in self.find_households()]
msg += ["Businesses:"]
msg += [" " + bus.name for bus in self.find_businesses()]
return "\n".join(msg)
@property
def locations(self):
return self.households + self.businesses
def find_people(self, name=''):
"""
find all the people with name in their name somewhere
"""
return [person for person in self.people if name.lower() in person.name.lower()]
def find_zip_codes(self, zip_code):
"""
find all the locations with this zip_code
"""
zip_code = str(zip_code).strip()
return [loc for loc in self.locations if loc.address.zip_code == zip_code]
def find_state(self, state):
"""
find all the locations in this state
"""
return [location for location in self.locations if location.address.state == state]
def create_sample():
"""
Create a sample Address Book
"""
chris = Person(last_name='Barker',
first_name='Chris',
middle_name='H',
cell_phone='(123) 555-7890',
email='PythonCHB@gmail.com',
)
emma = Person(last_name='Barker',
first_name='Emma',
middle_name='L',
cell_phone='(345) 555-9012',
email='emma@something.com',
)
donna = Person(last_name='Barker',
first_name='Donna',
middle_name='L',
cell_phone='(111) 555-1111',
email='dbarker@something.com',
)
barker_address = Address(line_1='123 Some St',
line_2='Apt 1234',
city='Seattle',
state='WA',
zip_code='98000',)
the_barkers = Household(name="The Barkers",
people=(chris, donna, emma),
address=barker_address)
joseph = Person(last_name='Sheedy',
first_name='Joseph',
cell_phone='(234) 555-8910',
email='js@some_thing.com',
)
cris = Person(last_name='Ewing',
first_name='Cris',
cell_phone='(345) 555-6789',
email='cris@a_fake_domain.com',
)
fulvio = Person(last_name='Casali',
first_name='Fulvio',
cell_phone='(345) 555-1234',
email='fulvio@a_fake_domain.com',
)
fred = Person(first_name="Fred",
last_name="Jones",
email='FredJones@some_company.com',
cell_phone="403-561-8911",
)
python_cert_address = Address('UW Professional and Continuing Education',
line_2='4333 Brooklyn Ave. NE',
city='Seattle',
state='WA',
zip_code='98105',
)
python_cert = Business(name='Python Certification Program',
people=(chris, joseph, cris, fulvio),
address=python_cert_address
)
address_book = AddressBook()
address_book.add_person(chris)
address_book.add_person(donna)
address_book.add_person(emma)
address_book.add_person(cris)
address_book.add_person(joseph)
address_book.add_person(fulvio)
address_book.add_person(fred)
address_book.add_household(the_barkers)
address_book.add_business(python_cert)
return address_book
if __name__ == "__main__":
address_book = create_sample()
print("Here is the address book")
print(address_book)