# %% 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 = User('Alice', 'Apricot')
# >>> print(result)
# Alice Apricot
#
# >>> result = User('Bob', 'Blackthorn', groups=[
# ... Group(gid=1, name='admins'),
# ... Group(gid=2, name='staff'),
# ... ])
# >>> print(result)
# Bob Blackthorn member of [1(admins), 2(staff)]
#
# >>> result = Accounts([
# ... User('Alice', 'Apricot', groups=[
# ... Group(gid=2, name='staff'),
# ... ]),
# ... User('Bob', 'Blackthorn', groups=[
# ... Group(gid=1, name='admins'),
# ... Group(gid=2, name='staff'),
# ... ]),
# ... User('Rick', 'Martinez'),
# ... ])
# >>>
# >>> print(result) # doctest: +NORMALIZE_WHITESPACE
# Alice Apricot member of [2(staff)]
# Bob Blackthorn member of [1(admins), 2(staff)]
# Rick Martinez
# %% 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'
>>> result = User('Alice', 'Apricot')
>>> print(result)
Alice Apricot
>>> result = User('Bob', 'Blackthorn')
>>> print(result)
Bob Blackthorn
>>> result = User('Rick', 'Martinez')
>>> print(result)
Rick Martinez
>>> Group(gid=1, name='admins')
1(admins)
>>> Group(gid=2, name='staff')
2(staff)
>>> Group(gid=3, name='users')
3(users)
>>> result = Accounts(users=[])
>>> print(result)
<BLANKLINE>
>>> result = User('Alice', 'Apricot', groups=[
... Group(gid=2, name='staff'),
... ])
>>> print(result)
Alice Apricot member of [2(staff)]
>>> result = User('Bob', 'Blackthorn', groups=[
... Group(gid=1, name='admins'),
... Group(gid=2, name='staff'),
... ])
>>> print(result)
Bob Blackthorn member of [1(admins), 2(staff)]
>>> result = Accounts([
... User('Alice', 'Apricot', groups=[
... Group(gid=2, name='staff'),
... ]),
... User('Bob', 'Blackthorn', groups=[
... Group(gid=1, name='admins'),
... Group(gid=2, name='staff'),
... ]),
... User('Rick', 'Martinez'),
... ])
>>>
>>> print(result) # doctest: +NORMALIZE_WHITESPACE
Alice Apricot member of [2(staff)]
Bob Blackthorn member of [1(admins), 2(staff)]
Rick Martinez
"""
# %% 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