14.3. Serialization List of Sequences
dumps: list[tuple] -> string
loads: string -> list[list]
14.3.1. Dumps
list[tuple] -> string
def dumps(obj, /, *, fieldseparator=',', recordseparator=';')dumps(DATA)
>>> def dumps(obj, /, *, fieldseparator=',', recordseparator=';'):
... keys = list(obj[0])
... values = list(obj[1:])
... header = fieldseparator.join(str(x) for x in keys)
... rows = [fieldseparator.join(str(x) for x in row) for row in values]
... result = recordseparator.join([header] + rows)
... return result
>>>
>>>
>>> DATA = [
... ('firstname', 'lastname', 'age'),
... ('Alice', 'Apricot', 30),
... ('Bob', 'Blackthorn', 31),
... ('Carol', 'Corn', 32),
... ('Dave', 'Durian', 33),
... ('Eve', 'Elderberry', 34),
... ('Mallory', 'Melon', 15),
... ]
>>>
>>> result = dumps(DATA)
>>> result
'firstname,lastname,age;Alice,Apricot,30;Bob,Blackthorn,31;Carol,Corn,32;Dave,Durian,33;Eve,Elderberry,34;Mallory,Melon,15'
14.3.2. Loads
string -> list[list]
def loads(string, /, *, fieldseparator=',', recordseparator=';')loads(DATA)
>>> def loads(string, /, *, fieldseparator=',', recordseparator=';'):
... records = string.split(recordseparator)
... result = [record.split(fieldseparator) for record in records]
... return result
>>>
>>>
>>> DATA = 'firstname,lastname,age;Alice,Apricot,30;Bob,Blackthorn,31;Carol,Corn,32;Dave,Durian,33;Eve,Elderberry,34;Mallory,Melon,15'
>>>
>>> result = loads(DATA)
>>> result
[['firstname', 'lastname', 'age'],
['Alice', 'Apricot', '30'],
['Bob', 'Blackthorn', '31'],
['Carol', 'Corn', '32'],
['Dave', 'Durian', '33'],
['Eve', 'Elderberry', '34'],
['Mallory', 'Melon', '15']]
14.3.3. Assignments
# %% About
# - Name: Serialization Dumps ListSequence
# - Difficulty: medium
# - Lines: 6
# - Minutes: 5
# %% 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 function `dumps()`, which serializes list[Sequence] to str:
# - Argument: `obj: list[tuple]`
# - Returns: `str`
# 2. Define `result: str` with result of `dumps()` function for `DATA`
# 3. Non-functional requirements:
# - Do not use `import` and any module
# - Quoting: none
# - Field Separator: `,`
# - Record Separator: `;`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj funkcję `dumps()`, która serializuje list[Sequence] do str:
# - Argument: `obj: list[tuple]`
# - Zwraca: `str`
# 2. Zdefiniuj `result: str` z wynikiem funkcji `dumps()` dla `DATA`
# 3. Wymagania niefunkcjonalne:
# - Nie używaj `import` ani żadnych modułów
# - Quoting: żadne
# - Field Separator: `,`
# - Record Separator: `;`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# firstname,lastname,age;Alice,Apricot,30;Bob,Blackthorn,31;Carol,Corn,32;Dave,Durian,33;Eve,Elderberry,34;Mallory,Melon,15
# %% Hints
# - `[x for x in data]`
# - `str.join()`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 12), \
'Python has an is invalid version; expected: `3.12` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `str`.'
>>> print(result)
firstname,lastname,age;Alice,Apricot,30;Bob,Blackthorn,31;Carol,Corn,32;Dave,Durian,33;Eve,Elderberry,34;Mallory,Melon,15
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
from typing import Callable
type data = list[tuple[str,str,int]]
dumps: Callable[[data], str]
result: str
# %% Data
DATA = [
('firstname', 'lastname', 'age'),
('Alice', 'Apricot', 30),
('Bob', 'Blackthorn', 31),
('Carol', 'Corn', 32),
('Dave', 'Durian', 33),
('Eve', 'Elderberry', 34),
('Mallory', 'Melon', 15),
]
# %% Result
def dumps(obj, /, *, fieldseparator=',', recordseparator=';'):
...
result = ...
# %% About
# - Name: Serialization Loads ListSequence
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5
# %% 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 function `loads()`, which deserialize from str to list[Sequence]:
# - Argument: `string: str`
# - Returns: `list[list[str]]`
# 2. Define `result: list[tuple]` with result of `dumps()` function for `DATA`
# 3. Non-functional requirements:
# - Do not use `import` and any module
# - Do not convert numbers to `int` or `float`, leave them as `str`
# - Quoting: none
# - Field Separator: `,`
# - Record Separator: `;`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj funkcję `loads()`, która deserializuje ze str do list[Sequence]:
# - Argument: `string: str`
# - Zwraca: `list[list[str]]`
# 2. Zdefiniuj `result: list[tuple]` z wynikiem funkcji `dumps()` dla `DATA`
# 3. Wymagania niefunkcjonalne:
# - Nie używaj `import` ani żadnych modułów
# - Nie konwertuj liczb do `int` lub `float`, pozostaw je jako `str`
# - Quoting: żadne
# - Field Separator: `,`
# - Record Separator: `;`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# [['firstname', 'lastname', 'age'],
# ['Alice', 'Apricot', '30'],
# ['Bob', 'Blackthorn', '31'],
# ['Carol', 'Corn', '32'],
# ['Dave', 'Durian', '33'],
# ['Eve', 'Elderberry', '34'],
# ['Mallory', 'Melon', '15']]
# %% Hints
# - `tuple()`
# - `str.split()`
# - `[x for x in data]`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 12), \
'Python has an is invalid version; expected: `3.12` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is list, \
'Variable `result` has an invalid type; expected: `list`.'
>>> assert all(type(x) is list for x in result), \
'Variable `result` has elements of an invalid type; all items should be: `list`.'
>>> from pprint import pprint
>>> pprint(result, sort_dicts=False, width=79)
[['firstname', 'lastname', 'age'],
['Alice', 'Apricot', '30'],
['Bob', 'Blackthorn', '31'],
['Carol', 'Corn', '32'],
['Dave', 'Durian', '33'],
['Eve', 'Elderberry', '34'],
['Mallory', 'Melon', '15']]
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
from typing import Callable
type data = list[tuple[str,...]]
loads: Callable[[str,...], data]
result: data
# %% Data
DATA = 'firstname,lastname,age;Alice,Apricot,30;Bob,Blackthorn,31;Carol,Corn,32;Dave,Durian,33;Eve,Elderberry,34;Mallory,Melon,15'
# %% Result
def loads(string, /, *, fieldseparator=',', recordseparator=';'):
...
result = ...