def get_all_data(location, use_cache=True): if isinstance(location, basestring): location = _location.woeid(location) try: if not use_cache: raise KeyError expire, data = __all_data_cache[location] if expire+__CACHE_EXPIRE < time.time(): raise KeyError return data except KeyError: pass raw = raw_yahoo_weather(location) current = raw['condition'] forecasts = [] for forecast in raw['forecasts']: high, low = int(forecast['high']), int(forecast['low']) data = ForecastData(parse_date(forecast['date']), high, low, forecast['text'], xrange(low, high+1)) forecasts.append(data) visib = raw['atmosphere']['visibility'] humid = raw['atmosphere']['humidity'] press = raw['atmosphere']['pressure'] data = WeatherData(city=raw['location']['city'], region=raw['location']['region'], country=raw['location']['country'], humidity=float(humid)/100 if humid else None, pressure=float(press)/10 if press else None, visibility=float(visib)/100 if visib else None, current=CurrentCondition(datetime.datetime.now(), int(current['temp']), current['text']), forecasts=forecasts, windchill=raw['wind']['chill'], winddirection=AngleData(deg=raw['wind']['direction']), windspeed=float(raw['wind']['speed'])) if use_cache: __all_data_cache[location] = (time.time(), data) return data
def _test_module(): global __CACHE_EXPIRE from pprint import pprint from location import woeid print '== Testing Raw Weather ==' print 'Retrieving raw weather data for Toronto, Canada' pprint(raw_yahoo_weather(woeid('Toronto, Canada'))) print print 'Retrieving raw weather data for Brasov, Romania' pprint(raw_yahoo_weather(woeid('Brasov, Romania'))) print print '== Testing Processed Weather ==' city = 'Toronto, Canada' __CACHE_EXPIRE = 5 print 'For:', 'Toronto, Canada' print '====== No Cache ======' pprint(get_all_data(city)) print '====== With Cache ======' pprint(get_all_data(city)) print '====== Expired Cache ======' time.sleep(6) pprint(get_all_data(city)) print '====== Disabled Cache ======' pprint(get_all_data(city, False))