Permalink
Browse files

Add support for parsing forecasts

Add the Forecast object, and add the dump_forecast function. Part of the
way to supporting alerting on temperature situations.
  • Loading branch information...
1 parent ceba8f5 commit 9843b259398dec5a36f3aa3120331daebeabaa56 @igable committed Sep 12, 2012
Showing with 69 additions and 18 deletions.
  1. +2 −0 .gitignore
  2. +1 −0 data_gc_ca_api/__init__.py
  3. +59 −18 data_gc_ca_api/cityweather.py
  4. +7 −0 weatherca
View
@@ -8,3 +8,5 @@ build/
dist/
MANIFEST
*.egg-info
+examples
+docs
@@ -0,0 +1 @@
+from cityweather import City, CityIndex
@@ -95,14 +95,14 @@ class City():
def __init__(self, dataurl):
self.tree = ElementTree()
-
try:
urlhandle = urllib.urlopen(dataurl)
except IOError:
print "[Error] Unable to open the data url: " + dataurl
sys.exit(1)
self.tree.parse(urlhandle)
+ self.forecast_object_list = self._make_forecast_list()
def get_quantity(self,path):
"""Get the quatity contained at the XML XPath"""
@@ -176,22 +176,63 @@ def _get_all_xpaths_with_attributes(self, pathlist, path, element):
else:
self._get_all_xpaths_with_attributes(pathlist, path + "/" + element.tag + xpathattrib, child)
- # This function will break is thre is any change in the city weather
- # XML format
- def get_available_forecast_names(self):
- forecasts = self.tree.findall('forecastGroup/forecast/period')
- forecastnames = []
- for forecast in forecasts:
- forecastnames.append(forecast.get("textForecastName"))
- return forecastnames
-
- # This function will break is thre is any change in the city weather
- # XML format
- def get_available_forecast_periods(self):
- forecasts = self.tree.findall('forecastGroup/forecast/period')
- forecastnames = []
- for forecast in forecasts:
- forecastnames.append(forecast.text)
- return forecastnames
+ def dump_forecast(self):
+ for forecast in self.forecast_object_list:
+ period = forecast.get_period().ljust(15)
+ high = " "
+ low = " "
+ if forecast.has_low():
+ low = forecast.get_low().ljust(4)
+ if forecast.has_high():
+ high = forecast.get_high().ljust(4)
+ print "%s Low:%s High:%s" % (period,low,high)
+
+
+ def _make_forecast_list(self):
+ forecasts_tree_list = self.tree.findall('forecastGroup/forecast')
+ forecast_object_list = []
+ for forecast_tree in forecasts_tree_list:
+ forecast_object_list.append(Forecast(forecast_tree))
+ return forecast_object_list
+
+class Forecast():
+
+ def __init__(self,forcast):
+ self.tree = forcast
+
+ def get_period(self):
+ return self.tree.findtext('period')
+
+ def get_text_summary(self):
+ return self.tree.findtext('textSummary')
+
+ def has_low(self):
+ low = False
+ for temperature in self.tree.findall('temperatures/temperature'):
+ if temperature.get('class') == 'low':
+ low = True
+ return low
+
+ def has_high(self):
+ high = False
+ for temperature in self.tree.findall('temperatures/temperature'):
+ if temperature.get('class') == 'high':
+ high = True
+ return high
+
+ def get_high(self):
+ if self.has_high():
+ for temperature in self.tree.findall('temperatures/temperature'):
+ if temperature.get('class') == 'high':
+ return temperature.text
+
+ def get_low(self):
+ if self.has_low():
+ for temperature in self.tree.findall('temperatures/temperature'):
+ if temperature.get('class') == 'low':
+ return temperature.text
+
+
+
View
@@ -61,6 +61,10 @@ def main():
parser.add_option("-a", "--list-quantities", action="store_true", dest="list_quantities",
help="Show a list of the available quantities")
+ parser.add_option("-d", "--dump-forecast", action="store_true", dest="dump_forecast",
+ help="Dump a short forecast")
+
+
(options, args) = parser.parse_args()
@@ -104,6 +108,9 @@ def main():
if options.list_quantities:
for q in cityObject.get_available_quantities():
print q
+ if options.dump_forecast:
+ #shouldn't be dump, should return string
+ cityObject.dump_forecast()
else:
if not options.quantity:
print "No requested quantity given, using temperature"

0 comments on commit 9843b25

Please sign in to comment.