Created
January 7, 2019 19:48
-
-
Save shiroyuki/156f23cf885c02989c879baf7bbf7910 to your computer and use it in GitHub Desktop.
Data mapping sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# models.py | |
from dataclasses import dataclass # Built-in in Python 3.6 | |
@dataclass | |
class Bar: | |
x: int | |
y: float | |
@dataclass | |
class Root: | |
a: str | |
b: Bar | |
c: int | |
def map_dict_to_root(data): | |
properties = dict() | |
for k in data: | |
if k == 'b': | |
properties[k] = map_dict_to_bar(data[k]) | |
else: | |
properties[k] = data[k] | |
return Root(**properties) | |
def map_dict_to_bar(data) | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment