From 425db7be81f4470832e8ed24bf9bed4273839e6f Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Fri, 5 Oct 2012 10:22:35 -0500 Subject: [PATCH] Weather: weather conditions readability fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * bold (^B) city name, condition elements * replace 32F and 32 F with 32°F --- modules/Weather.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/modules/Weather.py b/modules/Weather.py index da4ad41..68240ac 100644 --- a/modules/Weather.py +++ b/modules/Weather.py @@ -130,7 +130,7 @@ class Weather(Module): else: try: location = current['display_location']['full'] - reply = "Conditions for {0:s}: ".format(location) + reply = "Conditions for {0:s}: ".format(location) weather_str = current['weather'] if weather_str != '': @@ -158,41 +158,41 @@ class Weather(Module): humidity_str = current['relative_humidity'] if humidity_str != '': - reply += "Humidity: {0:s}. ".format(humidity_str) + reply += "Humidity: {0:s}. ".format(humidity_str) wind_str = current['wind_string'] if wind_str != '': - reply += "Wind: {0:s}. ".format(wind_str) + reply += "Wind: {0:s}. ".format(wind_str) pressure_in = current['pressure_in'] pressure_trend = current['pressure_trend'] if pressure_in != '': - reply += "Pressure: {0:s}\"".format(pressure_in) + reply += "Pressure: {0:s}\"".format(pressure_in) if pressure_trend != '': reply += " and {0:s}".format("dropping" if pressure_trend == '-' else "rising") reply += ". " heat_index_str = current['heat_index_string'] if heat_index_str != '' and heat_index_str != 'NA': - reply += "Heat index: {0:s}. ".format(heat_index_str) + reply += "Heat index: {0:s}. ".format(heat_index_str) windchill_str = current['windchill_string'] if windchill_str != '' and windchill_str != 'NA': - reply += "Wind chill: {0:s}. ".format(windchill_str) + reply += "Wind chill: {0:s}. ".format(windchill_str) visibility_mi = current['visibility_mi'] if visibility_mi != '': - reply += "Visibility: {0:s} miles. ".format(visibility_mi) + reply += "Visibility: {0:s} miles. ".format(visibility_mi) precip_in = current['precip_today_in'] if precip_in != '': - reply += "Precipitation today: {0:s}\". ".format(precip_in) + reply += "Precipitation today: {0:s}\". ".format(precip_in) observation_time = current['observation_time'] if observation_time != '': reply += "{0:s}. ".format(observation_time) - return reply.rstrip() + return self._prettify_weather_strings(reply.rstrip()) except KeyError as e: self.log.error("error or unexpected results in conditions reply") self.log.exception(e) @@ -217,5 +217,18 @@ class Weather(Module): return api_key + def _prettify_weather_strings(self, weather_str): + """ + Clean up output strings. + + For example, turn 32F into 32°F in input string. + + Input: + weather_str --- the string to clean up + + """ + + return re.sub(r'(\d+)\s*([FC])', r'\1°\2', weather_str) + # vi:tabstop=4:expandtab:autoindent # kate: indent-mode python;indent-width 4;replace-tabs on;