14.4. Serialization Loads

  • loads(str) -> object

14.4.1. SetUp

>>> from pprint import pprint

14.4.2. Sequence

>>> DATA = 'Mark,Watney,41'
>>>
>>> result = DATA.split(',')
>>>
>>> pprint(result)
['Mark', 'Watney', '41']

14.4.3. Mapping

>>> DATA = """
... firstname,lastname,age
... Mark,Watney,41
... """
>>>
>>> data = DATA.strip().splitlines()
>>> header, row = [tuple(x.split(',')) for x in data]
>>> result = dict(zip(header,row))
>>>
>>> pprint(result, sort_dicts=False)
{'firstname': 'Mark', 'lastname': 'Watney', 'age': '41'}

14.4.4. 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 = DATA.strip().splitlines()
>>> header, *rows = [tuple(x.split(',')) for x in data]
>>> result = [header] + rows
>>>
>>> pprint(result)
[('firstname', 'lastname', 'age'),
 ('Mark', 'Watney', '41'),
 ('Melissa', 'Lewis', '40'),
 ('Rick', 'Martinez', '39'),
 ('Alex', 'Vogel', '40'),
 ('Chris', 'Beck', '36'),
 ('Beth', 'Johanssen', '29')]

14.4.5. List of Mappings

>>> DATA = """
... firstname,lastname,age
... Mark,Watney,41
... Melissa,Lewis,40
... Rick,Martinez,39
... Alex,Vogel,40
... Chris,Beck,36
... Beth,Johanssen,29
... """
>>>
>>> data = DATA.strip().splitlines()
>>> header, *rows = [tuple(x.split(',')) for x in data]
>>> result = [dict(zip(header,row)) for row in rows]
>>>
>>> pprint(result, 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'}]

14.4.6. Case Study 1

from pprint import pprint


def loads(data):
    header, *lines = data.splitlines()
    header = tuple(header.split(','))
    rows = []
    for line in lines:
        *values, species = line.strip().split(',')
        values = [float(x) for x in values]
        row = tuple(values) + (species,)
        rows.append(row)
    return [header] + rows


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 = loads(DATA)
pprint(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.4.7. Case Study 2

from pprint import pprint


def loads(data):
    values = data.strip().split(',')
    result = User(*values)
    return result


class User:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    def __repr__(self):
        firstname = self.firstname
        lastname = self.lastname
        return f'User({firstname=}, {lastname=})'


DATA = 'Mark,Watney\n'

result = loads(DATA)
pprint(result)
# User(firstname='Mark', lastname='Watney')
from pprint import pprint


def loads(data):
    records = data.splitlines()
    result = []
    for record in records:
        values = record.strip().split(',')
        user = User(*values)
        result.append(user)
    return result


class User:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    def __repr__(self):
        firstname = self.firstname
        lastname = self.lastname
        return f'User({firstname=}, {lastname=})'


DATA = 'Mark,Watney\nMelissa,Lewis\nRick,Martinez\n'

result = loads(DATA)
pprint(result)
# [User(firstname='Mark', lastname='Watney'),
#  User(firstname='Melissa', lastname='Lewis'),
#  User(firstname='Rick', lastname='Martinez')]

14.4.8. Assignments