14.2. Serialization Dumps
dumps(object) -> str
14.2.1. Sequence
>>> DATA = ('Mark', 'Watney', 41)
>>>
>>> result = ','.join(map(str,DATA))
>>>
>>> print(result)
Mark,Watney,41
14.2.2. Mapping
>>> DATA = {
... 'firstname': 'Mark',
... 'lastname': 'Watney',
... 'age': 41,
... }
>>>
>>> header = ','.join(DATA.keys())
>>> row = ','.join(map(str,DATA.values()))
>>> result = '\n'.join([header, row])
>>>
>>> print(result)
firstname,lastname,age
Mark,Watney,41
14.2.3. List of Sequence
>>> DATA = [
... ('firstname', 'lastname', 'age'),
... ('Mark', 'Watney', 41),
... ('Melissa', 'Lewis', 40),
... ('Rick', 'Martinez', 39),
... ('Alex', 'Vogel', 40),
... ('Chris', 'Beck', 36),
... ('Beth', 'Johanssen', 29),
... ]
>>>
>>> data = [','.join(map(str,row)) for row in DATA]
>>> result = '\n'.join(data)
>>>
>>> print(result)
firstname,lastname,age
Mark,Watney,41
Melissa,Lewis,40
Rick,Martinez,39
Alex,Vogel,40
Chris,Beck,36
Beth,Johanssen,29
14.2.4. List of Mappings
>>> 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},
... ]
>>>
>>> header = tuple(DATA[0].keys())
>>> rows = [tuple(map(str,row.values())) for row in DATA]
>>> data = [header] + rows
>>> result = '\n'.join(','.join(row) for row in data)
>>>
>>> print(result)
firstname,lastname,age
Mark,Watney,41
Melissa,Lewis,40
Rick,Martinez,39
Alex,Vogel,40
Chris,Beck,36
Beth,Johanssen,29
14.2.5. Case Study 1
def dumps(data):
values = [','.join(map(str, row)) for row in data]
result = '\n'.join(values) + '\n'
return result
DATA = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
(7.0, 3.2, 4.7, 1.4, 'versicolor'),
(7.6, 3.0, 6.6, 2.1, 'virginica'),
(4.6, 3.1, 1.5, 0.2, 'setosa'),
]
result = dumps(DATA)
print(result)
# sepal_length,sepal_width,petal_length,petal_width,species
# 5.8,2.7,5.1,1.9,virginica
# 5.1,3.5,1.4,0.2,setosa
# 5.7,2.8,4.1,1.3,versicolor
# 6.3,2.9,5.6,1.8,virginica
# 6.4,3.2,4.5,1.5,versicolor
# 4.7,3.2,1.3,0.2,setosa
# 7.0,3.2,4.7,1.4,versicolor
# 7.6,3.0,6.6,2.1,virginica
# 4.6,3.1,1.5,0.2,setosa
14.2.6. Case Study 2
from pprint import pprint
def dumps(data):
values = vars(data).values()
result = ','.join(values) + '\n'
return result
class User:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
DATA = User('Mark', 'Watney')
result = dumps(DATA)
pprint(result)
# Mark,Watney
from pprint import pprint
def dumps(data):
values = [','.join(vars(row).values()) for row in data]
result = '\n'.join(values) + '\n'
return result
class User:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
DATA = [
User('Mark', 'Watney'),
User('Melissa', 'Lewis'),
User('Rick', 'Martinez'),
]
result = dumps(DATA)
pprint(result)
# 'Mark,Watney\nMelissa,Lewis\nRick,Martinez\n'
14.2.7. Assignments
# FIXME: Uspójnić zadania z pikle, json, toml
# %% 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: Serialization Dumps Tuple
# - Difficulty: easy
# - Lines: 5
# - Minutes: 5
# %% English
# 1. Define function `dumps()`:
# - Argument: `data: list[tuple]`
# - Returns: `str`
# - Function converts `data` to CSV format
# - Add quotes to values
# 2. Define `result: str` with
# result of `dumps()` function for `DATA`
# 3. Non-functional requirements:
# - Do not use `import` and any module
# - Quotechar: `"`
# - Quoting: all
# - Delimiter: `,`
# - Lineseparator: `\n`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj funkcję `dumps()`:
# - Argument: `data: list[tuple]`
# - Zwraca: `str`
# - Funkcja konwertuje `data` do formatu CSV
# - Dodaj cudzysłowia do wartości
# 2. Zdefiniuj `result: str` z
# wynikiem funkcji `dumps()` dla `DATA`
# 3. Wymagania niefunkcjonalne:
# - Nie używaj `import` ani żadnych modułów
# - Quotechar: `"`
# - Quoting: wszytkie
# - Delimiter: `,`
# - Lineseparator: `\n`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `[x for x in data]`
# - `str.join()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> print(result)
"Mark","Watney","41"
"""
DATA = ('Mark', 'Watney', 41)
# Define function `dumps()`:
# - Argument: `data: list[tuple]`
# - Returns: `str`
# - Function converts `data` to CSV format
# - Add quotes to values
# type: Callable[[list[tuple]], str]
def dumps(data):
...
# Define `result: str` with
# result of `dumps()` function for `DATA`
# type: str
result = ...
# FIXME: Uspójnić zadania z pikle, json, toml
# %% 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: Serialization Dumps Dict
# - Difficulty: easy
# - Lines: 5
# - Minutes: 5
# %% English
# 1. Define function `dumps()`:
# - Argument: `data: list`
# - Returns: `str`
# - Function converts `data` to CSV format
# - Add quotes to values
# 2. Define `result: str` with
# result of `dumps()` function for `DATA`
# 3. Non-functional requirements:
# - Do not use `import` and any module
# - Quotechar: `"`
# - Quoting: all
# - Delimiter: `,`
# - Lineseparator: `\n`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj funkcję `dumps()`:
# - Argument: `data: list`
# - Zwraca: `str`
# - Funkcja konwertuje `data` do formatu CSV
# - Dodaj cudzysłowia do wartości
# 2. Zdefiniuj `result: str` z
# wynikiem funkcji `dumps()` dla `DATA`
# 3. Wymagania niefunkcjonalne:
# - Nie używaj `import` ani żadnych modułów
# - Quotechar: `"`
# - Quoting: wszystkie
# - Delimiter: `,`
# - Lineseparator: `\n`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `[x for x in data]`
# - `str.join()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> print(result)
"Mark","Watney","41"
"""
DATA = {'firstname': 'Mark', 'lastname': 'Watney', 'age': 41}
# Define function `dumps()`:
# - Argument: `data: list[tuple]`
# - Returns: `str`
# - Function converts `data` to CSV format
# - Add quotes to values
# type: Callable[[list[tuple]], str]
def dumps(data):
...
# Define `result: str` with
# result of `dumps()` function for `DATA`
# type: str
result = ...
# FIXME: Uspójnić zadania z pikle, json, toml
# %% 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: Serialization Dumps ListTuple
# - Difficulty: medium
# - Lines: 6
# - Minutes: 5
# %% English
# 1. Define function `dumps()`:
# - Argument: `data: list[tuple]`
# - Returns: `str`
# - Function converts `data` to CSV format
# - Add quotes to values
# 2. Define `result: str` with
# result of `dumps()` function for `DATA`
# 3. Non-functional requirements:
# - Do not use `import` and any module
# - Quotechar: `"`
# - Quoting: all
# - Delimiter: `,`
# - Lineseparator: `\n`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj funkcję `dumps()`:
# - Argument: `data: list[tuple]`
# - Zwraca: `str`
# - Funkcja konwertuje `data` do formatu CSV
# - Dodaj cudzysłowia do wartości
# 2. Zdefiniuj `result: str` z
# wynikiem funkcji `dumps()` dla `DATA`
# 3. Wymagania niefunkcjonalne:
# - Nie używaj `import` ani żadnych modułów
# - Quotechar: `"`
# - Quoting: wszystkie
# - Delimiter: `,`
# - Lineseparator: `\n`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `[x for x in data]`
# - `str.join()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> print(result)
"firstname","lastname","age"
"Mark","Watney","41"
"Melissa","Lewis","40"
"Rick","Martinez","39"
"Alex","Vogel","40"
"Chris","Beck","36"
"Beth","Johanssen","29"
"""
DATA = [
('firstname', 'lastname', 'age'),
('Mark', 'Watney', 41),
('Melissa', 'Lewis', 40),
('Rick', 'Martinez', 39),
('Alex', 'Vogel', 40),
('Chris', 'Beck', 36),
('Beth', 'Johanssen', 29),
]
# Define function `dumps()`:
# - Argument: `data: list[tuple]`
# - Returns: `str`
# - Function converts `data` to CSV format
# - Add quotes to values
# type: Callable[[list[tuple]], str]
def dumps(data):
...
# Define `result: str` with
# result of `dumps()` function for `DATA`
# type: str
result = ...
# FIXME: Uspójnić zadania z pikle, json, toml
# %% 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: Serialization Dumps ListDict
# - Difficulty: medium
# - Lines: 7
# - Minutes: 5
# %% English
# 1. Modify function `dumps()`:
# - Argument: `data: list[tuple]`
# - Returns: `str`
# - Function converts `data` to CSV format
# - Add quotes to values
# - First line is a header
# 2. Define `result: str` with
# result of `dumps()` function for `DATA`
# 3. Non-functional requirements:
# - Do not use `import` and any module
# - Quotechar: `"`
# - Quoting: all
# - Delimiter: `,`
# - Lineseparator: `\n`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zmodyfikuj funkcję `dumps()`:
# - Argument: `data: list[tuple]`
# - Zwraca: `str`
# - Funkcja konwertuje `data` do formatu CSV
# - Dodaj cudzysłowia do wartości
# - Pierwsza linia to nagłówek
# 2. Zdefiniuj `result: str` z
# wynikiem funkcji `dumps()` dla `DATA`
# 3. Wymagania niefunkcjonalne:
# - Nie używaj `import` ani żadnych modułów
# - Quotechar: `"`
# - Quoting: wszystkie
# - Delimiter: `,`
# - Lineseparator: `\n`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `str.join()`
# - `dict.keys()`
# - `dict.values()`
# - `[x for x in data]`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> print(result)
"firstname","lastname","age"
"Mark","Watney","41"
"Melissa","Lewis","40"
"Rick","Martinez","39"
"Alex","Vogel","40"
"Chris","Beck","36"
"Beth","Johanssen","29"
"""
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},
]
# Modify function `dumps()`:
# - Argument: `data: list[tuple]`
# - Returns: `str`
# - Function converts `data` to CSV format
# - Add quotes to values
# - First line is a header
# type: Callable[[list[tuple]], str]
def dumps(data):
...
# Define `result: str` with
# result of `dumps()` function for `DATA`
# type: str
result = ...
# FIXME: Uspójnić zadania z pikle, json, toml
# %% 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: Serialization Dumps ListObjects
# - Difficulty: medium
# - Lines: 7
# - Minutes: 8
# %% English
# 1. Modify function `dumps()`:
# - Argument: `data: list[User]`
# - Returns: `str`
# - Function converts `data` to CSV format
# - Add quotes to values
# - First line is a header
# 2. Define `result: str` with
# result of `dumps()` function for `DATA`
# 3. Non-functional requirements:
# - Do not use `import` and any module
# - Quotechar: `"`
# - Quoting: all
# - Delimiter: `,`
# - Lineseparator: `\n`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zmodyfikuj funkcję `dumps()`:
# - Argument: `data: list[User]`
# - Zwraca: `str`
# - Funkcja konwertuje `data` do formatu CSV
# - Dodaj cudzysłowia do wartości
# - Pierwsza linia to nagłówek
# 2. Zdefiniuj `result: str` z
# wynikiem funkcji `dumps()` dla `DATA`
# 3. Wymagania niefunkcjonalne:
# - Nie używaj `import` ani żadnych modułów
# - Quotechar: `"`
# - Quoting: wszystkie
# - Delimiter: `,`
# - Lineseparator: `\n`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - `str.join()`
# - `dict.keys()`
# - `dict.values()`
# - `[x for x in data]`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> print(result)
"firstname","lastname","age"
"Mark","Watney","41"
"Melissa","Lewis","40"
"Rick","Martinez","39"
"Alex","Vogel","40"
"Chris","Beck","36"
"Beth","Johanssen","29"
"""
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),
]
# Modify function `dumps()`:
# - Argument: `data: list[User]`
# - Returns: `str`
# - Function converts `data` to CSV format
# - Add quotes to values
# - First line is a header
# type: Callable[[list[tuple]], str]
def dumps(data):
...
# Define `result: str` with
# result of `dumps()` function for `DATA`
# type: str
result = ...