Skip to content

Instantly share code, notes, and snippets.

@eirenik0
Last active December 18, 2015 20:38
Show Gist options
  • Save eirenik0/5841177 to your computer and use it in GitHub Desktop.
Save eirenik0/5841177 to your computer and use it in GitHub Desktop.
From Json VK output, creating lists this format ['Name', 'Address']
from collections import namedtuple
friends_json = [{
'uid': 16734834,
'first_name': 'Alena',
'last_name': 'Grin',
'deactivated': 'banned',
'photo': 'http://vk.com/images/deactivated_c.gif',
'online': 0,
'user_id': 16734834,
'lists': [26]
}, {
'uid': 138017573,
'first_name': 'Alena',
'last_name': 'Kotkova',
'city': '2',
'country': '1',
'photo': 'http://cs413026.vk.me/v413026573/1935/Mv17_gs8UZY.jpg',
'online': 1,
'user_id': 138017573,
'lists': [26]
}]
def get_friends_from_json(friends_json):
Friend = namedtuple('Friend', ["name", "addres", "city", "country"])
friends = []
for field in friends_json:
if 'country' in field:
if 'city' in field:
# Friends with city and country
friends.append(Friend(format(field, 'first_name', 'last_name'),
format(field, 'city', 'country'), True, True))
elif 'city' not in field:
friends.append(Friend((format(field, 'first_name', 'last_name')),
field['country'], False, True))
else:
# Who haven't home
friends.append(Friend((format(field, 'first_name', 'last_name')),
'Antarctica', False, False))
return friends
def format(field, key1, key2):
return ('%s %s' % (field[key1], field[key2]))
print get_friends(friends_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment