forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscientists.py
More file actions
44 lines (37 loc) · 1.21 KB
/
Copy pathscientists.py
File metadata and controls
44 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from typing import NamedTuple
scientists = [
{
"name": {"first": "Grace", "last": "Hopper"},
"birth": {"year": 1906, "month": 12, "day": 9},
"death": {"year": 1992, "month": 1, "day": 1},
},
{"name": {"first": "Euclid"}},
{"name": {"first": "Abu Nasr", "last": "Al-Farabi"}, "birth": None},
{
"name": {"first": "Srinivasa", "last": "Ramanujan"},
"birth": {"year": 1887},
"death": {"month": 4, "day": 26},
},
{
"name": {"first": "Ada", "last": "Lovelace"},
"birth": {"year": 1815},
"death": {"year": 1852},
},
{
"name": {"first": "Charles", "last": "Babbage"},
"birth": {"year": 1791, "month": 12, "day": 26},
"death": {"year": 1871, "month": 10, "day": 18},
},
]
class Person(NamedTuple):
name: str
life_span: tuple
def dict_to_person(info):
"""Convert a dictionary to a Person object"""
return Person(
name=f"{info['name']['first']} {info['name']['last']}",
life_span=(info["birth"]["year"], info["death"]["year"]),
)
def convert_pair(first, second):
"""Convert two dictionaries to Person objects"""
return dict_to_person(first), dict_to_person(second)