/step5_export_for_map.py

https://gitlab.com/dlts/github-africa
Python | 83 lines | 66 code | 14 blank | 3 comment | 6 complexity | 4c5a5b590e1e44d8c225ad1c22eedaec MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # vim: ai ts=4 sts=4 et sw=4 nu
  4. from __future__ import (unicode_literals, absolute_import,
  5. division, print_function)
  6. import logging
  7. import json
  8. import unicodecsv as csv
  9. from bson import json_util
  10. from africa_data import countries
  11. logger = logging.getLogger(__name__)
  12. logging.basicConfig(level=logging.DEBUG)
  13. all_users = json.load(open('step4.json'),
  14. object_hook=json_util.object_hook)
  15. csv_file = open('github-users.csv', 'w')
  16. headers = ['country_code', 'country_name',
  17. 'city', 'latitude', 'longitude', 'nb_users', 'percent']
  18. csv_writer = csv.DictWriter(csv_file, fieldnames=headers)
  19. csv_writer.writeheader()
  20. nb_total_users = len(all_users)
  21. logger.info("NB USERS: {}".format(nb_total_users))
  22. pc_of_total = lambda num: num / nb_total_users
  23. cities_map = {}
  24. countries_map = {}
  25. logger.info("Looping through users to build maps")
  26. for user in all_users:
  27. logger.debug(user['username'])
  28. if user['city_name'] not in cities_map.keys():
  29. cities_map[user['city_name']] = {'nb_users': 0,
  30. 'country_code': user['country_code'],
  31. 'country_name': user['country_name'],
  32. 'latitude': user['latitude'],
  33. 'longitude': user['longitude']}
  34. cities_map[user['city_name']]['nb_users'] += 1
  35. if user['country_code'] not in countries_map.keys():
  36. countries_map[user['country_code']] = {
  37. 'nb_users': 0,
  38. 'name': user['country_name'],
  39. 'latitude': countries[user['country_code']]['latitude'],
  40. 'longitude': countries[user['country_code']]['longitude']}
  41. countries_map[user['country_code']]['nb_users'] += 1
  42. logger.info("Looping through countries to write to CSV")
  43. for country_code, country in countries_map.items():
  44. logger.debug(country['name'])
  45. csv_writer.writerow({
  46. 'country_code': country_code,
  47. 'country_name': country['name'],
  48. 'city': "",
  49. 'latitude': country['latitude'],
  50. 'longitude': country['longitude'],
  51. 'nb_users': country['nb_users'],
  52. 'percent': pc_of_total(country['nb_users'])
  53. })
  54. logger.info("Looping through cities to write to CSV")
  55. for city_name, city in cities_map.items():
  56. logger.debug(city_name)
  57. if city_name is None:
  58. continue
  59. csv_writer.writerow({
  60. 'country_code': city['country_code'],
  61. 'country_name': city['country_name'],
  62. 'city': city_name,
  63. 'latitude': city['latitude'],
  64. 'longitude': city['longitude'],
  65. 'nb_users': city['nb_users'],
  66. 'percent': pc_of_total(city['nb_users'])
  67. })
  68. csv_file.close()
  69. logger.info("NB USERS: {}".format(nb_total_users))