19.13. OOP Recap

19.13.1. Atlassian Python API

$ python -m pip install atlassian-python-api
from atlassian import Jira


jira = Jira(
    url='https://example.com:8080',
    username='myusername',
    password='mypassword')

JQL = 'project = DEMO AND status IN ("To Do", "In Progress") ORDER BY issuekey'

result = jira.jql(JQL)
print(result)
from atlassian import Confluence


confluence = Confluence(
    url='https://example.com:8090',
    username='myusername',
    password='mypassword')

result = confluence.create_page(
    space='DEMO',
    title='This is the title',
    body='This is the body. You can use <strong>HTML tags</strong>!')

print(result)

19.13.2. Assignments

# %% About
# - Name: OOP Recap Class
# - Difficulty: easy
# - Lines: 4
# - Minutes: 2

# %% 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. 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ść

# %% Expected
# >>> isinstance(Astronaut, type)
# True
#
# >>> isinstance(SpaceAgency, type)
# True

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> from inspect import isclass

>>> assert isclass(Astronaut), \
'Object `Astronaut` has an invalid type; expected: `class`.'

>>> assert isclass(SpaceAgency), \
'Object `SpaceAgency` has an invalid type; expected: `class`.'

>>> isinstance(Astronaut, type)
True

>>> isinstance(SpaceAgency, type)
True
"""

# %% 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
Astronaut: type
SpaceAgency: type

# %% Data

# %% Result

# %% About
# - Name: OOP Recap Instance
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2

# %% 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. 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ść

# %% Expected
# >>> isinstance(mark, Astronaut)
# True
#
# >>> isinstance(nasa, SpaceAgency)
# True

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> from inspect import isclass

>>> assert isclass(Astronaut), \
'Object `Astronaut` has an invalid type; expected: `class`.'

>>> assert isclass(SpaceAgency), \
'Object `SpaceAgency` has an invalid type; expected: `class`.'

>>> isinstance(mark, Astronaut)
True

>>> isinstance(nasa, SpaceAgency)
True
"""

# %% 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
mark: object
nasa: object

# %% Data
class Astronaut:
    pass


class SpaceAgency:
    pass

# %% Result

# %% About
# - Name: OOP Recap Init
# - Difficulty: easy
# - Lines: 8
# - Minutes: 3

# %% 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 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ść

# %% Expected
# TODO: Write expected result

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> 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)>
"""

# %% 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
Astronaut: type
SpaceAgency: type

# %% Data

# %% Result
class Astronaut:
    pass

class SpaceAgency:
    pass


# %% About
# - Name: OOP Recap Instantiate
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2

# %% 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 code below
# 2. Set `mark` instance attributes to model the data:
#    {'name': 'Mark', 'country': 'USA', 'date': '1969-07-21'}
# 3. Set `nasa` instance attributes to model the data:
#    {'name': 'Nasa', 'country': 'USA', 'date': '1969-07-21'}
# 4. Run doctests - all must succeed

# %% Polish
# 1. Zmodyfikuj kod poniżej
# 2. Ustaw atrybuty instancji `mark` aby zamodelować dane:
#    {'name': 'Mark', 'country': 'USA', 'date': '1969-07-21'}
# 3. Ustaw atrybuty instancji `nasa` aby zamodelować dane:
#    {'name': 'Nasa', 'country': 'USA', 'date': '1969-07-21'}
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# TODO: Write expected result

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> 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()
"""

# %% 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
mark: object
nasa: object

# %% Data
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

# %% Result
mark = ...
nasa = ...

# %% About
# - Name: OOP Recap Vars
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2

# %% 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. 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ść

# %% Expected
# >>> result_mark
# {'name': 'Mark', 'country': 'USA', 'date': '1969-07-21'}
#
# >>> result_nasa
# {'name': 'Nasa', 'country': 'USA', 'date': '1969-07-21'}

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> 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'}
"""

# %% 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
result_mark: dict[str,str]
result_nasa: dict[str,str]

# %% Data
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


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

# %% Result
result_mark = ...
result_nasa = ...

# %% About
# - Name: OOP Recap Calculator
# - Difficulty: easy
# - Lines: 13
# - 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. Define class `Calculator` with methods:
#    - `__init__()` takes two arguments `a` and `b` and sets them as fields
#    - `add()` which returns sum of `a` and `b`
#    - `sub()` which returns difference of `a` and `b`
#    - `mul()` which returns product of `a` and `b`
#    - `div()` which returns division of `a` and `b`
#    - `mean()` which returns arithemetic average of `a` and `b`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj klasę `Calculator` z metodami:
#    - `__init__()` przyjmuje dwa argumenty `a` i `b` i ustawia je jako pola
#    - `add()` która zwraca sumę `a` i `b`
#    - `sub()` która zwraca różnicę `a` i `b`
#    - `mul()` która zwraca iloczyn `a` i `b`
#    - `div()` która zwraca iloraz `a` i `b`
#    - `mean()` która zwraca średnią arytmetyczną `a` i `b`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> calc = Calculator(1, 2)
#
# >>> calc.add()
# 3
#
# >>> calc.sub()
# -1
#
# >>> calc.mul()
# 2
#
# >>> calc.div()
# 0.5
#
# >>> calc.mean()
# 1.5

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> from inspect import isclass, ismethod

>>> assert isclass(Calculator), \
'Object `Calculator` has an invalid type; expected: `class`.'

>>> calc = Calculator(1, 2)

>>> assert ismethod(calc.add)
>>> assert ismethod(calc.sub)
>>> assert ismethod(calc.mul)
>>> assert ismethod(calc.div)
>>> assert ismethod(calc.mean)

>>> calc.add()
3
>>> calc.sub()
-1
>>> calc.mul()
2
>>> calc.div()
0.5
>>> calc.mean()
1.5
"""

# %% 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
Calculator: type
__init__: Callable[[object, int|float, int|float], None]
add: Callable[[object], int|float]
sub: Callable[[object], int|float]
mul: Callable[[object], int|float]
div: Callable[[object], int|float]
mean: Callable[[object], int|float]

# %% Data

# %% Result
class Calculator:
    ...

# %% 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ść

# %% Expected
# >>> 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 has an is invalid version; expected: `3.9` or newer.'

>>> 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