Skip to content

Instantly share code, notes, and snippets.

@fereria
Created January 16, 2018 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fereria/18f008fbc2ea297122ba64a375f391a2 to your computer and use it in GitHub Desktop.
Save fereria/18f008fbc2ea297122ba64a375f391a2 to your computer and use it in GitHub Desktop.
Dictをいい感じに整形して val.param 形式にしてくれる
# -*- coding: utf-8 -*-
# Dictをいい感じに整形して val.param 形式にしてくれる
# 参考 https://stackoverflow.com/questions/6578986/how-to-convert-json-data-into-a-python-object
import json
from collections import namedtuple
data = {"name": "John Smith", "hometown": {"name": "New York", "id": 123}}
func = lambda d: namedtuple('X', d.keys())(*d.values())
print func(data)
# X(hometown={'name': 'New York', 'id': 123}, name='John Smith')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment