1.5. About Entry Test
1.5.1. Assignments
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: About EntryTest ToListDict
# - Difficulty: easy
# - Lines: 2
# - Minutes: 5
# %% English
# 1. Define `result: list[dict]`:
# 2. Convert `DATA` from `list[tuple]` to `list[dict]`
# - key - name from the header
# - value - numerical value or species name
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[dict]`:
# 2. Przekonwertuj `DATA` z `list[tuple]` do `list[dict]`
# - klucz - nazwa z nagłówka
# - wartość - wartość numeryczna lub nazwa gatunku
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Why
# - Convert data from `list[tuple]` to `list[dict]`
# - `list[tuple]` is used to represent CSV data
# - `list[tuple]` is used to represent database rows
# - `list[dict]` is used to represent JSON data
# - CSV is the most popular format in data science
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> result = list(result)
>>> assert type(result) is list, \
'Result must be a list'
>>> assert len(result) > 0, \
'Result cannot be empty'
>>> assert all(type(element) is dict for element in result), \
'All elements in result must be a dict'
>>> from pprint import pprint
>>> pprint(result, sort_dicts=False)
[{'firstname': 'Mark', 'lastname': 'Watney', 'age': 41},
{'firstname': 'Melissa', 'lastname': 'Lewis', 'age': 40},
{'firstname': 'Rick', 'lastname': 'Martinez', 'age': 39},
{'firstname': 'Alex', 'lastname': 'Vogel', 'age': 40},
{'firstname': 'Chris', 'lastname': 'Beck', 'age': 36},
{'firstname': 'Beth', 'lastname': 'Johanssen', 'age': 29}]
"""
DATA = [
('firstname', 'lastname', 'age'),
('Mark', 'Watney', 41),
('Melissa', 'Lewis', 40),
('Rick', 'Martinez', 39),
('Alex', 'Vogel', 40),
('Chris', 'Beck', 36),
('Beth', 'Johanssen', 29),
]
# Convert `DATA` from `list[tuple]` to `list[dict]`
# - key - name from the header
# - value - numerical value or species name
# type: list[dict[str,str|int]]
result = ...
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: About EntryTest ToListTuple
# - Difficulty: easy
# - Lines: 5
# - Minutes: 5
# %% English
# 1. Load `DATA` from `list[dict]` format (similar to JSON)
# 2. Convert data to `result: list[tuple]`
# 3. Add header as a first line
# 4. Run doctests - all must succeed
# %% Polish
# 1. Wczytaj `DATA` z formatu `list[dict]` (podobny do JSON)
# 2. Przekonwertuj dane do `result: list[tuple]`
# 3. Dodaj nagłówek jako pierwszą linię
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Why
# - Convert data from `list[dict]` to `list[tuple]`
# - `list[dict]` is used to represent JSON data
# - `list[tuple]` is used to represent CSV data
# - `list[tuple]` is used to represent database rows
# - JSON is the most popular format in web development
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> result = list(result)
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert len(result) > 0, \
'Variable `result` should not be empty'
>>> assert all(type(row) is tuple for row in result), \
'Variable `result` should be a list[tuple]'
>>> from pprint import pprint
>>> pprint(result)
[('firstname', 'lastname', 'age'),
('Mark', 'Watney', 41),
('Melissa', 'Lewis', 40),
('Rick', 'Martinez', 39),
('Alex', 'Vogel', 40),
('Chris', 'Beck', 36),
('Beth', 'Johanssen', 29)]
"""
DATA = [
{'firstname': 'Mark', 'lastname': 'Watney', 'age': 41},
{'firstname': 'Melissa', 'lastname': 'Lewis', 'age': 40},
{'firstname': 'Rick', 'lastname': 'Martinez', 'age': 39},
{'firstname': 'Alex', 'lastname': 'Vogel', 'age': 40},
{'firstname': 'Chris', 'lastname': 'Beck', 'age': 36},
{'firstname': 'Beth', 'lastname': 'Johanssen', 'age': 29},
]
# Define `result` with `DATA` converted from `list[dict]` to `list[tuple]`
# Add header as a first line
# type: header = tuple[str,...]
# type: row = tuple[float,float,float,float,str]
# type: list[tuple[header|row,...]]
result = ...
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: About EntryTest Endswith
# - Difficulty: easy
# - Lines: 5
# - Minutes: 5
# %% English
# 1. Define `result: list[str]` with all email addresses having domain name in `DOMAINS`
# 2. Search for emails only in `DATA` -> `values`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[str]` z wszystkimi adresami email mającymi domenę z `DOMAINS`
# 2. Szukaj adresów email tylko w `DATA` -> `values`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Why
# - Check if you can filter data
# - Check if you know string methods
# - Check if you know how to iterate over `list[dict]`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is list, \
'Result must be a list'
>>> assert len(result) > 0, \
'Result cannot be empty'
>>> assert all(type(element) is str for element in result), \
'All elements in result must be a str'
>>> from pprint import pprint
>>> result = sorted(result)
>>> pprint(result)
['avogel@esa.int',
'bjohanssen@nasa.gov',
'cbeck@nasa.gov',
'mlewis@nasa.gov',
'mwatney@nasa.gov',
'rmartinez@nasa.gov']
"""
DATA = {
'project': 'myproject',
'app': 'ares',
'model': 'User',
'values': [
{'pk': 1, 'firstname': 'Melissa', 'lastname': 'Lewis', 'email': 'mlewis@nasa.gov'},
{'pk': 2, 'firstname': 'Rick', 'lastname': 'Martinez', 'email': 'rmartinez@nasa.gov'},
{'pk': 3, 'firstname': 'Alex', 'lastname': 'Vogel', 'email': 'avogel@esa.int'},
{'pk': 4, 'firstname': 'Pan', 'lastname': 'Twardowski', 'email': 'ptwardowski@polsa.gov.pl'},
{'pk': 5, 'firstname': 'Chris', 'lastname': 'Beck', 'email': 'cbeck@nasa.gov'},
{'pk': 6, 'firstname': 'Beth', 'lastname': 'Johanssen', 'email': 'bjohanssen@nasa.gov'},
{'pk': 7, 'firstname': 'Mark', 'lastname': 'Watney', 'email': 'mwatney@nasa.gov'},
{'pk': 8, 'firstname': 'Ivan', 'lastname': 'Ivanovich', 'email': 'iivanovich@roscosmos.ru'},
]}
DOMAINS = ('esa.int', 'nasa.gov')
# Define `result: list[str]` with all email addresses having domain name in `DOMAINS`
# Search for emails only in `DATA` -> `values`
# type: list[str]
result = ...
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: About EntryTest ListDict
# - Difficulty: medium
# - Lines: 9
# - Minutes: 13
# %% English
# 1. Define `result: list[dict]`, where each dict has keys:
# - ip: str
# - hosts: list[str]
# 2. Iterate over lines in `DATA` skipping comments (`#`) and empty lines
# 3. Extract from each line: `ip` and `hosts`
# 4. Add `ip` and `hosts` to `result` as a dict, example:
# {'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int']}
# 5. Each line must be a separate dict
# 6. Mind, that for 127.0.0.1 there will be two separate entries
# 7. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[dict]`, gdzie każdy dict ma klucze:
# - ip: str
# - hosts: list[str]
# 2. Iteruj po liniach w `DATA` pomijając komentarze (`#`) i puste linie
# 3. Wyciągnij z każdej linii: `ip` i `hosts`
# 4. Dodaj `ip` i `hosts` do `result` jako słownik, przykład:
# {'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int']}
# 5. Każda linia ma być osobnym słownikiem
# 6. Zwróć uwagę, że dla 127.0.0.1 będą dwa osobne wiersze
# 7. Uruchom doctesty - wszystkie muszą się powieść
# %% Why
# - Check if you know how to parse files
# - Check if you can filter strings
# - Check if you know string methods
# - Check if you know how to iterate over `list[dict]`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> result = list(result)
>>> assert len(result) > 0, \
'Result cannot be empty'
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert all(type(x) is dict for x in result), \
'All keys in `result` should be dict'
>>> from pprint import pprint
>>> pprint(result, width=120, sort_dicts=False)
[{'ip': '127.0.0.1', 'hosts': ['localhost']},
{'ip': '127.0.0.1', 'hosts': ['astromatt']},
{'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int']},
{'ip': '255.255.255.255', 'hosts': ['broadcasthost']},
{'ip': '::1', 'hosts': ['localhost']}]
"""
DATA = """##
# `/etc/hosts` structure:
# - ip: internet protocol address (IPv4 or IPv6)
# - hosts: host names
##
127.0.0.1 localhost
127.0.0.1 astromatt
10.13.37.1 nasa.gov esa.int
255.255.255.255 broadcasthost
::1 localhost"""
# Skip comments (`#`) and empty lines
# Extract from each line: `ip` and `hosts`
# Add `ip` and `hosts` to `result` as a dict, example:
# {'ip': '10.13.37.1', 'hosts': ['nasa.gov', 'esa.int']}
# Each line must be a separate dict
# Mind, that for 127.0.0.1 there will be two separate entries
# type: list[dict]
result = ...