13.5. Pickle Loads

13.5.1. SetUp

>>> import pickle

13.5.2. Assignments

# %% About
# - Name: Pickle Load Tuple
# - 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: tuple` with deserialized data from `DATA`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: tuple` z zdeserializowanymi danymi z `DATA`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# ('Mark', 'Watney', 41)

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

>>> assert type(result) is tuple, \
'Variable `result` has invalid type, should be tuple'

>>> from pprint import pprint
>>> pprint(result, width=79, sort_dicts=False)
('Mark', 'Watney', 41)
"""

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

# %% Imports
import pickle

# %% Types
result: tuple[str,str,int]

# %% Data
DATA = (
    b'\x80\x04\x95\x15\x00\x00\x00\x00\x00\x00\x00'
    b'\x8c\x04Mark\x94\x8c\x06Watney\x94K)\x87\x94.'
)

# %% Result

# %% About
# - Name: Pickle Load Dict
# - 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: dict` with deserialized data from `DATA`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: dict` z zdeserializowanymi danymi z `DATA`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# {'firstname': 'Mark', 'lastname': 'Watney', 'age': 41}

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

>>> assert type(result) is dict, \
'Variable `result` has invalid type, should be dict'

>>> from pprint import pprint
>>> pprint(result, width=79, sort_dicts=False)
{'firstname': 'Mark', 'lastname': 'Watney', 'age': 41}
"""

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

# %% Imports
import pickle

# %% Types
result: dict[str,str|int]

# %% Data
DATA = (
    b'\x80\x04\x954\x00\x00\x00\x00\x00\x00\x00'
    b'}\x94(\x8c\tfirstname\x94\x8c\x04Mark\x94'
    b'\x8c\x08lastname\x94\x8c\x06Watney\x94\x8c'
    b'\x03age\x94K)u.'
)


# %% Result

# %% About
# - Name: Pickle Load ListTuple
# - Difficulty: easy
# - Lines: 1
# - 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: list[tuple]` with deserialized data from `DATA`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: list[tuple]` z zdeserializowanymi danymi z `DATA`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# [('firstname', 'lastname', 'age'),
#  ('Mark', 'Watney', 41),
#  ('Melissa', 'Lewis', 40),
#  ('Rick', 'Martinez', 39),
#  ('Alex', 'Vogel', 40),
#  ('Chris', 'Beck', 36),
#  ('Beth', 'Johanssen', 29)]

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

>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert all(type(row) is tuple for row in result), \
'Variable `result` has invalid type, should be tuple'

>>> from pprint import pprint
>>> pprint(result, width=79, sort_dicts=False)
[('firstname', 'lastname', 'age'),
 ('Mark', 'Watney', 41),
 ('Melissa', 'Lewis', 40),
 ('Rick', 'Martinez', 39),
 ('Alex', 'Vogel', 40),
 ('Chris', 'Beck', 36),
 ('Beth', 'Johanssen', 29)]
"""

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

# %% Imports
import pickle

# %% Types
type header = tuple[str,str,str]
type row = tuple[str,str,int]
result: list[header|row]

# %% Data
DATA = (
    b"\x80\x04\x95\xa1\x00\x00\x00\x00\x00\x00\x00]"
    b"\x94(\x8c\tfirstname\x94\x8c\x08lastname\x94"
    b"\x8c\x03age\x94\x87\x94\x8c\x04Mark\x94"
    b"\x8c\x06Watney\x94K)\x87\x94\x8c\x07Melissa\x94"
    b"\x8c\x05Lewis\x94K(\x87\x94\x8c\x04Rick\x94"
    b"\x8c\x08Martinez\x94K'\x87\x94\x8c\x04Alex\x94"
    b"\x8c\x05Vogel\x94K(\x87\x94\x8c\x05Chris\x94"
    b"\x8c\x04Beck\x94K$\x87\x94\x8c\x04Beth\x94"
    b"\x8c\tJohanssen\x94K\x1d\x87\x94e."
)


# %% Result

# %% About
# - Name: Pickle Load ListDict
# - Difficulty: easy
# - Lines: 1
# - 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: list[dict]` with deserialized data from `DATA`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: list[dict]` z zdeserializowanymi danymi z `DATA`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# [{'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}]

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

>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert all(type(row) is dict for row in result), \
'Variable `result` has invalid type, should be dict'

>>> from pprint import pprint
>>> pprint(result, width=79, 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}]
"""

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

# %% Imports
import pickle

# %% Types
type row = dict[str,str|int]
result: list[row]

# %% Data
DATA = (
    b"\x80\x04\x95\xc9\x00\x00\x00\x00\x00\x00\x00]"
    b"\x94(}\x94(\x8c\tfirstname\x94\x8c\x04Mark\x94"
    b"\x8c\x08lastname\x94\x8c\x06Watney\x94"
    b"\x8c\x03age\x94K)u}\x94(h\x02\x8c\x07Melissa\x94h"
    b"\x04\x8c\x05Lewis\x94h\x06K(u}\x94(h\x02\x8c\x04"
    b"Rick\x94h\x04\x8c\x08Martinez\x94h\x06K'u}\x94"
    b"(h\x02\x8c\x04Alex\x94h\x04\x8c\x05Vogel\x94h\x06K"
    b"(u}\x94(h\x02\x8c\x05Chris\x94h\x04\x8c\x04Beck\x94"
    b"h\x06K$u}\x94(h\x02\x8c\x04Beth\x94h\x04\x8c\t"
    b"Johanssen\x94h\x06K\x1due."
)


# %% Result