15.5. Pickle Load
pickle.load()
-> load from filepickle.loads()
-> load from string (bytes)What if name was
pickle.from_file()
?What if name was
pickle.from_text()
?
15.5.1. SetUp
>>> import pickle
15.5.2. Deserialize Str
>>> pickle.loads(b'\x80\x04\x95\x0f\x00\x00\x00\x00\x00\x00\x00\x8c\x0bMark Watney\x94.')
'Mark Watney'
15.5.3. Deserialize Int
>>> pickle.loads(b'\x80\x03K\x01.')
1
>>> pickle.loads(b'\x80\x04K\x00.')
0
>>> pickle.loads(b'\x80\x04\x95\x06\x00\x00\x00\x00\x00\x00\x00J\xff\xff\xff\xff.')
-1
15.5.4. Deserialize Float
>>> pickle.loads(b'\x80\x04\x95\n\x00\x00\x00\x00\x00\x00\x00G?\xb9\x99\x99\x99\x99\x99\x9a.')
0.1
>>> pickle.loads(b'\x80\x04\x95\n\x00\x00\x00\x00\x00\x00\x00G?\xc9\x99\x99\x99\x99\x99\x9a.')
0.2
>>> pickle.loads(b'\x80\x04\x95\n\x00\x00\x00\x00\x00\x00\x00G?\xd3333333.')
0.3
15.5.5. Deserialize Sequence
>>> pickle.loads(b'\x80\x03]q\x00(K\x01K\x02K\x03e.')
[1, 2, 3]
>>> pickle.loads(b'\x80\x03K\x01K\x02K\x03\x87q\x00.')
(1, 2, 3)
>>> pickle.loads(b'\x80\x03cbuiltins\nset\nq\x00]q\x01(K\x01K\x02K\x03e\x85q\x02Rq\x03.')
{1, 2, 3}
15.5.6. Deserialize Mapping
>>> pickle.loads(b'\x80\x03}q\x00(X\x01\x00\x00\x00aq\x01K\x01X\x01\x00\x00\x00bq\x02K\x02X\x01\x00\x00\x00cq\x03K\x03u.')
{'a': 1, 'b': 2, 'c': 3}
15.5.7. Use Case - 1
Astronaut
>>> from dataclasses import dataclass, field, asdict
>>> from datetime import date, time, datetime, timezone, timedelta
>>> from pprint import pprint
>>> from typing import ClassVar
>>> import pickle
>>>
>>>
>>> @dataclass
... class Group:
... gid: int
... name: str
>>>
>>>
>>> @dataclass(frozen=True)
... class User:
... firstname: str
... lastname: str
... email: str | None = None
... birthdate: date | None = None
... height: int | float | None = field(default=None, metadata={'unit': 'cm', 'min': 156, 'max': 210})
... weight: int | float | None = field(default=None, metadata={'unit': 'kg', 'min': 50, 'max': 90})
... groups: list[Group] = field(default_factory=list)
... account_type: str = field(default='user', metadata={'choices': ['guest', 'user', 'admin']})
... account_created: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
... account_modified: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
... account_lastlogin: datetime | None = None
... account_expiration: timedelta | None = None
... AGE_MIN: ClassVar[int] = 30
... AGE_MAX: ClassVar[int] = 50
>>> DATA = b'\x80\x04\x95\xc1\x01\x00\x00\x00\x00\x00\x00\x8c\x08__main__\x94\x8c\x04User\x94\x93\x94)\x81\x94}\x94(\x8c\tfirstname\x94\x8c\x04Mark\x94\x8c\x08lastname\x94\x8c\x06Watney\x94\x8c\x05email\x94\x8c\x10mwatney@nasa.gov\x94\x8c\x04born\x94\x8c\x08datetime\x94\x8c\x04date\x94\x93\x94C\x04\x07\xb1\x04\x0c\x94\x85\x94R\x94\x8c\x06height\x94G@f@\x00\x00\x00\x00\x00\x8c\x06weight\x94G@R\xe0\x00\x00\x00\x00\x00\x8c\x06groups\x94]\x94(h\x00\x8c\x05Group\x94\x93\x94)\x81\x94}\x94(\x8c\x03gid\x94K\x01\x8c\x04name\x94\x8c\x05users\x94ubh\x17)\x81\x94}\x94(h\x1aK\x02h\x1b\x8c\x05staff\x94ube\x8c\x0caccount_type\x94\x8c\x04user\x94\x8c\x0faccount_created\x94h\x0c\x8c\x08datetime\x94\x93\x94C\n\x07\xb1\x07\x15\x028\x0f\x00\x00\x00\x94h\x0c\x8c\x08timezone\x94\x93\x94h\x0c\x8c\ttimedelta\x94\x93\x94K\x00K\x00K\x00\x87\x94R\x94\x85\x94R\x94\x86\x94R\x94\x8c\x10account_modified\x94h$C\n\x07\xb1\x07\x15\x028\x0f\x00\x00\x00\x94h-\x86\x94R\x94\x8c\x11account_lastlogin\x94N\x8c\x12account_expiration\x94Nub.'
>>> mark = pickle.loads(DATA)
>>>
>>> pprint(mark)
User(firstname='Mark',
lastname='Watney',
email='mwatney@nasa.gov',
birthdate=datetime.date(1969, 4, 12),
height=178.0,
weight=75.5,
groups=[Group(gid=1, name='users'), Group(gid=2, name='staff')],
account_type='user',
account_created=datetime.datetime(1969, 7, 21, 2, 56, 15, tzinfo=datetime.timezone.utc),
account_modified=datetime.datetime(1969, 7, 21, 2, 56, 15, tzinfo=datetime.timezone.utc),
account_lastlogin=None,
account_expiration=None)
15.5.8. 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: Pickle Load ListObjects
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define `result: list[User]` with data from `FILE`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[User]` z danymi z `FILE`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from os import remove
>>> remove(FILE)
>>> result
('Mark', 'Watney', 1)
"""
import pickle
FILE = r'_temporary.pkl'
with open(FILE, mode='wb') as file:
file.write(
b'\x80\x04\x95\x15\x00\x00\x00\x00\x00\x00\x00\x8c'
b'\x04Mark\x94\x8c\x06Watney\x94K\x01\x87\x94.'
)
# FIXME: zmienić na Iris list[tuple]
# %% 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: Pickle Load ListObjects
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define `result: list[User]` with data from `FILE`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[User]` z danymi z `FILE`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result)
[('Mark', 'Watney', 1),
('Melissa', 'Lewis', 3),
('Rick', 'Martinez', 1),
('Alex', 'Vogel', None),
('Chris', 'Beck', 1),
('Beth', 'Johanssen', 2)]
"""
import pickle
FILE = r'_temporary.pkl'
with open(FILE, mode='wb') as file:
file.write(
b'\x80\x04\x95\x81\x00\x00\x00\x00\x00\x00\x00]'
b'\x94(\x8c\x04Mark\x94\x8c\x06Watney\x94K\x01\x87'
b'\x94\x8c\x07Melissa\x94\x8c\x05Lewis\x94K\x03\x87'
b'\x94\x8c\x04Rick\x94\x8c\x08Martinez\x94K\x01\x87'
b'\x94\x8c\x04Alex\x94\x8c\x05Vogel\x94N\x87'
b'\x94\x8c\x05Chris\x94\x8c\x04Beck\x94K\x01\x87'
b'\x94\x8c\x04Beth\x94\x8c\tJohanssen\x94K\x02\x87\x94e.'
)
# FIXME: zmienić na list[dict]
# %% 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: Pickle Load ListObjects
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define `result: list[User]` with data from `FILE`
# 2. Use `pickle` module
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[User]` z danymi z `FILE`
# 2. Użyj modułu `pickle`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from os import remove
>>> remove(FILE)
>>> from pprint import pprint
>>> pprint(result, sort_dicts=False)
[{'firstname': 'Mark', 'lastname': 'Watney', 'group': 1},
{'firstname': 'Melissa', 'lastname': 'Lewis', 'group': 3},
{'firstname': 'Rick', 'lastname': 'Martinez', 'group': 1},
{'firstname': 'Alex', 'lastname': 'Vogel', 'group': None},
{'firstname': 'Beth', 'lastname': 'Johanssen', 'group': 2},
{'firstname': 'Chris', 'lastname': 'Beck', 'group': 1}]
"""
import pickle
FILE = r'_temporary.pkl'
with open(FILE, mode='wb') as file:
file.write(
b'\x80\x04\x95\xca\x00\x00\x00\x00\x00\x00\x00]'
b'\x94(}\x94(\x8c\tfirstname\x94\x8c\x04Mark\x94'
b'\x8c\x08lastname\x94\x8c\x06Watney\x94\x8c\x05group\x94K\x01u}'
b'\x94(h\x02\x8c\x07Melissa\x94h\x04\x8c\x05Lewis\x94h\x06K\x03u}'
b'\x94(h\x02\x8c\x04Rick\x94h\x04\x8c\x08Martinez\x94h\x06K\x01u}'
b'\x94(h\x02\x8c\x04Alex\x94h\x04\x8c\x05Vogel\x94h\x06Nu}'
b'\x94(h\x02\x8c\x04Beth\x94h\x04\x8c\tJohanssen\x94h\x06K\x02u}'
b'\x94(h\x02\x8c\x05Chris\x94h\x04\x8c\x04Beck\x94h\x06K\x01ue.'
)
class Group:
def __init__(self, gid, name):
self.gid = gid
self.name = name
def __repr__(self):
return f'{self.gid}({self.name})'
class User:
def __init__(self, firstname, lastname, age, groups=None):
self.firstname = firstname
self.lastname = lastname
self.age = age
self.groups = groups if groups else []
def __repr__(self):
clsname = self.__class__.__name__
firstname = self.firstname
lastname = self.lastname
groups = ','.join(repr(x) for x in self.groups)
age = self.age
return f'{clsname}({firstname=}, {lastname=}, {age=}, {groups=})'