CrazeeIvan / python-weather-api

Automatically exported from code.google.com/p/python-weather-api
MIT License
0 stars 0 forks source link

get_weather_from_noaa fails if data is missing #4

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Run the pywapi-noaa-example.py when there NOAA isn't reporting one of the 
items in the 
data structure--heat_index_f, for example.  

What is the expected output? What do you see instead?
The example fails with an IndexError at line 209 instead of running to 
completion.

What version of the product are you using? On what operating system?
Python 2.5.1 on a Macintosh OS X 10.5.7.

Please provide any additional information below.

Use this try/except block in get_weather_from_noaa

    for tag in data_structure:
        try:
            weather_data[tag] = current_observation.getElementsByTagName(tag)[0].firstChild.data
        except IndexError:
            weather_data[tag] = ''

Original issue reported on code.google.com by drdr...@gmail.com on 16 Jun 2009 at 2:48

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
After some thought, I think it's better to have no value when NOAA isn't 
reporting the value

for tag in data_structure:
    try:
        weather_data[tag] = current_observation.getElementsByTagName(tag)[0].firstChild.data
    except IndexError:
         pass

Original comment by drdr...@gmail.com on 26 Jun 2009 at 6:57

GoogleCodeExporter commented 8 years ago
Thank you for pointing to this issue, drdrang.
It is necessary to fix the issue in all functions.

> After some thought, I think it's better to have no value when NOAA isn't 
reporting 
the value

Why do you think so?

Original comment by qet...@gmail.com on 29 Jun 2009 at 11:34

GoogleCodeExporter commented 8 years ago
According to the Zen of Python:

   Errors should never pass silently.
   Unless explicitly silenced.

It's a matter of convention. In Python, it's most common to raise an
exception when a program tries to access a piece of data that isn't
there, and Python programmers expect to use try/except blocks to
handle exceptions. If pywapi puts empty strings into the dictionary,
programmers won't be able to use try/except to distinguish the
dictionary entries that are there from those that aren't.

If your library were written in Perl, I would be making the opposite
argument--Perl programmers expect unknown hash keys and list indexes
to return empty values. In my first message, I was thinking more like
a Perl programmer.

By the way, you may be interested in this post

http://www.leancrew.com/all-this/2009/06/new-weather-script-for-geektool/

in which I describe a use of pywapi.

Original comment by drdr...@gmail.com on 29 Jun 2009 at 7:10

GoogleCodeExporter commented 8 years ago
Added.

Original comment by qet...@gmail.com on 10 Aug 2009 at 8:34