Skip to content

Instantly share code, notes, and snippets.

@shiroyuki
Created January 7, 2019 19:48
Show Gist options
  • Save shiroyuki/156f23cf885c02989c879baf7bbf7910 to your computer and use it in GitHub Desktop.
Save shiroyuki/156f23cf885c02989c879baf7bbf7910 to your computer and use it in GitHub Desktop.
Data mapping sample
# 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