0

I am trying to handle a KeyError/AttributeError in my news feed program that parses the Yahoo! Finance RSS API and outputs key data in a Tkinter GUI.

I have excepted the error/s, but when I try to output an error message to the message widget, it does not configure. I know that it excepts the error however as I have coded a simple print message.

MY QUESTION IS: How do I fix the message widget configuration, so that the error message is output to it, as right now, nothing occurs.

Any advice or criticism to what I am doing wrong would be great.

Thank you for your time, here is my code:

def parse_company_feed(news_feed_message, rss_url):
    ''' This function parses the Yahoo! RSS API for data of the latest five articles, and writes it to the company news text file'''

    # Define the RSS feed to parse from, as the url passed in of the company the user chose
    feed = feedparser.parse(rss_url)

    try:
        # Define the file to write the news data to the company news text file
        with open('C:\\Users\\nicks_000\\PycharmProjects\\untitled\\SAT\\GUI\\Text Files\\companyNews.txt', mode='w') as outFile:

            # Create a list to store the news data parsed from the Yahoo! RSS
            news_data_write = []
            # Initialise a count
            count = 0
            news_data_write.append((feed['feed']['description'])+'\n')
            # For the number of articles to append to the file, append the article's title, link, and published date to the news_elements list
            for count in range(10):
                news_data_write.append(feed['entries'][count].title)
                news_data_write.append(feed['entries'][count].published)
                article_link = (feed['entries'][count].link)
                article_link = article_link.split('*')[1]
                news_data_write.append(article_link)
                # Add one to the count, so that the next article is parsed
                count+=1
                # For each item in the news_elements list, convert it to a string and write it to the company news text file
                for item in news_data_write:
                    item = str(item)
                    outFile.write(item+'\n')
                # For each article, write a new line to the company news text file, so that each article's data is on its own line
                outFile.write('\n')
                # Clear the news_elements list so that data is not written to the file more than once
                del(news_data_write[:])
    except KeyError and AttributeError:
        news_feed_message.configure(text = 'That company does not exist. Please enter another ticker.')
        print('nah m8, no feed')
    else:
        outFile.close()

    read_news_file(news_feed_message)
Nick
  • 63
  • 2
  • 9
  • 1
    http://stackoverflow.com/questions/6470428/catch-multiple-exceptions-in-one-line-except-block – Billal Begueradj Jul 25 '16 at 11:09
  • Did you actually look at the accepted answer of the duplicate, and compared it with your own code? It is quite evident to spot the difference and thus what you need to correct. – trincot Jul 25 '16 at 20:51
  • Did you actually look at my question? The message box widget does not configure, no matter what order I except the code in. I have done what is in that other answer, and guess what? That's not the problem. – Nick Jul 26 '16 at 10:05

1 Answers1

0

The syntax you are using is wrong. It should be:

except (KeyError, AttributeError):

not:

except KeyError and AttributeError:  # <- wrong!

As you can see if you try to run this code:

>>> try:
...     raise KeyError
... except (KeyError,AttributeError):
...     print('raised')
... 
raised
>>> try:
...     raise KeyError
... except KeyError and AttributeError:
...     print('raised')
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyError

In the second case what happens is that the expression KeyError and AttributeError is evaluated. And the result is:

>>> KeyError and AttributeError
<class 'AttributeError'>

because both KeyError and AttributeError are truthy values, and and returns the last truthy value it finds when the result is true (the first false one if the result is false). So basically that except statement is equivalent to just using except AttributeError:.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231