13.3. Pickle Dumps
13.3.1. SetUp
>>> import pickle
13.3.2. Assignments
# %% About
# - Name: Pickle Dump Tuple
# - 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: bytes` with serialized `DATA`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: bytes` z serializowanym `DATA`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = pickle.loads(result)
>>> 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: bytes
# %% Data
DATA = ('Mark', 'Watney', 41)
# %% Result
result = ...
# %% About
# - Name: Pickle Dump Dict
# - 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: bytes` with serialized `DATA`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: bytes` z serializowanym `DATA`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = pickle.loads(result)
>>> 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: bytes
# %% Data
DATA = {'firstname': 'Mark', 'lastname': 'Watney', 'age': 41}
# %% Result
result = ...
# %% About
# - Name: Pickle Dump 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: bytes` with serialized `DATA`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: bytes` z serializowanym `DATA`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = pickle.loads(result)
>>> 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
result: bytes
# %% Data
DATA = [
('firstname', 'lastname', 'age'),
('Mark', 'Watney', 41),
('Melissa', 'Lewis', 40),
('Rick', 'Martinez', 39),
('Alex', 'Vogel', 40),
('Chris', 'Beck', 36),
('Beth', 'Johanssen', 29),
]
# %% Result
result = ...
# %% About
# - Name: Pickle Dump 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: bytes` with serialized `DATA`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: bytes` z serializowanym `DATA`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = pickle.loads(result)
>>> 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
result: bytes
# %% Data
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},
]
# %% Result
result = ...
# %% About
# - Name: Pickle Dump ListObject
# - 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: bytes` with serialized `DATA`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: bytes` z serializowanym `DATA`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> result = pickle.loads(result)
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert all(type(row) is User for row in result), \
'Variable `result` has invalid type, should be User'
>>> from pprint import pprint
>>> pprint(result, width=79, sort_dicts=False)
[User(firstname='Mark', lastname='Watney', age=41),
User(firstname='Melissa', lastname='Lewis', age=40),
User(firstname='Rick', lastname='Martinez', age=39),
User(firstname='Alex', lastname='Vogel', age=40),
User(firstname='Chris', lastname='Beck', age=36),
User(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
result: bytes
# %% Data
class User:
def __init__(self, firstname, lastname, age):
self.firstname = firstname
self.lastname = lastname
self.age = age
def __repr__(self):
clsname = self.__class__.__name__
firstname = self.firstname
lastname = self.lastname
age = self.age
return f'{clsname}({firstname=}, {lastname=}, {age=})'
DATA = [
User('Mark', 'Watney', age=41),
User('Melissa', 'Lewis', age=40),
User('Rick', 'Martinez', age=39),
User('Alex', 'Vogel', age=40),
User('Chris', 'Beck', age=36),
User('Beth', 'Johanssen', age=29),
]
# %% Result
result = ...