class Forecast(object): def __init__(self): self.conn = Connection(api_type='dataservice') def location_from_postal_code(self, country_code='us', postal_code=None): if postal_code is None: raise ValueError('Missing postal code') if not self.valid_postal_code(postal_code): raise ValueError('Invalid postal code') self.location = self.conn.loc_postcode(country_code, postal_code) return self.location def valid_postal_code(self, postal_code): return re.search(r'^\d{5}(?:[-\s]\d{4})?$', postal_code) def get_forecast(self, type='1d', lkey=None): if lkey is None: try: lkey = self.location.lkey except AttributeError: raise ValueError('Location key required') self.forecast = self.conn.get_forecast(type, lkey=self.location.lkey, metric=False) return self.forecast def formatted_forecast(self): if self.forecast is None: raise ValueError('Forecast required') if self.location is None: raise ValueError('Location required') # One day forecasts only for k in self.forecast.forecasts: min = self.forecast.forecasts[k].temp_min.F max = self.forecast.forecasts[k].temp_max.F for k in self.forecast.forecasts: day_synopsis = self.forecast.forecasts[k].day.synopsis deg = u'\N{DEGREE SIGN}' formatted = f'{self.location.localized_name}:\nHigh: {max:.0f}{deg}\nLow: {min:.0f}{deg}\n{day_synopsis}' return formatted
class TestConnectionLive(TestCase): def setUp(self): self.conn = Connection() def test_wipe_api_key(self): self.conn.wipe_api_key() assert self.conn.API_KEY is None def test_loc_geoposition(self): res = self.conn.loc_geoposition(lat=51.5, lon=-0.5) assert isinstance(res, Location) assert int(res.lkey) == 327019 with self.assertRaises(ValueError): self.conn.loc_geoposition(lat=51.5, lon="potato") def test_out_of_range_lat(self): with self.assertRaises(RangeError): self.conn.loc_geoposition(lat=91.0, lon=-0.5) with self.assertRaises(RangeError): self.conn.loc_geoposition(lat=50.0, lon=183.1) def test_loc_string(self): with self.assertRaises(InvalidCountryCodeError): self.conn.loc_string(search_string="budapest", country_code="XXX") with self.assertRaises(NoResultsError): self.conn.loc_string(search_string="tqabBpmXsc") res = self.conn.loc_string(search_string="Ladoga", country_code="US") assert(isinstance(res, LocationSet)) assert(len(res) == 5) assert(isinstance(res[1], Location)) assert(res[1].lkey == '2152343') def test_loc_postcode(self): res = self.conn.loc_postcode(country_code="US", postcode=47954) assert isinstance(res, Location) assert res.lkey == '20721_PC' with self.assertRaises(InvalidCountryCodeError): self.conn.loc_postcode(country_code="USA", postcode=47954) with self.assertRaises(AssertionError): self.conn.loc_postcode(country_code="US", postcode="9999999") def test_loc_ip(self): res = self.conn.loc_ip("81.156.190.65") assert isinstance(res, Location) assert int(res.lkey) == 330732 def test_loc_lkey(self): res = self.conn.loc_lkey(330732) assert isinstance(res, Location) self.assertAlmostEqual(res.lat, 50.91, delta=1) self.assertAlmostEqual(res.lon, -1.5, delta=1) def test_get_current_wx(self): res = self.conn.get_current_wx(330732) assert isinstance(res, CurrentObs) assert isinstance(res.observations, OrderedDict) def test_get_forecast(self): res = self.conn.get_forecast(forecast_type="12h", lkey=330732) assert isinstance(res, HourlyForecasts) assert isinstance(res.forecasts, OrderedDict) assert len(res.forecasts) == 12