|  | 
|  | 1 | +#!/usr/bin/env python | 
|  | 2 | + | 
|  | 3 | +""" | 
|  | 4 | +sample data for NOSQL examples | 
|  | 5 | +
 | 
|  | 6 | +This version has a not completely-trival data model | 
|  | 7 | +""" | 
|  | 8 | + | 
|  | 9 | + | 
|  | 10 | +class Person(object): | 
|  | 11 | +    """ | 
|  | 12 | +    class to represent an individual person | 
|  | 13 | +    """ | 
|  | 14 | + | 
|  | 15 | +    def __init__(self, | 
|  | 16 | +                 last_name, | 
|  | 17 | +                 first_name='', | 
|  | 18 | +                 middle_name='', | 
|  | 19 | +                 cell_phone='', | 
|  | 20 | +                 email='', | 
|  | 21 | +                 ): | 
|  | 22 | +        """ | 
|  | 23 | +        initialize a Person object: | 
|  | 24 | +        """ | 
|  | 25 | +        self.first_name = first_name.strip() | 
|  | 26 | +        self.last_name = last_name.strip() | 
|  | 27 | +        self.middle_name = middle_name.strip() | 
|  | 28 | +        self.cell_phone = cell_phone.strip() | 
|  | 29 | +        self.email = email.strip() | 
|  | 30 | + | 
|  | 31 | +    @property | 
|  | 32 | +    def name(self): | 
|  | 33 | +        return " ".join([self.first_name, self.middle_name, self.last_name]) | 
|  | 34 | + | 
|  | 35 | +    def __str__(self): | 
|  | 36 | +        msg = '{first_name} {middle_name} {last_name}'.format(**self.__dict__) | 
|  | 37 | +        return msg | 
|  | 38 | + | 
|  | 39 | +    def __repr__(self): | 
|  | 40 | +        """ | 
|  | 41 | +        not a good ___repr__, but want to have something here | 
|  | 42 | +        """ | 
|  | 43 | +        return self.__str__() | 
|  | 44 | + | 
|  | 45 | + | 
|  | 46 | +class Address(object): | 
|  | 47 | +    """ | 
|  | 48 | +    class that represents an address | 
|  | 49 | +    """ | 
|  | 50 | + | 
|  | 51 | +    def __init__(self, | 
|  | 52 | +                 line_1='', | 
|  | 53 | +                 line_2='', | 
|  | 54 | +                 city='', | 
|  | 55 | +                 state='', | 
|  | 56 | +                 zip_code='', | 
|  | 57 | +                 ): | 
|  | 58 | +        """ | 
|  | 59 | +        initialize an address | 
|  | 60 | +        """ | 
|  | 61 | + | 
|  | 62 | +        self.line_1 = line_1.strip() | 
|  | 63 | +        self.line_2 = line_2.strip() | 
|  | 64 | +        self.city = city.strip() | 
|  | 65 | +        self.state = state.strip() | 
|  | 66 | +        self.zip_code = str(zip_code).strip() | 
|  | 67 | + | 
|  | 68 | +    def __str__(self): | 
|  | 69 | +        msg = "{line_1}\n{line_2}\n{city} {state} {zip_code}\n".format(**self.__dict__) | 
|  | 70 | +        return msg | 
|  | 71 | + | 
|  | 72 | + | 
|  | 73 | +class Household(object): | 
|  | 74 | +    """ | 
|  | 75 | +    Class that represents a Household. | 
|  | 76 | +
 | 
|  | 77 | +    A household has one or more people, and a Location | 
|  | 78 | +    """ | 
|  | 79 | + | 
|  | 80 | +    def __init__(self, | 
|  | 81 | +                 name='', | 
|  | 82 | +                 people=(), | 
|  | 83 | +                 address=None, | 
|  | 84 | +                 phone='' | 
|  | 85 | +                 ): | 
|  | 86 | +        self.name = name.strip() | 
|  | 87 | +        self.people = list(people) | 
|  | 88 | +        self.address = address | 
|  | 89 | +        self.phone = phone.strip() | 
|  | 90 | + | 
|  | 91 | +    def __str__(self): | 
|  | 92 | +        msg = [self.name + ":"] | 
|  | 93 | +        msg += [str(self.address)] | 
|  | 94 | +        return "\n".join(msg) | 
|  | 95 | + | 
|  | 96 | +    def __repr__(self): | 
|  | 97 | +        return self.__str__() | 
|  | 98 | + | 
|  | 99 | + | 
|  | 100 | +class Business(Household): | 
|  | 101 | +    """ | 
|  | 102 | +    Class that represents a Business | 
|  | 103 | +
 | 
|  | 104 | +    A business has one or more people, | 
|  | 105 | +    and address and a phone number | 
|  | 106 | +    """ | 
|  | 107 | +    # Same as household now, but you never know. | 
|  | 108 | +    pass | 
|  | 109 | + | 
|  | 110 | + | 
|  | 111 | +class AddressBook(object): | 
|  | 112 | +    """ | 
|  | 113 | +    And address book -- has people, households, businesses. | 
|  | 114 | +
 | 
|  | 115 | +    All fully cross-referenced | 
|  | 116 | +    """ | 
|  | 117 | + | 
|  | 118 | +    def __init__(self, | 
|  | 119 | +                 people=(), | 
|  | 120 | +                 businesses=(), | 
|  | 121 | +                 households=(), | 
|  | 122 | +                 ): | 
|  | 123 | +        self.people = list(people) | 
|  | 124 | +        self.businesses = list(businesses) | 
|  | 125 | +        self.households = list(households) | 
|  | 126 | + | 
|  | 127 | +    def add_person(self, person): | 
|  | 128 | +        self.people.append(person) | 
|  | 129 | + | 
|  | 130 | +    def add_household(self, household): | 
|  | 131 | +        self.households.append(household) | 
|  | 132 | + | 
|  | 133 | +    def add_business(self, business): | 
|  | 134 | +        self.businesses.append(business) | 
|  | 135 | + | 
|  | 136 | +    def __str__(self): | 
|  | 137 | +        msg = ["An Address Book:"] | 
|  | 138 | +        msg += ["People:"] | 
|  | 139 | +        msg += ["  " + person.name for person in self.find_people()] | 
|  | 140 | +        msg += ["Households:"] | 
|  | 141 | +        msg += ["  " + house.name for house in self.find_households()] | 
|  | 142 | +        msg += ["Businesses:"] | 
|  | 143 | +        msg += ["  " + bus.name for bus in self.find_businesses()] | 
|  | 144 | + | 
|  | 145 | +        return "\n".join(msg) | 
|  | 146 | + | 
|  | 147 | +    @property | 
|  | 148 | +    def locations(self): | 
|  | 149 | +        return self.households + self.businesses | 
|  | 150 | + | 
|  | 151 | +    def find_people(self, name=''): | 
|  | 152 | +        """ | 
|  | 153 | +        find all the people with name in their name somewhere | 
|  | 154 | +        """ | 
|  | 155 | +        return [person for person in self.people if name.lower() in person.name.lower()] | 
|  | 156 | + | 
|  | 157 | +    def find_zip_codes(self, zip_code): | 
|  | 158 | +        """ | 
|  | 159 | +        find all the locations with this zip_code | 
|  | 160 | +        """ | 
|  | 161 | +        zip_code = str(zip_code).strip() | 
|  | 162 | +        return [loc for loc in self.locations if loc.address.zip_code == zip_code] | 
|  | 163 | + | 
|  | 164 | +    def find_state(self, state): | 
|  | 165 | +        """ | 
|  | 166 | +        find all the locations in this state | 
|  | 167 | +        """ | 
|  | 168 | +        return [location for location in self.locations if location.address.state == state] | 
|  | 169 | + | 
|  | 170 | + | 
|  | 171 | +def create_sample(): | 
|  | 172 | +    """ | 
|  | 173 | +    Create a sample Address Book | 
|  | 174 | +    """ | 
|  | 175 | +    chris = Person(last_name='Barker', | 
|  | 176 | +                   first_name='Chris', | 
|  | 177 | +                   middle_name='H', | 
|  | 178 | +                   cell_phone='(123) 555-7890', | 
|  | 179 | + | 
|  | 180 | +                   ) | 
|  | 181 | + | 
|  | 182 | +    emma = Person(last_name='Barker', | 
|  | 183 | +                  first_name='Emma', | 
|  | 184 | +                  middle_name='L', | 
|  | 185 | +                  cell_phone='(345) 555-9012', | 
|  | 186 | + | 
|  | 187 | +                  ) | 
|  | 188 | + | 
|  | 189 | +    donna = Person(last_name='Barker', | 
|  | 190 | +                   first_name='Donna', | 
|  | 191 | +                   middle_name='L', | 
|  | 192 | +                   cell_phone='(111) 555-1111', | 
|  | 193 | + | 
|  | 194 | +                   ) | 
|  | 195 | + | 
|  | 196 | +    barker_address = Address(line_1='123 Some St', | 
|  | 197 | +                             line_2='Apt 1234', | 
|  | 198 | +                             city='Seattle', | 
|  | 199 | +                             state='WA', | 
|  | 200 | +                             zip_code='98000',) | 
|  | 201 | + | 
|  | 202 | +    the_barkers = Household(name="The Barkers", | 
|  | 203 | +                            people=(chris, donna, emma), | 
|  | 204 | +                            address=barker_address) | 
|  | 205 | + | 
|  | 206 | +    joseph = Person(last_name='Sheedy', | 
|  | 207 | +                    first_name='Joseph', | 
|  | 208 | +                    cell_phone='(234) 555-8910', | 
|  | 209 | +                    email='js@some_thing.com', | 
|  | 210 | +                    ) | 
|  | 211 | + | 
|  | 212 | +    cris = Person(last_name='Ewing', | 
|  | 213 | +                  first_name='Cris', | 
|  | 214 | +                  cell_phone='(345) 555-6789', | 
|  | 215 | +                  email='cris@a_fake_domain.com', | 
|  | 216 | +                  ) | 
|  | 217 | + | 
|  | 218 | +    fulvio = Person(last_name='Casali', | 
|  | 219 | +                    first_name='Fulvio', | 
|  | 220 | +                    cell_phone='(345) 555-1234', | 
|  | 221 | +                    email='fulvio@a_fake_domain.com', | 
|  | 222 | +                    ) | 
|  | 223 | + | 
|  | 224 | +    fred = Person(first_name="Fred", | 
|  | 225 | +                  last_name="Jones", | 
|  | 226 | +                  email='FredJones@some_company.com', | 
|  | 227 | +                  cell_phone="403-561-8911", | 
|  | 228 | +                  ) | 
|  | 229 | + | 
|  | 230 | +    python_cert_address = Address('UW Professional and Continuing Education', | 
|  | 231 | +                                  line_2='4333 Brooklyn Ave. NE', | 
|  | 232 | +                                  city='Seattle', | 
|  | 233 | +                                  state='WA', | 
|  | 234 | +                                  zip_code='98105', | 
|  | 235 | +                                  ) | 
|  | 236 | + | 
|  | 237 | +    python_cert = Business(name='Python Certification Program', | 
|  | 238 | +                           people=(chris, joseph, cris, fulvio), | 
|  | 239 | +                           address=python_cert_address | 
|  | 240 | +                           ) | 
|  | 241 | + | 
|  | 242 | + | 
|  | 243 | +    address_book = AddressBook() | 
|  | 244 | + | 
|  | 245 | +    address_book.add_person(chris) | 
|  | 246 | +    address_book.add_person(donna) | 
|  | 247 | +    address_book.add_person(emma) | 
|  | 248 | +    address_book.add_person(cris) | 
|  | 249 | +    address_book.add_person(joseph) | 
|  | 250 | +    address_book.add_person(fulvio) | 
|  | 251 | +    address_book.add_person(fred) | 
|  | 252 | + | 
|  | 253 | +    address_book.add_household(the_barkers) | 
|  | 254 | + | 
|  | 255 | +    address_book.add_business(python_cert) | 
|  | 256 | + | 
|  | 257 | +    return address_book | 
|  | 258 | + | 
|  | 259 | + | 
|  | 260 | +if __name__ == "__main__": | 
|  | 261 | +    address_book = create_sample() | 
|  | 262 | + | 
|  | 263 | +    print("Here is the address book") | 
|  | 264 | +    print(address_book) | 
|  | 265 | + | 
|  | 266 | + | 
0 commit comments