def get_location_from_wikipedia(name): data = climate.get_coordinates(name) location = ephem.Observer() # pyephem is a little weird, only works right when these are strings location.lat = str(data['lat']) location.lon = str(data['lng']) try: # on the other hand, elevation must be specified as a float location.elevation = float(data['elevation']) except: # ignore, might not have been specified, # or might be in a format float or pyephem can't handle # e.g. Whitehorse uses "670–1702" pass return location
def test_latlng_known_data(self): """ Test getting latitude, longitudes, and elevations of some test cities. Parses their Wikipedia page to get this information. This is also tested implicitly by test_percent_sun for Portland, Oregon, since we need Wikipedia latlng to compute that particular data set.""" known_data = { "Toronto": {"lat": 43.7, "lng": -79.4, "elevation": 76}, "Auckland": {"lat": -36.8406, "lng": 174.74, "elevation": 196}, "Akureyri": {"lat": 65.6833, "lng": -18.1}, "Alert, Nunavut": {"lat": 82.5014, "lng": -62.3389}, "Buenos Aires": {"lat": -34.6033, "lng": -58.3817, "elevation": 25}, } for city, data in known_data.items(): actual_data = climate.get_coordinates(city) for key, expected_value in data.items(): self.assertEqual(actual_data[key], expected_value)