Skip to content

Instantly share code, notes, and snippets.

@altnight
Last active May 11, 2016 16:30
Show Gist options
  • Save altnight/d7619528f9d2cd40f5e5f17e38d1e690 to your computer and use it in GitHub Desktop.
Save altnight/d7619528f9d2cd40f5e5f17e38d1e690 to your computer and use it in GitHub Desktop.
d = {
"success": True,
"calendars": [
{
"id": 123,
"year": 2014,
"months": [
{
"id": 50,
"monthnumber": 1,
"monthname": "Yanuary 2014",
"month_days": [
{
"id" : 20,
"daynumber" : 1,
"dayname": "Monday",
"day_events" : [{
"id": 1000,
"name": "MacBook Pro",
"group": [
{
"name": "R",
"members": [
{
"name": "foo",
"age": 20,
"skils": []
},
{
"name": "bar",
"age": 22,
"skils": [{
"name": "art"
}]
},
{
"name": "boo",
"tel": "12312341212",
"age": 20,
"skils": []
}
]
}
]
}]
},
{
"id" : 21,
"daynumber" : 2,
"dayname": "Thursday",
"day_events" : [{
"id": 1001,
"name": "iPhone"
}]
}
]
},{
"id": 500,
"monthnumber": 2,
"monthname": "Febru 2014",
"month_days": [
{
"id" : 2014,
"daynumber" : 1,
"dayname": "Monday",
"day_events" : [{
"id": 100120,
"name": "MacBook Pro"
}]
},
{
"id" : 2231,
"daynumber" : 2,
"dayname": "Thursday",
"day_events" : [{
"id": 107701,
"name": "iPhone"
}]
}
]
}
]
},
{
"id": 456,
"year": 2013,
"months": [
{
"id": 50,
"monthnumber": 1,
"monthname": "Yanuary 2013",
"month_days": [
{
"id" : 20,
"daynumber" : 10,
"dayname": "Sunday",
"day_events" : [{
"id": 1005,
"name": "asd 2013"
}]
},
{
"id" : 21,
"daynumber" : 20,
"dayname": "Friday",
"day_events" : [{
"id": 1008,
"name": "cvbcv"
}]
}
]
}
]
}
]
}
def _call(in_dict, out_dict={}):
for k, v in in_dict.items():
if isinstance(v, (bool, str, int)):
out_dict.setdefault(k, True)
elif isinstance(v, list):
for i in v:
out_dict.setdefault(k, {})
_call(i, out_dict[k])
elif isinstance(v, dict):
out_dict[k].setdefault(v, {})
return _call(v, out_dict[k])
else:
print('nothing')
return out_dict
def _format(d, prefix=''):
for k, v in d.items():
if isinstance(v, (bool, str, int)):
print('{}{}'.format(prefix, k))
elif isinstance(v, dict):
prev_prefix = prefix
prefix += '{}[]__'.format(k)
_format(v, prefix)
prefix = prev_prefix
# success
# calendars[]__id
# calendars[]__year
# calendars[]__months[]__id
# calendars[]__months[]__monthnumber
# calendars[]__months[]__monthname
# calendars[]__months[]__month_days[]__id
# calendars[]__months[]__month_days[]__daynumber
# calendars[]__months[]__month_days[]__dayname
# calendars[]__months[]__month_days[]__day_events[]__id
# calendars[]__months[]__month_days[]__day_events[]__name
# calendars[]__months[]__month_days[]__day_events[]__group[]__name
# calendars[]__months[]__month_days[]__day_events[]__group[]__members[]__name
# calendars[]__months[]__month_days[]__day_events[]__group[]__members[]__age
# calendars[]__months[]__month_days[]__day_events[]__group[]__members[]__skils[]__name
# calendars[]__months[]__month_days[]__day_events[]__group[]__members[]__tel
# print(_format(_call(d)))
from pprint import pprint
pprint(_call(d))
_format(_call(d))
@altnight
Copy link
Author

{'calendars': {'id': True,
               'months': {'id': True,
                          'month_days': {'day_events': {'group': {'members': {'age': True,
                                                                              'name': True,
                                                                              'skils': {'name': True},
                                                                              'tel': True},
                                                                  'name': True},
                                                        'id': True,
                                                        'name': True},
                                         'dayname': True,
                                         'daynumber': True,
                                         'id': True},
                          'monthname': True,
                          'monthnumber': True},
               'year': True},
 'success': True}
calendars[]__months[]__monthnumber
calendars[]__months[]__monthname
calendars[]__months[]__id
calendars[]__months[]__month_days[]__day_events[]__group[]__name
calendars[]__months[]__month_days[]__day_events[]__group[]__members[]__age
calendars[]__months[]__month_days[]__day_events[]__group[]__members[]__tel
calendars[]__months[]__month_days[]__day_events[]__group[]__members[]__name
calendars[]__months[]__month_days[]__day_events[]__group[]__members[]__skils[]__name
calendars[]__months[]__month_days[]__day_events[]__id
calendars[]__months[]__month_days[]__day_events[]__name
calendars[]__months[]__month_days[]__dayname
calendars[]__months[]__month_days[]__daynumber
calendars[]__months[]__month_days[]__id
calendars[]__id
calendars[]__year
success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment