7.4. Pickle File

7.4.1. Rationale

  • File extension pkl

7.4.2. Serialize to File

>>> import pickle
>>>
>>>
>>> DATA = [1, 2, 3]
>>>
>>> with open('filename.pkl', mode='wb') as file:
...     pickle.dump(DATA, file)

7.4.3. Deserialize from File

Load from file:

>>> import pickle
>>>
>>>
>>> with open('filename.pkl', mode='rb') as file:
...     result = pickle.load(file)
>>>
>>> print(result)
[1, 2, 3]

7.4.4. Assignments

Code 7.2. Solution
"""
* Assignment: Pickle File Serialize
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

English:
    1. Save `DATA` to `FILE`
    2. Use `pickle` module
    3. Run doctests - all must succeed

Polish:
    1. Zapisz `DATA` do `FILE`
    2. Użyj modułu `pickle`
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> with open(FILE, mode='rb') as file:
    ...     result = pickle.load(file)

    >>> result  # doctest: +NORMALIZE_WHITESPACE
    [Astronaut(name='Jan Twardowski', missions=[Mission(year=1969, name='Apollo 18'), Mission(year=2024, name='Artemis 3')]),
     Astronaut(name='Mark Watney', missions=[Mission(year=2035, name='Ares 3')]),
     Astronaut(name='Melissa Lewis', missions=[])]

    >>> from os import remove
    >>> remove(FILE)
"""

import pickle
from dataclasses import dataclass, field

FILE = r'_temporary.pkl'


@dataclass
class Astronaut:
    name: str
    missions: list = field(default_factory=list)


@dataclass
class Mission:
    year: int
    name: str


DATA = [
    Astronaut('Jan Twardowski', missions=[
        Mission(1969, 'Apollo 18'),
        Mission(2024, 'Artemis 3')]),

    Astronaut('Mark Watney', missions=[
        Mission(2035, 'Ares 3')]),

    Astronaut('Melissa Lewis'),
]


Code 7.3. Solution
"""
* Assignment: Pickle File Deserialize
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

English:
    1. Define `result: list[Astronaut]` with data from `FILE`
    2. Use `pickle` module
    3. Run doctests - all must succeed

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

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> result  # doctest: +NORMALIZE_WHITESPACE
    [Astronaut(name='Jan Twardowski', missions=[Mission(year=1969, name='Apollo 18'), Mission(year=2024, name='Artemis 3')]),
     Astronaut(name='Mark Watney', missions=[Mission(year=2035, name='Ares 3')]),
     Astronaut(name='Melissa Lewis', missions=[])]
    >>> from os import remove
    >>> remove(FILE)
"""

import pickle
from dataclasses import dataclass, field

FILE = r'_temporary.pkl'
DATA = (b'\x80\x04\x95\xf6\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\r'
        b'pickle_file_a\x94\x8c\tAstronaut\x94\x93\x94)\x81\x94}\x94'
        b'(\x8c\x04name\x94\x8c\x0eJan Twardowski\x94\x8c\x08missions'
        b'\x94]\x94(h\x01\x8c\x07Mission\x94\x93\x94)\x81\x94}\x94(\x8c'
        b'\x04year\x94M\xb1\x07h\x06\x8c\tApollo 18\x94ubh\x0b)\x81\x94}'
        b'\x94(h\x0eM\xe8\x07h\x06\x8c\tArtemis 3\x94ubeubh\x03)\x81\x94}'
        b'\x94(h\x06\x8c\x0bMark Watney\x94h\x08]\x94h\x0b)\x81\x94}\x94'
        b'(h\x0eM\xf3\x07h\x06\x8c\x06Ares 3\x94ubaubh\x03)\x81\x94}\x94'
        b'(h\x06\x8c\rMelissa Lewis\x94h\x08]\x94ube.')

with open(FILE, mode='wb') as file:
    file.write(DATA)


@dataclass
class Astronaut:
    name: str
    missions: list = field(default_factory=list)


@dataclass
class Mission:
    year: int
    name: str