Python WHILE problem

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Python WHILE problem

Postby leke » Mon Feb 15, 2010 11:44 am

Hi, I'm having some problems testing for false. If the code below doesn't connect to http://www.rssweather.com/wx/fi/pirkkala/rss.php , (for example, you are offline) it returns an error. I tested the error value with an IF conditional which seemed to return FALSE for fd.entries, so I tried to code a WHILE loop to keep trying until fd.entries returned TRUE. Here is a simplified version of my code...
Code: Select all
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import feedparser

df = feedparser.parse('http://www.rssweather.com/wx/fi/pirkkala/rss.php')

while (df.entries == False):
    df = feedparser.parse('http://www.rssweather.com/wx/fi/pirkkala/rss.php')
else:
    rss_feed_summary = str(df.entries[0].summary)
    rss_feed_time = str(df.entries[0].updated)
   
sms = rss_feed_summary+"\n"+rss_feed_time
print sms

If I go offline and run this, I get...
leke@leke ~/Desktop/saunasms_src $ python fpTest.py
Traceback (most recent call last):
File "fpTest.py", line 11, in <module>
rss_feed_summary = str(df.entries[0].summary)
IndexError: list index out of range
...If I go online, everything works as expected.

What am I doing wrong here?
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland

Postby LeeNukes » Mon Feb 15, 2010 2:22 pm

Am I right in thinking that you've set it so that whilst the link isn't available, go and get the link?

Won't that slow it down a bit for it to constantly check for the .rss to be available?
User avatar
LeeNukes
LXF regular
 
Posts: 954
Joined: Sun Jun 21, 2009 8:11 pm
Location: At the bar

Postby nelz » Mon Feb 15, 2010 2:35 pm

A time.sleep call in the while loop would help, then put print df at the start of the else clause to see what see it df is actually a list.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8497
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby leke » Mon Feb 15, 2010 3:01 pm

LeeNukes wrote:Am I right in thinking that you've set it so that whilst the link isn't available, go and get the link?

Yes, for some reason whilst online, the link isn't available sometimes. This is quite disastrous since the script is run on the server once a day.
LeeNukes wrote:Won't that slow it down a bit for it to constantly check for the .rss to be available?

I guess it would. Hey, how about a timer ;) :D
Code: Select all
while (df.entries == False):
    time.sleep(10)
    print df
    df = feedparser.parse('http://www.rssweather.com/wx/fi/pirkkala/rss.php')
else:
    rss_feed_summary = str(df.entries[0].summary)
    rss_feed_time = str(df.entries[0].updated)

I added that timer and the print statement. But weirdly enough, I get the same error with no time delay and nothing printed. Does this mean python doesn't see df.entries as false?
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland

Postby leke » Mon Feb 15, 2010 3:04 pm

whoops, I see you said at the start of the else condition. This is what I get...
leke@leke ~/Desktop/saunasms_src $ python fpTest.py
{'feed': {}, 'encoding': 'utf-8', 'bozo': 1, 'version': None, 'entries': [], 'bozo_exception': URLError(gaierror(-2, 'Name or service not known'),)}
Traceback (most recent call last):
File "fpTest.py", line 15, in <module>
rss_feed_summary = str(df.entries[0].summary)
IndexError: list index out of range

If I ran df.entries i would get [] back in return.
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland

Postby leke » Mon Feb 15, 2010 3:43 pm

W00t, we cracked it guys. I messed up my False testing. Adding while (not df.entries): made it recognise that it was false and to run the true condition sequence.

Thanks :) I hope that works out a little better. If you are interested, here is the original script...

Code: Select all
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

import time

import urllib2, cookielib, re
import feedparser

df = feedparser.parse('http://www.rssweather.com/wx/fi/pirkkala/rss.php')
rss_feed_summary = str(df.entries[0].summary)
rss_feed_time = str(df.entries[0].updated)
sms = rss_feed_summary+"\n"+rss_feed_time # Message to send.

username = "" # Oma.Saunalahti account (your phonenumber in format 045...)
password = "" # Oma.Saunalahti account password
sender = "" # Your Saunalahti number (your phonenumber in format 045...)
recipients = "" # Receivers phone number (phonenumber in format 045...)


u = 'https://oma.saunalahti.fi/settings/smsSend'

d = r"username="+username+r"&login=Sisään&password="+password

c = cookielib.CookieJar()

o = urllib2.build_opener(urllib2.HTTPCookieProcessor(c))

o.addheaders = [('Referer', 'https://oma.saunalahti.fi/settings/'),

    ('Content-Type', 'application/x-www-form-urlencoded'),

    ('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')]

s = o.open(u, d)

p = s.read()

s.close()


d = "sender="+sender+"&recipients="+recipients+"&text="+sms+"&size="+str(len(sms))+"&send=Lähetä"

s = o.open(u,d)

d = s.read()

s.close()
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest