Guest User

Untitled

a guest
Dec 17th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import json
  3. import numpy as np
  4. import unittest
  5.  
  6. # constants
  7. FILENAME = 'customers.json'
  8. INTERCOM_COORDS = [53.339428,-6.257664]
  9. WITHIN_DISTANCE = 100
  10. EARTH_RADIUS = 6367
  11.  
  12. def read_file_as_json(filename):
  13. """reads a json file and returns an object"""
  14. with open(filename, 'r') as json_file:
  15. data = json.load(json_file)
  16. return data
  17.  
  18. def haversine(coord1, coord2):
  19. """calculates the great-circle distance between two coordinates using haversine formula and
  20. returns the distance in km."""
  21. lat1, lon1 = map(np.deg2rad, coord1)
  22. lat2, lon2 = map(np.deg2rad, coord2)
  23.  
  24. delta_lat = abs(lat2 - lat1)
  25. delta_lon = abs(lon2 - lon1)
  26.  
  27. a = np.sin(delta_lat / 2.0)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(delta_lon / 2.0)**2
  28.  
  29. central_angle = 2 * np.arcsin(np.sqrt(a))
  30. distance = EARTH_RADIUS * central_angle
  31. return distance
  32.  
  33. def in_distance(coord1, coord2, distance):
  34. """checks whether two coordinates within a given distance"""
  35. return haversine(coord1, coord2) <= distance
  36.  
  37. def customer_coords(customer):
  38. """returns a list containing longitude and latitude of given customer"""
  39. return map(float, [customer['latitude'], customer['longitude']])
  40.  
  41. def customers_in_distance(customers):
  42. """returns the names and user ids of customers within 100km, sorted by User ID"""
  43. customers = [(c['name'], c['user_id']) for c in customers if in_distance(INTERCOM_COORDS, customer_coords(c), WITHIN_DISTANCE)]
  44. customers = sorted(customers, key=lambda tup: tup[1])
  45. return customers
  46.  
  47. def print_customers(customers):
  48. """prints names and user ids of customers within 100 km from Intercom office."""
  49. for customer in customers:
  50. print "{0} - {1}".format(customer[0], customer[1])
  51.  
  52. class TestCustomers(unittest.TestCase):
  53. def setUp(self):
  54. self.json = read_file_as_json(FILENAME)
  55.  
  56. def test_read_file_as_json(self):
  57. self.assertEqual(len(self.json), 32)
  58.  
  59. def test_haversine(self):
  60. self.assertEqual(haversine([0,0], [0,0]), 0)
  61. self.assertEqual(int(haversine(INTERCOM_COORDS, [51.92893,-10.27699])), 313)
  62.  
  63. def test_in_distance(self):
  64. self.assertEqual(in_distance([0,0], [0,0], 0), True)
  65. self.assertEqual(in_distance(INTERCOM_COORDS, [51.92893,-10.27699], WITHIN_DISTANCE), False)
  66.  
  67. def test_customer_coords(self):
  68. first_customer_coords = customer_coords(self.json[0])
  69. self.assertEqual(first_customer_coords, [52.986375, -6.043701])
  70.  
  71. def test_customers_in_distance(self):
  72. customers = customers_in_distance(self.json)
  73. self.assertEqual(len(customers), 16)
  74.  
  75. def test_print_customers(self):
  76. customers = customers_in_distance(self.json)
  77. print '\n'
  78. print_customers(customers)
  79.  
  80. if __name__ == "__main__":
  81. unittest.main()
Add Comment
Please, Sign In to add comment