4.7. Relations

4.7.1. Relations

from typing import List


class Astronaut:
    def __init__(self, name: str, experience: List[Missions] = ()) -> None:
        self.name = name
        self.experience = experience

class Mission:
    def __init__(self, year: int, name: str) -> None:
        self.year = year
        self.name = name


CREW = [
    Astronaut('Jan Twardowski', experience=(
        Mission(1969, 'Apollo 11'),
        Mission(2024, 'Artemis 3'))),

    Astronaut('Mark Watney', experience=(
        Mission(2035, 'Ares 3'))),

    Astronaut('Melissa Lewis'),
]

4.7.2. Assignments

4.7.2.1. Basic Address Book

English
  1. Create Address Book based on input data (see below)

  2. Model data using classes and relations

  3. We should have three classes, name it

Polish
  1. Dla danych z listingu poniżej napisz książkę adresową

  2. W zadaniu mamy do czynienia z trzema klasami, wymień je.

  3. Zamodeluj problem wykorzystując trzy klasy i relacje między nimi

The whys and wherefores
  • OOP modeling

  • working with objects

  • nesting objects and relations

  • casting objects to str

Input
Listing 483. Address Book
[
    {"first_name": "Jan", "last_name": "Twardowski", "addresses": [
        {"street": "Kamienica Pod św. Janem Kapistranem", "city": "Kraków", "post_code": "31-008", "region": "Małopolskie", "country": "Poland"}]},

    {"first_name": "José", "last_name": "Jiménez", "addresses": [
        {"street": "2101 E NASA Pkwy", "city": "Houston", "post_code": 77058, "region": "Texas", "country": "USA"},
        {"street": "", "city": "Kennedy Space Center", "post_code": 32899, "region": "Florida", "country": "USA"}]},

    {"first_name": "Mark", "last_name": "Watney", "addresses": [
        {"street": "4800 Oak Grove Dr", "city": "Pasadena", "post_code": 91109, "region": "California", "country": "USA"},
        {"street": "2825 E Ave P", "city": "Palmdale", "post_code": 93550, "region": "California", "country": "USA"}]},

    {"first_name": "Иван", "last_name": "Иванович", "addresses": [
        {"street": "", "city": "Космодро́м Байкону́р", "post_code": "", "region": "Кызылординская область", "country": "Қазақстан"},
        {"street": "", "city": "Звёздный городо́к", "post_code": 141160, "region": "Московская область", "country": "Россия"}]},

    {"first_name": "Melissa", "last_name": "Lewis", "addresses": []},

    {"first_name": "Alex", "last_name": "Vogel", "addresses": [
        {"street": "Linder Hoehe", "city": "Köln", "post_code": 51147, "region": "North Rhine-Westphalia", "country": "Germany"}]}
]

4.7.2.2. Object serialization to CSV

  • Complexity level: hard

  • Lines of code to write: 60 lines

  • Estimated time of completion: 20 min

  • Solution: solution/relations_flat.py

English
  1. Using csv.DictWriter() save contacts from addressbook to CSV file

  2. How to write relations to CSV file (contact has many addresses)?

  3. Recreate object structure from CSV file

Polish
  1. Za pomocą csv.DictWriter() zapisz kontakty z książki adresowej w pliku

  2. Jak zapisać w CSV dane relacyjne (kontakt ma wiele adresów)?

  3. Odtwórz strukturę obiektów na podstawie danych odczytanych z pliku

Non functional requirements
  1. All fields must be enclosed by double quote " character

  2. Use ; to separate columns

  3. Use utf-8 encoding

  4. Use Unix newline

Input
class Contact:
     def __init__(self, first_name, last_name, addresses=()):
         self.first_name = first_name
         self.last_name = last_name
         self.addresses = addresses


 class Address:
     def __init__(self, location, city):
         self.location = location
         self.city = city


 INPUT = [
     Contact(first_name='Jan', last_name='Twardowski', addresses=(
         Address(location='Johnson Space Center', city='Houston, TX'),
         Address(location='Kennedy Space Center', city='Merritt Island, FL'),
         Address(location='Jet Propulsion Laboratory', city='Pasadena, CA'),
     )),
     Contact(first_name='Mark', last_name='Watney'),
     Contact(first_name='Melissa', last_name='Lewis', addresses=()),
 ]