def test_geti_basic_chain(self): my_dict = {'key1': 99} value = getni(my_dict, 'key1') assert value == 99 value = getni(my_dict, 'key99') assert value == None value = getni(my_dict, 'key99', fallback=99) assert value == 99
def test_getni_get_two_level_chain(self): my_dict = {'key1': {'key2': True}} value = getni(my_dict, 'key1') assert value == my_dict['key1'] value = getni(my_dict, 'key1.key2') assert value == True value = getni(my_dict, 'key1.key3', False) assert value == False
def get_geographic_coordinate(self, address): ''' Find the geographic coordinate data for a given address ''' url = f'http://maps.google.com/maps/api/geocode/json?address={address}' data = http_get(url) self.__raise_zero_results_(data) result = head_list(data['results']) φ, λ, full_address = getni(result, 'geometry.location.lat'), getni( result, 'geometry.location.lng'), getni(result, 'formatted_address') return φ, λ, full_address
def get_temperature_by_geographic_coordinate(self, latitude, longitude): ''' Find the temperature data for a given location (Geographic Coordinate) ''' appid = settings.OPENWEATHERMAP_API_KEY url = f'http://api.openweathermap.org/data/2.5/weather?lat={latitude}&lon={longitude}&appid={appid}' data = http_get(url) self.__raise_for_invalid_result(data) city, country, degrees = getni(data, 'name'), getni( data, 'sys.country'), getni(data, 'main.temp') return city, country, degrees
def test_getni_biggest_level_chain(self): my_dict = {'key1': {'key2': {'key3': {'key4': {'key5': 'Hi'}}}}} value = getni(my_dict, 'key1.key2.key3.key4.key5') assert value == 'Hi' value = getni(my_dict, 'key1.key2.key3.key4.key5.key6') assert value == None value = getni(my_dict, 'key99.key2.key3.key4.key5') assert value == None value = getni(my_dict, 'key1.key2.key3.key4.key5.key6', 'Hello') assert value == 'Hello'
def __get_elements(self, origins, destinations): mode = 'driving' sensor = 'false' origin = '|'.join([str(o['lat']) + ',' + o['lng'] for o in origins]) destination = '|'.join([str(o['lat']) + ',' + o['lng'] for o in destinations]) url = f'http://maps.googleapis.com/maps/api/distancematrix/json?origins={origin}&destinations={destination}&mode={mode}&sensor={sensor}' data = http_get(url) # Check if response contains a valid result self.__statis_is_valid(getni(data, 'status')) destination_addresses = getni(data, 'destination_addresses') origin_addresses = getni(data, 'origin_addresses') rows = getni(data, 'rows') elements = [] for i, (d, o) in enumerate( zip(destination_addresses, origin_addresses)): # Group elements by index distances = [r['elements'][i] for r in rows] # Find shortest distance between multiple destinations best_distance = min( distances, key=lambda e: getni( e, 'distance.value')) elements.append({ 'destination': d, 'origin': o, 'distance': getni(best_distance, 'distance.value'), }) return elements
def __raise_zero_results_(self, data): if getni(data, 'status') == 'ZERO_RESULTS': raise Exception('Address not found')
def __raise_for_invalid_result(self, data): if getni(data, 'cod') != 200: raise Exception(getni(data, 'message'))