How can Python print the value of an attribute but complain it does not exist?

Discussion in 'Python' started by Emre Sevinc, Oct 10, 2007.

  1. Emre  Sevinc

    Emre Sevinc Guest

    Hello,

    I'm a Python newbie and I'm having a strange trouble with the
    following code:

    generatefeedvector-debug.py
    ===============================================================
    import feedparser
    import re

    def getwordcounts(url):
    # Parse the feed
    d = feedparser.parse(url)
    wc = {}

    # Loop over all the entries
    for e in d.entries:
    if 'summary' in e: summary = e.summary
    else: summary = e.description

    # Extract a list of words
    words = getwords(e.title + ' ' + summary)
    for word in words:
    wc.setdefault(word, 0)
    wc[word] += 1

    print d.feed.title
    return d.feed.title

    def getwords(html):
    # Remove all the HTML tags
    txt = re.compile(r'<[^>]+>').sub('', html)

    # Split words by all non-alpha characters
    words = re.compile(r'[^A-Z^a-z]+').split(txt)

    # Convert to lowercase
    return [word.lower() for word in words if word != '']

    apcount = {}
    wordcounts = {}
    for feedurl in file('feedlist1-2.txt'):
    title = getwordcounts(feedurl)
    ==================================================================

    When I run it:

    $ python generatefeedvector-debug.py
    Signal vs. Noise
    Traceback (most recent call last):
    File "generatefeedvector-debug.py", line 37, in ?
    title = getwordcounts(feedurl)
    File "generatefeedvector-debug.py", line 21, in getwordcounts
    print d.feed.title
    File "/var/lib/python-support/python2.4/feedparser.py", line 236, in
    __getattr__
    raise AttributeError, "object has no attribute '%s'" % key
    AttributeError: object has no attribute 'title'


    The strange thing is that it DOES print the value d.feed.title then
    complains AttributeError: object has no attribute 'title'. What am I
    doing wrong?

    The file feedlist1-2.txt includes just a single line:

    $ cat feedlist1-2.txt
    http://feeds.feedburner.com/37signals/beMH

    I'm using Python 2.4 on Debian GNU/Linux.

    Any ideas about how to fix this error message?

    Regards,

    --
    Emre Sevinc
     
    Emre Sevinc, Oct 10, 2007
    #1
    1. Advertising

  2. Emre  Sevinc

    Dan Guest

    On Oct 10, 3:03 pm, Emre Sevinc <> wrote:
    > Hello,
    >
    > I'm a Python newbie and I'm having a strange trouble with the
    > following code:
    >
    > generatefeedvector-debug.py
    > ===============================================================
    > import feedparser
    > import re
    >
    > def getwordcounts(url):
    > # Parse the feed
    > d = feedparser.parse(url)
    > wc = {}
    >
    > # Loop over all the entries
    > for e in d.entries:
    > if 'summary' in e: summary = e.summary
    > else: summary = e.description
    >
    > # Extract a list of words
    > words = getwords(e.title + ' ' + summary)
    > for word in words:
    > wc.setdefault(word, 0)
    > wc[word] += 1
    >
    > print d.feed.title
    > return d.feed.title
    >
    > def getwords(html):
    > # Remove all the HTML tags
    > txt = re.compile(r'<[^>]+>').sub('', html)
    >
    > # Split words by all non-alpha characters
    > words = re.compile(r'[^A-Z^a-z]+').split(txt)
    >
    > # Convert to lowercase
    > return [word.lower() for word in words if word != '']
    >
    > apcount = {}
    > wordcounts = {}
    > for feedurl in file('feedlist1-2.txt'):
    > title = getwordcounts(feedurl)
    > ==================================================================
    >
    > When I run it:
    >
    > $ python generatefeedvector-debug.py
    > Signal vs. Noise
    > Traceback (most recent call last):
    > File "generatefeedvector-debug.py", line 37, in ?
    > title = getwordcounts(feedurl)
    > File "generatefeedvector-debug.py", line 21, in getwordcounts
    > print d.feed.title
    > File "/var/lib/python-support/python2.4/feedparser.py", line 236, in
    > __getattr__
    > raise AttributeError, "object has no attribute '%s'" % key
    > AttributeError: object has no attribute 'title'
    >
    > The strange thing is that it DOES print the value d.feed.title then
    > complains AttributeError: object has no attribute 'title'. What am I
    > doing wrong?
    >
    > The file feedlist1-2.txt includes just a single line:
    >
    > $ cat feedlist1-2.txthttp://feeds.feedburner.com/37signals/beMH
    >
    > I'm using Python 2.4 on Debian GNU/Linux.
    >
    > Any ideas about how to fix this error message?
    >
    > Regards,
    >
    > --
    > Emre Sevinc


    This is a bit of a guess, but prehaps the file has a blank line, so
    the first url is fine, but the second (non-existant) url doesn't have
    a title. You can test this by doing:

    > for feedurl in file('feedlist1-2.txt'):
    > if feedurl.strip():
    > title = getwordcounts(feedurl)


    -Dan
     
    Dan, Oct 10, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Johnson
    Replies:
    0
    Views:
    1,253
    Bill Johnson
    Jul 8, 2005
  2. LT
    Replies:
    7
    Views:
    2,133
    Phlip
    Jul 25, 2004
  3. saneman
    Replies:
    2
    Views:
    345
    Thomas Tutone
    Oct 23, 2007
  4. Bill Johnson

    CS0234 Global does not exist ... but it genuinely does

    Bill Johnson, Jul 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    212
    Bill Johnson
    Jul 8, 2005
  5. Adam Akhtar
    Replies:
    9
    Views:
    182
    Seebs
    Jan 27, 2010
Loading...

Share This Page