11.6. OOP Recap

11.6.1. Assignments

# %% About
# - Name: OOP Recap Nested
# - Difficulty: medium
# - Lines: 9
# - Minutes: 5

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Modify classes to overwrite `__str__()` and `__repr__()` methods
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zmodyfikuj klasy aby nadpisać metody `__str__()` and `__repr__()`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> result = Accounts(users=[])
# >>> print(result)
# <BLANKLINE>
#
# >>> result = User('Alice', 'Apricot', groups=[
# ...     Group(gid=1, name='users'),
# ... ])
# >>> print(result)
# Alice Apricot member of [1(users)]
#
# >>> result = User('Bob', 'Blackthorn', groups=[
# ...     Group(gid=1, name='users'),
# ...     Group(gid=2, name='staff'),
# ... ])
# >>> print(result)
# Bob Blackthorn member of [1(users), 2(staff)]
#
# >>> result = User('Carol', 'Corn', groups=[
# ...     Group(gid=1, name='users'),
# ...     Group(gid=2, name='staff'),
# ...     Group(gid=3, name='admins'),
# ... ])
# >>> print(result)
# Carol Corn member of [1(users), 2(staff), 3(admins)]
#
# >>> result = User('Dave', 'Durian')
# >>> print(result)
# Dave Durian
#
# >>> result = Accounts([
# ...     User('Alice', 'Apricot', groups=[
# ...         Group(gid=1, name='users'),
# ...     ]),
# ...     User('Bob', 'Blackthorn', groups=[
# ...         Group(gid=1, name='users'),
# ...         Group(gid=2, name='staff'),
# ...     ]),
# ...     User('Carol', 'Corn', groups=[
# ...         Group(gid=1, name='users'),
# ...         Group(gid=2, name='staff'),
# ...         Group(gid=3, name='admins'),
# ...     ]),
# ...     User('Dave', 'Durian'),
# ... ])
# >>>
# >>> print(result)  # doctest: +NORMALIZE_WHITESPACE
# Alice Apricot member of [1(users)]
# Bob Blackthorn member of [1(users), 2(staff)]
# Carol Corn member of [1(users), 2(staff), 3(admins)]
# Dave Durian

# %% Hints
# - Define `Accounts.__str__()`
# - Define `User.__str__()`
# - Define `Group.__repr__()`
# - Printing list will call repr on all elements

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> alice = User('Alice', 'Apricot')
>>> print(alice)
Alice Apricot

>>> bob = User('Bob', 'Blackthorn')
>>> print(bob)
Bob Blackthorn

>>> carol = User('Carol', 'Corn')
>>> print(carol)
Carol Corn

>>> Group(gid=1, name='users')
1(users)
>>> Group(gid=2, name='staff')
2(staff)
>>> Group(gid=3, name='admins')
3(admins)

>>> result = Accounts(users=[])
>>> print(result)
<BLANKLINE>

>>> result = User('Alice', 'Apricot', groups=[
...     Group(gid=1, name='users'),
... ])
>>> print(result)
Alice Apricot member of [1(users)]

>>> result = User('Bob', 'Blackthorn', groups=[
...     Group(gid=1, name='users'),
...     Group(gid=2, name='staff'),
... ])
>>> print(result)
Bob Blackthorn member of [1(users), 2(staff)]

>>> result = User('Carol', 'Corn', groups=[
...     Group(gid=1, name='users'),
...     Group(gid=2, name='staff'),
...     Group(gid=3, name='admins'),
... ])
>>> print(result)
Carol Corn member of [1(users), 2(staff), 3(admins)]

>>> result = User('Dave', 'Durian')
>>> print(result)
Dave Durian

>>> result = Accounts([
...     User('Alice', 'Apricot', groups=[
...         Group(gid=1, name='users'),
...     ]),
...     User('Bob', 'Blackthorn', groups=[
...         Group(gid=1, name='users'),
...         Group(gid=2, name='staff'),
...     ]),
...     User('Carol', 'Corn', groups=[
...         Group(gid=1, name='users'),
...         Group(gid=2, name='staff'),
...         Group(gid=3, name='admins'),
...     ]),
...     User('Dave', 'Durian'),
... ])
>>>
>>> print(result)  # doctest: +NORMALIZE_WHITESPACE
Alice Apricot member of [1(users)]
Bob Blackthorn member of [1(users), 2(staff)]
Carol Corn member of [1(users), 2(staff), 3(admins)]
Dave Durian
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
from typing import Callable
Accounts: type
User: type
Group: type
__repr__: Callable[[object], str]
__str__: Callable[[object], str]

# %% Data

# %% Result
class Accounts:
    def __init__(self, users):
        self.users = users


class User:
    def __init__(self, firstname, lastname, groups=None):
        self.firstname = firstname
        self.lastname = lastname
        self.groups = groups if groups else []


class Group:
    def __init__(self, gid, name):
        self.gid = gid
        self.name = name