4.7. Relations

4.7.1. Relations

from typing import Sequence, List


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

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


CREW: List[Astronaut] = [
    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. OOP Relations Model

English
  1. Use data from "Input" section (see below)

  2. Model data using classes and relations

  3. We should have three classes, name it

  4. Create instances dynamically based on DATA

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

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

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

  4. Twórz instancje dynamicznie na podstawie DATA

The whys and wherefores
  • OOP modeling

  • working with objects

  • nesting objects and relations

  • casting objects to str

Input
Listing 4.69. Python List[dict] or JSON?
[
    {"firstname": "Jan", "lastname": "Twardowski", "addresses": [
        {"street": "Kamienica Pod św. Janem Kapistranem", "city": "Kraków", "postcode": "31-008", "region": "Małopolskie", "country": "Poland"}]},

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

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

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

    {"firstname": "Melissa", "lastname": "Lewis", "addresses": []},

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

4.7.2.2. OOP Relations Flatten

English
  1. Use code from "Input" section (see below)

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

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

  4. Recreate object structure from CSV file

  5. Non-functional requirements:

    • All fields must be enclosed by double quote " character

    • Use ; to separate columns

    • Use utf-8 encoding

    • Use Unix \n newline

Polish
  1. Użyj kodu z sekcji "Input" (patrz poniżej)

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

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

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

  5. Wymagania niefunkcjonalne:

    • Wszystkie pola muszą być otoczone znakiem cudzysłowu "

    • Użyj ; do oddzielenia kolumn

    • Użyj kodowania utf-8

    • Użyj zakończenia linii Unix \n

Input
class Contact:
     def __init__(self, firstname, lastname, addresses=()):
         self.firstname = firstname
         self.lastname = lastname
         self.addresses = addresses


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


 DATA = [
     Contact(firstname='Jan', lastname='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(firstname='Mark', lastname='Watney'),
     Contact(firstname='Melissa', lastname='Lewis', addresses=()),
 ]

4.7.2.3. OOP Relations Nested

English
  1. Client can open a bank account

  2. Client can have many accounts

  3. Bank has many clients

  4. Each account has unique number generated when opening an account

  5. Client can ask about number of all of his accounts

  6. Client can add money to the account

  7. Client can withdraw money from the account

Polish
  1. Klient może otworzyć konto w banku

  2. Klient może mieć wiele kont

  3. Bank może mieć wielu klientów

  4. Każde konto ma unikalny numer, który jest generowany przy zakładaniu

  5. Klient może odpytać o numery wszystkich swoich kont

  6. Klient może wpłacić pieniądze na swoje konto

  7. Klient może wybrać pieniądze z bankomatu