16.25. OOP Recap

16.25.1. Assignments

"""
* Assignment: OOP Recap Class
* Type: class assignment
* Complexity: easy
* Lines of code: 4 lines
* Time: 2 min

English:
    1. Define class `Astronaut`
    2. Define class `SpaceAgency`
    3. Run doctests - all must succeed

Polish:
    1. Zdefiniuj klasę `Astronaut`
    2. Zdefiniuj klasę `SpaceAgency`
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import isclass

    >>> assert isclass(Astronaut)
    >>> assert isclass(SpaceAgency)
"""

# Define class `Astronaut`
# type: type
...

# Define class `SpaceAgency`
# type: type
...

"""
* Assignment: OOP Recap Instance
* Type: class assignment
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Create instance `mark` of a class `Astronaut`
    2. Create instance `nasa` of a class `SpaceAgency`
    3. Run doctests - all must succeed

Polish:
    1. Stwórz instancję `mark` klasy `Astronaut`
    2. Stwórz instancję `nasa` klasy `SpaceAgency`
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import isclass

    >>> assert isclass(Astronaut)
    >>> assert isclass(SpaceAgency)
    >>> assert isinstance(mark, Astronaut)
    >>> assert isinstance(nasa, SpaceAgency)
"""

class Astronaut:
    pass


class SpaceAgency:
    pass


# Create instance `mark` of a class `Astronaut`
# type: Astronaut
...

# Create instance `nasa` of a class `SpaceAgency`
# type: SpaceAgency
...

"""
* Assignment: OOP Recap Fields
* Type: class assignment
* Complexity: easy
* Lines of code: 6 lines
* Time: 2 min

English:
    1. Modify code below
    2. Add type annotation attibutes to model the data:
       a. Mark, USA, 1969-07-21
       b. Nasa, USA, 1969-07-21
    3. Run doctests - all must succeed

Polish:
    1. Zmodyfikuj kod poniżej
    2. Dodaj anotację typów atrybutów by zamodelować dane:
       a. Mark, USA, 1969-07-21
       b. Nasa, USA, 1969-07-21
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert len(Astronaut.__annotations__) == 3
    >>> assert len(SpaceAgency.__annotations__) == 3
"""


# Mark, USA, 1969-07-21
# Nasa, USA, 1969-07-21

class Astronaut:
    ...


class SpaceAgency:
    ...


"""
* Assignment: OOP Recap Fields
* Type: class assignment
* Complexity: easy
* Lines of code: 6 lines
* Time: 3 min

English:
    1. Modify code below
    2. Set attibutes of mark and nasa instances to model the data:
       a. Mark, USA, 1969-07-21
       b. Nasa, USA, 1969-07-21
    3. Run doctests - all must succeed

Polish:
    1. Zmodyfikuj kod poniżej
    2. Ustaw atrybuty instancji mark i nasa by zamodelować dane:
       a. Mark, USA, 1969-07-21
       b. Nasa, USA, 1969-07-21
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert isinstance(mark, Astronaut)
    >>> assert isinstance(nasa, SpaceAgency)
    >>> assert 'Mark' in vars(mark).values()
    >>> assert 'USA' in vars(mark).values()
    >>> assert '1969-07-21' in vars(mark).values()
    >>> assert 'Nasa' in vars(nasa).values()
    >>> assert 'USA' in vars(nasa).values()
    >>> assert '1969-07-21' in vars(nasa).values()
"""


class Astronaut:
    name: str
    country: str
    date: str


class SpaceAgency:
    name: str
    country: str
    date: str


mark = Astronaut()
nasa = SpaceAgency()

# Mark, USA, 1969-07-21
# Nasa, USA, 1969-07-21


"""
* Assignment: OOP Recap Vars
* Type: class assignment
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Define `result_mark: dict[str,str]`
       with all attributes and values of `mark` object
    2. Define `result_nasa: dict[str,str]`
       with all attributes and values of `nasa` object
    3. Use `vars()`
    4. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result_mark: dict[str,str]`
       z wszystkimi atrybutami i wartościami obiektu `mark`
    2. Zdefiniuj `result_nasa: dict[str,str]`
       z wszystkimi atrybutami i wartościami obiektu `nasa`
    3. Użyj `vars()`
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert type(result_mark) is dict
    >>> assert type(result_nasa) is dict

    >>> result_mark
    {'name': 'Mark', 'country': 'USA', 'date': '1969-07-21'}

    >>> result_nasa
    {'name': 'Nasa', 'country': 'USA', 'date': '1969-07-21'}
"""


class Astronaut:
    name: str
    country: str
    date: str


class SpaceAgency:
    name: str
    country: str
    date: str


mark = Astronaut()
nasa = SpaceAgency()

mark.name = 'Mark'
mark.country = 'USA'
mark.date = '1969-07-21'

nasa.name = 'Nasa'
nasa.country = 'USA'
nasa.date = '1969-07-21'


# Dict with all attributes and values of `mark` object
# type: dict[str,str]
result_mark = ...

# Dict with all attributes and values of `nasa` object
# type: dict[str,str]
result_nasa = ...


"""
* Assignment: OOP Recap Init
* Type: class assignment
* Complexity: easy
* Lines of code: 8 lines
* Time: 3 min

English:
    1. Modify code below
    2. Define method `__init__()` to set attributes:
       `self.name`, `self.country`, `self.date`
    3. Attributes must be set from arguments passed at initialization
    4. Run doctests - all must succeed

Polish:
    1. Zmodyfikuj kod poniżej
    2. Zdefiniuj metodę `__init__()` aby ustawiała atrybuty:
       `self.name`, `self.country`, `self.date`
    3. Atrybuty muszą być ustawiane z argumentów podanych przy inicjalizacji
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import ismethod, signature

    >>> mark = Astronaut('Mark', 'USA', '1969-07-21')
    >>> nasa = Astronaut('Nasa', 'USA', '1969-07-21')

    >>> assert ismethod(mark.__init__)
    >>> assert ismethod(nasa.__init__)

    >>> signature(Astronaut.__init__)
    <Signature (self, name, country, date)>
    >>> signature(SpaceAgency.__init__)
    <Signature (self, name, country, date)>

    >>> signature(mark.__init__)
    <Signature (name, country, date)>
    >>> signature(nasa.__init__)
    <Signature (name, country, date)>
"""


# Define method to set: self.name, self.country, self.date
# Attributes must be set from arguments passed to __init__()
# type: type[Astronaut]
class Astronaut:
    name: str
    country: str
    date: str


# Define method to set: self.name, self.country, self.date
# Attributes must be set from arguments passed to __init__()
# type: type[Astronaut]
class SpaceAgency:
    name: str
    country: str
    date: str


"""
* Assignment: OOP Recap Instantiate
* Type: class assignment
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Modify code below
    2. Create instances of an `Astronaut` and `SpaceAgency` classes
    3. Use positional arguments to pass values at the initialization
    4. Run doctests - all must succeed

Polish:
    1. Zmodyfikuj kod poniżej
    2. Stwórz instancje klas `Astronaut` i `SpaceAgency`
    3. Użyj argumentów pozycyjnych do przekazania wartości przy inicjalizacji
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert isinstance(mark, Astronaut)
    >>> assert isinstance(nasa, SpaceAgency)
    >>> assert 'Mark' in vars(mark).values()
    >>> assert 'USA' in vars(mark).values()
    >>> assert '1969-07-21' in vars(mark).values()
    >>> assert 'Nasa' in vars(nasa).values()
    >>> assert 'USA' in vars(nasa).values()
    >>> assert '1969-07-21' in vars(nasa).values()
"""


class Astronaut:
    def __init__(self, name, country, date):
        self.name = name
        self.country = country
        self.date = date


class SpaceAgency:
    def __init__(self, name, country, date):
        self.name = name
        self.country = country
        self.date = date


# use positional arguments to create instance with: Mark, USA, 1969-07-21
# type: Astronaut
mark = ...

# use positional arguments to create instance with: Nasa, USA, 1969-07-21
# type: SpaceAgency
nasa = ...


"""
* Assignment: OOP Recap Instantiate
* Type: class assignment
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Modify code below
    2. Create instances of an `Astronaut` and `SpaceAgency` classes
    3. Use keyword arguments to pass values at the initialization
    4. Run doctests - all must succeed

Polish:
    1. Zmodyfikuj kod poniżej
    2. Stwórz instancje klas `Astronaut` i `SpaceAgency`
    3. Użyj argumentów nazwanych do przekazania wartości przy inicjalizacji
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert isinstance(mark, Astronaut)
    >>> assert isinstance(nasa, SpaceAgency)
    >>> assert 'Mark' in vars(mark).values()
    >>> assert 'USA' in vars(mark).values()
    >>> assert '1969-07-21' in vars(mark).values()
    >>> assert 'Nasa' in vars(nasa).values()
    >>> assert 'USA' in vars(nasa).values()
    >>> assert '1969-07-21' in vars(nasa).values()
"""


class Astronaut:
    def __init__(self, name, country, date):
        self.name = name
        self.country = country
        self.date = date


class SpaceAgency:
    def __init__(self, name, country, date):
        self.name = name
        self.country = country
        self.date = date


# use keyword arguments to create instance with: Mark, USA, 1969-07-21
# type: Astronaut
mark = ...

# use keyword arguments to create instance with: Nasa, USA, 1969-07-21
# type: SpaceAgency
nasa = ...